request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import axios from "axios";
  2. import {
  3. UniAdapter
  4. } from 'uniapp-axios-adapter'
  5. import store from '@/store'
  6. import {
  7. getAccessToken,
  8. setAccessToken,
  9. setRefreshToken,
  10. refreshToken,
  11. isRefreshRequest
  12. } from "./auth"
  13. // 每次请求都创建一个新的实例
  14. const instance = axios.create({
  15. baseURL: "https://test.api.chelvc.com",
  16. // baseURL: "http://192.168.68.77:11000",
  17. // baseURL: "https://358175z5l5.yicp.fun",
  18. timeout: 10000,
  19. adapter: UniAdapter
  20. });
  21. instance.interceptors.request.use((config) => {
  22. uni.showLoading({
  23. title: '加载中'
  24. })
  25. // 带上token 和其他请求头信息
  26. if (store.getters.accessToken) {
  27. config.headers['Authorization'] = `Bearer ${getAccessToken()}`
  28. }
  29. console.log("@@@@config",config)
  30. config.headers = {
  31. ...config.headers,
  32. platform: store.getters.app.system.osName.toUpperCase(),
  33. terminal: 'APPLET', // TODO:
  34. version: store.getters.app.system.appVersion.toUpperCase(),
  35. // scope: store.getters.scope,
  36. device: store.getters.app.system.deviceId,
  37. timestamp: new Date().getTime()
  38. }
  39. return config
  40. })
  41. instance.interceptors.response.use(async (res) => {
  42. uni.hideLoading()
  43. const {
  44. code,
  45. data,
  46. message
  47. } = res.data
  48. if (data && data.accessToken) {
  49. setAccessToken(data.accessToken)
  50. }
  51. if (data && data.refreshToken) {
  52. setRefreshToken(data.refreshToken)
  53. }
  54. // 未知错误
  55. if (code === 'ERROR') {
  56. uni.showToast({
  57. title: `${message ? message :'未知错误'}`,
  58. icon: 'none'
  59. })
  60. return res.data
  61. }
  62. // 请求错误
  63. if (code === 'BAD_REQUEST') {
  64. uni.showToast({
  65. title: `${message ? message :'请求异常'}`,
  66. icon: 'none'
  67. })
  68. return res.data
  69. }
  70. // 拒绝访问
  71. if (code === 'FORBIDDEN') {
  72. uni.showToast({
  73. title: `${message ? message :'拒绝访问'}`,
  74. icon: 'none'
  75. })
  76. return
  77. }
  78. // 请求参数异常
  79. if (code === 'PARAMETER_INVALID') {
  80. const errorTextList = Object.keys(res.data.data).map(key => {
  81. return res.data.data[key]
  82. })
  83. uni.showToast({
  84. title: errorTextList.join(','),
  85. icon: 'none'
  86. })
  87. return res.data
  88. }
  89. // 访问资源异常
  90. if (code === 'UNAVAILABLE') {
  91. uni.showToast({
  92. title: `${message ? message :'访问资源不可用'}`,
  93. icon: 'none'
  94. })
  95. return
  96. }
  97. // 未登录
  98. if (code === 'UNAUTHORIZED') {
  99. uni.navigateTo({
  100. url: '/pages/login/index'
  101. })
  102. return res.data
  103. }
  104. // 身份切换
  105. if (code === 'SCOPE_CHANGED') {
  106. uni.reLaunch({
  107. url: '/pages/login/index'
  108. })
  109. uni.showToast({
  110. title: '您的身份信息已切换',
  111. icon: 'none',
  112. })
  113. return res.data
  114. }
  115. if (code === 'TOKEN_CHANGED') {
  116. uni.showModal({
  117. title: '提示',
  118. content: res.data.messsage,
  119. showCancel: false,
  120. success: (res) => {
  121. if (res.confirm) {
  122. // 小程序用户跳转到我的页面 , 登录状态为未登录
  123. }
  124. }
  125. })
  126. return res.data
  127. }
  128. // token过期处理
  129. if (code === 'TOKEN_EXPIRED' && !isRefreshRequest(res.config)) {
  130. // 刷新token
  131. const isSuccess = await refreshToken()
  132. if (isSuccess) {
  133. // 重新请求
  134. instance.config.headers.authorization = `Bearer ${getAccessToken()}`
  135. const resp = await instance.request(res.config)
  136. return resp
  137. } else {
  138. // 无权限
  139. store.commit('SET_ACCESS_TOKEN', '')
  140. store.commit('SET_REFRESH_TOKEN', '')
  141. uni.showModal({
  142. title: '提示',
  143. content: "您的登录信息已过期,请重新登录",
  144. showCancel: false,
  145. success: (res) => {
  146. uni.navigateTo({
  147. url: '/pages/login/index'
  148. })
  149. }
  150. })
  151. return res.data
  152. }
  153. }
  154. return res.data
  155. }, (err) => {
  156. console.log("->", err)
  157. uni.hideLoading()
  158. });
  159. export default instance;