常熟
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

7 months ago
import Vue from 'vue';
import store from '@/store';
// 定义 API URL
7 months ago
const CS_API_URL = 'http://changshu.js-dyyj.com';
7 months ago
const JDSZ_API_URL = 'https://api.cloud.sz-trip.com';
const NEWAPIURL = process.env.NODE_ENV === 'development' ? CS_API_URL : CS_API_URL;
// 获取token
const getToken = () => {
const userInfoFromStorage = uni.getStorageSync('userInfo');
if (userInfoFromStorage) {
const userInfo = JSON.parse(userInfoFromStorage);
if (userInfo.token) {
return userInfo.token;
}
}
7 months ago
return store.state.user.userInfo.token;
7 months ago
};
// 定义错误处理函数
const handleError = (res, reject) => {
setTimeout(() => {
uni.showToast({
title: res.data?.msg || res.msg,
icon: 'none'
});
reject(res);
}, 0);
if (res.data?.code === 401) {
store.commit('changeLoginPath');
}
};
// 挂载到 Vue 原型上
Vue.prototype.NEWAPIURL = NEWAPIURL;
// #ifdef H5
Vue.prototype.NEWAPIURL = '/api';
// #endif
Vue.prototype.Post = (params = {}, apiurl) => {
const token = getToken();
if (token) {
params.token = token;
}
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中'
});
uni.request({
method: params.method || 'GET',
7 months ago
url: params && params.apiType === 'jdsz' ? JDSZ_API_URL + apiurl : CS_API_URL + apiurl,
7 months ago
data: params,
header: {
'content-type': 'application/json',
'token': token || ''
},
success: (res) => {
console.log('success', res.data);
uni.hideLoading()
if (res.data.code === 200 || res.data.code === 1) {
resolve(res.data);
} else {
handleError(res, reject);
}
},
fail: (err) => {
uni.hideLoading()
console.log('err', err);
handleError(err, reject);
}
});
});
};
export default NEWAPIURL;