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.

65 lines
2.0 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months 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.vuex_loginInfo.token
  24. if(token) {
  25. config.header.Authorization = 'Basic Og=='
  26. config.header['O-Login-Token'] = token
  27. }
  28. let noToken = config.custom?.noToken
  29. if(noToken&&config.header.Authorization) {
  30. delete config.header.Authorization
  31. }
  32. return config
  33. }, config => { // 可使用async await 做异步操作
  34. return Promise.reject(config)
  35. })
  36. // 响应拦截
  37. uni.$u.http.interceptors.response.use(async (response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  38. const data = response.data
  39. console.log('结果')
  40. console.log(data)
  41. if(data.code==401||data.code==700) {
  42. vm.$store.commit('goLogin')
  43. }
  44. if(data.code!==200) {
  45. return vm.$u.toast(data.msg)
  46. }
  47. return data === undefined ? {} : data
  48. }, (response) => {
  49. // 对响应错误做点什么 (statusCode !== 200)
  50. return Promise.reject(response)
  51. })
  52. }