asd/asd-wx/util/api.js
愉快的大福 340a467ba1 init
2024-11-21 11:32:11 +08:00

89 lines
2.3 KiB
JavaScript

import {
baseUrl
} from "@/util/config.js" //请求根路径(服务器地址)
// 全局请求路径,也就是后端的请求基准路径
import store from '@/common/store/index.js'
const BASE_URL = baseUrl
// const BASE_URL = 'http://172.16.11.46:4567/dev-api/'
// const BASE_URL = 'http://172.16.60.18/api/'
// const BASE_URL = 'http://172.16.60.18:9005/'
/**
* @description request 封装
*/
export const myRequest = (options) => { //传入的options是一个json对象
return new Promise((resolve, reject) => {
// console.log('15', uni.getStorageSync('token'), store.getters.token);
// if (!uni.getStorageSync('token')) {
// uni.navigateTo({
// url: '/loginPackage/pages/Login'
// })
// return
// }
uni.request({
url: BASE_URL + options.url,
method: options.method || "GET",
header: {
// Authorization: uni.getStorageSync('token')
Authorization: 'Bearer' + ' ' + store.getters.token,
Accept: '*/*'
// Authorization:'Bearer' + ' '+ 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImUyNjk5NDczLTJjNWUtNDI3My1iNWU2LTM1ZjE3YzAwZmE4NyJ9.b1ncS_09vfLYXoAS7IV3l5Vqo3h9j5j-_K57LpEQNzxIwdW3ja1M6mUaJl2yzWR5VXWrlYTILNW4kGSIs5arnA'
},
data: options.data || {},
dataType: options.dataType || "json",
success: (res) => {
if (res.data.code == 401) {
uni.navigateTo({
url: '/loginPackage/pages/Login'
})
store.commit('setPropName', {
propName: 'token',
value: ""
})
store.commit('setPropName', {
propName: 'userInfo',
value: {}
});
uni.removeStorageSync('username');
uni.removeStorageSync('password');
uni.removeStorageSync('token');
}
if (res.data.code !== 200 && res.data.code !== 401) {
return uni.showToast({
title: res.data.msg,
icon: 'none'
})
}
if (options.showToast) {
uni.showToast({
title: options.msg || res.data.msg,
mask: true,
duration: 2000
});
}
if (options.switchTab) {
setTimeout(() => {
uni.switchTab({
url: options.switchTab
})
}, 1500);
}
if (options.navigateTo) {
setTimeout(() => {
uni.navigateTo({
url: options.navigateTo
})
}, 1500);
}
if (options.navigateBack) {
setTimeout(function() {
uni.navigateBack()
}, 1500);
}
resolve(res)
}
})
})
}