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.

101 lines
3.3 KiB

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