123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import { commonConfig } from '@/common/config.js';
- import { showFullScreenLoading , tryHideFullScreenLoading } from '../utils/loading.js'
- let showModal = false;
- module.exports = (vm) => {
-
- uni.$u.http.setConfig((config) => {
-
- config.baseURL = commonConfig.baseUrl;
- return config
- })
-
-
- uni.$u.http.interceptors.request.use((config) => {
-
-
- config.data = config.data || {}
-
- if(!config?.custom?.auth) {
-
-
- config.header.Authorization = `Bearer ${vm.distribution_user_info.accessToken}`;
- }
- if(!config?.custom?.noload){
- showFullScreenLoading()
- }
- return config
- }, config => {
- return Promise.reject(config)
- })
-
- let unlogin = function(){
- let pages = getCurrentPages();
- let backUrl = pages[pages.length - 1].route;
- let options =uni.$u.queryParams( pages[pages.length - 1].options);
- let fullBackUrl = backUrl+options;
- uni.setStorageSync('backUrlDistribution',fullBackUrl);
- tryHideFullScreenLoading()
- uni.setStorageSync('lifeDataDistribution',{});
- uni.$u.vuex('distribution_user_info', {});
- console.log("dfsfsdf")
- uni.reLaunch({
- url: "/pages/login/index"
- })
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
- uni.$u.http.interceptors.response.use((response) => {
-
- const data = response.data
-
- const custom = response.config?.custom
- if (data.code !== 200) {
- console.log('data====',data);
-
- if (custom.toast !== false) {
- const unshowmsg = ['令牌不能为空','登录用户:null 不存在'];
- if (!unshowmsg.includes(data.msg)) {
- uni.$u.toast(data.msg)
-
- }else {
- tryHideFullScreenLoading()
- }
-
- }
- if(data.msg == "令牌验证失败" || data.msg == "令牌不能为空" || data.code == 401){
- unlogin()
- }
-
- if(data.msg == "用户不存在!"||data.msg == "用户未注册"){
- uni.clearStorage();
- unlogin()
- }
-
- return Promise.reject(data)
-
-
-
-
-
-
-
- }
-
- tryHideFullScreenLoading()
- return data === undefined ? {} : data
- }, (response) => {
- tryHideFullScreenLoading()
-
- const data = response.data;
-
-
- let errMap = {
- '404':'接口不存在'
- };
- if (response.statusCode in errMap) {
- uni.$u.toast(errMap[response.statusCode])
- }
- if(data.msg == "令牌不能为空" || data.code == 401){
- unlogin()
- }
- return Promise.reject(response)
- })
- }
|