upgrade.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <template>
  2. <uni-popup
  3. class="upgrade-unipopup"
  4. ref="popup"
  5. type="center"
  6. :animation="false"
  7. :mask-click="false"
  8. style="z-index: 999">
  9. <view class="upgrade-popup">
  10. <image
  11. class="header-bg"
  12. src="../static/common/upgrade_bg.png"
  13. mode="widthFix"></image>
  14. <view class="main">
  15. <view class="version"
  16. >{{ t("version.newVersion") }}{{ versionName }}</view
  17. >
  18. <view class="content" v-if="versionDesc">
  19. <text class="title">{{ t("version.updateInfo") }}</text>
  20. <view class="desc" v-html="versionDesc"></view>
  21. </view>
  22. <!--下载状态-进度条显示 -->
  23. <view class="footer" v-if="isStartDownload">
  24. <view class="progress-view" @click="handleInstallApp">
  25. <view style="height: 100%">
  26. <view class="txt">{{ percentText }}</view>
  27. <view class="progress" :style="setProStyle"></view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 强制更新 -->
  32. <view class="footer" v-else-if="isForceUpdate">
  33. <view class="btn upgrade force" @click="handleUpgrade">{{
  34. t("version.updateNow")
  35. }}</view>
  36. </view>
  37. <!-- 可选择更新 -->
  38. <view class="footer" v-else>
  39. <view class="btn close" @click="handleClose">{{
  40. t("version.updateLater")
  41. }}</view>
  42. <view class="btn upgrade" @click="handleUpgrade">{{
  43. t("version.updateNow")
  44. }}</view>
  45. </view>
  46. </view>
  47. </view>
  48. </uni-popup>
  49. </template>
  50. <script>
  51. import { isHotUpdateRunning } from "@/utils/hot-update.js";
  52. import { getAppVersion } from "@/api/app.js";
  53. import { checkVersion, downloadApp, installApp } from "@/utils/upgrade.js";
  54. // 引用全局变量$t
  55. import { getCurrentInstance } from "vue";
  56. export default {
  57. data() {
  58. return {
  59. isForceUpdate: true, //是否强制更新
  60. versionName: "", //版本名称
  61. versionDesc: "", //更新说明
  62. downloadUrl: "", //APP下载链接
  63. isDownloadFinish: false, //是否下载完成
  64. hasProgress: false, //是否能显示进度条
  65. currentPercent: 0, //当前下载百分比
  66. isStartDownload: false, //是否开始下载
  67. fileName: "", //下载后app本地路径名称
  68. t: null, // 全局翻译函数
  69. };
  70. },
  71. computed: {
  72. //设置进度条样式,实时更新进度位置
  73. setProStyle() {
  74. return {
  75. width: (510 * this.currentPercent) / 100 + "rpx", //510:按钮进度条宽度
  76. };
  77. },
  78. //百分比文字
  79. percentText() {
  80. let percent = this.currentPercent;
  81. if (typeof percent !== "number" || isNaN(percent))
  82. return this.t("version.downloading"); // 下载中
  83. if (percent < 100) return `${this.t("version.download")}${percent}%`;
  84. return this.t("version.install"); // 下载完成
  85. },
  86. },
  87. onBackPress(options) {
  88. // 禁用返回
  89. if (options.from == "backbutton") {
  90. return true;
  91. }
  92. },
  93. mounted() {
  94. //检测版本
  95. // #ifdef APP
  96. /**
  97. * 这里保留原来的页面级整包更新检查,但增加一个非常轻量的互斥判断。
  98. *
  99. * 为什么要加这层判断:
  100. * 1. 当前项目原本就在登录页和首页自动挂载这个组件。
  101. * 2. 本次新增的 .wgt 热更新改到了 App 启动阶段,如果这里还无条件发起旧检查,
  102. * 启动时就可能同时出现“热更新弹窗”和“整包升级弹窗”。
  103. * 3. 这不是为了废掉原有逻辑,而是为了让原有逻辑在“启动热更新正在执行”时先让路,
  104. * 避免两个升级流程互相打架。
  105. *
  106. * 不加这段代码会怎样:
  107. * - App.vue onLaunch 触发热更新检查
  108. * - 页面 mounted 又触发旧的整包升级检查
  109. * - 用户可能连续看到两个升级提示,甚至两个流程同时下载,体验会非常差
  110. */
  111. if (!isHotUpdateRunning()) {
  112. this.appCheckVersion();
  113. } else {
  114. console.log(
  115. "skip page upgrade check because startup hot update is running"
  116. );
  117. }
  118. // #endif
  119. const { appContext } = getCurrentInstance();
  120. const t = appContext.config.globalProperties.$t;
  121. this.t = t;
  122. },
  123. created() {
  124. uni.$on("upgrade-app", this.bindEmit);
  125. },
  126. beforeDestroy() {
  127. uni.$off("upgrade-app", this.bindEmit);
  128. },
  129. methods: {
  130. async appCheckVersion() {
  131. const { data: remote } = await getAppVersion();
  132. const up = await checkVersion({
  133. name: remote.appVersion, //最新版本名称
  134. code: remote.appVersion, //最新版本号
  135. content: "", //更新内容
  136. url: remote.url, //下载链接
  137. forceUpdate: true, //是否强制升级
  138. });
  139. if (up) {
  140. this.open();
  141. }
  142. },
  143. open() {
  144. //打开升级弹窗
  145. console.log("open upgrade popup");
  146. this.$refs.popup.open();
  147. },
  148. bindEmit(e) {
  149. let { name, content, url, forceUpdate } = e;
  150. this.isForceUpdate = forceUpdate;
  151. this.versionName = name;
  152. this.versionDesc = content;
  153. this.downloadUrl = url;
  154. },
  155. //更新
  156. handleUpgrade() {
  157. if (this.downloadUrl) {
  158. this.isStartDownload = true;
  159. this.currentPercent = 0; // 初始化进度
  160. downloadApp(this.downloadUrl, (current, downloadedSize, totalSize) => {
  161. // 始终显示进度条,无需hasProgress判断
  162. this.currentPercent = current;
  163. })
  164. .then((fileName) => {
  165. this.isDownloadFinish = true;
  166. this.fileName = fileName;
  167. this.currentPercent = 100; // 确保最终显示100%
  168. if (fileName) {
  169. // 下载完成后,自动安装
  170. console.log("🚀 ~ handleUpgrade ~ fileName:", fileName);
  171. this.handleInstallApp();
  172. }
  173. })
  174. .catch((e) => {
  175. console.error("🚀 ~ handleUpgrade ~ e:", e);
  176. this.currentPercent = 0; // 失败时重置
  177. });
  178. } else {
  179. uni.showToast({
  180. title: this.t("version.downloadLinkNotExist"),
  181. icon: "none",
  182. });
  183. }
  184. },
  185. //安装app
  186. handleInstallApp() {
  187. //下载完成才能安装,防止下载过程中点击
  188. if (this.isDownloadFinish && this.fileName) {
  189. installApp(this.fileName, () => {
  190. this.$refs.popup.close();
  191. //安装成功,关闭升级弹窗
  192. // uni.navigateBack()
  193. });
  194. }
  195. },
  196. //关闭返回
  197. handleClose() {
  198. // uni.navigateBack()
  199. this.$refs.popup.close();
  200. },
  201. },
  202. };
  203. </script>
  204. <style>
  205. page {
  206. background: rgba(0, 0, 0, 0.5);
  207. /**设置窗口背景半透明*/
  208. }
  209. </style>
  210. <style lang="scss" scoped>
  211. :deep(.upgrade-unipopup) {
  212. z-index: 9999 !important;
  213. }
  214. .upgrade-popup {
  215. width: 580rpx;
  216. height: auto;
  217. position: fixed;
  218. top: 50%;
  219. left: 50%;
  220. transform: translate(-50%, -50%);
  221. background: #fff;
  222. border-radius: 20rpx;
  223. box-sizing: border-box;
  224. border: 1px solid #eee;
  225. }
  226. .header-bg {
  227. width: 100%;
  228. margin-top: -112rpx;
  229. }
  230. .main {
  231. padding: 10rpx 30rpx 30rpx;
  232. box-sizing: border-box;
  233. .version {
  234. font-size: 36rpx;
  235. color: #026df7;
  236. font-weight: 700;
  237. width: 100%;
  238. text-align: center;
  239. overflow: hidden;
  240. text-overflow: ellipsis;
  241. white-space: nowrap;
  242. letter-spacing: 1px;
  243. }
  244. .content {
  245. margin-top: 60rpx;
  246. .title {
  247. font-size: 28rpx;
  248. font-weight: 700;
  249. color: #000000;
  250. }
  251. .desc {
  252. box-sizing: border-box;
  253. margin-top: 20rpx;
  254. font-size: 28rpx;
  255. color: #6a6a6a;
  256. max-height: 40vh;
  257. overflow-y: auto;
  258. }
  259. }
  260. .footer {
  261. width: 100%;
  262. display: flex;
  263. justify-content: center;
  264. align-items: center;
  265. position: relative;
  266. flex-shrink: 0;
  267. margin-top: 100rpx;
  268. .btn {
  269. width: 246rpx;
  270. display: flex;
  271. justify-content: center;
  272. align-items: center;
  273. position: relative;
  274. z-index: 999;
  275. height: 96rpx;
  276. box-sizing: border-box;
  277. font-size: 32rpx;
  278. border-radius: 10rpx;
  279. letter-spacing: 2rpx;
  280. &.force {
  281. width: 500rpx;
  282. }
  283. &.close {
  284. border: 1px solid #e0e0e0;
  285. margin-right: 25rpx;
  286. color: #000;
  287. }
  288. &.upgrade {
  289. background-color: #026df7;
  290. color: white;
  291. }
  292. }
  293. .progress-view {
  294. width: 510rpx;
  295. height: 90rpx;
  296. display: flex;
  297. position: relative;
  298. align-items: center;
  299. border-radius: 6rpx;
  300. background-color: #dcdcdc;
  301. display: flex;
  302. justify-content: flex-start;
  303. padding: 0px;
  304. box-sizing: border-box;
  305. border: none;
  306. overflow: hidden;
  307. &.active {
  308. background-color: #026df7;
  309. }
  310. .progress {
  311. height: 100%;
  312. background-color: #026df7;
  313. padding: 0px;
  314. box-sizing: border-box;
  315. border: none;
  316. border-top-left-radius: 10rpx;
  317. border-bottom-left-radius: 10rpx;
  318. transition: width 0.3s ease; // 添加平滑过渡动画
  319. }
  320. .txt {
  321. font-size: 28rpx;
  322. position: absolute;
  323. top: 50%;
  324. left: 50%;
  325. transform: translate(-50%, -50%);
  326. color: #fff;
  327. }
  328. }
  329. }
  330. }
  331. </style>