request.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // console.log('fullBackUrl',fullBackUrl);
  41. // const backArr = ['productdetails'];//需要登录返回的页面
  42. // const hasBackArr = backArr.some(backPage => backUrl.includes(backPage));
  43. // if(hasBackArr){
  44. // console.log('包含');
  45. // uni.setStorage({
  46. // key: 'backUrl',
  47. // data: fullBackUrl,
  48. // success: function () {
  49. // // console.log('setStorage success');
  50. // }
  51. // });
  52. // }else{
  53. // console.log('不包含');
  54. // uni.removeStorage({
  55. // key: 'backUrl',
  56. // success: function (res) {
  57. // // console.log('success');
  58. // }
  59. // });
  60. // }
  61. uni.setStorage({
  62. key: 'backUrl',
  63. data: fullBackUrl,
  64. success: function () {
  65. // console.log('setStorage success');
  66. }
  67. });
  68. tryHideFullScreenLoading()
  69. if(showModal){return}
  70. showModal = true;
  71. uni.showModal({
  72. title: '提示',
  73. content: '你需要登录后,才可使用此功能!',
  74. success: res => {
  75. if (res.confirm) {
  76. uni.$u.route('/pages/login/login');
  77. }else{
  78. uni.removeStorage({
  79. key: 'backUrl',
  80. success: function (res) {
  81. // console.log('success');
  82. }
  83. });
  84. let pages = getCurrentPages();
  85. // console.log('pages',pages);
  86. if(pages.length>1){
  87. uni.navigateBack()
  88. }
  89. }
  90. },
  91. complete() {
  92. showModal = false
  93. uni.$u.vuex('vuex_member_info', {});
  94. }
  95. })
  96. }
  97. }
  98. // 响应拦截
  99. uni.$u.http.interceptors.response.use((response) => {/* 对响应成功做点什么 可使用async await 做异步操作*/
  100. // console.log('response====response',response);
  101. const data = response.data
  102. // 自定义参数
  103. const custom = response.config?.custom
  104. if (data.code !== 200) {
  105. console.log('data====',data);
  106. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  107. if (custom.toast !== false) {
  108. const unshowmsg = ['令牌不能为空'];
  109. if (!unshowmsg.includes(data.msg)) {
  110. uni.$u.toast(data.msg)
  111. }
  112. // uni.$u.toast(data.msg)
  113. }
  114. if(data.msg == "令牌验证失败" || data.msg == "令牌不能为空" || data.code == 401){
  115. unlogin()
  116. }
  117. if(data.msg == "用户不存在!"||data.msg == "用户未注册"){
  118. uni.clearStorage();
  119. unlogin()
  120. }
  121. return Promise.reject(data)
  122. // 如果需要catch返回,则进行reject
  123. // if (custom?.catch) {
  124. // return Promise.reject(data)
  125. // } else {
  126. // // 否则返回一个pending中的promise,请求不会进入catch中
  127. // return new Promise(() => { })
  128. // }
  129. }
  130. // console.log('data--',data);
  131. tryHideFullScreenLoading()
  132. return data === undefined ? {} : data
  133. }, (response) => {
  134. tryHideFullScreenLoading()
  135. // console.log('response==',response);
  136. const data = response.data;
  137. // console.log('data==',data);
  138. // 对响应错误做点什么 (statusCode !== 200)
  139. let errMap = {
  140. '404':'接口不存在'
  141. };
  142. if (response.statusCode in errMap) {
  143. uni.$u.toast(errMap[response.statusCode])
  144. }
  145. if(data.msg == "令牌不能为空" || data.code == 401){
  146. unlogin()
  147. }
  148. return Promise.reject(response)
  149. })
  150. }