123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- const install = (Vue, vm) => {
- Vue.prototype.$u.http.setConfig({
- baseUrl: vm.config.baseUrl,
- loadingText: '努力加载中~',
- loadingTime: 800,
- originalData: true,
- dataType: 'text'
- });
- let isRefreshing = false;
-
- Vue.prototype.$u.http.interceptor.request = (config) => {
- if (vm.vuex_token) {
- config.header.Authorization = `Bearer ${vm.vuex_token}`;
- }
-
- config.url = config.url + '?t=' + Date.now();
- let noTokenList = [
- '/client/wechat/h5/code/',
- '/client/auth/sendSmsCodeV2',
- '/client/auth/verifyCodeV2',
- '/client/auth/loginToWxOpenId',
- '/client/auth/sendSmsCodeV3',
- '/client/auth/loginToMobile'
- ];
- if (noTokenList.includes(config.url)) config.header.noToken = true;
- return config;
- };
-
- Vue.prototype.$u.http.interceptor.response = (res) => {
- if (res.data) {
-
- const jsonBigint = require('json-bigint')({ storeAsString: true });
- res = jsonBigint.parse(res.data);
- if (res.code == 200) {
- return res;
- } else if (res.code == 401 || (res.code == 400 && res.msg == '令牌不能为空')) {
- if (!isRefreshing) {
- login(vm);
- isRefreshing = true;
- }
- return false;
- } else {
-
- const noTipMsg = ['无任何订单'];
- const index = noTipMsg.findIndex((item) => res?.msg.indexOf(item) > -1);
- if (index < 0) {
- uni.showToast({
- title: res.msg || '程序错误!',
- duration: 2000,
- icon: 'none'
- });
- }
- return false;
- }
- } else {
- uni.showToast({
- title: '程序错误!',
- duration: 2000,
- icon: 'error'
- });
- return false;
- }
- };
- };
- const login = (vm) => {
- const backUrl = location.href,
- openidPage = vm.config.onlyWxLogin,
- needValidPage = openidPage.filter((item) => backUrl.indexOf(item) > -1);
-
- const ua = window.navigator.userAgent.toLowerCase();
-
- if (ua.match(/MicroMessenger/i) == 'micromessenger') {
-
- if (backUrl.indexOf('backUrl') === -1) {
- vm.$u.route('pages/wechatLogin/wechatLogin', { backUrl: encodeURIComponent(backUrl), loginType: needValidPage.length ? 2 : 1 });
- }
- } else {
-
- if (backUrl.indexOf('backUrl') === -1) {
- localStorage.setItem('backUrl', encodeURIComponent(backUrl));
- }
- uni.showModal({
- title: '提示',
- content: '未查询到登录信息或信息已失效, 请重新登录',
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- localStorage.removeItem('lifeData');
- uni.redirectTo({
- url: '/pages/center/phoneLogin/phoneLogin'
- });
- }
- }
- });
- }
- };
- export default {
- install
- };
|