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.

96 lines
3.4 KiB

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