feat: 端点登录

This commit is contained in:
caijun
2026-01-25 20:22:58 +08:00
parent f9e1a0cc18
commit f43f2ff3c2
12 changed files with 551 additions and 444 deletions

View File

@@ -1,10 +1,10 @@
/* process.env.NODE_ENV设置生产环境模式 */
// #ifndef MP
const baseURL = process.env.NODE_ENV === "production" ? "http://10.0.64.25:48080" : "http://10.28.117.48:48080"
const baseURL = process.env.NODE_ENV === "production" ? "http://10.0.64.25:48080" : "http://10.0.64.25:48080"
const webSocketUrl = process.env.NODE_ENV === "production" ? "http://10.0.64.25:48080" :
"ws://10.28.117.183:30000/api/message/websocket"
const report = process.env.NODE_ENV === 'production' ? 'http://10.0.64.25:48080' : 'http://10.28.117.183:8200'
const pcURL = process.env.NODE_ENV === 'production' ? 'http://10.0.64.25:48080' : 'http://10.28.117.183:3000'
"ws://10.0.64.25:48080/api/message/websocket"
const report = process.env.NODE_ENV === 'production' ? 'http://10.0.64.25:48080' : 'http://10.0.64.25:48080'
const pcURL = process.env.NODE_ENV === 'production' ? 'http://10.0.64.25:48080' : 'http://10.0.64.25:48080'
// #endif
// #ifdef MP
@@ -15,7 +15,7 @@ const pcURL = process.env.NODE_ENV === 'production' ? 'http://10.0.64.25:48080'
// #endif
console.log(process.env.NODE_ENV === "production",'判断环境----------')
const define = {
copyright: "Copyright @ 2025 引迈信息技术有限公司版权所有",
copyright: "Copyright @ 2026 软通动力",
sysVersion: "V6.0",
baseURL, // 接口前缀
report,

View File

@@ -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