Files
lgp-wx-plus/api/env.js
年糕崽崽 516f077568 初始化
缺陷:主题配色需要优化整体的同风格
2026-08-14 23:17:19 +08:00

93 lines
3.3 KiB
JavaScript
Raw Permalink 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.
/**
* 小程序运行环境与品牌配置(统一入口)
*
* 老代码在 interceptor.js 里把 5 个 baseURL 用注释切换,极易把测试地址发上线。
* 这里把所有环境相关的值收口到一个文件interceptor.js 只读这里的常量。
*
* 【品牌区分】
* lgp-wx 和 lgp-wx-new 是同一套代码的两个目录差异只有品牌brm=铂尔曼 / wl=维伦)。
* 因为 uni-app 条件编译(#ifdef只支持平台维度MP-WEIXIN 等),不支持自定义变量,
* 而本项目又是纯 HBuilderX 工程(没有 CLI 环境变量注入),唯一可靠的品牌区分点
* 是 manifest.json 的 mp-weixin.appid。这里用 BRAND 常量收敛:
* - 铂尔曼项目lgp-wxBRAND = 'brm'
* - 维伦项目lgp-wx-newBRAND = 'wl'
* 同步到另一个项目时只改这一行 BRANDbaseURL / appid / 名称会自动跟着切。
*
* 【环境区分】
* ENV 用 process.env.NODE_ENV 区分开发/生产uni-app 编译时注入),
* 默认生产,开发环境用 LOCAL_BASE。
*/
/** 当前品牌:'brm' 铂尔曼 / 'wl' 维伦 —— 同步项目时改这一行 */
export const BRAND = 'wl';
/**
* 品牌维度的配置表。
* appid 必须与 manifest.json 的 mp-weixin.appid 保持一致(构建时校验,防止串包)。
* baseURL 是该品牌的线上 API 地址。
* name 是该品牌的展示名。
*/
export const BRAND_CONFIG = {
brm: {
appid: 'wx679d36842570cea7',
baseURL: 'https://brm.wx.api.borman.top/api/wx/',
name: '佛山铂尔曼',
},
wl: {
appid: 'wx57b54060a579c31a',
baseURL: 'https://wx.api.borman.top/api/wx/',
name: '维伦家具',
},
};
/** 本地联调地址(开发环境用,按需指向本机或测试服) */
export const LOCAL_BASE = 'http://127.0.0.1:18004/api/wx/';
/**
* 当前环境:开发模式下走本地地址,否则走品牌线上地址。
* process.env.NODE_ENV 由 uni-app 编译时注入HBuilderX 运行=development发行=production
*/
const ENV = process.env.NODE_ENV === 'development' ? 'dev' : 'prod';
/** 当前生效的 API baseURLinterceptor.js 只读这一个值 */
export const API_BASE =
ENV === 'dev' ? LOCAL_BASE : BRAND_CONFIG[BRAND].baseURL;
/** 当前品牌名(页面标题等展示用) */
export const BRAND_NAME = BRAND_CONFIG[BRAND].name;
/** 当前品牌 appid与 manifest.json 校验用) */
export const BRAND_APPID = BRAND_CONFIG[BRAND].appid;
/**
* 启动时校验 manifest.json 的 appid 与 BRAND 是否匹配。
* 不匹配说明改了 BRAND 忘了改 manifest或反之构建出的包会串品牌必须报错。
*/
function checkBrandAppid() {
// #ifdef MP-WEIXIN
// 小程序端用 wx.getAccountInfoSync 拿真实 appid 校验
try {
const info = wx.getAccountInfoSync();
const realAppid = info && info.miniProgram && info.miniProgram.appId;
if (realAppid && realAppid !== BRAND_APPID) {
// eslint-disable-next-line no-console
console.error(
`[env] 品牌不匹配:当前 BRAND=${BRAND}(期望 appid=${BRAND_APPID}),但小程序真实 appid=${realAppid}。请检查 manifest.json 或 api/env.js 的 BRAND。`,
);
}
} catch (e) {
// 获取失败不阻塞,仅打日志
}
// #endif
}
checkBrandAppid();
export default {
BRAND,
API_BASE,
BRAND_NAME,
BRAND_APPID,
ENV,
};