request.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { commonConfig } from '@/common/config.js';
  2. import {againToken} from '../utils/againToken.js'
  3. import { showFullScreenLoading , tryHideFullScreenLoading } from '../utils/loading.js'
  4. let showModal = false;
  5. // 此vm参数为页面的实例,可以通过它引用vuex中的变量
  6. module.exports = (vm) => {
  7. // 初始化请求配置
  8. uni.$u.http.setConfig((config) => {
  9. /* config 为默认全局配置*/
  10. config.baseURL = commonConfig.baseUrl; /* 根域名 */
  11. return config
  12. })
  13. // 请求拦截
  14. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  15. // console.log('config========',config);
  16. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  17. config.data = config.data || {}
  18. // 根据custom参数中配置的是否需要token,添加对应的请求头
  19. if(!config?.custom?.auth) {
  20. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  21. // config.header.token = vm.vuex_user_info.token;
  22. config.header.Authorization = `Bearer ${vm.vuex_user_info.accessToken}`;
  23. }
  24. if(!config?.custom?.noload){
  25. showFullScreenLoading()
  26. }
  27. return config
  28. }, config => { // 可使用async await 做异步操作
  29. return Promise.reject(config)
  30. })
  31. let unlogin = function(){
  32. if(vm.vuex_user_info.userid&&vm.vuex_wechatOpenid) {
  33. againToken(vm.$u,vm.vuex_wechatOpenid,vm.vuex_user_info.userid)
  34. } else {
  35. let pages = getCurrentPages();
  36. // console.log('pages',pages);
  37. let backUrl = pages[pages.length - 1].route;
  38. let options =uni.$u.queryParams( pages[pages.length - 1].options);
  39. let fullBackUrl = backUrl+options;
  40. // const backArr = ['productdetails'];//需要登录返回的页面
  41. // const hasBackArr = backArr.some(backPage => backUrl.includes(backPage));
  42. // if(hasBackArr){
  43. // console.log('包含');
  44. // uni.setStorage({
  45. // key: 'backUrl',
  46. // data: fullBackUrl,
  47. // success: function () {
  48. // // console.log('setStorage success');
  49. // }
  50. // });
  51. // }else{
  52. // console.log('不包含');
  53. // uni.removeStorage({
  54. // key: 'backUrl',
  55. // success: function (res) {
  56. // // console.log('success');
  57. // }
  58. // });
  59. // }
  60. uni.setStorage({
  61. key: 'backUrl',
  62. data: fullBackUrl,
  63. success: function () {
  64. // console.log('setStorage success');
  65. }
  66. });
  67. tryHideFullScreenLoading()
  68. if(showModal){return}
  69. showModal = true;
  70. uni.showModal({
  71. title: '提示',
  72. content: '你需要登录后,才可使用此功能!',
  73. success: res => {
  74. if (res.confirm) {
  75. uni.$u.route('/pages/login/login');
  76. }else{
  77. uni.removeStorage({
  78. key: 'backUrl',
  79. success: function (res) {
  80. // console.log('success');
  81. }
  82. });
  83. let pages = getCurrentPages();
  84. // console.log('pages',pages);
  85. if(pages.length>1){
  86. uni.navigateBack()
  87. }
  88. }
  89. },
  90. complete() {
  91. showModal = false
  92. }
  93. })
  94. }
  95. }
  96. // 响应拦截
  97. uni.$u.http.interceptors.response.use((response) => {/* 对响应成功做点什么 可使用async await 做异步操作*/
  98. // console.log('response====response',response);
  99. const data = response.data
  100. // 自定义参数
  101. const custom = response.config?.custom
  102. if (data.code !== 200) {
  103. console.log('data====',data);
  104. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  105. if (custom.toast !== false) {
  106. const unshowmsg = ['令牌不能为空'];
  107. if (!unshowmsg.includes(data.msg)) {
  108. uni.$u.toast(data.msg)
  109. }
  110. // uni.$u.toast(data.msg)
  111. }
  112. if(data.msg == "令牌验证失败" || data.msg == "令牌不能为空" || data.code == 401){
  113. unlogin()
  114. }
  115. if(data.msg == "用户不存在!"||data.msg == "用户未注册"){
  116. uni.clearStorage();
  117. unlogin()
  118. }
  119. return Promise.reject(data)
  120. // 如果需要catch返回,则进行reject
  121. // if (custom?.catch) {
  122. // return Promise.reject(data)
  123. // } else {
  124. // // 否则返回一个pending中的promise,请求不会进入catch中
  125. // return new Promise(() => { })
  126. // }
  127. }
  128. // console.log('data--',data);
  129. tryHideFullScreenLoading()
  130. return data === undefined ? {} : data
  131. }, (response) => {
  132. tryHideFullScreenLoading()
  133. // console.log('response==',response);
  134. const data = response.data;
  135. // console.log('data==',data);
  136. // 对响应错误做点什么 (statusCode !== 200)
  137. let errMap = {
  138. '404':'接口不存在'
  139. };
  140. if (response.statusCode in errMap) {
  141. uni.$u.toast(errMap[response.statusCode])
  142. }
  143. if(data.msg == "令牌不能为空" || data.code == 401){
  144. unlogin()
  145. }
  146. return Promise.reject(response)
  147. })
  148. }