From 4a45efcec76cbe047d069aef04e315082ff0775d Mon Sep 17 00:00:00 2001
From: Jacob <hlnie@tohours.com>
Date: 星期一, 12 十月 2026 18:04:20 +0800
Subject: [PATCH] Merge branch 'master' into sit

---
 scripts/manifest.js |  122 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/scripts/manifest.js b/scripts/manifest.js
new file mode 100644
index 0000000..067088e
--- /dev/null
+++ b/scripts/manifest.js
@@ -0,0 +1,122 @@
+/**
+ * manifest.json 鐢熸垚鍣�
+ *
+ * manifest.json 鏄� HBuilderX 鐨勭紪璇戣緭鍏ワ紝浣嗗唴鍚幆澧冪浉鍏� appid锛屼笉閫傚悎鐩存帴鎻愪氦锛�
+ * 鍥犳妯℃澘鍏ュ簱銆佺敓鎴愮墿蹇界暐锛�
+ *
+ *   manifest.json.demo锛堝叆搴擄紝妯℃澘锛�  鈹�鈹�娉ㄥ叆鐜 appid鈹�鈹�鈻�  manifest.json锛�.gitignore锛�
+ *
+ * appid 鏁版嵁婧愬敮涓�锛歝onfig/env/<env>.js 鐨� UNI_APPID锛圖Cloud 鏍囪瘑锛夈�丄PPID锛堝井淇″皬绋嬪簭 appid锛�
+ * 璇存槑锛氭ā鏉块噷 mp-weixin 鐨� appid 鍏佽鍐欐垚 null 鎴栦笉瀛樺湪锛岀敓鎴愭椂浼氳嚜鍔ㄥ啓鍏�/琛ラ綈銆�
+ */
+const fs = require('fs');
+const path = require('path');
+
+const ROOT = path.resolve(__dirname, '..');
+const TEMPLATE = path.join(ROOT, 'manifest.json.demo');
+const TARGET = path.join(ROOT, 'manifest.json');
+
+function fail(msg) {
+	console.error('[manifest] ' + msg);
+	process.exit(1);
+}
+
+/** 鍙浛鎹㈢涓�澶� "appid" : <瀛楃涓叉垨 null>锛屼繚鐣欏師鏈夌殑绌烘牸/缂╄繘椋庢牸 */
+function replaceAppId(text, value) {
+	let done = false;
+	const result = text.replace(/"appid"(\s*:\s*)(?:"[^"]*"|null)/, function(match, sep) {
+		done = true;
+		return '"appid"' + sep + '"' + value + '"';
+	});
+	return { text: result, done: done };
+}
+
+/** 娉ㄥ叆涓や釜 appid锛氶《灞傦紙DCloud 鏍囪瘑锛�+ mp-weixin 鍧楀唴锛堝井淇″皬绋嬪簭 appid锛� */
+function injectAppIds(text, env) {
+	// 1) 椤跺眰 appid锛堢涓�澶勫嚭鐜板嵆涓洪《灞傦級
+	const top = replaceAppId(text, env.UNI_APPID);
+	if (!top.done) fail('妯℃澘涓湭鎵惧埌椤跺眰 "appid" 瀛楁锛�' + path.relative(ROOT, TEMPLATE));
+	text = top.text;
+
+	// 2) mp-weixin 涓嬬殑 appid锛屽彧鍦ㄨ鍖哄潡鍐呮浛鎹㈢涓�澶�
+	const idx = text.indexOf('"mp-weixin"');
+	if (idx < 0) fail('妯℃澘涓湭鎵惧埌 "mp-weixin" 閰嶇疆鍧楋細' + path.relative(ROOT, TEMPLATE));
+	const head = text.slice(0, idx);
+	const body = text.slice(idx);
+	const scoped = replaceAppId(body, env.APPID);
+	if (scoped.done) return head + scoped.text;
+
+	// 3) 瀛楁琚垹鎺夋椂锛屾寜鍚岀骇瀛愰」缂╄繘琛ヤ竴琛�
+	const m = body.match(/^("mp-weixin"\s*:\s*\{)(\r?\n)([ \t]*)/);
+	if (!m) fail('妯℃澘鐨� "mp-weixin" 鍧楁牸寮忓紓甯革紝鏃犳硶鍐欏叆 appid');
+	const nl = m[2];
+	const indent = m[3];
+	return head + m[1] + nl + indent + '"appid" : "' + env.APPID + '",' + nl + indent + body.slice(m[0].length);
+}
+
+/** 缁熻銆岄櫎 appid 琛屼互澶栥�嶇殑宸紓锛堟寜琛屽唴瀹规瘮杈冿紝涓嶅彈琛屽彿浣嶇Щ褰卞搷锛� */
+function otherDiff(a, b) {
+	function count(text) {
+		const m = new Map();
+		text.split(/\r?\n/).forEach(function(line) {
+			const l = line.replace(/\s+$/, '');
+			if (/^\s*"appid"\s*:/.test(l.trim())) return;
+			m.set(l, (m.get(l) || 0) + 1);
+		});
+		return m;
+	}
+	const ma = count(a);
+	const mb = count(b);
+	const keys = new Set(Array.from(ma.keys()).concat(Array.from(mb.keys())));
+	let diff = 0;
+	const sample = [];
+	keys.forEach(function(k) {
+		const d = Math.abs((ma.get(k) || 0) - (mb.get(k) || 0));
+		if (!d) return;
+		diff += d;
+		if (sample.length < 3 && k.trim()) sample.push(k.trim().slice(0, 40));
+	});
+	return { count: diff, sample: sample };
+}
+
+/**
+ * 鎸夌幆澧冪敓鎴� manifest.json
+ * @param {object} env config/env/<env>.js 瀵煎嚭鐨勯厤缃�
+ * @returns {{ file: string, changed: boolean }}
+ */
+function generate(env) {
+	if (!fs.existsSync(TEMPLATE)) {
+		fail('鏈壘鍒版ā鏉� ' + path.relative(ROOT, TEMPLATE) +
+			'\n  璇锋妸 manifest.json 鐨勬ā鏉夸繚瀛樹负 manifest.json.demo锛堥渶鍏ュ簱锛夊悗鍐嶇敓鎴�');
+	}
+
+	const rendered = injectAppIds(fs.readFileSync(TEMPLATE, 'utf8'), env);
+	const exists = fs.existsSync(TARGET);
+
+	if (exists) {
+		const prev = fs.readFileSync(TARGET, 'utf8');
+		// 琛屽熬宸紓锛圕RLF/LF锛変笉绠楀彉鍖栵紝閬垮厤鏃犳剰涔夐噸鍐�
+		if (prev.replace(/\r\n/g, '\n') === rendered.replace(/\r\n/g, '\n')) {
+			console.log('[manifest] manifest.json 宸叉槸鐩爣鍐呭锛屾棤闇�閲嶅啓');
+			return { file: TARGET, changed: false };
+		}
+		const diffs = otherDiff(prev, rendered);
+		if (diffs.count) {
+			console.log('[manifest] 娉ㄦ剰锛氱幇鏈� manifest.json 鏈� ' + diffs.count + ' 琛屽唴瀹逛笌妯℃澘涓嶅悓锛屾湰娆′細琚ā鏉胯鐩�' +
+				(diffs.sample.length ? '锛堝 ' + diffs.sample.join(' | ') + '锛�' : ''));
+			console.log('[manifest]       闇�瑕侀暱鏈熶繚鐣欑殑鏀瑰姩璇峰啓杩� manifest.json.demo');
+		}
+	}
+
+	fs.writeFileSync(TARGET, rendered, 'utf8');
+	console.log('[manifest] 宸茬敱 manifest.json.demo 鐢熸垚 manifest.json' +
+		'锛圖Cloud ' + env.UNI_APPID + ' / 灏忕▼搴� ' + env.APPID + '锛�');
+	return { file: TARGET, changed: true };
+}
+
+module.exports = {
+	TEMPLATE: TEMPLATE,
+	TARGET: TARGET,
+	generate: generate,
+	injectAppIds: injectAppIds
+};

--
Gitblit v1.4.0