From 102d036f3c119bf286ca1afc63bd52d66cf23ea2 Mon Sep 17 00:00:00 2001
From: whycq <10027870+whycq@user.noreply.gitee.com>
Date: 星期五, 10 二月 2023 12:46:24 +0800
Subject: [PATCH] #
---
uni_modules/uni-upgrade-center-app/uniCloud/cloudfunctions/check-version/index.js | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 167 insertions(+), 0 deletions(-)
diff --git a/uni_modules/uni-upgrade-center-app/uniCloud/cloudfunctions/check-version/index.js b/uni_modules/uni-upgrade-center-app/uniCloud/cloudfunctions/check-version/index.js
new file mode 100644
index 0000000..b46f10b
--- /dev/null
+++ b/uni_modules/uni-upgrade-center-app/uniCloud/cloudfunctions/check-version/index.js
@@ -0,0 +1,167 @@
+'use strict';
+exports.main = async (event, context) => {
+ /**
+ * 妫�娴嬪崌绾� 浣跨敤璇存槑
+ * 涓婁紶鍖咃細
+ * 1. 鏍规嵁浼犲弬锛屽厛妫�娴嬩紶鍙傛槸鍚﹀畬鏁达紝appid appVersion wgtVersion 蹇呬紶
+ * 2. 鍏堜粠鏁版嵁搴撳彇鍑烘墍鏈夎骞冲彴锛堜粠涓婁笅鏂囪鍙栧钩鍙颁俊鎭紝榛樿 Andriod锛夌殑鎵�鏈夌嚎涓婂彂琛屾洿鏂�
+ * 3. 鍐嶄粠鎵�鏈夌嚎涓婂彂琛屾洿鏂颁腑鍙栧嚭鐗堟湰鏈�澶х殑涓�鐗堛�傚鏋滃彲浠ワ紝灏介噺鍏堟娴媤gt鐨勭嚎涓婂彂琛岀増鏇存柊
+ * 4. 浣跨敤涓婃鍙栧嚭鐨勭増鏈寘鐨勭増鏈彿 鍜屼紶鍙� appVersion銆亀gtVersion 鏉ユ娴嬫槸鍚︽湁鏇存柊锛屽繀椤诲悓鏃跺ぇ浜庤繖涓ら」锛屽惁鍒欒繑鍥炴殏鏃犳洿鏂�
+ * 5. 濡傛灉搴撲腑 wgt鍖� 鐗堟湰澶т簬浼犲弬 appVersion锛屼絾鏄笉婊¤冻 min_uni_version < appVersion锛屽垯涓嶄細浣跨敤wgt鏇存柊锛屼細鎺ョ潃鍒ゆ柇搴撲腑 app鍖卾ersion 鏄惁澶т簬 appVersion
+ */
+
+ let {
+ appid,
+ appVersion,
+ wgtVersion,
+ } = event;
+
+ const platform_Android = 'Android';
+ const platform_iOS = 'iOS';
+ const package_app = 'native_app';
+ const package_wgt = 'wgt';
+ const app_version_db_name = 'opendb-app-versions'
+
+ let platform = platform_Android;
+
+ // 浜戝嚱鏁癠RL鍖栬姹�
+ if (event.headers) {
+ let body;
+ try {
+ if (event.httpMethod.toLocaleLowerCase() === 'get') {
+ body = event.queryStringParameters;
+ } else {
+ body = JSON.parse(event.body);
+ }
+ } catch (e) {
+ return {
+ code: 500,
+ msg: '璇锋眰閿欒'
+ };
+ }
+
+ appid = body.appid;
+ appVersion = body.appVersion;
+ wgtVersion = body.wgtVersion;
+
+ platform = /iPhone|iPad/.test(event.headers) ? platform_iOS : platform_Android;
+ } else if (context.OS) {
+ platform = context.OS === 'android'
+ ? platform_Android
+ : context.OS === 'ios'
+ ? platform_iOS
+ : platform_Android;
+ }
+
+ if (appid && appVersion && wgtVersion && platform) {
+ const collection = uniCloud.database().collection(app_version_db_name);
+
+ const record = await collection.where({
+ appid,
+ platform,
+ stable_publish: true
+ })
+ .orderBy('create_date', 'desc')
+ .get();
+
+ if (record && record.data && record.data.length > 0) {
+ const appVersionInDb = record.data.find(item => item.type === package_app) || {};
+ const wgtVersionInDb = record.data.find(item => item.type === package_wgt) || {};
+ const hasAppPackage = !!Object.keys(appVersionInDb).length;
+ const hasWgtPackage = !!Object.keys(wgtVersionInDb).length;
+
+ // 鍙栦袱涓増鏈腑鐗堟湰鍙锋渶澶х殑鍖咃紝鐗堟湰涓�鏍凤紝浣跨敤wgt鍖�
+ let stablePublishDb = hasAppPackage
+ ? hasWgtPackage
+ ? compare(wgtVersionInDb.version, appVersionInDb.version) >= 0
+ ? wgtVersionInDb
+ : appVersionInDb
+ : appVersionInDb
+ : wgtVersionInDb;
+
+ const {
+ version,
+ min_uni_version
+ } = stablePublishDb;
+
+ // 搴撲腑鐨剉ersion蹇呴』婊¤冻鍚屾椂澶т簬appVersion鍜寃gtVersion鎵嶈锛屽洜涓轰笂娆℃洿鏂板彲鑳芥槸wgt鏇存柊
+ const appUpdate = compare(version, appVersion) === 1; // app鍖呭彲鐢ㄦ洿鏂�
+ const wgtUpdate = compare(version, wgtVersion) === 1; // wgt鍖呭彲鐢ㄦ洿鏂�
+
+ if (Object.keys(stablePublishDb).length && appUpdate && wgtUpdate) {
+ // 鍒ゆ柇鏄惁鍙敤wgt鏇存柊
+ if (min_uni_version && compare(min_uni_version, appVersion) < 1) {
+ return {
+ code: 101,
+ message: 'wgt鏇存柊',
+ ...stablePublishDb
+ };
+ } else if (hasAppPackage && compare(appVersionInDb.version, appVersion) === 1) {
+ return {
+ code: 102,
+ message: '鏁村寘鏇存柊',
+ ...appVersionInDb
+ };
+ }
+ }
+
+ return {
+ code: 0,
+ message: '褰撳墠鐗堟湰宸茬粡鏄渶鏂扮殑锛屼笉闇�瑕佹洿鏂�'
+ };
+ }
+
+ return {
+ code: -101,
+ message: '鏆傛棤鏇存柊鎴栨鏌ppid鏄惁濉啓姝g‘'
+ };
+ }
+
+ return {
+ code: -102,
+ message: '璇锋鏌ヤ紶鍙傛槸鍚﹀~鍐欐纭�'
+ };
+};
+
+/**
+ * 瀵规瘮鐗堟湰鍙凤紝濡傞渶瑕侊紝璇疯嚜琛屼慨鏀瑰垽鏂鍒�
+ * 鏀寔姣斿 ("3.0.0.0.0.1.0.1", "3.0.0.0.0.1") ("3.0.0.1", "3.0") ("3.1.1", "3.1.1.1") 涔嬬被鐨�
+ * @param {Object} v1
+ * @param {Object} v2
+ * v1 > v2 return 1
+ * v1 < v2 return -1
+ * v1 == v2 return 0
+ */
+function compare(v1 = '0', v2 = '0') {
+ v1 = String(v1).split('.')
+ v2 = String(v2).split('.')
+ const minVersionLens = Math.min(v1.length, v2.length);
+
+ let result = 0;
+ for (let i = 0; i < minVersionLens; i++) {
+ const curV1 = Number(v1[i])
+ const curV2 = Number(v2[i])
+
+ if (curV1 > curV2) {
+ result = 1
+ break;
+ } else if(curV1 < curV2) {
+ result = -1
+ break;
+ }
+ }
+
+ if (result === 0 && (v1.length !== v2.length)) {
+ const v1BiggerThenv2 = v1.length > v2.length;
+ const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
+ for (let i = minVersionLens; i < maxLensVersion.length; i++) {
+ const curVersion = Number(maxLensVersion[i])
+ if (curVersion > 0) {
+ v1BiggerThenv2 ? result = 1 : result = -1
+ break;
+ }
+ }
+ }
+
+ return result;
+}
--
Gitblit v1.9.1