| | |
| | | import { |
| | | useUserStore |
| | | } from "@/store" |
| | | import { |
| | | getToken |
| | | } from "@/utils/auth" |
| | | import storage from "@/utils/storage" |
| | | import { |
| | | showMessage |
| | |
| | | import { |
| | | Base64 |
| | | } from 'js-base64' |
| | | // 重试队列,每一项将是一个待执行的函数形式 |
| | | let requestQueue = [] |
| | | |
| | | // 防止重复提交 |
| | | const repeatSubmit = config => { |
| | |
| | | } |
| | | } |
| | | |
| | | // 是否正在刷新token的标记 |
| | | let isRefreshing = false |
| | | |
| | | // 刷新token |
| | | const refreshToken = async (http, config) => { |
| | | // 是否在获取token中,防止重复获取 |
| | | if (!isRefreshing) { |
| | | // 修改登录状态为true |
| | | isRefreshing = true |
| | | // 等待登录完成 |
| | | await useUserStore().authLogin() |
| | | // 登录完成之后,开始执行队列请求 |
| | | requestQueue.forEach(cb => cb()) |
| | | // 重试完了清空这个队列 |
| | | requestQueue = [] |
| | | isRefreshing = false |
| | | // 重新执行本次请求 |
| | | return http.request(config) |
| | | } |
| | | |
| | | return new Promise(resolve => { |
| | | // 将resolve放进队列,用一个函数形式来保存,等登录后直接执行 |
| | | requestQueue.push(() => { |
| | | resolve(http.request(config)) |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | // 请求拦截器 |
| | | function requestInterceptors(http) { |
| | | http.interceptors.request.use((config) => { // 可使用async await 做异步操作 |
| | | |
| | | http.interceptors.request.use((config) => { |
| | | const {detail} = useUserStore().$state?.userInfo || {} |
| | | // 假设有token值需要在头部需要携带 |
| | | let accessToken = uni.getStorageSync('accessToken'); |
| | | let accessToken = useUserStore()?.$state?.userInfo?.access_token; |
| | | if (accessToken) { |
| | | config.header['Blade-Auth'] = 'bearer ' + accessToken; |
| | | } |
| | | |
| | | if (detail?.areaCode) { |
| | | config.header['areaCode'] = detail.areaCode |
| | | } |
| | | // 安全请求header |
| | | config.header['Blade-Requested-With'] = 'BladeHttpRequest'; |
| | | // 客户端认证参数 |
| | | config.header['Authorization'] = 'Basic ' + Base64.encode(website.clientId + ':' + website.clientSecret); |
| | | |
| | | // #ifndef H5 |
| | | let url = config.url |
| | | if (process.env.NODE_ENV == 'development' && !url.startsWith("http")) { |
| | | url = url.substring(url.indexOf('/api') + 4) |
| | | config.url = url |
| | | } |
| | | // #endif |
| | | |
| | | // 额外参数 |
| | | // config.data = config.data || {}; |
| | | // config.data.pf = uni.getSystemInfoSync().platform; |
| | | // config.data.sys = uni.getSystemInfoSync().system; |
| | | |
| | | // 演示custom 用处 |
| | | // if (config.custom.auth) { |
| | | // config.header.token = 'token' |
| | | // } |
| | | // if (config.custom.loading) { |
| | | // uni.showLoading() |
| | | // } |
| | | /** |
| | | /* 演示 |
| | | if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求 |
| | | return Promise.reject(config) |
| | | } |
| | | **/ |
| | | return config |
| | | }, config => { // 可使用async await 做异步操作 |
| | | return Promise.reject(config) |
| | | }) |
| | | } |
| | | |
| | | // 响应拦截器 |
| | | function responseInterceptors(http) { |
| | | http.interceptors.response.use((response) => { |
| | | console.log(response, 2222) |
| | | // 若有数据返回则通过 |
| | | if (response.data.access_token || response.data.key) { |
| | | return response.data |
| | | } |
| | | // 服务端返回的状态码不等于200,则reject() |
| | | if (response.data.code !== 200) { |
| | | uni.showToast({ |
| | | title: response.data.msg, |
| | | icon: 'none' |
| | | }); |
| | | let res = response |
| | | const status = res.data.error_code || res.data.code || res.statusCode |
| | | const message = res?.data?.msg || res?.data?.error_description || res?.data?.message || '系统错误' |
| | | if (status !== 200) { |
| | | uni.showToast({title: message, icon: 'none'}); |
| | | return Promise.reject(response); |
| | | } |
| | | return response.data; |
| | | return response; |
| | | }, (response) => { |
| | | console.log(response, 111) |
| | | /* 对响应错误做点什么 (statusCode !== 200)*/ |
| | | uni.showToast({ |
| | | title: response.data.msg, |
| | | icon: 'none' |
| | | }); |
| | | if (response.statusCode == 401) { |
| | | uni.showToast({title: response?.data?.msg, icon: 'none'}); |
| | | if (response.statusCode === 401) { |
| | | const pages = getCurrentPages() |
| | | const currentPage = pages[pages.length - 1] |
| | | uni.redirectTo({ |
| | | url: `/pages/login/login-account?redirect=/${currentPage.route}` |
| | | url: `/pages/login/index?redirect=/${currentPage.route}` |
| | | }) |
| | | } |
| | | return Promise.reject(response) |