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.

102 lines
3.3 KiB

4 months ago
1 month ago
4 months ago
1 week ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 week ago
4 months ago
4 months ago
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. import { loginApi, } from '@/config/api.js'
  8. let num = 0
  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. let loginInfo = uni.getStorageSync('loginInfo') || {}
  27. let token = loginInfo.token
  28. if(token) {
  29. config.header.Authorization = 'Bearer ' + token
  30. }
  31. let noToken = config.custom?.noToken
  32. if(noToken&&config.header.Authorization) {
  33. delete config.header.Authorization
  34. }
  35. // console.log('config')
  36. // console.log(config)
  37. return config
  38. }, config => { // 可使用async await 做异步操作
  39. return Promise.reject(config)
  40. })
  41. // 响应拦截
  42. uni.$u.http.interceptors.response.use(async (response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  43. const data = response.data
  44. data.code = data.errorcode
  45. // console.log('请求结果')
  46. // console.log(data)
  47. if(data.code==401) {
  48. console.log('报401的接口')
  49. console.log(response.config.url)
  50. // return false
  51. if(num) return uni.$u.toast('登录异常,请回题库重进')
  52. num ++
  53. let loginInfo = uni.getStorageSync('loginInfo')
  54. const res = await loginApi({
  55. "type": loginInfo.type,
  56. "username": loginInfo.phone,
  57. })
  58. res.data.phone = loginInfo.phone
  59. res.data.type = loginInfo.type
  60. uni.setStorageSync('loginInfo', res.data);
  61. // num = 0
  62. let obj = response.config
  63. let method = obj.method.toLowerCase()
  64. if(method=='get') {
  65. return uni.$u.http[method](obj.url, {params: obj.params})
  66. }else{
  67. return uni.$u.http[method](obj.url, obj.data )
  68. }
  69. }
  70. // 自定义参数
  71. const custom = response.config?.custom
  72. if (data.code !== 0) {
  73. console.log('不正常的code')
  74. console.log(data)
  75. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  76. if (custom.toast !== false) {
  77. uni.$u.toast(data.message)
  78. }
  79. // 如果需要catch返回,则进行reject
  80. if (custom?.catch) {
  81. return Promise.reject(data)
  82. } else {
  83. // 否则返回一个pending中的promise,请求不会进入catch中
  84. return new Promise(() => { })
  85. }
  86. }
  87. return data === undefined ? {} : data
  88. }, (response) => {
  89. // 对响应错误做点什么 (statusCode !== 200)
  90. return Promise.reject(response)
  91. })
  92. }