12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { config } from '@/common/config.js';
- import store from '@/store/index.js'
- const install = (Vue, vm) => {
- uni.$u.http.setConfig({
- baseURL: config.baseUrl,
- dataType: 'json',
- showLoading: true,
- loadingText: '请求中...',
- loadingTime: 500,
- originalData: false,
- loadingMask: true,
-
- header: {
- 'content-type': 'application/json;charset=UTF-8'
- }
- });
-
- uni.$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 = ['/wechat/h5/user', '/client/auth/verifyCode'];
- if (noTokenList.includes(config.url)) config.header.noToken = true;
- return config;
- }
-
- uni.$u.http.interceptor.response = (res) => {
- if (res.code == 200) {
- return res;
- } else if (res.msg == "令牌不能为空" || res.code == 401) {
- const backUrl = location.href
- const loginUrl = 'phoneLogin'
- if (backUrl.indexOf(loginUrl) > 0) {} else {
- localStorage.setItem('backUrl', location.href)
- store.state.vuex_hasLogin = false;
- alert('还未登录,即将跳转登录');
- uni.navigateTo({
- url: "/pages/phoneLogin/phoneLogin"
- });
- return;
- }
- } else {
- uni.showToast({
- title: res.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- }
- export default {
- install
- }
|