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.

123 lines
3.6 KiB

4 months ago
import Vue from 'vue';
import store from '@/store';
1 month ago
import md5 from 'js-md5';
4 months ago
// 定义 API URL
1 month ago
const DEV_API_URL = 'http://1.13.193.49';
2 months ago
// const DEV_API_URL = 'https://epic.js-dyyj.com';
2 months ago
// const PROD_API_URL = 'https://epic.js-dyyj.com';
1 month ago
const PROD_API_URL = 'http://1.13.193.49';
4 months ago
const NEWAPIURL = process.env.NODE_ENV === 'development' ? DEV_API_URL : PROD_API_URL;
1 month ago
const DEV_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx';
// const DEV_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx';
1 month ago
// const DEV_API_URL_DES = 'http://1.13.193.49:8083/xcx';
// const PROD_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx';
1 month ago
const PROD_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx';
2 months ago
const NEWAPIURL_DES = process.env.NODE_ENV === 'development' ? DEV_API_URL_DES : PROD_API_URL_DES;
4 months ago
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;
};
2 months ago
const getUserId = () => {
const userInfoFromStorage = uni.getStorageSync('userInfo');
if (userInfoFromStorage) {
const userInfo = JSON.parse(userInfoFromStorage);
if (userInfo.id) {
return userInfo.id;
}
}
return store.state.user.userInfo.id;
};
4 months ago
// 定义错误处理函数 noForceLogin 不强制登录
const handleError = (res, reject, noForceLogin) => {
if (res.data?.code === 401) {
if (noForceLogin) {
reject(res)
return
}
store.commit('changeLoginPath');
}
setTimeout(() => {
uni.showToast({
title: res.data?.msg || res.msg,
icon: 'none'
});
reject(res);
}, 0);
};
// 挂载到 Vue 原型上
Vue.prototype.NEWAPIURL = NEWAPIURL;
Vue.prototype.NEWAPIURLIMG = 'https://des.dayunyuanjian.cn';
2 months ago
Vue.prototype.NEWAPIURL = NEWAPIURL;
2 months ago
Vue.prototype.NEWAPIURL_DES = NEWAPIURL_DES;
4 months ago
// #ifdef H5
Vue.prototype.NEWAPIURL = '/api';
2 months ago
Vue.prototype.NEWAPIURL_DES = '/api_des';
4 months ago
// #endif
2 months ago
Vue.prototype.Post = (params = {}, apiurl,customUrl) => {
4 months ago
const token = getToken();
if (token) {
params.token = token;
}
2 months ago
let baseUrl = Vue.prototype.NEWAPIURL
if (customUrl) {
const urls = {
DES: Vue.prototype.NEWAPIURL_DES,
}
baseUrl = urls[customUrl]
} else {
baseUrl = Vue.prototype.NEWAPIURL
}
4 months ago
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中'
});
1 month ago
let userID = ''
const userInfoFromStorage = uni.getStorageSync('userInfo');
if (userInfoFromStorage) {
const userInfo = JSON.parse(userInfoFromStorage);
if (userInfo.id) {
userID = userInfo.id.toString();
}
}
let time = new Date().getTime()
let sign = userID+ time
4 months ago
uni.request({
method: params.method || 'GET',
2 months ago
url:baseUrl + apiurl,
4 months ago
data: params,
header: {
'content-type': 'application/json',
2 months ago
'token': token || '',
'userId': getUserId() || '',
1 month ago
'sign':md5(md5(sign.toString())),
'timestamp':time
4 months ago
},
success: (res) => {
uni.hideLoading()
if (res.data.code === 200 || res.data.code === 1) {
resolve(res.data);
} else {
handleError(res, reject);
}
},
fail: (err) => {
console.log('err', err);
uni.hideLoading()
handleError(err, reject, params.noForceLogin);
}
});
});
};
export default NEWAPIURL;