| | |
| | | import Request from '@/common/luch-request/index.js' |
| | | import {ACCESS_TOKEN} from '@/common/util/constants.js' |
| | | import { |
| | | ACCESS_TOKEN |
| | | } from '@/common/util/constants.js' |
| | | import configService from './config.service.js' |
| | | import tip from '@/common/util/tip.js'; |
| | | import store from '@/store/index.js'; |
| | |
| | | |
| | | |
| | | const http = new Request() |
| | | http.setConfig((config) => { /* 设置全局配置 */ |
| | | http.setConfig((config) => { |
| | | /* 设置全局配置 */ |
| | | config.baseUrl = apiUrl /* 根域名不同 */ |
| | | config.header = { |
| | | ...config.header |
| | |
| | | return statusCode === 200 |
| | | } |
| | | |
| | | http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */ |
| | | http.interceptor.request((config, cancel) => { |
| | | /* 请求之前拦截器 */ |
| | | config.header = { |
| | | ...config.header, |
| | | 'X-Access-Token':getTokenStorage() |
| | |
| | | }) |
| | | |
| | | // 必须使用异步函数,注意 |
| | | http.interceptor.response(async (response) => { /* 请求之后拦截器 */ |
| | | http.interceptor.response(async (response) => { |
| | | /* 请求之后拦截器 */ |
| | | // if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject() |
| | | // return Promise.reject(response) |
| | | // } |
| | |
| | | const token = uni.getStorageSync(ACCESS_TOKEN) |
| | | console.log("------异常响应------",token) |
| | | console.log("------异常响应------",data.status) |
| | | switch (data.status) { |
| | | switch (data.code) { |
| | | case 403: |
| | | tip.error('拒绝访问'); |
| | | break |
| | | case 500: |
| | | this.$tip.alert('服务器遇到一个错误,暂时无法处理您的请求'); |
| | | // if(!token || data.message=="Token失效,请重新登录"){ |
| | | // let timeout=setTimeout(tip.alert('登录已过期'), 1000); |
| | | // store.dispatch('Logout').then(() => { |
| | | // clearTimeout(timeout) |
| | | // window.location.reload() |
| | | // }) |
| | | // } |
| | | if (!token || data.message === "Token失效,请重新登录") { |
| | | // 先提示用户 |
| | | tip.alert('登录已过期'); |
| | | |
| | | // 延迟执行登出并刷新页面,避免提示被中断 |
| | | const timeout = setTimeout(() => { |
| | | store.dispatch('Logout') |
| | | .then(() => { |
| | | window.location.reload(); |
| | | }) |
| | | .catch((error) => { |
| | | console.error('Logout failed:', error); |
| | | window.location.reload(); // 出错时也强制刷新 |
| | | }); |
| | | }, 1000); |
| | | } |
| | | break |
| | | case 404: |
| | | break |