学员端小程序
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.

86 lines
2.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { H5_API, WX_API,httpPrefix } from '@/config/site.config.js';
  2. import { checkToken } 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 vm.$store.dispatch('refreshToken')
  42. setTimeout(()=>{
  43. let obj = response.config
  44. let method = obj.method.toLowerCase()
  45. if(method=='get') {
  46. return uni.$u.http[method](obj.url, {params: obj.params})
  47. }else{
  48. return uni.$u.http[method](obj.url, obj.data )
  49. }
  50. })
  51. }
  52. if(data.code==401) {
  53. vm.$store.commit('goLogin')
  54. }
  55. // 自定义参数
  56. const custom = response.config?.custom
  57. if (data.code !== 0&&data.code!=406) {
  58. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  59. if (custom.toast !== false) {
  60. uni.$u.toast(data.msg)
  61. }
  62. // 如果需要catch返回,则进行reject
  63. if (custom?.catch) {
  64. return Promise.reject(data)
  65. } else {
  66. // 否则返回一个pending中的promise,请求不会进入catch中
  67. return new Promise(() => { })
  68. }
  69. }
  70. return data === undefined ? {} : data
  71. }, (response) => {
  72. // 对响应错误做点什么 (statusCode !== 200)
  73. return Promise.reject(response)
  74. })
  75. }