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

122 lines
4.0 KiB

1 year ago
11 months 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
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months 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/json;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. // get请求映射params参数
  37. // console.log(config.method)
  38. // if (config.method === 'GET' && config.params) {
  39. // // config.params = qs.stringify(config.params, { allowDots: true })
  40. // let url = config.url + '?';
  41. // for (const propName of Object.keys(config.params)) {
  42. // const value = config.params[propName];
  43. // const part = encodeURIComponent(propName) + '='
  44. // if (value !== null && typeof(value) !== "undefined") {
  45. // if (typeof value === 'object') {
  46. // for (const key of Object.keys(value)) {
  47. // let params = propName + '[' + key + ']';
  48. // const subPart = params + '='
  49. // console.log(qs.stringify(value[key]))
  50. // url += subPart + decodeURIComponent(value[key]) + "&";
  51. // }
  52. // } else {
  53. // url += part + encodeURI(value) + "&";
  54. // }
  55. // }
  56. // }
  57. // url = url.slice(0, -1);
  58. // // console.log(url)
  59. // config.params = {};
  60. // config.url = url;
  61. // }
  62. return config
  63. }, config => { // 可使用async await 做异步操作
  64. return Promise.reject(config)
  65. })
  66. // 响应拦截
  67. uni.$u.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  68. const data = response.data
  69. // console.log('请求结果')
  70. // console.log(data)
  71. if(data.code==406) {
  72. // vm.$store.dispatch('refreshToken')
  73. uni.$u.toast('登录过期,请重新登录')
  74. setTimeout(()=>{
  75. vm.$store.commit('goLogin')
  76. },1500)
  77. return false
  78. }
  79. if(data.code==401) {
  80. vm.$store.commit('goLogin')
  81. }
  82. // 自定义参数
  83. const custom = response.config?.custom
  84. if (data.code !== 0) {
  85. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  86. if (custom.toast !== false) {
  87. uni.$u.toast(data.msg)
  88. }
  89. // 如果需要catch返回,则进行reject
  90. if (custom?.catch) {
  91. return Promise.reject(data)
  92. } else {
  93. // 否则返回一个pending中的promise,请求不会进入catch中
  94. return new Promise(() => { })
  95. }
  96. }
  97. // 如果不需要token就把header里的token删除,并且不需要去刷新token
  98. let noToken = response.config.custom?.noToken
  99. if(!noToken) {
  100. checkToken(vm)
  101. }
  102. return data === undefined ? {} : data
  103. }, (response) => {
  104. // 对响应错误做点什么 (statusCode !== 200)
  105. return Promise.reject(response)
  106. })
  107. }