feat:小程序首页白屏问题
This commit is contained in:
1 parent
b22de4d6b2
commit
6412d3be5c
10 files changed
+209
-84
No files matched your search
@@ -1,6 +1,16 @@
|
||||
import request from "@/sheep/utils/request"
|
||||
|
||||
export default {
|
||||
// ========== 轮播图相关接口 ==========
|
||||
/**
|
||||
* 获取轮播图列表
|
||||
*/
|
||||
getSwiperList: () =>
|
||||
request({
|
||||
url: "/integrated/sysBanner/app/slides",
|
||||
method: "GET",
|
||||
}),
|
||||
|
||||
// 公告分页列表
|
||||
getNoticePage: (params: { current: number; size: number }) =>
|
||||
request({
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
import { throttle } from "radash"
|
||||
|
||||
function normalizePath(path = "") {
|
||||
if (!path) return ""
|
||||
const purePath = path.split("?")[0] || ""
|
||||
if (!purePath) return ""
|
||||
return purePath.startsWith("/") ? purePath : `/${purePath}`
|
||||
}
|
||||
|
||||
function getCurrentPagePath() {
|
||||
const currentPage = getCurrentPage()
|
||||
return normalizePath(currentPage?.route || "")
|
||||
}
|
||||
|
||||
function paramsToQuery(params: { [key: string]: string }) {
|
||||
let query = []
|
||||
for (let key in params) {
|
||||
@@ -14,6 +26,10 @@ function _jump(path = "", params = {}, type = 1) {
|
||||
if (!path) {
|
||||
console.error("router::jump>>path 不能为空")
|
||||
}
|
||||
const targetPath = normalizePath(path)
|
||||
if (targetPath && getCurrentPagePath() === targetPath) {
|
||||
return
|
||||
}
|
||||
if (hasTabbar(path)) {
|
||||
uni.switchTab({ url: path })
|
||||
return
|
||||
|
||||
@@ -22,3 +22,29 @@ export const filePath = (path?: string | null) => {
|
||||
|
||||
return `${base}${ensureLeadingSlash(rawPath)}`
|
||||
}
|
||||
|
||||
export const parseHtml = (html: string) => {
|
||||
const origin = "https://stgl.pj.gov.cn"
|
||||
return html.replace(/<img\b[^>]*>/gi, (imgTag) => {
|
||||
let next = imgTag
|
||||
|
||||
next = next.replace(/\bsrc\s*=\s*(['"])(.*?)\1/i, (_m, quote: string, src: string) => {
|
||||
let resolved = src
|
||||
if (resolved?.startsWith("/api")) resolved = `${origin}${resolved}`
|
||||
if (resolved?.startsWith("//")) resolved = `https:${resolved}`
|
||||
return `src=${quote}${resolved}${quote}`
|
||||
})
|
||||
|
||||
const styleMatch = next.match(/\bstyle\s*=\s*(['"])([\s\S]*?)\1/i)
|
||||
const required = "max-width: 100%; height: auto; display: block;"
|
||||
if (styleMatch) {
|
||||
const quote = styleMatch[1]
|
||||
const styleValue = styleMatch[2] ?? ""
|
||||
if (styleValue.includes("max-width") || styleValue.includes("width")) return next
|
||||
const merged = styleValue.trim().length ? `${styleValue.replace(/;?\s*$/, "; ")}${required}` : required
|
||||
return next.replace(/\bstyle\s*=\s*(['"])([\s\S]*?)\1/i, `style=${quote}${merged}${quote}`)
|
||||
}
|
||||
|
||||
return next.replace(/<img\b/i, `<img style="${required}"`)
|
||||
})
|
||||
}
|
||||
+51
-41
@@ -4,7 +4,7 @@ import { isEmpty } from "radash"
|
||||
|
||||
import * as router from "@@/router"
|
||||
import * as constant from "@@/constant"
|
||||
import { createCooldown, getBaseApiUrl } from "@@/utils/app"
|
||||
import { getBaseApiUrl } from "@@/utils/app"
|
||||
|
||||
const TOKEN_KEY = constant.storageConstant.TOKEN
|
||||
const USER_INFO_KEY = constant.storageConstant.USERINFO
|
||||
@@ -18,8 +18,13 @@ const SKIP_AUTH_URLS = [
|
||||
const SKIP_REFRESH_TOKEN = ["/sys/auth/refresh-token", "/auth/token/check_token"]
|
||||
|
||||
let refreshTokenLock = true
|
||||
const runLoginRedirect = createCooldown(1500)
|
||||
let refreshTokenPromise: Promise<any> | null = null
|
||||
const BASIC_AUTH = "Basic " + base64.encode(import.meta.env.VITE_OAUTH2_APP_CLIENT)
|
||||
const buildErrorResponse = (error: any, fallbackMsg: string = "请求失败") => ({
|
||||
code: error?.statusCode || -1,
|
||||
data: error?.data ?? null,
|
||||
msg: error?.data?.msg || error?.errMsg || error?.message || fallbackMsg,
|
||||
})
|
||||
|
||||
const https = new Request({
|
||||
baseURL: getBaseApiUrl(),
|
||||
@@ -58,13 +63,21 @@ https.interceptors.request.use(async (config) => {
|
||||
// 过期,刷新token
|
||||
const newTokenStorage = await refreshToken()
|
||||
if (newTokenStorage) {
|
||||
const tokenValue = getTokenValue(newTokenStorage)
|
||||
tokenValue = getTokenValue(newTokenStorage)
|
||||
if (tokenValue) config_header.Authorization = tokenValue
|
||||
} else {
|
||||
return Promise.reject({
|
||||
statusCode: 424,
|
||||
errMsg: "refresh token failed",
|
||||
data: { msg: "登录已过期,请重新登录" },
|
||||
config,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
config_header["Authorization"] = tokenValue
|
||||
}
|
||||
config.header = config_header
|
||||
return config
|
||||
}, Promise.reject)
|
||||
|
||||
@@ -79,7 +92,6 @@ https.interceptors.response.use(
|
||||
msg: null,
|
||||
})
|
||||
}
|
||||
|
||||
return Promise.resolve(response.data)
|
||||
},
|
||||
async (error) => {
|
||||
@@ -87,23 +99,16 @@ https.interceptors.response.use(
|
||||
// 处理 424 错误(token过期)
|
||||
if (error.statusCode === 424) {
|
||||
// 判断是否在登录页面
|
||||
const currentPage = router.getCurrentPage()
|
||||
const currentRoute = currentPage?.route
|
||||
if (currentRoute === "pages/root/login" || currentRoute === "/pages/root/login") {
|
||||
return
|
||||
}
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
runLoginRedirect(() => {
|
||||
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" })
|
||||
uni.navigateTo({ url: "/pages/root/login" })
|
||||
})
|
||||
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" })
|
||||
router.jump("/pages/root/login")
|
||||
}
|
||||
// 显示错误信息
|
||||
else if (error?.data?.msg) {
|
||||
uni.showToast({ title: error.data.msg, icon: "none", mask: true })
|
||||
}
|
||||
return Promise.resolve(error.data)
|
||||
return Promise.resolve(buildErrorResponse(error))
|
||||
},
|
||||
)
|
||||
|
||||
@@ -120,34 +125,39 @@ const isTokenExpires = (tokenStorage: GlobalSetting.TokenStorage): boolean => {
|
||||
}
|
||||
|
||||
async function refreshToken(): Promise<any> {
|
||||
refreshTokenLock = false
|
||||
try {
|
||||
const tokenStorage = uni.getStorageSync(TOKEN_KEY) as GlobalSetting.TokenStorage
|
||||
const res: any = await https.middleware({
|
||||
url: "/auth/oauth2/token",
|
||||
header: {
|
||||
skipToken: "true",
|
||||
Authorization: BASIC_AUTH,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
method: "POST",
|
||||
params: { refresh_token: tokenStorage.refreshToken, grant_type: "refresh_token", scope: "server" },
|
||||
})
|
||||
const { data } = res || {}
|
||||
if (data) {
|
||||
tokenStorage.accessToken = data?.accessToken?.tokenValue
|
||||
tokenStorage.refreshToken = data?.refreshToken?.tokenValue
|
||||
tokenStorage.accessTokenExpiresIn = data?.accessToken?.expiresAt
|
||||
tokenStorage.refreshTokenExpiresIn = data?.refreshToken?.expiresAt
|
||||
uni.setStorageSync(TOKEN_KEY, tokenStorage)
|
||||
return tokenStorage
|
||||
if (refreshTokenPromise) return refreshTokenPromise
|
||||
refreshTokenPromise = (async () => {
|
||||
refreshTokenLock = false
|
||||
try {
|
||||
const tokenStorage = uni.getStorageSync(TOKEN_KEY) as GlobalSetting.TokenStorage
|
||||
const res: any = await https.middleware({
|
||||
url: "/auth/oauth2/token",
|
||||
header: {
|
||||
skipToken: "true",
|
||||
Authorization: BASIC_AUTH,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
method: "POST",
|
||||
params: { refresh_token: tokenStorage.refreshToken, grant_type: "refresh_token", scope: "server" },
|
||||
})
|
||||
const { data } = res || {}
|
||||
if (data) {
|
||||
tokenStorage.accessToken = data?.accessToken?.tokenValue
|
||||
tokenStorage.refreshToken = data?.refreshToken?.tokenValue
|
||||
tokenStorage.accessTokenExpiresIn = data?.accessToken?.expiresAt
|
||||
tokenStorage.refreshTokenExpiresIn = data?.refreshToken?.expiresAt
|
||||
uni.setStorageSync(TOKEN_KEY, tokenStorage)
|
||||
return tokenStorage
|
||||
}
|
||||
return null
|
||||
} catch (error) {
|
||||
return false
|
||||
} finally {
|
||||
refreshTokenLock = true
|
||||
refreshTokenPromise = null
|
||||
}
|
||||
return null
|
||||
} catch (error) {
|
||||
return false
|
||||
} finally {
|
||||
refreshTokenLock = true
|
||||
}
|
||||
})()
|
||||
return refreshTokenPromise
|
||||
}
|
||||
|
||||
// 请求函数
|
||||
|
||||
Reference in new issue
Block a user