您的位置:首页 > 新闻 > 热点要闻 > 花色98堂新网名内容与理念_阿里邮箱注册_摘抄一则新闻_时空seo助手

花色98堂新网名内容与理念_阿里邮箱注册_摘抄一则新闻_时空seo助手

2024/12/5 21:04:53 来源:https://blog.csdn.net/haodanzj/article/details/143701855  浏览:    关键词:花色98堂新网名内容与理念_阿里邮箱注册_摘抄一则新闻_时空seo助手
花色98堂新网名内容与理念_阿里邮箱注册_摘抄一则新闻_时空seo助手

import { apiUrl } from '@/config/global-config.js'
import { useUserStore } from '../stores'
import { usePageRoute } from "@/composable/usePageRoute.js"// 默认配置
const DEFAULT_CONFIG = {timeout: 60000,// showLoading: true,// loadingText: '加载中...'
}// 添加刷新token的方法
const refreshToken = async () => {const userStore = useUserStore()try {const response = await uni.request({url: apiUrl + '/auth/refresh',  // 替换成你的刷新token接口method: 'POST',header: {'Content-Type': 'application/json','Authorization': `Bearer ${userStore.token}`}})if (response.data.code === 200) {userStore.setToken(response.data.data.token)return true}return false} catch (error) {return false}
}// 添加一个变量来存储刷新token的Promise
let isRefreshing = false
let refreshSubscribers = []// 执行等待的请求
const onRefreshed = (token) => {refreshSubscribers.forEach(callback => callback(token))refreshSubscribers = []
}// 添加请求到队列
const addSubscriber = (callback) => {refreshSubscribers.push(callback)
}// 请求拦截器
const requestInterceptor = (config) => {const userStore = useUserStore()const token = userStore.tokenconst isMapApi = /apis\.map\.qq\.com/.test(config.url)if (token && !isMapApi) {config.header.Authorization = `Bearer ${token}`}// if (config.showLoading) {// 	uni.showLoading({ title: config.loadingText })// }return config
}// 响应拦截器
const responseInterceptor = async (response) => {const userStore = useUserStore()// const { code, msg, data } = response.dataconst code = response.data.codeconst msg = response.data.msgconst data = response.data// const data = response.data.data// console.log(data,code,msg);switch (code) {case 200:return data// return Promise.resolve(data)case 401: {// 如果是刷新token的请求失败,直接登出if (config.url.includes('/auth/refresh')) {await handleLogout()return Promise.reject(new Error('登录已失效'))}// 处理token刷新if (!isRefreshing) {isRefreshing = truetry {const refreshResult = await refreshToken()if (refreshResult) {const newToken = userStore.tokenonRefreshed(newToken)// 重试当前请求config.header.Authorization = `Bearer ${newToken}`return request(config)} else {await handleLogout()return Promise.reject(new Error('登录已失效'))}} finally {isRefreshing = false}}// 返回一个Promise,将请求暂存return new Promise((resolve) => {addSubscriber((token) => {config.header.Authorization = `Bearer ${token}`resolve(request(config))})})// 上面是刷新token 如果没有刷新token的接口直接登出  把上面的401代码注释 解开下面的两行注释//await handleLogout()//return Promise.reject(new Error('登录已失效'))}default:uni.showToast({icon: 'none',title: msg || '请求错误',duration: 2000})return Promise.reject(new Error(msg || '请求错误'))}
}// 添加统一的登出处理方法
const handleLogout = async () => {const userStore = useUserStore()const pageRoute = usePageRoute()const fullPagePath = pageRoute.getCurrentFullPagePath()uni.setStorageSync('fullPage', fullPagePath)uni.$msgBox('登录已失效,请重新登录', 2000)userStore.clearToken()userStore.clearUser()await uni.$delay(2000)uni.reLaunch({ url: '/pages/login/login' })
}// 统一请求方法
const request = (options = {}) => {const config = {...DEFAULT_CONFIG,...options,header: {'Content-Type': 'application/json',...options.header}}config.url = apiUrl + config.url// 应用请求拦截器const interceptedConfig = requestInterceptor(config)return new Promise((resolve, reject) => {uni.request({...interceptedConfig,success: async (res) => {try {const result = await responseInterceptor(res)resolve(result)} catch (error) {reject(error)}},fail: (error) => {uni.showToast({icon: 'none',title: '网络错误,请检查网络连接',duration: 2000})reject(error)},complete: () => {// if (config.showLoading) {// 	uni.hideLoading()// }}})})
}// 导出请求方法
export const $get = (url, data, options = {}) => {return request({url,data,method: 'GET',...options})
}export const $post = (url, data, options = {}) => {return request({url,data,method: 'POST',...options})
}// 挂载到全局
uni.$get = $get
uni.$post = $post

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com