123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import axios from "axios";
- import {
- UniAdapter
- } from 'uniapp-axios-adapter'
- import store from '@/store'
- import {
- getAccessToken,
- setAccessToken,
- setRefreshToken,
- refreshToken,
- isRefreshRequest
- } from "./auth"
- // 每次请求都创建一个新的实例
- const instance = axios.create({
- baseURL: "https://test.api.chelvc.com",
- // baseURL: "http://192.168.68.77:11000",
- // baseURL: "https://358175z5l5.yicp.fun",
- timeout: 10000,
- adapter: UniAdapter
- });
- instance.interceptors.request.use((config) => {
- uni.showLoading({
- title: '加载中'
- })
- // 带上token 和其他请求头信息
- if (store.getters.accessToken) {
- config.headers['Authorization'] = `Bearer ${getAccessToken()}`
- }
- console.log("@@@@config",config)
- config.headers = {
- ...config.headers,
- platform: store.getters.app.system.osName.toUpperCase(),
- terminal: 'APPLET', // TODO:
- version: store.getters.app.system.appVersion.toUpperCase(),
- // scope: store.getters.scope,
- device: store.getters.app.system.deviceId,
- timestamp: new Date().getTime()
- }
- return config
- })
- instance.interceptors.response.use(async (res) => {
- uni.hideLoading()
- const {
- code,
- data,
- message
- } = res.data
- if (data && data.accessToken) {
- setAccessToken(data.accessToken)
- }
- if (data && data.refreshToken) {
- setRefreshToken(data.refreshToken)
- }
- // 未知错误
- if (code === 'ERROR') {
- uni.showToast({
- title: `${message ? message :'未知错误'}`,
- icon: 'none'
- })
- return res.data
- }
- // 请求错误
- if (code === 'BAD_REQUEST') {
- uni.showToast({
- title: `${message ? message :'请求异常'}`,
- icon: 'none'
- })
- return res.data
- }
- // 拒绝访问
- if (code === 'FORBIDDEN') {
- uni.showToast({
- title: `${message ? message :'拒绝访问'}`,
- icon: 'none'
- })
- return
- }
- // 请求参数异常
- if (code === 'PARAMETER_INVALID') {
- const errorTextList = Object.keys(res.data.data).map(key => {
- return res.data.data[key]
- })
- uni.showToast({
- title: errorTextList.join(','),
- icon: 'none'
- })
- return res.data
- }
- // 访问资源异常
- if (code === 'UNAVAILABLE') {
- uni.showToast({
- title: `${message ? message :'访问资源不可用'}`,
- icon: 'none'
- })
- return
- }
- // 未登录
- if (code === 'UNAUTHORIZED') {
- uni.navigateTo({
- url: '/pages/login/index'
- })
- return res.data
- }
- // 身份切换
- if (code === 'SCOPE_CHANGED') {
- uni.reLaunch({
- url: '/pages/login/index'
- })
- uni.showToast({
- title: '您的身份信息已切换',
- icon: 'none',
- })
- return res.data
- }
- if (code === 'TOKEN_CHANGED') {
- uni.showModal({
- title: '提示',
- content: res.data.messsage,
- showCancel: false,
- success: (res) => {
- if (res.confirm) {
- // 小程序用户跳转到我的页面 , 登录状态为未登录
- }
- }
- })
- return res.data
- }
- // token过期处理
- if (code === 'TOKEN_EXPIRED' && !isRefreshRequest(res.config)) {
- // 刷新token
- const isSuccess = await refreshToken()
- if (isSuccess) {
- // 重新请求
- instance.config.headers.authorization = `Bearer ${getAccessToken()}`
- const resp = await instance.request(res.config)
- return resp
- } else {
- // 无权限
- store.commit('SET_ACCESS_TOKEN', '')
- store.commit('SET_REFRESH_TOKEN', '')
- uni.showModal({
- title: '提示',
- content: "您的登录信息已过期,请重新登录",
- showCancel: false,
- success: (res) => {
- uni.navigateTo({
- url: '/pages/login/index'
- })
- }
- })
- return res.data
- }
- }
- return res.data
- }, (err) => {
- console.log("->", err)
- uni.hideLoading()
- });
- export default instance;
|