添加配置项

This commit is contained in:
zpc 2025-03-23 00:08:44 +08:00
parent 4b7a94a46f
commit 846aa96fa9
5 changed files with 118 additions and 87 deletions

View File

@ -30,25 +30,23 @@ class ConfigManager {
} }
isLoading = true; isLoading = true;
loadPromise = new Promise(async (resolve, reject) => { loadPromise = new Promise((resolve, reject) => {
const res = await RequestManager.request({ RequestManager.get('config')
url: 'config', .then(res => {
method: "GET", if (res.status === 1 && res.data) {
data: {}, configData = res.data;
Loading: isLoading console.log('全局配置数据加载成功');
}); resolve(configData);
console.log(res); } else {
console.error('加载配置数据失败:', res.msg || '未知错误');
if (res.status === 1 && res.data) { reject(new Error(res.msg || '加载配置失败'));
configData = res.data; }
uni.setStorageSync("configData", configData); })
console.log('全局配置数据加载成功'); .catch(err => {
resolve(configData); console.error('请求配置数据失败:', err);
} else { isLoading = false;
console.error('加载配置数据失败:', res.msg || '未知错误'); reject(err);
reject(new Error(res.msg || '加载配置失败')); });
}
}); });
return loadPromise; return loadPromise;
@ -85,13 +83,28 @@ class ConfigManager {
if (configData != null) { if (configData != null) {
return configData; return configData;
} }
this.loadConfig();
return defaultValue; return defaultValue;
} }
return key in configData ? configData[key] : 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 * @returns {Promise} 返回刷新完成的Promise
@ -122,23 +135,6 @@ class ConfigManager {
return key in appSetting ? appSetting[key] : null; 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 配置键 * @param {String} key 配置键
@ -158,12 +154,11 @@ class ConfigManager {
return key in configData ? configData[key] : defaultValue; return key in configData ? configData[key] : defaultValue;
} }
/** /**
* 获取应用设置 * 获取应用设置
* @param {String} key 设置键 * @param {String} key 设置键
* @returns {Object|String|null} 设置值 * @returns {Object|String|null} 设置值
*/ */
static async getAppSettingAsync(key = null) { static async getAppSettingAsync(key = null) {
let appSetting = await this.getAsync('app_setting'); let appSetting = await this.getAsync('app_setting');
if (key == null) { if (key == null) {
@ -171,7 +166,6 @@ class ConfigManager {
} }
return key in appSetting ? appSetting[key] : null; return key in appSetting ? appSetting[key] : null;
} }
} }
export default ConfigManager; export default ConfigManager;

63
common/env.js Normal file
View File

@ -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;

View File

@ -3,6 +3,8 @@
* 封装统一的网络请求方法 * 封装统一的网络请求方法
*/ */
import EnvConfig from '@/common/env.js'
class RequestManager { class RequestManager {
/** /**
* 发送网络请求 * 发送网络请求
@ -49,28 +51,8 @@ class RequestManager {
client = "h5" client = "h5"
// #endif // #endif
// 获取应用实例和基础URL // 获取API基础URL
const app = getApp() const apiBaseUrl = EnvConfig.apiBaseUrl
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/'
}
}
// 拼接完整请求地址确保URL格式正确 // 拼接完整请求地址确保URL格式正确
let requestUrl = '' let requestUrl = ''
@ -81,7 +63,7 @@ class RequestManager {
} else { } else {
// 否则拼接基础URL和相对路径 // 否则拼接基础URL和相对路径
// 确保基础URL以/结尾,而请求路径不以/开头 // 确保基础URL以/结尾,而请求路径不以/开头
const baseUrlWithSlash = siteBaseUrl.endsWith('/') ? siteBaseUrl : siteBaseUrl + '/' const baseUrlWithSlash = apiBaseUrl.endsWith('/') ? apiBaseUrl : apiBaseUrl + '/'
const path = url.startsWith('/') ? url.substring(1) : url const path = url.startsWith('/') ? url.substring(1) : url
requestUrl = baseUrlWithSlash + path requestUrl = baseUrlWithSlash + path
} }

31
main.js
View File

@ -3,37 +3,30 @@ import App from './App'
import Mixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js' import Mixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
import common from '@/common/common.js' import common from '@/common/common.js'
import { gotopage } from '@/common/gotopage.js' import { gotopage } from '@/common/gotopage.js'
import RequestManager from '@/common/request.js'
import ConfigManager from '@/common/config.js' import ConfigManager from '@/common/config.js'
import RequestManager from '@/common/request.js'
import EnvConfig from '@/common/env.js'
// 基础配置
const baseUrl = 'https://manghe.zpc-xy.com'
const imageUrl = 'https://mh.shhuanmeng.com'
const loginPage = "https://xinglanmh.shequtuangou.vip/login.html"
// 全局变量配置 // 全局变量配置
Vue.prototype.siteBaseUrl = baseUrl + '/api/' Vue.prototype.siteBaseUrl = EnvConfig.apiBaseUrl
Vue.prototype.$baseUrl = baseUrl Vue.prototype.$baseUrl = EnvConfig.baseUrl
Vue.prototype.$z_img2 = imageUrl + '/zcq/' Vue.prototype.$z_img2 = EnvConfig.imageUrl + '/zcq/'
Vue.prototype.$img = url => imageUrl + '/static/web' + url Vue.prototype.$img = url => EnvConfig.imageBaseUrl + url
Vue.prototype.$img1 = url => imageUrl + '/static/web/static/' + url Vue.prototype.$img1 = url => EnvConfig.iconBaseUrl + url
Vue.prototype.$sys = () => uni.getSystemInfoSync() Vue.prototype.$sys = () => uni.getSystemInfoSync()
Vue.prototype.$loginPage = loginPage Vue.prototype.$loginPage = EnvConfig.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.$wxloginPage = EnvConfig.wxLoginUrl
// 公共方法 // 公共方法
Vue.prototype.gotoPage = gotopage Vue.prototype.gotoPage = gotopage
Vue.prototype.$noMultipleClicks = common.noMultipleClicks Vue.prototype.$noMultipleClicks = common.noMultipleClicks
Vue.prototype.$c = common Vue.prototype.$c = common
// 全局配置管理器
Vue.prototype.$config = ConfigManager
// 全局请求方法 // 全局请求方法
Vue.prototype.req = RequestManager.request Vue.prototype.req = RequestManager.request
Vue.prototype.$request = RequestManager Vue.prototype.$request = RequestManager
// 全局配置管理器
Vue.prototype.$config = ConfigManager
// #ifdef H5 // #ifdef H5
function loadScript(url) { function loadScript(url) {
var script = document.createElement('script') var script = document.createElement('script')
@ -78,7 +71,7 @@ const app = new Vue({
// 创建全局数据对象 // 创建全局数据对象
app.globalData = { app.globalData = {
siteBaseUrl: baseUrl + '/api/' siteBaseUrl: EnvConfig.apiBaseUrl
} }
// 应用启动时加载全局配置 // 应用启动时加载全局配置

View File

@ -291,7 +291,6 @@ export default {
lffBarrage, lffBarrage,
}, },
data() { data() {
// console.log(this.$config.getGoodType());
let tabList = this.$config.getGoodType(); let tabList = this.$config.getGoodType();
return { return {
z_imgPath: this.$z_img2 + "shouye/", z_imgPath: this.$z_img2 + "shouye/",