江西小程序管理端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.8 KiB

1 year ago
12 months ago
1 year ago
1 year ago
1 year ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { H5_API, WX_API,httpPrefix } from './site.config.js'
  2. import { checkToken } from './utils'
  3. const ContentType = ['application/json;charset=utf-8', 'application/x-www-form-urlencoded','multipart/form-data', 'application/x-www-form-urlencoded; charset=UTF-8'];
  4. // 此vm参数为页面的实例,可以通过它引用vuex中的变量
  5. module.exports = (vm) => {
  6. // 初始化请求配置
  7. uni.$u.http.setConfig((config) => {
  8. let prefix = config.custom.prefix?config.custom.prefix: httpPrefix
  9. /* config 为默认全局配置*/
  10. config.baseURL = H5_API+ WX_API + prefix; /* 根域名 */
  11. console.log(config)
  12. config.header['tenant-id'] = vm.$store.state.user.vuex_TenantId
  13. return config
  14. })
  15. // 请求拦截
  16. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  17. console.log(config)
  18. let contentTypeIndex = config.header.contentType
  19. if(contentTypeIndex) {
  20. config.header['content-type'] = ContentType[contentTypeIndex]
  21. }
  22. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  23. config.data = config.data || {}
  24. // 根据custom参数中配置的是否需要token,添加对应的请求头
  25. let token = vm.$store.state.user.vuex_loginInfo.accessToken
  26. if(token) {
  27. config.header.Authorization = 'Bearer ' + token
  28. }
  29. let noToken = config.custom?.noToken
  30. if(noToken&&config.header.Authorization) {
  31. delete config.header.Authorization
  32. }
  33. return config
  34. }, config => { // 可使用async await 做异步操作
  35. return Promise.reject(config)
  36. })
  37. // 响应拦截
  38. uni.$u.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  39. const data = response.data
  40. // console.log('请求结果')
  41. // console.log(data)
  42. if(data.code==406) {
  43. // vm.$store.dispatch('refreshToken')
  44. uni.$u.toast('登录过期,请重新登录')
  45. setTimeout(()=>{
  46. vm.$store.commit('goLogin')
  47. },1500)
  48. return false
  49. }
  50. if(data.code==401) {
  51. vm.$store.commit('goLogin')
  52. }
  53. // 自定义参数
  54. const custom = response.config?.custom
  55. if (data.code !== 0) {
  56. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  57. if (custom.toast !== false) {
  58. uni.$u.toast(data.msg)
  59. }
  60. // 如果需要catch返回,则进行reject
  61. if (custom?.catch) {
  62. return Promise.reject(data)
  63. } else {
  64. // 否则返回一个pending中的promise,请求不会进入catch中
  65. return new Promise(() => { })
  66. }
  67. }
  68. // 如果不需要token就把header里的token删除,并且不需要去刷新token
  69. let noToken = response.config.custom?.noToken
  70. if(!noToken) {
  71. checkToken(vm)
  72. }
  73. return data === undefined ? {} : data
  74. }, (response) => {
  75. // 对响应错误做点什么 (statusCode !== 200)
  76. return Promise.reject(response)
  77. })
  78. }