upgrade.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { getCurrentWgtInfo } from "./hot-update";
  2. /**
  3. * @description 检查app版本是否需要升级
  4. * @param name:最新版本名称
  5. * @param code:最新版本号
  6. * @param content:更新内容
  7. * @param url:下载链接
  8. * @param forceUpdate:是否强制升级
  9. */
  10. export const checkVersion = async ({
  11. name, //最新版本名称
  12. code, //最新版本号
  13. content, //更新内容
  14. url, //下载链接
  15. forceUpdate, //是否强制升级
  16. }) => {
  17. // const selfVersionCode = uni.getAppBaseInfo().appVersion;
  18. const info = await getCurrentWgtInfo();
  19. if (code > info.version) {
  20. let platform = uni.getSystemInfoSync().platform; //手机平台
  21. if (platform === "android") {
  22. uni.$emit("upgrade-app", {
  23. name,
  24. content,
  25. url,
  26. forceUpdate,
  27. });
  28. return true;
  29. } else {
  30. uni.showModal({
  31. title: "发现新版本 V" + name,
  32. content: "请到App store进行升级",
  33. showCancel: false,
  34. });
  35. }
  36. }
  37. return false;
  38. };
  39. //获取当前页面url
  40. const getCurrentPageRoute = () => {
  41. let currentRoute;
  42. let pages = getCurrentPages(); // 获取栈实例
  43. if (pages && pages.length) {
  44. currentRoute = pages[pages.length - 1].route;
  45. }
  46. return currentRoute;
  47. };
  48. /**
  49. * @description H5+下载App
  50. * @param downloadUrl:App下载链接
  51. * @param progressCallBack:下载进度回调
  52. */
  53. export const downloadApp = (downloadUrl, progressCallBack = () => {}) => {
  54. console.log("🚀 ~ downloadApp ~ downloadUrl:", downloadUrl);
  55. // 将模拟进度变量移到事件监听外部,确保状态能累积
  56. let mockProgress = 0;
  57. // 进度更新节流控制变量
  58. let lastProgressTime = 0;
  59. const PROGRESS_INTERVAL = 300; // 限制300ms内最多更新一次进度
  60. return new Promise((resolve, reject) => {
  61. // 创建下载任务
  62. const downloadTask = plus.downloader.createDownload(
  63. downloadUrl,
  64. { method: "GET" },
  65. (task, status) => {
  66. console.log("🚀 ~ returnnewPromise ~ task, status:", task, status);
  67. if (status == 200) {
  68. resolve(task.filename);
  69. } else {
  70. reject("fail");
  71. uni.showToast({
  72. title: "下载失败",
  73. duration: 1500,
  74. icon: "none",
  75. });
  76. }
  77. }
  78. );
  79. downloadTask.addEventListener("statechanged", (task, status) => {
  80. // 获取当前时间戳用于节流控制
  81. const now = Date.now();
  82. switch (task.state) {
  83. case 1: // 开始
  84. mockProgress = 0; // 仅在开始时重置
  85. progressCallBack(0, 0, task.totalSize || 0);
  86. break;
  87. case 2: // 已连接到服务器
  88. break;
  89. case 3: // 已接收到数据
  90. const hasProgress = task.totalSize && task.totalSize > 0;
  91. if (hasProgress) {
  92. mockProgress = 0;
  93. const current = parseInt(
  94. (100 * task.downloadedSize) / task.totalSize
  95. );
  96. progressCallBack(current, task.downloadedSize, task.totalSize);
  97. } else {
  98. // 模拟进度:添加节流控制和平滑增长
  99. if (
  100. mockProgress < 95 &&
  101. now - lastProgressTime > PROGRESS_INTERVAL
  102. ) {
  103. // 根据时间间隔动态调整增长速度,避免停滞感
  104. const baseIncrement = 1;
  105. const maxIncrement =
  106. mockProgress < 30 ? 3 : mockProgress < 60 ? 2 : 1;
  107. const increment =
  108. Math.floor(Math.random() * maxIncrement) + baseIncrement;
  109. mockProgress = Math.min(mockProgress + increment, 95);
  110. lastProgressTime = now; // 更新最后更新时间
  111. progressCallBack(mockProgress, task.downloadedSize, 0);
  112. }
  113. }
  114. break;
  115. case 4: // 下载完成
  116. progressCallBack(100, task.downloadedSize, task.totalSize || 0);
  117. break;
  118. }
  119. });
  120. downloadTask.start();
  121. });
  122. };
  123. /**
  124. * @description H5+安装APP
  125. * @param fileName:app文件名
  126. * @param callBack:安装成功回调
  127. */
  128. export const installApp = (fileName, callBack = () => {}) => {
  129. console.log("🚀 ~ installApp ~ fileName:", fileName);
  130. //注册广播监听app安装情况
  131. onInstallListening(callBack);
  132. //开始安装
  133. plus.runtime.install(
  134. plus.io.convertLocalFileSystemURL(fileName),
  135. {},
  136. () => {
  137. //成功跳转到安装界面
  138. },
  139. function (error) {
  140. console.log("🚀 ~ plus.runtime.install ~ error:", error);
  141. uni.showToast({
  142. title: "安装失败",
  143. duration: 1500,
  144. icon: "none",
  145. });
  146. }
  147. );
  148. };
  149. /**
  150. * @description 注册广播监听APP是否成功
  151. * @param callBack:安装成功回调函数
  152. */
  153. const onInstallListening = (callBack = () => {}) => {
  154. let mainActivity = plus.android.runtimeMainActivity(); //获取activity
  155. //生成广播接收器
  156. let receiver = plus.android.implements(
  157. "io.dcloud.android.content.BroadcastReceiver",
  158. {
  159. onReceive: (context, intent) => {
  160. //接收广播回调
  161. plus.android.importClass(intent);
  162. mainActivity.unregisterReceiver(receiver); //取消监听
  163. callBack();
  164. },
  165. }
  166. );
  167. let IntentFilter = plus.android.importClass("android.content.IntentFilter");
  168. let Intent = plus.android.importClass("android.content.Intent");
  169. let filter = new IntentFilter();
  170. filter.addAction(Intent.ACTION_PACKAGE_ADDED); //监听apk安装
  171. filter.addDataScheme("package");
  172. mainActivity.registerReceiver(receiver, filter); //注册广播
  173. };