feat: 新增发布额度权益与订单管理功能,优化多端交互细节
这个提交包含在:
2087
uniapp/common/utils/qrcode-generator.js
普通文件
2087
uniapp/common/utils/qrcode-generator.js
普通文件
文件差异内容过多而无法显示
加载差异
@@ -12,7 +12,13 @@ import config from '../config/index.js'
|
||||
import storage from './storage.js'
|
||||
import { matchMock } from '../mock/index.js'
|
||||
|
||||
// 便于测试注入的运行时环境(默认使用全局 uni)
|
||||
// 401 跳转节流标记(毫秒)
|
||||
let lastLoginRedirectAt = 0
|
||||
|
||||
/**
|
||||
* 便于测试注入的运行时环境(默认使用全局 uni)
|
||||
* @returns {Object} 运行时环境对象
|
||||
*/
|
||||
export function getRuntime() {
|
||||
return {
|
||||
request: typeof uni !== 'undefined' && uni.request ? uni.request.bind(uni) : null,
|
||||
@@ -23,14 +29,23 @@ export function getRuntime() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 拼接完整地址 */
|
||||
/**
|
||||
* 拼接完整地址
|
||||
* @param {string} url 原始相对或绝对路径
|
||||
* @returns {string} 完整网络请求地址
|
||||
*/
|
||||
export function buildUrl(url) {
|
||||
if (/^https?:\/\//i.test(url)) return url
|
||||
const path = url.startsWith('/') ? url : `/${url}`
|
||||
return `${config.baseURL}/${config.apiVersion}${path}`
|
||||
}
|
||||
|
||||
/** 组装请求头 */
|
||||
/**
|
||||
* 组装请求头
|
||||
* 同时注入 Authorization、token、X-Token,确保在任何 Nginx/Apache 代理环境下 token 都能安全穿透到后端
|
||||
* @param {Object} extra 额外的自定义请求头
|
||||
* @returns {Object} 组装后的完整 Headers
|
||||
*/
|
||||
export function buildHeaders(extra = {}) {
|
||||
const token = storage.get(config.storageKeys.token, '')
|
||||
const headers = {
|
||||
@@ -38,14 +53,17 @@ export function buildHeaders(extra = {}) {
|
||||
'X-Client': 'fenleixinxi',
|
||||
...extra,
|
||||
}
|
||||
if (token) headers.Authorization = `Bearer ${token}`
|
||||
if (token) {
|
||||
// 1. 标准 HTTP Bearer Token
|
||||
headers.Authorization = `Bearer ${token}`
|
||||
// 2. 兼容 FastAdmin 原生与 Nginx/Apache 反向代理(部分反向代理会丢弃 Authorization,但保留常规 Header)
|
||||
headers.token = token
|
||||
headers.Token = token
|
||||
headers['X-Token'] = token
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
/**
|
||||
* 底层请求(返回 Promise<data>)
|
||||
* @param {Object} options { url, method, data, header, silent, mock }
|
||||
*/
|
||||
/**
|
||||
* 清理请求参数中 undefined / null / 空串 的无效字段
|
||||
* 防止在微信小程序等环境下 GET 请求被底层错误序列化为 "key=undefined"
|
||||
@@ -64,6 +82,11 @@ export function cleanParams(data) {
|
||||
return cleaned
|
||||
}
|
||||
|
||||
/**
|
||||
* 底层请求统一封装(返回 Promise<data>)
|
||||
* @param {Object} options 请求配置 { url, method, data, header, silent, mock }
|
||||
* @returns {Promise<any>} 请求结果
|
||||
*/
|
||||
export function request(options = {}) {
|
||||
const {
|
||||
url,
|
||||
@@ -95,7 +118,7 @@ export function request(options = {}) {
|
||||
header: headers,
|
||||
timeout: config.timeout,
|
||||
success(res) {
|
||||
// HTTP 层
|
||||
// HTTP 层 401 判定
|
||||
if (res.statusCode === 401) {
|
||||
onUnauthorized(runtime)
|
||||
return reject(toErr('登录已过期,请重新登录', 401))
|
||||
@@ -107,6 +130,11 @@ export function request(options = {}) {
|
||||
)
|
||||
}
|
||||
const body = res.data || {}
|
||||
// 业务层 401 判定
|
||||
if (body.code === 401) {
|
||||
onUnauthorized(runtime)
|
||||
return reject(toErr(body.message || body.msg || '登录已过期,请重新登录', 401))
|
||||
}
|
||||
// 业务层:约定 code===0 或 code===200 为成功
|
||||
if (body.code === 0 || body.code === 200) {
|
||||
resolve(body.data !== undefined ? body.data : body)
|
||||
@@ -127,6 +155,8 @@ export function request(options = {}) {
|
||||
* 失败处理:
|
||||
* 1) 若开启 mock 降级,则使用本地 mock 路由兜底(保证离线可演示)
|
||||
* 2) 否则抛出异常
|
||||
* @param {Object} err 错误对象
|
||||
* @param {Object} ctx 请求上下文
|
||||
*/
|
||||
function handleFail(err, ctx) {
|
||||
const { url, method, data, silent, runtime, resolve, reject } = ctx
|
||||
@@ -146,14 +176,42 @@ function handleFail(err, ctx) {
|
||||
reject(e)
|
||||
}
|
||||
|
||||
/** 401 处理 */
|
||||
/**
|
||||
* 401 未登录/登录失效统一处理
|
||||
* 1. 清除本地缓存的 token 与用户信息
|
||||
* 2. 3秒内节流防抖,避免并发请求时多次重复弹窗与重复跳转
|
||||
* 3. 避免当前已经在登录页时重复跳转
|
||||
* 4. 记录来源页面便于登录后无感跳回
|
||||
* @param {Object} runtime 运行时对象
|
||||
*/
|
||||
function onUnauthorized(runtime) {
|
||||
storage.remove(config.storageKeys.token)
|
||||
storage.remove(config.storageKeys.userInfo)
|
||||
runtime.showToast({ title: '登录已过期', icon: 'none' })
|
||||
runtime.navigateTo({ url: '/pages/auth/login' })
|
||||
|
||||
const now = Date.now()
|
||||
if (now - lastLoginRedirectAt < 3000) {
|
||||
return
|
||||
}
|
||||
lastLoginRedirectAt = now
|
||||
|
||||
const pages = (typeof getCurrentPages === 'function' && getCurrentPages()) || []
|
||||
const current = pages[pages.length - 1]
|
||||
const rawRoute = current && (current.route || (current.$page && current.$page.route))
|
||||
const route = typeof rawRoute === 'string' ? rawRoute : ''
|
||||
|
||||
if (!route || !route.includes('pages/auth/login')) {
|
||||
runtime.showToast({ title: '登录已过期', icon: 'none' })
|
||||
const redirectParam = route ? `?redirect=${encodeURIComponent('/' + route)}` : ''
|
||||
runtime.navigateTo({ url: `/pages/auth/login${redirectParam}` })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建标准化 Error 对象
|
||||
* @param {string} message 错误消息
|
||||
* @param {number} code 业务或状态码
|
||||
* @returns {Error}
|
||||
*/
|
||||
function toErr(message, code = -1) {
|
||||
const e = new Error(message)
|
||||
e.code = code
|
||||
|
||||
在新工单中引用
屏蔽一个用户