diff --git a/common/config.js b/common/config.js index 53f6f6c..4f751db 100644 --- a/common/config.js +++ b/common/config.js @@ -30,25 +30,23 @@ class ConfigManager { } isLoading = true; - loadPromise = new Promise(async (resolve, reject) => { - const res = await RequestManager.request({ - url: 'config', - method: "GET", - data: {}, - Loading: isLoading - }); - console.log(res); - - if (res.status === 1 && res.data) { - configData = res.data; - uni.setStorageSync("configData", configData); - console.log('全局配置数据加载成功'); - resolve(configData); - } else { - console.error('加载配置数据失败:', res.msg || '未知错误'); - reject(new Error(res.msg || '加载配置失败')); - } - + loadPromise = new Promise((resolve, reject) => { + RequestManager.get('config') + .then(res => { + if (res.status === 1 && res.data) { + configData = res.data; + console.log('全局配置数据加载成功'); + resolve(configData); + } else { + console.error('加载配置数据失败:', res.msg || '未知错误'); + reject(new Error(res.msg || '加载配置失败')); + } + }) + .catch(err => { + console.error('请求配置数据失败:', err); + isLoading = false; + reject(err); + }); }); return loadPromise; @@ -85,13 +83,28 @@ class ConfigManager { if (configData != null) { return configData; } - this.loadConfig(); return defaultValue; } - return key in configData ? configData[key] : defaultValue; } + /** + * 盒子类型 + * @returns {Object} 商品类型对象 + */ + static getGoodType() { + let goodType = this.get('good_type'); + if (goodType != null) { + return goodType.map(item => { + return { + id: item.value, + title: item.name + } + }); + } + return []; + } + /** * 刷新配置数据 * @returns {Promise} 返回刷新完成的Promise @@ -122,23 +135,6 @@ class ConfigManager { return key in appSetting ? appSetting[key] : null; } - /** - * 盒子类型 - * @returns {Object} 商品类型对象 - */ - static getGoodType() { - let goodType = this.get('good_type'); - if (goodType != null) { - return goodType.map(item => { - return { - id: item.value, - title: item.name - } - }); - } - return []; - } - /** * 获取指定键的配置值 * @param {String} key 配置键 @@ -158,12 +154,11 @@ class ConfigManager { return key in configData ? configData[key] : defaultValue; } - /** - * 获取应用设置 - * @param {String} key 设置键 - * @returns {Object|String|null} 设置值 - */ + * 获取应用设置 + * @param {String} key 设置键 + * @returns {Object|String|null} 设置值 + */ static async getAppSettingAsync(key = null) { let appSetting = await this.getAsync('app_setting'); if (key == null) { @@ -171,7 +166,6 @@ class ConfigManager { } return key in appSetting ? appSetting[key] : null; } - } export default ConfigManager; \ No newline at end of file diff --git a/common/env.js b/common/env.js new file mode 100644 index 0000000..fbecb11 --- /dev/null +++ b/common/env.js @@ -0,0 +1,63 @@ +/** + * 项目环境配置文件 + * 集中管理所有环境相关的配置参数 + */ + +// 开发环境配置 +const development = { + // API基础URL + baseUrl: 'https://manghe.zpc-xy.com', + // 图片资源URL + imageUrl: 'https://mh.shhuanmeng.com', + // 登录页面URL + loginPage: 'https://xinglanmh.shequtuangou.vip/login.html', + // 微信APPID + wxAppId: 'wx0e33d80d35e4a3b1' +}; + +// 生产环境配置 +const production = { + baseUrl: 'https://manghe.zpc-xy.com', + imageUrl: 'https://mh.shhuanmeng.com', + loginPage: 'https://xinglanmh.shequtuangou.vip/login.html', + wxAppId: 'wx0e33d80d35e4a3b1' +}; + +// 测试环境配置 +const testing = { + baseUrl: 'https://manghe.zpc-xy.com', + imageUrl: 'https://mh.shhuanmeng.com', + loginPage: 'https://xinglanmh.shequtuangou.vip/login.html', + wxAppId: 'wx0e33d80d35e4a3b1' +}; + +// 根据环境变量选择对应配置 +let currentEnv = development; + +// 判断当前环境 +// #ifdef H5 +if (process.env.NODE_ENV === 'production') { + currentEnv = production; +} else if (process.env.NODE_ENV === 'testing') { + currentEnv = testing; +} +// #endif + +// #ifdef MP-WEIXIN +currentEnv = production; // 小程序默认使用生产环境配置 +// #endif + +// 衍生配置 +const config = { + ...currentEnv, + // API请求完整路径 + apiBaseUrl: currentEnv.baseUrl + '/api/', + // 微信登录重定向URL + wxLoginUrl: `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${currentEnv.wxAppId}&redirect_uri=${escape(currentEnv.loginPage)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`, + // 图片资源路径 + imageBaseUrl: currentEnv.imageUrl + '/static/web', + // 图标资源路径 + iconBaseUrl: currentEnv.imageUrl + '/static/web/static/' +}; + +export default config; \ No newline at end of file diff --git a/common/request.js b/common/request.js index 63433dd..1f63544 100644 --- a/common/request.js +++ b/common/request.js @@ -3,6 +3,8 @@ * 封装统一的网络请求方法 */ +import EnvConfig from '@/common/env.js' + class RequestManager { /** * 发送网络请求 @@ -49,28 +51,8 @@ class RequestManager { client = "h5" // #endif - // 获取应用实例和基础URL - const app = getApp() - let siteBaseUrl = '' - - if (app && app.globalData && app.globalData.siteBaseUrl) { - siteBaseUrl = app.globalData.siteBaseUrl - } else { - // 尝试从Vue原型获取 - try { - const Vue = require('vue').default - if (Vue.prototype && Vue.prototype.siteBaseUrl) { - siteBaseUrl = Vue.prototype.siteBaseUrl - } - } catch (e) { - console.error('获取siteBaseUrl失败', e) - } - - if (!siteBaseUrl) { - // 兜底处理 - siteBaseUrl = 'https://manghe.zpc-xy.com/api/' - } - } + // 获取API基础URL + const apiBaseUrl = EnvConfig.apiBaseUrl // 拼接完整请求地址,确保URL格式正确 let requestUrl = '' @@ -81,7 +63,7 @@ class RequestManager { } else { // 否则拼接基础URL和相对路径 // 确保基础URL以/结尾,而请求路径不以/开头 - const baseUrlWithSlash = siteBaseUrl.endsWith('/') ? siteBaseUrl : siteBaseUrl + '/' + const baseUrlWithSlash = apiBaseUrl.endsWith('/') ? apiBaseUrl : apiBaseUrl + '/' const path = url.startsWith('/') ? url.substring(1) : url requestUrl = baseUrlWithSlash + path } diff --git a/main.js b/main.js index 920ca68..4838072 100644 --- a/main.js +++ b/main.js @@ -3,37 +3,30 @@ import App from './App' import Mixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js' import common from '@/common/common.js' import { gotopage } from '@/common/gotopage.js' -import RequestManager from '@/common/request.js' import ConfigManager from '@/common/config.js' - - -// 基础配置 -const baseUrl = 'https://manghe.zpc-xy.com' -const imageUrl = 'https://mh.shhuanmeng.com' -const loginPage = "https://xinglanmh.shequtuangou.vip/login.html" +import RequestManager from '@/common/request.js' +import EnvConfig from '@/common/env.js' // 全局变量配置 -Vue.prototype.siteBaseUrl = baseUrl + '/api/' -Vue.prototype.$baseUrl = baseUrl -Vue.prototype.$z_img2 = imageUrl + '/zcq/' -Vue.prototype.$img = url => imageUrl + '/static/web' + url -Vue.prototype.$img1 = url => imageUrl + '/static/web/static/' + url +Vue.prototype.siteBaseUrl = EnvConfig.apiBaseUrl +Vue.prototype.$baseUrl = EnvConfig.baseUrl +Vue.prototype.$z_img2 = EnvConfig.imageUrl + '/zcq/' +Vue.prototype.$img = url => EnvConfig.imageBaseUrl + url +Vue.prototype.$img1 = url => EnvConfig.iconBaseUrl + url Vue.prototype.$sys = () => uni.getSystemInfoSync() -Vue.prototype.$loginPage = loginPage -Vue.prototype.$wxloginPage = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx0e33d80d35e4a3b1&redirect_uri=${escape(loginPage)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect` +Vue.prototype.$loginPage = EnvConfig.loginPage +Vue.prototype.$wxloginPage = EnvConfig.wxLoginUrl // 公共方法 Vue.prototype.gotoPage = gotopage Vue.prototype.$noMultipleClicks = common.noMultipleClicks Vue.prototype.$c = common - +// 全局配置管理器 +Vue.prototype.$config = ConfigManager // 全局请求方法 Vue.prototype.req = RequestManager.request Vue.prototype.$request = RequestManager -// 全局配置管理器 -Vue.prototype.$config = ConfigManager - // #ifdef H5 function loadScript(url) { var script = document.createElement('script') @@ -78,7 +71,7 @@ const app = new Vue({ // 创建全局数据对象 app.globalData = { - siteBaseUrl: baseUrl + '/api/' + siteBaseUrl: EnvConfig.apiBaseUrl } // 应用启动时加载全局配置 diff --git a/pages/shouye/index.vue b/pages/shouye/index.vue index 83c1905..3baee36 100644 --- a/pages/shouye/index.vue +++ b/pages/shouye/index.vue @@ -291,7 +291,6 @@ export default { lffBarrage, }, data() { - // console.log(this.$config.getGoodType()); let tabList = this.$config.getGoodType(); return { z_imgPath: this.$z_img2 + "shouye/",