upgrade.vue 8.6 KB

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