更新
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
import config from '@/config/index.js'
|
||||
|
||||
// 全局存储当前路由
|
||||
let currentRoute = ''
|
||||
|
||||
/**
|
||||
* 设置当前路由
|
||||
* @param {string} route
|
||||
*/
|
||||
export function setCurrentRoute(route) {
|
||||
currentRoute = route
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已登录
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isLogin() {
|
||||
try {
|
||||
const token = uni.getStorageSync('token')
|
||||
return !!token
|
||||
} catch (e) {
|
||||
console.error('检查登录状态失败:', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前页面路径
|
||||
* @returns {string}
|
||||
*/
|
||||
export function getCurrentRoute() {
|
||||
try {
|
||||
// 如果有存储的路由,优先使用
|
||||
if (currentRoute) {
|
||||
return currentRoute
|
||||
}
|
||||
|
||||
const pages = getCurrentPages()
|
||||
if (pages && pages.length > 0) {
|
||||
const currentPage = pages[pages.length - 1]
|
||||
const route = currentPage ? `/${currentPage.route}` : ''
|
||||
currentRoute = route
|
||||
return route
|
||||
}
|
||||
|
||||
return ''
|
||||
} catch (e) {
|
||||
console.error('获取当前路由失败:', e)
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前路由是否在白名单中
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isInWhitelist(route) {
|
||||
try {
|
||||
const currentRoute = route || getCurrentRoute()
|
||||
if (!currentRoute) return false
|
||||
|
||||
return config.whitelist.some(path => currentRoute.includes(path))
|
||||
} catch (e) {
|
||||
console.error('检查白名单失败:', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查登录状态,未登录则跳转到登录页
|
||||
* @returns {boolean} 是否已登录
|
||||
*/
|
||||
export function checkLogin() {
|
||||
try {
|
||||
const currentRoute = getCurrentRoute()
|
||||
|
||||
// 如果当前路由为空,可能是页面刚初始化,暂不检查
|
||||
if (!currentRoute) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 如果在白名单中,不检查登录状态
|
||||
if (isInWhitelist(currentRoute)) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 检查是否已登录
|
||||
if (isLogin()) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 未登录,跳转到登录页
|
||||
if (currentRoute !== '/pages/ucenter/login/index') {
|
||||
uni.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: '/pages/ucenter/login/index',
|
||||
fail: (err) => {
|
||||
console.error('跳转登录页失败:', err)
|
||||
}
|
||||
})
|
||||
}, 1500)
|
||||
}
|
||||
|
||||
return false
|
||||
} catch (e) {
|
||||
console.error('检查登录失败:', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
export function logout() {
|
||||
try {
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('userInfo')
|
||||
|
||||
uni.showToast({
|
||||
title: '已退出登录',
|
||||
icon: 'success',
|
||||
duration: 1500
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: '/pages/ucenter/login/index',
|
||||
fail: (err) => {
|
||||
console.error('跳转登录页失败:', err)
|
||||
}
|
||||
})
|
||||
}, 1500)
|
||||
} catch (e) {
|
||||
console.error('退出登录失败:', e)
|
||||
}
|
||||
}
|
||||
@@ -1,208 +1,73 @@
|
||||
import http from 'luch-request'
|
||||
import Request from 'luch-request'
|
||||
import sysConfig from '@/config'
|
||||
import store from '@/store'
|
||||
|
||||
// 创建实例
|
||||
const httpInstance = http.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
|
||||
timeout: 10000,
|
||||
header: {
|
||||
'Content-Type': 'application/json;charset=UTF-8'
|
||||
}
|
||||
const request = new Request()
|
||||
|
||||
// 错误码常量
|
||||
const ERROR_CODES = {
|
||||
TOKEN_EXPIRED: 2000,
|
||||
TOKEN_INVALID: 2001
|
||||
}
|
||||
|
||||
// HTTP状态码处理映射
|
||||
const HTTP_ERROR_MESSAGES = {
|
||||
400: '请求参数错误',
|
||||
401: '未授权,请重新登录',
|
||||
403: '拒绝访问',
|
||||
404: '请求地址不存在',
|
||||
500: '服务器内部错误',
|
||||
502: '网络连接失败',
|
||||
503: '服务不可用',
|
||||
504: '网关超时'
|
||||
}
|
||||
|
||||
request.setConfig((config) => {
|
||||
config.baseURL = sysConfig.baseURL
|
||||
config.header = {
|
||||
'content-type': 'application/json',
|
||||
}
|
||||
config.timeout = 30000
|
||||
config.sslVerify = false
|
||||
return config
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
httpInstance.interceptors.request.use(
|
||||
(config) => {
|
||||
// 从本地存储获取 token
|
||||
const token = uni.getStorageSync('token')
|
||||
if (token) {
|
||||
config.header = {
|
||||
...config.header,
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
request.interceptors.request.use((config) => {
|
||||
// 从本地存储获取 token
|
||||
const token = store.state.user.token || ''
|
||||
config.header.Authorization = `Bearer ${token}`
|
||||
|
||||
// 添加时间戳,防止缓存
|
||||
if (config.method === 'GET') {
|
||||
config.params = {
|
||||
...config.params,
|
||||
_t: Date.now()
|
||||
}
|
||||
}
|
||||
return config
|
||||
}, (error) => {
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
// 显示加载提示
|
||||
if (config.loading !== false) {
|
||||
uni.showLoading({
|
||||
title: config.loadingText || '加载中...',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
request.interceptors.response.use((response) => {
|
||||
const { code, message } = response.data || {}
|
||||
|
||||
return config
|
||||
},
|
||||
(config) => {
|
||||
return Promise.reject(config)
|
||||
}
|
||||
)
|
||||
// 处理 token 过期或无效
|
||||
if (code === ERROR_CODES.TOKEN_EXPIRED || code === ERROR_CODES.TOKEN_INVALID) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: message || '登录已过期,请重新登录',
|
||||
duration: 1500
|
||||
})
|
||||
setTimeout(() => {
|
||||
store.dispatch('userLogout')
|
||||
}, 100)
|
||||
return Promise.reject(response.data)
|
||||
}
|
||||
|
||||
// 响应拦截器
|
||||
httpInstance.interceptors.response.use(
|
||||
(response) => {
|
||||
// 隐藏加载提示
|
||||
if (response.config.loading !== false) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
return response.data
|
||||
}, (error) => {
|
||||
const { statusCode, errMsg } = error || {}
|
||||
const errorMessage = HTTP_ERROR_MESSAGES[statusCode] || errMsg || '请求失败,请稍后重试'
|
||||
|
||||
const { statusCode, data } = response
|
||||
if (statusCode === 401) {
|
||||
store.dispatch('userLogout')
|
||||
}
|
||||
|
||||
// HTTP 状态码判断
|
||||
if (statusCode !== 200) {
|
||||
showError('网络请求失败')
|
||||
return Promise.reject(response)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
// 业务状态码判断
|
||||
if (data.code !== undefined) {
|
||||
// 成功响应
|
||||
if (data.code === 0 || data.code === 200 || data.code === 1) {
|
||||
return data.data !== undefined ? data.data : data
|
||||
}
|
||||
|
||||
// 业务错误处理
|
||||
if (data.code === 401) {
|
||||
// token 失效,跳转登录页
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('userInfo')
|
||||
uni.showToast({
|
||||
title: '登录已过期,请重新登录',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/login/index'
|
||||
})
|
||||
}, 2000)
|
||||
return Promise.reject(data)
|
||||
}
|
||||
|
||||
// 其他业务错误
|
||||
showError(data.message || data.msg || '请求失败')
|
||||
return Promise.reject(data)
|
||||
}
|
||||
|
||||
return data
|
||||
},
|
||||
(error) => {
|
||||
// 隐藏加载提示
|
||||
if (error.config && error.config.loading !== false) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
|
||||
let errorMessage = '网络请求失败'
|
||||
|
||||
if (error.response) {
|
||||
const { statusCode, data } = error.response
|
||||
switch (statusCode) {
|
||||
case 400:
|
||||
errorMessage = data?.message || '请求参数错误'
|
||||
break
|
||||
case 401:
|
||||
errorMessage = '登录已过期,请重新登录'
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('userInfo')
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/login/index'
|
||||
})
|
||||
}, 2000)
|
||||
break
|
||||
case 403:
|
||||
errorMessage = '没有权限访问'
|
||||
break
|
||||
case 404:
|
||||
errorMessage = '请求的资源不存在'
|
||||
break
|
||||
case 500:
|
||||
errorMessage = '服务器内部错误'
|
||||
break
|
||||
case 502:
|
||||
errorMessage = '网关错误'
|
||||
break
|
||||
case 503:
|
||||
errorMessage = '服务不可用'
|
||||
break
|
||||
case 504:
|
||||
errorMessage = '网关超时'
|
||||
break
|
||||
default:
|
||||
errorMessage = data?.message || `请求失败 (${statusCode})`
|
||||
}
|
||||
} else if (error.errMsg) {
|
||||
// 网络错误
|
||||
if (error.errMsg.includes('timeout')) {
|
||||
errorMessage = '请求超时,请检查网络连接'
|
||||
} else if (error.errMsg.includes('network')) {
|
||||
errorMessage = '网络连接失败,请检查网络'
|
||||
}
|
||||
}
|
||||
|
||||
showError(errorMessage)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 显示错误提示
|
||||
function showError(message) {
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
}
|
||||
|
||||
// 封装常用请求方法
|
||||
export default {
|
||||
// GET 请求
|
||||
get(url, params = {}, config = {}) {
|
||||
return httpInstance.get(url, {
|
||||
params,
|
||||
...config
|
||||
})
|
||||
},
|
||||
|
||||
// POST 请求
|
||||
post(url, data = {}, config = {}) {
|
||||
return httpInstance.post(url, data, config)
|
||||
},
|
||||
|
||||
// PUT 请求
|
||||
put(url, data = {}, config = {}) {
|
||||
return httpInstance.put(url, data, config)
|
||||
},
|
||||
|
||||
// DELETE 请求
|
||||
delete(url, data = {}, config = {}) {
|
||||
return httpInstance.delete(url, {
|
||||
data,
|
||||
...config
|
||||
})
|
||||
},
|
||||
|
||||
// 文件上传
|
||||
upload(url, filePath, formData = {}, config = {}) {
|
||||
return httpInstance.upload(url, {
|
||||
filePath,
|
||||
formData,
|
||||
name: 'file',
|
||||
...config
|
||||
})
|
||||
},
|
||||
|
||||
// 文件下载
|
||||
download(url, config = {}) {
|
||||
return httpInstance.download(url, config)
|
||||
},
|
||||
|
||||
// 原始实例,用于特殊场景
|
||||
instance: httpInstance
|
||||
}
|
||||
export default request
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* @Descripttion: 工具集
|
||||
* @version: 1.2
|
||||
* @LastEditors: sakuya
|
||||
* @LastEditTime: 2022年5月24日00:28:56
|
||||
*/
|
||||
import CryptoJS from 'crypto-js';
|
||||
import sysConfig from "@/config";
|
||||
|
||||
const tool = {}
|
||||
|
||||
/* localStorage */
|
||||
tool.data = {
|
||||
set(key, data, datetime = 0) {
|
||||
//加密
|
||||
if(sysConfig.LS_ENCRYPTION == "AES"){
|
||||
data = tool.crypto.AES.encrypt(JSON.stringify(data), sysConfig.LS_ENCRYPTION_key)
|
||||
}
|
||||
let cacheValue = {
|
||||
content: data,
|
||||
datetime: parseInt(datetime) === 0 ? 0 : new Date().getTime() + parseInt(datetime) * 1000
|
||||
}
|
||||
return uni.setStorageSync(key, JSON.stringify(cacheValue))
|
||||
},
|
||||
get(key) {
|
||||
try {
|
||||
const value = JSON.parse(uni.getStorageSync(key))
|
||||
if (value) {
|
||||
let nowTime = new Date().getTime()
|
||||
if (nowTime > value.datetime && value.datetime != 0) {
|
||||
uni.removeStorageSync(key)
|
||||
return null;
|
||||
}
|
||||
//解密
|
||||
if(sysConfig.LS_ENCRYPTION == "AES"){
|
||||
value.content = JSON.parse(tool.crypto.AES.decrypt(value.content, sysConfig.LS_ENCRYPTION_key))
|
||||
}
|
||||
return value.content
|
||||
}
|
||||
return null
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
},
|
||||
remove(key) {
|
||||
return uni.removeStorageSync(key)
|
||||
},
|
||||
clear() {
|
||||
return uni.clearStorageSync()
|
||||
}
|
||||
}
|
||||
|
||||
/*sessionStorage - 仅在 H5 环境下可用*/
|
||||
// #ifdef H5
|
||||
tool.session = {
|
||||
set(table, settings) {
|
||||
const _set = JSON.stringify(settings)
|
||||
return sessionStorage.setItem(table, _set)
|
||||
},
|
||||
get(table) {
|
||||
const data = sessionStorage.getItem(table)
|
||||
try {
|
||||
return JSON.parse(data)
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
},
|
||||
remove(table) {
|
||||
return sessionStorage.removeItem(table)
|
||||
},
|
||||
clear() {
|
||||
return sessionStorage.clear()
|
||||
}
|
||||
}
|
||||
|
||||
/*cookie - 仅在 H5 环境下可用*/
|
||||
tool.cookie = {
|
||||
set(name, value, config = {}) {
|
||||
const cfg = {
|
||||
expires: null,
|
||||
path: null,
|
||||
domain: null,
|
||||
secure: false,
|
||||
httpOnly: false,
|
||||
...config
|
||||
}
|
||||
let cookieStr = `${name}=${encodeURIComponent(value)}`
|
||||
if (cfg.expires) {
|
||||
const exp = new Date()
|
||||
exp.setTime(exp.getTime() + parseInt(cfg.expires) * 1000)
|
||||
cookieStr += `;expires=${exp.toUTCString()}`
|
||||
}
|
||||
if (cfg.path) {
|
||||
cookieStr += `;path=${cfg.path}`
|
||||
}
|
||||
if (cfg.domain) {
|
||||
cookieStr += `;domain=${cfg.domain}`
|
||||
}
|
||||
document.cookie = cookieStr
|
||||
},
|
||||
get(name) {
|
||||
const arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"))
|
||||
if (arr != null) {
|
||||
return decodeURIComponent(arr[2])
|
||||
}
|
||||
return null
|
||||
},
|
||||
remove(name) {
|
||||
const exp = new Date()
|
||||
exp.setTime(exp.getTime() - 1)
|
||||
document.cookie = `${name}=;expires=${exp.toUTCString()}`
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
// 非 H5 环境下提供空实现,避免调用报错
|
||||
tool.session = {
|
||||
set() { console.warn('sessionStorage 仅在 H5 环境下可用') },
|
||||
get() { return null },
|
||||
remove() {},
|
||||
clear() {}
|
||||
}
|
||||
tool.cookie = {
|
||||
set() { console.warn('cookie 仅在 H5 环境下可用') },
|
||||
get() { return null },
|
||||
remove() {}
|
||||
}
|
||||
// #endif
|
||||
|
||||
/* 复制对象 */
|
||||
tool.objCopy = function (obj) {
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
/* 日期格式化 */
|
||||
tool.dateFormat = function (date, fmt='yyyy-MM-dd hh:mm:ss') {
|
||||
date = new Date(date)
|
||||
var o = {
|
||||
"M+" : date.getMonth()+1, //月份
|
||||
"d+" : date.getDate(), //日
|
||||
"h+" : date.getHours(), //小时
|
||||
"m+" : date.getMinutes(), //分
|
||||
"s+" : date.getSeconds(), //秒
|
||||
"q+" : Math.floor((date.getMonth()+3)/3), //季度
|
||||
"S" : date.getMilliseconds() //毫秒
|
||||
};
|
||||
if(/(y+)/.test(fmt)) {
|
||||
fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
|
||||
}
|
||||
for(var k in o) {
|
||||
if(new RegExp("("+ k +")").test(fmt)){
|
||||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
|
||||
}
|
||||
}
|
||||
return fmt;
|
||||
}
|
||||
/* 千分符 */
|
||||
tool.groupSeparator = function (num) {
|
||||
num = num + '';
|
||||
if(!num.includes('.')){
|
||||
num += '.'
|
||||
}
|
||||
return num.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
|
||||
return $1 + ',';
|
||||
}).replace(/\.$/, '');
|
||||
}
|
||||
|
||||
/* 常用加解密 */
|
||||
tool.crypto = {
|
||||
//MD5加密
|
||||
MD5(data){
|
||||
return CryptoJS.MD5(data).toString()
|
||||
},
|
||||
//BASE64加解密
|
||||
BASE64: {
|
||||
encrypt(data){
|
||||
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))
|
||||
},
|
||||
decrypt(cipher){
|
||||
return CryptoJS.enc.Base64.parse(cipher).toString(CryptoJS.enc.Utf8)
|
||||
}
|
||||
},
|
||||
//AES加解密
|
||||
AES: {
|
||||
encrypt(data, secretKey, config={}){
|
||||
if(secretKey.length % 8 != 0){
|
||||
console.warn("[SCUI error]: 秘钥长度需为8的倍数,否则解密将会失败。")
|
||||
}
|
||||
const result = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(secretKey), {
|
||||
iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
|
||||
mode: CryptoJS.mode[config.mode || "ECB"],
|
||||
padding: CryptoJS.pad[config.padding || "Pkcs7"]
|
||||
})
|
||||
return result.toString()
|
||||
},
|
||||
decrypt(cipher, secretKey, config={}){
|
||||
const result = CryptoJS.AES.decrypt(cipher, CryptoJS.enc.Utf8.parse(secretKey), {
|
||||
iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
|
||||
mode: CryptoJS.mode[config.mode || "ECB"],
|
||||
padding: CryptoJS.pad[config.padding || "Pkcs7"]
|
||||
})
|
||||
return CryptoJS.enc.Utf8.stringify(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default tool
|
||||
Reference in New Issue
Block a user