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.

92 lines
3.1 KiB

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