| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- // fmode-app-update v1.0.0 — App 版本更新检查(2026-11-23 新建)
- //
- // ## 为什么重写
- //
- // 老接口 `app_update` 在当前集群上**已经不存在**(实测 GET /parse/functions/app_update
- // 与 POST 均 404)。App 侧的更新检查因此完全失效 —— 用户装上去之后,
- // 除非自己去下载页重装,否则永远停留旧版本。
- //
- // 本函数接管这件事,职责单一:给一个 appid,返回该 App 的最新版本信息。
- //
- // ## 为什么不让客户端直接查 App 表
- //
- // 能查(App 表 public-read),但那样客户端就绑死了 Parse 的 REST 格式、
- // 绑死了 class 名和字段名,将来换存储或加灰度逻辑就要改客户端、发版、用户升级……
- // 而现在更新检查本身失效时,用户连升级都做不到,形成死锁。
- // 所以:**客户端只认这个接口的契约,服务端想怎么变都行。**
- //
- // ## 接口
- //
- // GET /api/functions/fmode-app-update?appid=cn.fmode.app
- // 或 POST /api/functions { id:<FunctionId>, params:{ appid, currentVersionCode } }
- //
- // 返回:
- // { code: 200, data: { appid, name, version, versionCode, url, changelog,
- // changelogUrl, isForce, size, platform, publishedAt } }
- // 或 { code: 404, msg: '未登记的 App' }
- //
- // 本函数**不需要登录**:更新检查发生在登录前(比如启动时),
- // 且返回的都是公开信息,不涉及用户数据。
- //
- // ## 设计要点
- //
- // · 只返回 isEnabled != false 的记录(下架的应用不该推给用户)
- // · url 优先 apkUrl,回落 downUrl —— 历史数据两者写法不统一
- // · versionCode 缺失时用 version 解析补齐,保证客户端能正确比较
- // · 支持灰度:传 currentVersionCode 时,若服务端配了 minVersionCode
- // 且当前版本低于它,返回 isForce=true(强制更新)
- // · 强缓存 60s:更新检查可能频繁调用,但也不能缓存太久,
- // 否则发了新版用户要等很久才能查到
- var APP_CLASS = 'App';
- var CACHE_TTL_MS = 60 * 1000;
- /** 进程内小缓存:{ appid: { at, data } } */
- var _cache = {};
- function _ok(response, data) {
- response.status(200).json({ code: 200, data: data });
- }
- function _fail(response, code, msg) {
- response.status(200).json({ code: code, msg: msg });
- }
- /**
- * 把 "1.0.12" 解析成可比较的整数。
- *
- * 规则:主*10000 + 次*100 + 修订。
- * 这样 1.0.9 < 1.0.10 能正确比较 —— 字符串直接比会判错。
- */
- function _parseVersionCode(v) {
- if (!v) return 0;
- var m = String(v).match(/(\d+)\.(\d+)\.(\d+)/);
- if (!m) return 0;
- return Number(m[1]) * 10000 + Number(m[2]) * 100 + Number(m[3]);
- }
- /** 取请求参数:GET 走 query,POST 走全局 params */
- function _param(request, name) {
- if (request && request.query && request.query[name] != null) {
- return request.query[name];
- }
- try {
- if (typeof params !== 'undefined' && params && params[name] != null) {
- return params[name];
- }
- } catch (e) {
- /* params 未注入时忽略 */
- }
- return null;
- }
- async function _fetchApp(appid) {
- /*
- * 用平台注入的 Parse 全局对象查询 —— 现有云函数都是这么做的
- * (fmodeagent-status / fmode-file-hub 均如此),不要自己拼 REST 请求:
- * 服务端内部地址和 master key 都不该硬编码在函数里。
- *
- * useMasterKey 是必须的:App 表虽公开可读,但走 master key
- * 能避开 ACL 差异,行为更稳定。
- */
- var q = new Parse.Query('App');
- q.equalTo('appid', appid);
- q.descending('updatedAt');
- q.limit(1);
- return await q.first({ useMasterKey: true });
- }
- async function handler(request, response) {
- var appid = _param(request, 'appid') || _param(request, 'appId');
- if (!appid) {
- _fail(response, 400, '缺少 appid 参数');
- return;
- }
- appid = String(appid).trim();
- // 命中缓存直接返回
- var hit = _cache[appid];
- if (hit && Date.now() - hit.at < CACHE_TTL_MS) {
- _ok(response, hit.data);
- return;
- }
- var row;
- try {
- row = await _fetchApp(appid);
- } catch (e) {
- _fail(response, 502, '查询失败:' + (e && e.message ? e.message : e));
- return;
- }
- if (!row) {
- _fail(response, 404, '未登记的 App:' + appid);
- return;
- }
- // 下架的应用不推送更新
- if (row.isEnabled === false) {
- _fail(response, 404, '该 App 已下架');
- return;
- }
- var versionCode = Number(row.versionCode) || _parseVersionCode(row.version);
- /*
- * 灰度/强制更新:客户端把当前 versionCode 带上来,
- * 若低于服务端配置的 minVersionCode,则强制更新。
- * 这样服务端改一个字段就能让所有人升级,不用发新版客户端。
- */
- var currentCode = Number(_param(request, 'currentVersionCode')) || 0;
- var minCode = Number(row.minVersionCode) || 0;
- var isForce = row.isForce === true;
- if (currentCode > 0 && minCode > 0 && currentCode < minCode) {
- isForce = true;
- }
- var data = {
- appid: row.appid,
- name: row.name || '',
- version: row.version || '',
- versionCode: versionCode,
- // 历史数据 apkUrl / downUrl 写法不统一,按优先级取
- url: row.apkUrl || row.downUrl || '',
- changelog: row.changelog || row.content || '',
- changelogUrl: row.changelogUrl || '',
- isForce: isForce,
- size: Number(row.size) || 0,
- platform: row.platform || ['android'],
- publishedAt: row.updatedAt || '',
- // 图标与开机图,供客户端做更新卡片
- logo: row.logo || '',
- bgImg: row.bgImg || '',
- };
- _cache[appid] = { at: Date.now(), data: data };
- _ok(response, data);
- }
- // 平台约定:必须显式导出 handler
- if (typeof module !== 'undefined' && module.exports) {
- module.exports = { handler: handler };
- }
|