洛阳学员端
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.

97 lines
3.3 KiB

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