zm2000 245bbc9287 fix: 修复蓝牙秤重量解析错误并升级版本至 1.0.131
蓝牙秤解析修复(src/core/store/modules/bluetoothScale.js):
- HC-06 类 '=' 帧协议改为“整数部分 + 小数部分”合并后整串逆序,
  修复实际 1523g 被解析成 523.1g 的问题(=325.100 → 1523g)

APK 版本对齐(android-offline/prepare.cjs):
- 打包注入时把 www/manifest.json 的应用版本强制对齐到 package.json,
  修复应用内 plus.runtime.getProperty 读到旧版本导致反复弹更新

版本号升级:
- package.json:1.0.128 → 1.0.131
- manifest.json:versionName 1.0.127 → 1.0.131 / versionCode 1000131

今日打包统计(src/pages/packing/packMode.vue):
- 统计统一在 onShow 拉取,onLoad 不再重复请求
- 完成打包后刷新今日统计
- 打开统计面板时强制刷新
2026-10-24 18:00:32 +08:00

210 lines
9.2 KiB
JavaScript

/**
* Android 离线打包准备脚本
* 职责:校验 config/android.json、拷贝 App 资源(www)、覆盖图标/启动图。
* 包名/版本/签名/应用名/appid/appKey 等由 simpleDemo/build.gradle 在构建时动态读取
* config/android.json 与 package.json,本脚本不再写入工程文件。
*
* 用法:
* node android-offline/prepare.cjs
* node android-offline/prepare.cjs <wwwSource> # 可选:覆盖 www 资源目录
*/
const fs = require('fs')
const path = require('path')
const ROOT = path.resolve(__dirname, '..')
const OFFLINE_DIR = __dirname
const CONFIG_PATH = path.join(ROOT, 'config', 'android.json')
const log = (...args) => console.log('[prepare]', ...args)
const warn = (...args) => console.warn('[prepare][警告]', ...args)
// ---------- 1. 读取配置 ----------
let config
try {
config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'))
} catch (error) {
if (error.code === 'ENOENT') {
console.error(`[prepare][错误] 未找到配置文件: ${CONFIG_PATH}`)
console.error('请复制 config/android.example.json 为 config/android.json 并填写(appKey、signing 等),详见 config/README.md')
} else {
console.error(`[prepare][错误] 配置文件解析失败 ${CONFIG_PATH}: ${error.message}`)
}
process.exit(1)
}
const required = ['appId', 'appName', 'packageName']
for (const key of required) {
if (!config[key]) {
console.error(`[prepare][错误] config.json 缺少必填项: ${key}`)
process.exit(1)
}
}
// ---------- 1.5 版本一致性校验 ----------
// 离线包的 versionName/versionCode 由 simpleDemo/build.gradle 从 package.json 推导
// (versionCode = X*1000000 + Y*1000 + Z);而 src/manifest.json 的 versionName/versionCode
// 是 HBuilderX 云打包使用的值。两者必须一致,否则同一版本会出现两个 APK 版本号,
// 应用内更新提示、版本排查都会对不上号。
// 注:编译产物 www/manifest.json 内含 version.name/version.code(来自 src/manifest.json)。
// plus.runtime.getProperty(appid).version 读的正是这个值,不是 Android 原生 versionName。
// 因此除一致性校验外,下面还会把注入 APK 的 www/manifest.json 版本强制对齐到 package.json。
try {
const pkgVersion = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf-8')).version
const [x, y, z] = String(pkgVersion).split('.').map((n) => parseInt(n, 10))
const expectedCode = x * 1000000 + y * 1000 + z
const manifestText = fs.readFileSync(path.join(ROOT, 'src', 'manifest.json'), 'utf-8')
const mVersion = (manifestText.match(/"versionName"\s*:\s*"([^"]*)"/) || [])[1]
const mCode = Number((manifestText.match(/"versionCode"\s*:\s*(\d+)/) || [])[1])
log(`版本: package.json=${pkgVersion} (versionCode ${expectedCode}) | src/manifest.json=${mVersion} (versionCode ${mCode})`)
if (mVersion !== pkgVersion || mCode !== expectedCode) {
warn('package.json 与 src/manifest.json 版本号不一致,请两处同步(否则云打包/离线包会出现不同版本号)')
}
} catch (error) {
warn(`版本一致性校验已跳过: ${error.message}`)
}
// ---------- 2. 定位 SDK 工程 ----------
const AS_DIR = path.join(OFFLINE_DIR, 'HBuilder-Integrate-AS')
const DEMO_DIR = path.join(AS_DIR, 'simpleDemo')
if (!fs.existsSync(AS_DIR)) {
console.error('[prepare][错误] 未找到离线 SDK 工程: ' + AS_DIR)
console.error('请先下载离线 SDK 并解压到 android-offline/ 目录,详见 README.md')
process.exit(1)
}
if (!fs.existsSync(DEMO_DIR)) {
console.error('[prepare][错误] 未找到 simpleDemo 工程: ' + DEMO_DIR)
process.exit(1)
}
let failed = false
// ---------- 3. 签名配置预检(Gradle 构建时从 config 动态读取) ----------
if (!config.signing || !config.signing.keystore) {
warn('config/android.json 中 signing.keystore 为空,Gradle 构建时将因缺少签名配置而失败')
} else if (!fs.existsSync(path.resolve(ROOT, config.signing.keystore))) {
warn(`签名文件不存在: ${path.resolve(ROOT, config.signing.keystore)}`)
}
if (!config.appKey) {
warn('config/android.json 中 appKey 为空,请到 https://dev.dcloud.net.cn 申请后填写')
}
// ---------- 4. 拷贝 App 资源到 assets/apps/{appId}/www ----------
const wwwTarget = path.join(DEMO_DIR, 'src', 'main', 'assets', 'apps', config.appId, 'www')
// 可选外部 www 源覆盖:node prepare.cjs <wwwSource>;其次用 config.android.json 的 wwwSource
const wwwOverride = process.argv[2] ? path.resolve(ROOT, process.argv[2]) : ''
const candidates = [
...(wwwOverride ? [wwwOverride] : []),
...(config.wwwSource ? [path.resolve(ROOT, config.wwwSource)] : []),
// 优先使用 CLI 最新编译产物(dist/build/app-plus)。
// unpackage/resources 是 HBuilderX 本地打包产物,可能已过期,仅作兜底,
// 避免把旧代码打进 APK。
path.join(ROOT, 'dist', 'build', 'app-plus'),
path.join(ROOT, 'unpackage', 'resources', config.appId, 'www'),
]
const wwwSource = candidates.find((dir) => fs.existsSync(dir))
if (!wwwSource) {
failed = true
console.error('[prepare][错误] 未找到 App 资源,请先执行 npm run build:app-plus(或 HBuilderX 生成本地打包资源)')
} else {
fs.rmSync(wwwTarget, { recursive: true, force: true })
fs.cpSync(wwwSource, wwwTarget, { recursive: true })
log(`✓ 拷贝 App 资源: ${path.relative(ROOT, wwwSource)} -> assets/apps/${config.appId}/www`)
}
// ---------- 4.5 对齐编译器版本(隐藏"HTML5+ Runtime 版本不一致"提示) ----------
// CLI 构建资源时写入的 compilerVersion(如 5.07)与离线 SDK Runtime 版本不一致时,
// 启动会弹"本应用使用HBuilderX %s 编译,而手机端SDK版本是 %s"对话框。
// 此处把 www 内 manifest.json / app-config-service.js 的 compilerVersion 对齐到
// config/android.json 的 runtimeVersion(默认 5.24,与离线 SDK 一致),即可消除提示。
const runtimeVersion = config.runtimeVersion || '5.24'
function alignCompilerVersion (filePath) {
try {
if (!fs.existsSync(filePath)) return
let text = fs.readFileSync(filePath, 'utf-8')
const re = /"compilerVersion"\s*:\s*"[^"]*"/g
text = text.replace(re, '"compilerVersion": "' + runtimeVersion + '"')
fs.writeFileSync(filePath, text, 'utf-8')
log(`✓ 对齐 compilerVersion -> ${runtimeVersion}: ${path.relative(ROOT, filePath)}`)
} catch (e) {
warn(`对齐 compilerVersion 失败 ${filePath}: ${e.message}`)
}
}
alignCompilerVersion(path.join(wwwTarget, 'manifest.json'))
alignCompilerVersion(path.join(wwwTarget, 'app-config-service.js'))
// ---------- 4.6 对齐 www/manifest.json 的应用版本(防止应用内读旧版本反复弹更新) ----------
// plus.runtime.getProperty(appid).version 读取的是 www/manifest.json 的 version.name,
// 而非 Android 原生 versionName。若 src/manifest.json 忘记与 package.json 同步,
// 会出现 APK 系统版本是新的、但应用内读到的仍是旧版本,导致“已更新却反复弹更新”。
// 此处把注入 APK 的 www/manifest.json 版本强制对齐到 package.json(唯一版本源)。
function alignAppVersion (filePath) {
try {
if (!fs.existsSync(filePath)) return
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf-8'))
const parts = String(pkg.version).split('.').map((n) => parseInt(n, 10) || 0)
const code = (parts[0] || 0) * 1000000 + (parts[1] || 0) * 1000 + (parts[2] || 0)
let text = fs.readFileSync(filePath, 'utf-8')
const replaced = text.replace(
/"version"\s*:\s*\{[^}]*\}/,
`"version":{"name":"${pkg.version}","code":${code}}`
)
if (replaced !== text) {
fs.writeFileSync(filePath, replaced, 'utf-8')
log(`✓ 对齐应用版本 -> ${pkg.version} (code ${code}): ${path.relative(ROOT, filePath)}`)
}
} catch (e) {
warn(`对齐应用版本失败 ${filePath}: ${e.message}`)
}
}
alignAppVersion(path.join(wwwTarget, 'manifest.json'))
// ---------- 9. 覆盖图标与启动图 ----------
function findResFiles(pattern) {
const resDir = path.join(DEMO_DIR, 'src', 'main', 'res')
const hits = []
if (!fs.existsSync(resDir)) return hits
const walk = (dir) => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name)
if (entry.isDirectory()) walk(full)
else if (pattern.test(entry.name)) hits.push(full)
}
}
walk(resDir)
return hits
}
function applyImage(cfgPath, patterns, desc) {
if (!cfgPath) return
const image = path.resolve(ROOT, cfgPath)
if (!fs.existsSync(image)) {
warn(`${desc}不存在,跳过: ${image}`)
return
}
const targets = findResFiles(patterns)
if (targets.length === 0) {
warn(`${desc}: 未在 res 下找到目标文件(正则: ${patterns}),请手动替换`)
return
}
for (const target of targets) {
fs.copyFileSync(image, target)
log(`✓ 覆盖${desc}: ${path.relative(ROOT, target)}`)
}
}
applyImage(config.icon, /^(icon\.png|ic_launcher(\.png)?)$/i, '应用图标')
applyImage(config.splash, /^splash(\.png|_screen\.png)?$/i, '启动图')
// ---------- 5. 总结 ----------
console.log('\n[prepare] 完成' + (failed ? '(部分失败,请查看上方错误)' : ''))
console.log('后续:包名/版本/签名/应用名/appid/appKey 由 Gradle 构建时从 config/android.json 与 package.json 动态读取')
if (!failed) process.exit(0)
process.exit(1)