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

88 lines
2.9 KiB

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