index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. module.exports = async (event, context) => {
  3. /**
  4. * 检测升级 使用说明
  5. * 上传包:
  6. * 1. 根据传参,先检测传参是否完整,appid appVersion wgtVersion 必传
  7. * 2. 先从数据库取出所有该平台(从上下文读取平台信息,默认 Andriod)的所有线上发行更新
  8. * 3. 再从所有线上发行更新中取出版本最大的一版。如果可以,尽量先检测wgt的线上发行版更新
  9. * 4. 使用上步取出的版本包的版本号 和传参 appVersion、wgtVersion 来检测是否有更新,必须同时大于这两项,否则返回暂无更新
  10. * 5. 如果库中 wgt包 版本大于传参 appVersion,但是不满足 min_uni_version < appVersion,则不会使用wgt更新,会接着判断库中 app包version 是否大于 appVersion
  11. */
  12. let {
  13. appid,
  14. appVersion,
  15. wgtVersion,
  16. } = event;
  17. const platform_Android = 'Android';
  18. const platform_iOS = 'iOS';
  19. const package_app = 'native_app';
  20. const package_wgt = 'wgt';
  21. const app_version_db_name = 'opendb-app-versions'
  22. let platform = platform_Android;
  23. // 云函数URL化请求
  24. if (event.headers) {
  25. let body;
  26. try {
  27. if (event.httpMethod.toLocaleLowerCase() === 'get') {
  28. body = event.queryStringParameters;
  29. } else {
  30. body = JSON.parse(event.body);
  31. }
  32. } catch (e) {
  33. return {
  34. code: 500,
  35. msg: '请求错误'
  36. };
  37. }
  38. appid = body.appid;
  39. appVersion = body.appVersion;
  40. wgtVersion = body.wgtVersion;
  41. platform = /iPhone|iPad/.test(event.headers) ? platform_iOS : platform_Android;
  42. } else if (context.OS) {
  43. platform = context.OS === 'android' ?
  44. platform_Android :
  45. context.OS === 'ios' ?
  46. platform_iOS :
  47. platform_Android;
  48. }
  49. if (appid && appVersion && wgtVersion && platform) {
  50. const collection = uniCloud.database().collection(app_version_db_name);
  51. const record = await collection.where({
  52. appid,
  53. platform,
  54. stable_publish: true
  55. })
  56. .orderBy('create_date', 'desc')
  57. .get();
  58. if (record && record.data && record.data.length > 0) {
  59. const appVersionInDb = record.data.find(item => item.type === package_app) || {};
  60. const wgtVersionInDb = record.data.find(item => item.type === package_wgt) || {};
  61. const hasAppPackage = !!Object.keys(appVersionInDb).length;
  62. const hasWgtPackage = !!Object.keys(wgtVersionInDb).length;
  63. // 取两个版本中版本号最大的包,版本一样,使用wgt包
  64. let stablePublishDb = hasAppPackage ?
  65. hasWgtPackage ?
  66. compare(wgtVersionInDb.version, appVersionInDb.version) >= 0 ?
  67. wgtVersionInDb :
  68. appVersionInDb :
  69. appVersionInDb :
  70. wgtVersionInDb;
  71. const {
  72. version,
  73. min_uni_version
  74. } = stablePublishDb;
  75. // 库中的version必须满足同时大于appVersion和wgtVersion才行,因为上次更新可能是wgt更新
  76. const appUpdate = compare(version, appVersion) === 1; // app包可用更新
  77. const wgtUpdate = compare(version, wgtVersion) === 1; // wgt包可用更新
  78. if (Object.keys(stablePublishDb).length && appUpdate && wgtUpdate) {
  79. // 判断是否可用wgt更新
  80. if (min_uni_version && compare(min_uni_version, appVersion) < 1) {
  81. return {
  82. code: 101,
  83. message: 'wgt更新',
  84. ...stablePublishDb
  85. };
  86. } else if (hasAppPackage && compare(appVersionInDb.version, appVersion) === 1) {
  87. return {
  88. code: 102,
  89. message: '整包更新',
  90. ...appVersionInDb
  91. };
  92. }
  93. }
  94. return {
  95. code: 0,
  96. message: '当前版本已经是最新的,不需要更新'
  97. };
  98. }
  99. return {
  100. code: -101,
  101. message: '暂无更新或检查appid是否填写正确'
  102. };
  103. }
  104. return {
  105. code: -102,
  106. message: '请检查传参是否填写正确'
  107. };
  108. };
  109. /**
  110. * 对比版本号,如需要,请自行修改判断规则
  111. * 支持比对 ("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") 之类的
  112. * @param {Object} v1
  113. * @param {Object} v2
  114. * v1 > v2 return 1
  115. * v1 < v2 return -1
  116. * v1 == v2 return 0
  117. */
  118. function compare(v1 = '0', v2 = '0') {
  119. v1 = String(v1).split('.')
  120. v2 = String(v2).split('.')
  121. const minVersionLens = Math.min(v1.length, v2.length);
  122. let result = 0;
  123. for (let i = 0; i < minVersionLens; i++) {
  124. const curV1 = Number(v1[i])
  125. const curV2 = Number(v2[i])
  126. if (curV1 > curV2) {
  127. result = 1
  128. break;
  129. } else if (curV1 < curV2) {
  130. result = -1
  131. break;
  132. }
  133. }
  134. if (result === 0 && (v1.length !== v2.length)) {
  135. const v1BiggerThenv2 = v1.length > v2.length;
  136. const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
  137. for (let i = minVersionLens; i < maxLensVersion.length; i++) {
  138. const curVersion = Number(maxLensVersion[i])
  139. if (curVersion > 0) {
  140. v1BiggerThenv2 ? result = 1 : result = -1
  141. break;
  142. }
  143. }
  144. }
  145. return result;
  146. }