江西小程序管理端
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.

90 lines
3.0 KiB

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