|
|
|
import Vue from 'vue';
|
|
|
|
import store from '@/store';
|
|
|
|
|
|
|
|
// 定义 API URL
|
|
|
|
const CS_API_URL = 'http://changshu.js-dyyj.com';
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return store.state.user.userInfo.token;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 定义错误处理函数
|
|
|
|
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',
|
|
|
|
url: params && params.apiType === 'jdsz' ? JDSZ_API_URL + apiurl : CS_API_URL + apiurl,
|
|
|
|
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;
|
|
|
|
|