manghe/public/js/AppConfigManager.js
2025-05-18 12:45:06 +08:00

99 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class AppConfigManager {
constructor(configList, goodsTypeList) {
this.rawList = configList;
this.goodsTypeList = goodsTypeList;
this.configMap = this._parseConfigList(configList);
}
getGoodsType(key) {
if (this.goodsTypeList == null || key == null) {
return null;
}
return this.goodsTypeList.find(it => it.value == key);
}
getGoodsTypeName(key) {
return this.getGoodsType(key)?.name;
}
/**
* 内部方法:将原始列表转为对象映射结构
* @param {Array} configList 原始配置数组
* @returns {Object} 映射后的对象
*/
_parseConfigList(configList) {
const result = {};
configList.forEach(item => {
try {
result[item.key] = JSON.parse(item.value);
} catch (e) {
result[item.key] = {}; // 解析失败设为空对象
console.warn(`配置项 ${item.key} JSON解析失败`, e);
}
});
return result;
}
/**
* 获取指定 key 的配置项对象
* @param {String} key 要获取的键名
* @returns {Object|null} 对应的配置对象
*/
get(key) {
return this.configMap.hasOwnProperty(key) ? this.configMap[key] : null;
}
/**
* 获取全部配置映射对象
* @returns {Object}
*/
getAll() {
return this.configMap;
}
/**
* 获取微信支付配置列表
* @returns {Object}
*/
getWxPayConfig() {
return this.get("weixinpay_setting")?.merchants;
}
getWxPayConfigByType(order_num) {
if (!order_num || order_num.length < 6) {
console.warn('订单号无效');
return null;
}
// 取第 3~6 位字符作为前缀DEA、DEC、DED...
const order_key = order_num.substr(3, 3);
// console.log('订单号:', order_num, '订单前缀:', order_key);
if(order_key=="MON"){
return {
name: '货币支付',
mch_id: '无',
order_prefix: 'MON',
appid: '',
appsecret: ''
}
}
if(order_key=="ZFA"){
return {
name: '支付宝支付',
mch_id: '',
order_prefix: 'ZFA',
appid: '',
appsecret: ''
}
}
const wxPayConfigList = this.getWxPayConfig();
if (!Array.isArray(wxPayConfigList)) {
console.warn('微信支付配置无效');
return null;
}
// 在配置中查找匹配的 order_prefix
const match = wxPayConfigList.find(cfg => cfg.order_prefix === order_key);
return match || null; // 找不到返回 null
}
}