init
This commit is contained in:
commit
1ab9f9c458
1319 files changed
+137505
No files matched your search
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* 认证工具类
|
||||
*/
|
||||
|
||||
const TOKEN_KEY = 'token';
|
||||
const USER_INFO_KEY = 'userInfo';
|
||||
|
||||
/**
|
||||
* 获取Token
|
||||
*/
|
||||
export function getToken() {
|
||||
return uni.getStorageSync(TOKEN_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Token
|
||||
*/
|
||||
export function setToken(token) {
|
||||
return uni.setStorageSync(TOKEN_KEY, token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除Token
|
||||
*/
|
||||
export function removeToken() {
|
||||
return uni.removeStorageSync(TOKEN_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
export function getUserInfo() {
|
||||
return uni.getStorageSync(USER_INFO_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户信息
|
||||
*/
|
||||
export function setUserInfo(userInfo) {
|
||||
return uni.setStorageSync(USER_INFO_KEY, userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除用户信息
|
||||
*/
|
||||
export function removeUserInfo() {
|
||||
return uni.removeStorageSync(USER_INFO_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已登录
|
||||
*/
|
||||
export function isLoggedIn() {
|
||||
const token = getToken();
|
||||
return !!token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有认证信息
|
||||
*/
|
||||
export function clearAuth() {
|
||||
removeToken();
|
||||
removeUserInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
export function login(loginData) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 模拟登录API调用
|
||||
setTimeout(() => {
|
||||
if (loginData.username === 'admin' && loginData.password === 'admin') {
|
||||
const token = 'mock-token-' + Date.now();
|
||||
const userInfo = {
|
||||
id: 1,
|
||||
username: loginData.username,
|
||||
nickname: '管理员',
|
||||
avatar: '/static/avatar.png',
|
||||
title: '系统管理员',
|
||||
};
|
||||
|
||||
setToken(token);
|
||||
setUserInfo(userInfo);
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
data: {
|
||||
token,
|
||||
userInfo,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
reject(new Error('用户名或密码错误'));
|
||||
}
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
export function logout() {
|
||||
return new Promise((resolve) => {
|
||||
clearAuth();
|
||||
|
||||
// 跳转到登录页
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login',
|
||||
});
|
||||
|
||||
resolve({ success: true });
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查登录状态并跳转
|
||||
*/
|
||||
export function checkAuth() {
|
||||
if (!isLoggedIn()) {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
/**
|
||||
* 网络请求工具类
|
||||
*/
|
||||
|
||||
import { getToken, logout } from './auth.js';
|
||||
|
||||
// 基础URL配置 - 修正为本地后端地址
|
||||
const BASE_URL = 'http://localhost:8080';
|
||||
|
||||
// 请求超时时间
|
||||
const TIMEOUT = 10000;
|
||||
|
||||
/**
|
||||
* 网络请求封装
|
||||
*/
|
||||
export function request(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = getToken();
|
||||
|
||||
// 默认配置
|
||||
const defaultOptions = {
|
||||
url: BASE_URL + options.url,
|
||||
method: options.method || 'GET',
|
||||
timeout: TIMEOUT,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header,
|
||||
},
|
||||
data: options.data || {},
|
||||
};
|
||||
|
||||
// 添加Authorization header
|
||||
if (token) {
|
||||
defaultOptions.header['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// 显示加载提示
|
||||
if (options.loading !== false) {
|
||||
uni.showLoading({
|
||||
title: options.loadingText || '加载中...',
|
||||
mask: true,
|
||||
});
|
||||
}
|
||||
|
||||
uni.request({
|
||||
...defaultOptions,
|
||||
success: (res) => {
|
||||
// 隐藏加载提示
|
||||
if (options.loading !== false) {
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
const { statusCode, data } = res;
|
||||
|
||||
// 请求成功
|
||||
if (statusCode === 200) {
|
||||
// 业务成功
|
||||
if (data.code === 200 || data.success === true) {
|
||||
resolve(data);
|
||||
} else {
|
||||
// 业务失败
|
||||
const message = data.message || data.msg || '请求失败';
|
||||
|
||||
// Token过期,跳转登录
|
||||
if (data.code === 401) {
|
||||
uni.showToast({
|
||||
title: '登录已过期,请重新登录',
|
||||
icon: 'none',
|
||||
});
|
||||
logout();
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示错误信息
|
||||
if (options.showError !== false) {
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
|
||||
reject(new Error(message));
|
||||
}
|
||||
} else {
|
||||
// HTTP状态码错误
|
||||
const message = `请求失败 (${statusCode})`;
|
||||
|
||||
if (options.showError !== false) {
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
|
||||
reject(new Error(message));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
// 隐藏加载提示
|
||||
if (options.loading !== false) {
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
const message = err.errMsg || '网络请求失败';
|
||||
|
||||
if (options.showError !== false) {
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
|
||||
reject(new Error(message));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
*/
|
||||
export function get(url, params = {}, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
*/
|
||||
export function post(url, data = {}, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
*/
|
||||
export function put(url, data = {}, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
*/
|
||||
export function del(url, params = {}, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
data: params,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
export function uploadFile(url, filePath, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = getToken();
|
||||
|
||||
// 显示上传进度
|
||||
if (options.loading !== false) {
|
||||
uni.showLoading({
|
||||
title: options.loadingText || '上传中...',
|
||||
mask: true,
|
||||
});
|
||||
}
|
||||
|
||||
uni.uploadFile({
|
||||
url: BASE_URL + url,
|
||||
filePath,
|
||||
name: options.name || 'file',
|
||||
header: {
|
||||
Authorization: token ? `Bearer ${token}` : '',
|
||||
...options.header,
|
||||
},
|
||||
formData: options.formData || {},
|
||||
success: (res) => {
|
||||
if (options.loading !== false) {
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
try {
|
||||
const data = JSON.parse(res.data);
|
||||
if (data.code === 200 || data.success === true) {
|
||||
resolve(data);
|
||||
} else {
|
||||
const message = data.message || data.msg || '上传失败';
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none',
|
||||
});
|
||||
reject(new Error(message));
|
||||
}
|
||||
} catch (e) {
|
||||
reject(new Error('响应数据解析失败'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
if (options.loading !== false) {
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
const message = err.errMsg || '上传失败';
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none',
|
||||
});
|
||||
reject(new Error(message));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user