import Vue from 'vue'; import store from '@/store'; import md5 from 'js-md5'; // 定义 API URL const DEV_API_URL = 'https://epic.dayunyuanjian.cn'; // const DEV_API_URL = 'https://epic.js-dyyj.com'; // const PROD_API_URL = 'https://epic.js-dyyj.com'; const PROD_API_URL = 'https://epic.dayunyuanjian.cn'; const NEWAPIURL = process.env.NODE_ENV === 'development' ? DEV_API_URL : PROD_API_URL; // const DEV_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx'; const DEV_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx'; // const DEV_API_URL_DES = 'http://192.168.124.8:8083/xcx'; // const PROD_API_URL_DES = 'https://des.dayunyuanjian.cn/xcx'; const PROD_API_URL_DES = 'https://des.dayunyuanjian.cn/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://des.dayunyuanjian.cn'; 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: '加载中' }); 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 uni.request({ method: params.method || 'GET', url:baseUrl + apiurl, data: params, header: { 'content-type': 'application/json', 'token': token || '', 'userId': getUserId() || '', 'sign':md5(md5(sign.toString())), 'timestamp':time }, 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;