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.
 
 
 
 

110 lines
3.2 KiB

import Vue from 'vue';
import store from '@/store';
// 定义 API URL
const DEV_API_URL = 'https://epic.new.js-dyyj.com';
// const DEV_API_URL = 'https://epic.js-dyyj.com';
// const PROD_API_URL = 'https://epic.js-dyyj.com';
const PROD_API_URL = 'https://epic.new.js-dyyj.com';
const NEWAPIURL = process.env.NODE_ENV === 'development' ? DEV_API_URL : PROD_API_URL;
const DEV_API_URL_DES = 'http://192.168.124.177:8083/xcx';
// const DEV_API_URL_DES = 'https://des.js-dyyj.com/xcx';
const PROD_API_URL_DES = 'https://des.js-dyyj.com/xcx';
const NEWAPIURL_DES = process.env.NODE_ENV === 'development' ? DEV_API_URL_DES : PROD_API_URL_DES;
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 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;
};
// 定义错误处理函数 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://epic.js-dyyj.com';
Vue.prototype.NEWAPIURL = NEWAPIURL;
Vue.prototype.NEWAPIURL_DES = NEWAPIURL_DES;
// #ifdef H5
Vue.prototype.NEWAPIURL = '/api';
Vue.prototype.NEWAPIURL_DES = '/api_des';
// #endif
Vue.prototype.Post = (params = {}, apiurl,customUrl) => {
const token = getToken();
if (token) {
params.token = token;
}
let baseUrl = Vue.prototype.NEWAPIURL
if (customUrl) {
const urls = {
DES: Vue.prototype.NEWAPIURL_DES,
}
baseUrl = urls[customUrl]
} else {
baseUrl = Vue.prototype.NEWAPIURL
}
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中'
});
uni.request({
method: params.method || 'GET',
url:baseUrl + apiurl,
data: params,
header: {
'content-type': 'application/json',
'token': token || '',
'userId': getUserId() || '',
},
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) => {
console.log('err', err);
uni.hideLoading()
handleError(err, reject, params.noForceLogin);
}
});
});
};
export default NEWAPIURL;