upgrade.js 5.8 KB

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