123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import { commonConfig } from '@/common/config.js';
- import {againToken} from '../utils/againToken.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.vuex_user_info.accessToken}`;
- }
- if(!config?.custom?.noload){
- showFullScreenLoading()
- }
- return config
- }, config => {
- return Promise.reject(config)
- })
-
- let unlogin = function(){
- if(vm.vuex_user_info.userid&&vm.vuex_wechatOpenid) {
- againToken(vm.$u,vm.vuex_wechatOpenid,vm.vuex_user_info.userid)
- } else {
- 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.setStorage({
- key: 'backUrl',
- data: fullBackUrl,
- success: function () {
-
- }
- });
- tryHideFullScreenLoading()
- if(showModal){return}
- showModal = true;
- uni.showModal({
- title: '提示',
- content: '你需要登录后,才可使用此功能!',
- success: res => {
- if (res.confirm) {
- uni.$u.route('/pages/login/login');
- }else{
- uni.removeStorage({
- key: 'backUrl',
- success: function (res) {
-
- }
- });
- let pages = getCurrentPages();
-
- if(pages.length>1){
- uni.navigateBack()
- }
- }
- },
- complete() {
- showModal = false
- uni.$u.vuex('vuex_member_info', {});
- }
- })
- }
- }
-
-
- 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 = ['令牌不能为空'];
- if (!unshowmsg.includes(data.msg)) {
- uni.$u.toast(data.msg)
- }
-
- }
- 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)
- })
- }
|