http.interceptor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //免登录接口
  2. let noLoginUrl = [
  3. '/addons/shop/api.common/init',
  4. '/addons/shop/api.common/area',
  5. '/addons/shop/api.ems/send',
  6. '/addons/shop/api.sms/send',
  7. '/addons/shop/api.login/login',
  8. '/addons/shop/api.login/mobilelogin',
  9. '/addons/shop/api.login/register',
  10. '/addons/shop/api.login/resetpwd',
  11. '/addons/shop/api.login/wxLogin',
  12. '/addons/shop/api.login/wechatMobileLogin',
  13. '/addons/shop/api.login/appLogin',
  14. '/addons/shop/api.login/getWechatMobile',
  15. '/addons/shop/api.login/getWechatOpenid',
  16. '/addons/shop/api.user/getSigned',
  17. '/addons/third/api/getAuthUrl',
  18. '/addons/third/api/callback',
  19. '/addons/third/api/account',
  20. '/addons/shop/api.goods/category',
  21. '/addons/shop/api.goods/index',
  22. '/addons/shop/api.goods/detail',
  23. '/addons/shop/api.goods/lists',
  24. '/addons/shop/api.goods/getWxCode',
  25. '/addons/shop/api.category/index',
  26. '/addons/shop/api.category/alls',
  27. '/addons/shop/api.comment/index',
  28. '/addons/shop/api.coupon/couponDetail',
  29. '/addons/shop/api.coupon/couponList',
  30. '/addons/shop/api.page/index',
  31. '/addons/shop/api.page/lists',
  32. '/addons/shop/api.score/exchangeList',
  33. '/addons/shop/api.attribute/index'
  34. ];
  35. //设置session_id
  36. const getSessionId = function(vm) {
  37. let session = vm.$util.getDb('session');
  38. if (!session) {
  39. let guid = vm.$u.guid();
  40. vm.$util.setDb('session', guid);
  41. return guid;
  42. }
  43. return session;
  44. }
  45. // 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
  46. // 同时,我们也可以在此使用getApp().globalData,如果你把token放在getApp().globalData的话,也是可以使用的
  47. const install = (Vue, vm) => {
  48. let url = 'http://thinkphp_fastadmin_tailored.test';
  49. // #ifdef H5
  50. if(typeof window.fastUrl !== 'undefined'){
  51. url = window.fastUrl;
  52. }
  53. // #endif
  54. Vue.prototype.$u.http.setConfig({
  55. baseUrl: url,
  56. header: {
  57. 'content-type': 'application/json'
  58. },
  59. originalData: true
  60. });
  61. // 请求拦截,配置Token等参数
  62. Vue.prototype.$u.http.interceptor.request = (config) => {
  63. //在需要登录的接口,请求前判断token 是否存在,不存在则到登录
  64. let url = config.url.split('?').shift();
  65. console.log(noLoginUrl.includes(url),url)
  66. if (!noLoginUrl.includes(url) && !vm.vuex_token) {
  67. vm.$u.route('/pages/login/mobilelogin');
  68. return false;
  69. }
  70. config.header.token = vm.vuex_token;
  71. //设置session_id
  72. config.header.sid = getSessionId(vm);
  73. config.header.uid = vm.vuex_user.id || 0;
  74. const res = uni.getSystemInfoSync();
  75. config.header.platform = res.platform || '';
  76. config.header.model = res.model || '';
  77. config.header.brand = res.brand || '';
  78. config.header['x-requested-with'] = 'xmlhttprequest';
  79. if (config.method == 'POST') {
  80. config.data['__token__'] = vm.vuex__token__;
  81. }
  82. return config;
  83. }
  84. // 响应拦截,判断状态码是否通过
  85. Vue.prototype.$u.http.interceptor.response = (res) => {
  86. //返回__token__,设置
  87. if (res.header && res.header.__token__) {
  88. vm.$u.vuex('vuex__token__', res.header.__token__);
  89. }
  90. let result = res.data || {};
  91. if(result.data && result.data.__token__){
  92. vm.$u.vuex('vuex__token__', result.data.__token__);
  93. }
  94. switch (result.code) {
  95. case 1:
  96. case 0:
  97. return result;
  98. break;
  99. case 401:
  100. //需要登录的接口,当token 过期时,到登录页面
  101. vm.$u.vuex('vuex_token', '');
  102. vm.$u.route('/pages/login/mobilelogin');
  103. return result;
  104. break;
  105. case 403: //没有权限访问
  106. uni.showToast({
  107. icon: "none",
  108. title: result.msg
  109. })
  110. return result;
  111. break;
  112. default:
  113. if (res.statusCode == 200) {
  114. return res.data;
  115. } else {
  116. console.error(res)
  117. vm.$u.toast('网络请求错误!');
  118. return false;
  119. }
  120. }
  121. }
  122. }
  123. export default {
  124. install
  125. }