江西小程序管理端
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.

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