更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -1,7 +1,3 @@
|
||||
/**
|
||||
* 统一请求封装:baseURL、Bearer Token、业务 code 处理。
|
||||
* 接口地址统一从 .env 的 VITE_API_BASE_URL 读取(见 api/config.js)。
|
||||
*/
|
||||
import { getToken, logout, isSSOEnabled, getRefreshToken, setAuthTokens, isLoggedIn } from '@/utils/auth.js'
|
||||
import { getGuestToken, clearGuestToken } from '@/utils/guest.js'
|
||||
import { resolveBaseURL, getBaseURL } from '@/api/config.js'
|
||||
@@ -12,10 +8,6 @@ function buildFullUrl(url) {
|
||||
return resolveBaseURL().replace(/\/$/, '') + url
|
||||
}
|
||||
|
||||
/**
|
||||
* 用 refresh_token 换取新的访问令牌(独立封装,避免与 request 递归调用)。
|
||||
* 仅在统一认证模式(VITE_AUTH_MODE=sso)下使用。
|
||||
*/
|
||||
function refreshAccessToken() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const refresh = getRefreshToken()
|
||||
@@ -43,7 +35,6 @@ function refreshAccessToken() {
|
||||
})
|
||||
}
|
||||
|
||||
/** 续期后重放原请求,返回与 request 一致的 data 解包结果 */
|
||||
async function replayRequest(options) {
|
||||
const token = await refreshAccessToken()
|
||||
const res = await new Promise((resolve, reject) => {
|
||||
@@ -69,10 +60,6 @@ async function replayRequest(options) {
|
||||
return typeof res.data !== 'undefined' ? res.data : res
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ url: string, method?: string, data?: object, header?: object, silent?: boolean }} options
|
||||
* @returns {Promise<any>} 业务 data 字段(若无 data 则返回整包)
|
||||
*/
|
||||
export function request(options = {}) {
|
||||
const { url, method = 'GET', data, header = {}, silent = false, timeout = 30000 } = options
|
||||
const headers = {
|
||||
@@ -95,8 +82,6 @@ export function request(options = {}) {
|
||||
const body = res.data || {}
|
||||
|
||||
if (status === 401 || body.code === 401) {
|
||||
// 游客令牌过期/被吊销:只清游客态并回门户重新授权,
|
||||
// 不进登录引导(免登录模块对游客令牌报 403 端不匹配,不会走到这里)。
|
||||
if (!isLoggedIn() && getGuestToken()) {
|
||||
clearGuestToken()
|
||||
if (!silent) {
|
||||
@@ -108,24 +93,17 @@ export function request(options = {}) {
|
||||
reject(new Error(body.msg || '未授权'))
|
||||
return
|
||||
}
|
||||
// 统一认证模式:先用 refresh_token 静默续期并重放请求,
|
||||
// 续期失败(令牌过期/被吊销/被踢下线)才真正登出。
|
||||
if (isSSOEnabled()) {
|
||||
try {
|
||||
resolve(await replayRequest(options))
|
||||
return
|
||||
} catch (e) {
|
||||
// 续期失败,继续走下面的登出流程
|
||||
}
|
||||
}
|
||||
logout()
|
||||
if (!silent) {
|
||||
uni.showToast({ title: body.msg || '请重新登录', icon: 'none' })
|
||||
}
|
||||
// 门户首页 nav 是默认落地页,允许游客停留。
|
||||
// 若 401 发生在 nav(多为残留/过期令牌触发的 /api/home),
|
||||
// 登出后回 nav 以游客态重载,而不是强跳统一认证登录页;
|
||||
// 业务内页(需登录)仍照常引导到登录。
|
||||
const onNav = (() => {
|
||||
const ps = (typeof getCurrentPages === 'function' && getCurrentPages()) || []
|
||||
const cur = ps.length ? ps[ps.length - 1] : null
|
||||
@@ -144,14 +122,12 @@ export function request(options = {}) {
|
||||
const msg = body.msg || `请求失败(${status})`
|
||||
if (!silent) uni.showToast({ title: msg, icon: 'none' })
|
||||
const err = new Error(msg)
|
||||
// 保留响应体:登录等接口用 reason 等字段做差异化引导
|
||||
err.code = typeof body.code !== 'undefined' ? body.code : status
|
||||
err.response = body
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 统一后端 { code, msg, data }
|
||||
if (typeof body.code !== 'undefined' && body.code !== 200) {
|
||||
const msg = body.msg || '操作失败'
|
||||
if (!silent) uni.showToast({ title: msg, icon: 'none' })
|
||||
@@ -179,10 +155,6 @@ export function request(options = {}) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入 Authorization:正式令牌优先;未登录但持有游客令牌(手机号授权签发)时带游客令牌,
|
||||
* 由后端各控制器自行决定是否放行 guest 端(不放行者报 403,不影响游客态浏览)。
|
||||
*/
|
||||
function applyAuth(headers) {
|
||||
const token = getToken()
|
||||
if (token) {
|
||||
@@ -195,7 +167,6 @@ function applyAuth(headers) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 返回完整响应体(含 code/msg),用于需要自行判断 code 的场景 */
|
||||
export function requestRaw(options = {}) {
|
||||
const { url, method = 'GET', data, header = {} } = options
|
||||
const headers = {
|
||||
|
||||
Reference in new issue
Block a user