feat: 端点登录
This commit is contained in:
268
utils/request.js
268
utils/request.js
@@ -1,119 +1,177 @@
|
||||
import define from './define'
|
||||
import {
|
||||
useLocale
|
||||
} from '@/locale/useLocale';
|
||||
import { useLocale } from '@/locale/useLocale';
|
||||
|
||||
const {
|
||||
getBackLocale
|
||||
} = useLocale();
|
||||
const { getBackLocale } = useLocale();
|
||||
let host = define.baseURL
|
||||
const defaultOpt = {
|
||||
load: true
|
||||
}
|
||||
const defaultOpt = { load: true }
|
||||
|
||||
// 示例
|
||||
// async xxxx(code) {
|
||||
// var res = await this.request({
|
||||
// url: '/api/System/DictionaryData/All',
|
||||
// method: 'GET',
|
||||
// data,
|
||||
// options: {
|
||||
// load: false
|
||||
// }
|
||||
// })
|
||||
// if (!res) return
|
||||
// console.log(res)
|
||||
// }
|
||||
// ------------- token刷新核心配置 -------------
|
||||
const whiteList = ['/login', '/system/auth/refresh-token']
|
||||
let requestQueue = []
|
||||
let isRefreshingToken = false
|
||||
const isRelogin = { show: false }
|
||||
|
||||
// ------------- 核心request方法 -------------
|
||||
function request(config) {
|
||||
config.options = Object.assign(defaultOpt, config.options)
|
||||
const token = uni.getStorageSync('token') || ''
|
||||
const tenantId = uni.getStorageSync('tenantId') || ''
|
||||
const systemCode = uni.getStorageSync('systemCode') || ''
|
||||
const locale = getBackLocale()
|
||||
let header = {
|
||||
"accept" : 'application/json, text/plain, */*',
|
||||
"App-Code": systemCode,
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
"Jnpf-Origin": "app",
|
||||
"Vue-Version": "3",
|
||||
"Accept-Language": locale,
|
||||
"tenant-id": tenantId,
|
||||
...config.header
|
||||
}
|
||||
header['App-Code'] = encodeURIComponent(header['App-Code'])
|
||||
if (token) header['Authorization'] = token
|
||||
// 测试 todo
|
||||
console.log(config.url,config.url.includes('admin-api'),'config.url.includes---')
|
||||
if(config.url.includes('admin-api')){
|
||||
host = 'http://10.28.117.48:48080'
|
||||
}else {
|
||||
host = define.baseURL
|
||||
}
|
||||
let url = config.url.indexOf('http') > -1 ? config.url : host + config.url
|
||||
|
||||
config.options = Object.assign(defaultOpt, config.options)
|
||||
const token = uni.getStorageSync('token') || ''
|
||||
const refreshToken = uni.getStorageSync('refreshToken') || ''
|
||||
const tenantId = '1' || uni.getStorageSync('tenantId')
|
||||
const systemCode = uni.getStorageSync('systemCode') || ''
|
||||
const locale = getBackLocale()
|
||||
|
||||
if (config.options.load) {
|
||||
uni.showLoading({
|
||||
title: config.options.loadText || '正在加载'
|
||||
})
|
||||
}
|
||||
// 构建请求头
|
||||
let header = {
|
||||
"accept": 'application/json, text/plain, */*',
|
||||
"App-Code": systemCode,
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
"Jnpf-Origin": "app",
|
||||
"Vue-Version": "3",
|
||||
"Accept-Language": locale,
|
||||
"tenant-id": tenantId,
|
||||
...config.header
|
||||
}
|
||||
header['App-Code'] = encodeURIComponent(header['App-Code'])
|
||||
if (token) header['Authorization'] = token
|
||||
// let url = config.url.indexOf('http') > -1 ? config.url : host + config.url
|
||||
let url = host + config.url
|
||||
console.log(url,'url---')
|
||||
// 3. 显示加载中
|
||||
if (config.options.load) {
|
||||
uni.showLoading({ title: config.options.loadText || '正在加载' })
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: url,
|
||||
data: config.data || {},
|
||||
method: config.method || 'GET',
|
||||
header: header,
|
||||
timeout: define.timeout,
|
||||
success: res => {
|
||||
if (res.statusCode === 200) {
|
||||
if (res.data.code == 200 || res.data.code == 0) {
|
||||
resolve(res.data)
|
||||
} else {
|
||||
ajaxError(res.data)
|
||||
reject(res.data.msg)
|
||||
}
|
||||
} else {
|
||||
ajaxError(res.data)
|
||||
reject(res.errMsg)
|
||||
}
|
||||
uni.hideLoading();
|
||||
},
|
||||
fail: err => {
|
||||
uni.showToast({
|
||||
title: '连接服务器失败',
|
||||
icon: 'none',
|
||||
})
|
||||
setTimeout(function () {
|
||||
uni.hideLoading();
|
||||
}, 2000);
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
// 4. 返回Promise,核心改造逻辑
|
||||
return new Promise((resolve, reject) => {
|
||||
// 封装核心请求逻辑
|
||||
const coreRequest = () => {
|
||||
uni.request({
|
||||
url: url,
|
||||
data: config.data || {},
|
||||
method: config.method || 'GET',
|
||||
header: header,
|
||||
timeout: define.timeout,
|
||||
success: async (res) => {
|
||||
uni.hideLoading();
|
||||
if (res.statusCode === 200) {
|
||||
if (res.data.code == 200 || res.data.code == 0) {
|
||||
resolve(res.data)
|
||||
} else if ([401, 600, 601, 602].includes(res.data.code)) {
|
||||
// 白名单接口不处理
|
||||
const isWhiteList = whiteList.some(v => config.url.includes(v))
|
||||
if (isWhiteList) {
|
||||
ajaxError(res.data)
|
||||
reject(res.data.msg)
|
||||
return
|
||||
}
|
||||
|
||||
// ------------- 核心调整:刷新token请求改为问号拼接参数 -------------
|
||||
if (!refreshToken) {
|
||||
await handleAuthorized()
|
||||
reject(res.data.msg)
|
||||
return
|
||||
}
|
||||
|
||||
if (isRefreshingToken) {
|
||||
requestQueue.push(() => coreRequest())
|
||||
return
|
||||
}
|
||||
|
||||
isRefreshingToken = true
|
||||
try {
|
||||
// 拼接URL参数:和PC端一致,POST方法 + ?refreshToken=xxx
|
||||
const refreshUrl = `${host}/admin-api/system/auth/refresh-token?refreshToken=${encodeURIComponent(refreshToken)}`
|
||||
// 调用刷新token接口(POST方法 + URL参数,无请求体)
|
||||
const refreshRes = await uni.request({
|
||||
url: refreshUrl, // 带参数的URL
|
||||
method: 'POST', // 保持POST方法
|
||||
header: {
|
||||
'tenant-id': tenantId, // 携带租户ID
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: {} // 无请求体,参数全在URL上
|
||||
})
|
||||
|
||||
// 刷新成功处理
|
||||
if (refreshRes.statusCode === 200 && refreshRes.data.code === 200) {
|
||||
const newTokenData = refreshRes.data.data
|
||||
// 存储新token(建议用项目封装的setToken方法)
|
||||
uni.setStorageSync('token', newTokenData.token)
|
||||
uni.setStorageSync('refreshToken', newTokenData.refreshToken)
|
||||
// 更新请求头
|
||||
header['Authorization'] = newTokenData.token
|
||||
// 重试当前请求
|
||||
coreRequest()
|
||||
// 执行队列请求
|
||||
requestQueue.forEach(cb => cb())
|
||||
requestQueue = []
|
||||
} else {
|
||||
await handleAuthorized()
|
||||
reject('刷新token失败:' + (refreshRes.data?.msg || '接口返回异常'))
|
||||
}
|
||||
} catch (err) {
|
||||
await handleAuthorized()
|
||||
reject('刷新token异常:' + err.errMsg)
|
||||
} finally {
|
||||
isRefreshingToken = false
|
||||
requestQueue = []
|
||||
}
|
||||
} else {
|
||||
ajaxError(res.data)
|
||||
reject(res.data.msg)
|
||||
}
|
||||
} else {
|
||||
ajaxError(res.data)
|
||||
reject(res.errMsg)
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: '连接服务器失败', icon: 'none' })
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
coreRequest()
|
||||
})
|
||||
}
|
||||
|
||||
// ------------- 辅助函数 -------------
|
||||
function ajaxError(data) {
|
||||
uni.showToast({
|
||||
title: data.msg || '请求出错,请重试',
|
||||
icon: 'none',
|
||||
complete() {
|
||||
if (data.code === 600 || data.code === 601 || data.code === 602 || data.code === 401) {
|
||||
setTimeout(() => {
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('cid')
|
||||
uni.removeStorageSync('userInfo')
|
||||
uni.removeStorageSync('permissionList')
|
||||
uni.removeStorageSync('sysVersion')
|
||||
uni.removeStorageSync('dynamicModelExtra')
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/index'
|
||||
})
|
||||
}, 1500)
|
||||
}
|
||||
}
|
||||
})
|
||||
uni.showToast({
|
||||
title: data.msg || '请求出错,请重试',
|
||||
icon: 'none',
|
||||
complete() {
|
||||
if ([600, 601, 602, 401].includes(data.code) && !isRefreshingToken) {
|
||||
setTimeout(() => handleAuthorized(), 1500)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function handleAuthorized() {
|
||||
if (!isRelogin.show) {
|
||||
isRelogin.show = true
|
||||
const res = await uni.showModal({
|
||||
title: '登录过期',
|
||||
content: '您的登录已过期,请重新登录',
|
||||
showCancelButton: false,
|
||||
confirmText: '重新登录'
|
||||
})
|
||||
if (res.confirm) {
|
||||
// 清除缓存(建议用项目封装的removeToken方法)
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('refreshToken')
|
||||
uni.removeStorageSync('cid')
|
||||
uni.removeStorageSync('userInfo')
|
||||
uni.removeStorageSync('permissionList')
|
||||
uni.removeStorageSync('sysVersion')
|
||||
uni.removeStorageSync('dynamicModelExtra')
|
||||
uni.reLaunch({ url: '/pages/login/index' })
|
||||
}
|
||||
isRelogin.show = false
|
||||
}
|
||||
}
|
||||
|
||||
export default request
|
||||
Reference in New Issue
Block a user