upgrade-popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <template>
  2. <view class="mask flex-center">
  3. <view class="content botton-radius">
  4. <view class="content-top">
  5. <text class="content-top-text">{{title}}</text>
  6. <image class="content-top" style="top: 0;" width="100%" height="100%" src="../static/bg_top.png">
  7. </image>
  8. </view>
  9. <view class="content-header"></view>
  10. <view class="content-body">
  11. <view class="title">
  12. <text>{{subTitle}}</text>
  13. <!-- <text style="padding-left:20rpx;font-size: 0.5em;color: #666;">v.{{version}}</text> -->
  14. </view>
  15. <view class="body">
  16. <scroll-view class="box-des-scroll" scroll-y="true">
  17. <text class="box-des">
  18. {{contents}}
  19. </text>
  20. </scroll-view>
  21. </view>
  22. <view class="footer flex-center">
  23. <template v-if="isiOS">
  24. <button class="content-button" style="border: none;color: #fff;" plain @click="jumpToAppStore">
  25. {{downLoadBtnTextiOS}}
  26. </button>
  27. </template>
  28. <template v-else>
  29. <template v-if="!downloadSuccess">
  30. <view class="progress-box flex-column" v-if="downloading">
  31. <progress class="progress" border-radius="35" :percent="downLoadPercent"
  32. activeColor="#3DA7FF" show-info stroke-width="10" />
  33. <view style="width:100%;font-size: 28rpx;display: flex;justify-content: space-around;">
  34. <text>{{downLoadingText}}</text>
  35. <text>({{downloadedSize}}/{{packageFileSize}}M)</text>
  36. </view>
  37. </view>
  38. <button v-else class="content-button" style="border: none;color: #fff;" plain
  39. @click="downloadPackage">
  40. {{downLoadBtnText}}
  41. </button>
  42. </template>
  43. <button v-else-if="downloadSuccess && !installed" class="content-button"
  44. style="border: none;color: #fff;" plain :loading="installing" :disabled="installing"
  45. @click="installPackage">
  46. {{installing ? '正在安装……' : '下载完成,立即安装'}}
  47. </button>
  48. <button v-if="installed && isWGT" class="content-button" style="border: none;color: #fff;" plain
  49. @click="restart">
  50. 安装完毕,点击重启
  51. </button>
  52. </template>
  53. </view>
  54. </view>
  55. <image v-if="!is_mandatory" class="close-img" src="../static/app_update_close.png"
  56. @click.stop="closeUpdate"></image>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. const localFilePathKey = '__localFilePath__'
  62. const platform_iOS = 'iOS';
  63. let downloadTask = null;
  64. /**
  65. * 对比版本号,如需要,请自行修改判断规则
  66. * 支持比对 ("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") 之类的
  67. * @param {Object} v1
  68. * @param {Object} v2
  69. * v1 > v2 return 1
  70. * v1 < v2 return -1
  71. * v1 == v2 return 0
  72. */
  73. function compare(v1 = '0', v2 = '0') {
  74. v1 = String(v1).split('.')
  75. v2 = String(v2).split('.')
  76. const minVersionLens = Math.min(v1.length, v2.length);
  77. let result = 0;
  78. for (let i = 0; i < minVersionLens; i++) {
  79. const curV1 = Number(v1[i])
  80. const curV2 = Number(v2[i])
  81. if (curV1 > curV2) {
  82. result = 1
  83. break;
  84. } else if(curV1 < curV2) {
  85. result = -1
  86. break;
  87. }
  88. }
  89. if (result === 0 && (v1.length !== v2.length)) {
  90. const v1BiggerThenv2 = v1.length > v2.length;
  91. const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
  92. for (let i = minVersionLens; i < maxLensVersion.length; i++) {
  93. const curVersion = Number(maxLensVersion[i])
  94. if (curVersion > 0) {
  95. v1BiggerThenv2 ? result = 1 : result = -1
  96. break;
  97. }
  98. }
  99. }
  100. return result;
  101. }
  102. export default {
  103. data() {
  104. return {
  105. // 从之前下载安装
  106. installForBeforeFilePath: '',
  107. // 安装
  108. installed: false,
  109. installing: false,
  110. // 下载
  111. downloadSuccess: false,
  112. downloading: false,
  113. downLoadPercent: 0,
  114. downloadedSize: 0,
  115. packageFileSize: 0,
  116. tempFilePath: '', // 要安装的本地包地址
  117. // 默认安装包信息
  118. title: '更新日志',
  119. contents: '',
  120. is_mandatory: false,
  121. // 可自定义属性
  122. subTitle: '发现新版本',
  123. downLoadBtnTextiOS: '立即跳转更新',
  124. downLoadBtnText: '立即下载更新',
  125. downLoadingText: '安装包下载中,请稍后…'
  126. }
  127. },
  128. onLoad({
  129. local_storage_key
  130. }) {
  131. if (!local_storage_key) {
  132. console.error('local_storage_key为空,请检查后重试')
  133. uni.navigateBack()
  134. return;
  135. };
  136. const localPackageInfo = uni.getStorageSync(local_storage_key);
  137. if (!localPackageInfo) {
  138. console.error('安装包信息为空,请检查后重试')
  139. uni.navigateBack()
  140. return;
  141. };
  142. const requiredKey = ['version', 'url', 'type']
  143. for (let key in localPackageInfo) {
  144. if (requiredKey.indexOf(key) !== -1 && !localPackageInfo[key]) {
  145. console.error(`参数 ${key} 必填,请检查后重试`)
  146. uni.navigateBack()
  147. return;
  148. }
  149. }
  150. Object.assign(this, localPackageInfo)
  151. this.checkLocalStoragePackage()
  152. },
  153. onBackPress() {
  154. // 强制更新不允许返回
  155. if (this.is_mandatory) {
  156. return true
  157. }
  158. downloadTask && downloadTask.abort()
  159. },
  160. computed: {
  161. isWGT() {
  162. return this.type === 'wgt'
  163. },
  164. isiOS() {
  165. return !this.isWGT ? this.platform.includes(platform_iOS) : false;
  166. }
  167. },
  168. methods: {
  169. checkLocalStoragePackage() {
  170. // 如果已经有下载好的包,则直接提示安装
  171. const localFilePathRecord = uni.getStorageSync(localFilePathKey)
  172. if (localFilePathRecord) {
  173. const {
  174. version,
  175. savedFilePath
  176. } = localFilePathRecord
  177. // 比对版本
  178. if (compare(version, this.version) === 0) {
  179. this.downloadSuccess = true;
  180. this.installForBeforeFilePath = savedFilePath;
  181. this.tempFilePath = savedFilePath
  182. } else {
  183. // 如果保存的包版本小,则直接删除
  184. this.deleteSavedFile(savedFilePath)
  185. }
  186. }
  187. },
  188. async closeUpdate() {
  189. if (this.downloading) {
  190. if (this.is_mandatory) {
  191. return uni.showToast({
  192. title: '下载中,请稍后……',
  193. icon: 'none',
  194. duration: 500
  195. })
  196. }
  197. uni.showModal({
  198. title: '是否取消下载?',
  199. cancelText: '否',
  200. confirmText: '是',
  201. success: res => {
  202. if (res.confirm) {
  203. downloadTask && downloadTask.abort()
  204. uni.navigateBack()
  205. }
  206. }
  207. });
  208. return;
  209. }
  210. if (this.downloadSuccess && this.tempFilePath) {
  211. // 包已经下载完毕,稍后安装,将包保存在本地
  212. await this.saveFile(this.tempFilePath, this.version)
  213. uni.navigateBack()
  214. return;
  215. }
  216. uni.navigateBack()
  217. },
  218. downloadPackage() {
  219. this.downloading = true;
  220. //下载包
  221. downloadTask = uni.downloadFile({
  222. url: this.url,
  223. success: res => {
  224. if (res.statusCode == 200) {
  225. this.downloadSuccess = true;
  226. this.tempFilePath = res.tempFilePath
  227. // 强制更新,直接安装
  228. if (this.is_mandatory) {
  229. this.installPackage();
  230. }
  231. }
  232. },
  233. complete: () => {
  234. this.downloading = false;
  235. this.downLoadPercent = 0
  236. this.downloadedSize = 0
  237. this.packageFileSize = 0
  238. downloadTask = null;
  239. }
  240. });
  241. downloadTask.onProgressUpdate(res => {
  242. this.downLoadPercent = res.progress;
  243. this.downloadedSize = (res.totalBytesWritten / Math.pow(1024, 2)).toFixed(2);
  244. this.packageFileSize = (res.totalBytesExpectedToWrite / Math.pow(1024, 2)).toFixed(2);
  245. });
  246. },
  247. installPackage() {
  248. // #ifdef APP-PLUS
  249. // wgt资源包安装
  250. if (this.isWGT) {
  251. this.installing = true;
  252. }
  253. plus.runtime.install(this.tempFilePath, {
  254. force: false
  255. }, async res => {
  256. this.installing = false;
  257. this.installed = true;
  258. // 如果是安装之前的包,安装成功后删除之前的包
  259. if (this.installForBeforeFilePath) {
  260. await this.deleteSavedFile(this.installForBeforeFilePath)
  261. this.installForBeforeFilePath = '';
  262. }
  263. // wgt包,安装后会提示 安装成功,是否重启
  264. if (this.isWGT) {
  265. // 强制更新安装完成重启
  266. if (this.is_mandatory) {
  267. uni.showLoading({
  268. icon: 'none',
  269. title: '安装成功,正在重启……'
  270. })
  271. setTimeout(() => {
  272. uni.hideLoading()
  273. this.restart();
  274. }, 1000)
  275. } else {
  276. uni.showLoading({
  277. icon: 'none',
  278. title: '安装成功,重启应用体验新版',
  279. duration: 1000
  280. })
  281. }
  282. }
  283. }, async err => {
  284. // 如果是安装之前的包,安装失败后删除之前的包
  285. if (this.installForBeforeFilePath) {
  286. await this.deleteSavedFile(this.installForBeforeFilePath)
  287. this.installForBeforeFilePath = '';
  288. }
  289. // 安装失败需要重新下载安装包
  290. this.installed = false;
  291. uni.showModal({
  292. title: '更新失败',
  293. content: err.message,
  294. showCancel: false
  295. });
  296. });
  297. // 非wgt包,安装跳出覆盖安装,此处直接返回上一页
  298. if (!this.isWGT) {
  299. uni.navigateBack()
  300. }
  301. // #endif
  302. },
  303. restart() {
  304. this.installed = false;
  305. // #ifdef APP-PLUS
  306. //更新完重启app
  307. plus.runtime.restart();
  308. // #endif
  309. },
  310. async saveFile(tempFilePath, version) {
  311. const [err, res] = await uni.saveFile({
  312. tempFilePath
  313. })
  314. if (err) {
  315. return;
  316. }
  317. uni.setStorageSync(localFilePathKey, {
  318. version,
  319. savedFilePath: res.savedFilePath
  320. })
  321. },
  322. deleteSavedFile(filePath) {
  323. uni.removeStorageSync(localFilePathKey)
  324. return uni.removeSavedFile({
  325. filePath
  326. })
  327. },
  328. jumpToAppStore() {
  329. plus.runtime.openURL(this.url);
  330. }
  331. }
  332. }
  333. </script>
  334. <style>
  335. page {
  336. background: transparent;
  337. }
  338. .flex-center {
  339. /* #ifndef APP-NVUE */
  340. display: flex;
  341. /* #endif */
  342. justify-content: center;
  343. align-items: center;
  344. }
  345. .mask {
  346. position: fixed;
  347. left: 0;
  348. top: 0;
  349. right: 0;
  350. bottom: 0;
  351. background-color: rgba(0, 0, 0, .65);
  352. }
  353. .botton-radius {
  354. border-bottom-left-radius: 30rpx;
  355. border-bottom-right-radius: 30rpx;
  356. }
  357. .content {
  358. position: relative;
  359. top: 0;
  360. width: 600rpx;
  361. background-color: #fff;
  362. box-sizing: border-box;
  363. padding: 0 50rpx;
  364. font-family: Source Han Sans CN;
  365. }
  366. .text {
  367. /* #ifndef APP-NVUE */
  368. display: block;
  369. /* #endif */
  370. line-height: 200px;
  371. text-align: center;
  372. color: #FFFFFF;
  373. }
  374. .content-top {
  375. position: absolute;
  376. top: -195rpx;
  377. left: 0;
  378. width: 600rpx;
  379. height: 270rpx;
  380. }
  381. .content-top-text {
  382. font-size: 45rpx;
  383. font-weight: bold;
  384. color: #F8F8FA;
  385. position: absolute;
  386. top: 120rpx;
  387. left: 50rpx;
  388. z-index: 1;
  389. }
  390. .content-header {
  391. height: 70rpx;
  392. }
  393. .title {
  394. font-size: 33rpx;
  395. font-weight: bold;
  396. color: #3DA7FF;
  397. line-height: 38px;
  398. }
  399. .footer {
  400. height: 150rpx;
  401. display: flex;
  402. align-items: center;
  403. justify-content: space-around;
  404. }
  405. .box-des-scroll {
  406. box-sizing: border-box;
  407. padding: 0 40rpx;
  408. height: 200rpx;
  409. text-align: left;
  410. }
  411. .box-des {
  412. font-size: 26rpx;
  413. color: #000000;
  414. line-height: 50rpx;
  415. }
  416. .progress-box {
  417. width: 100%;
  418. }
  419. .progress {
  420. width: 90%;
  421. height: 40rpx;
  422. border-radius: 35px;
  423. }
  424. .close-img {
  425. width: 70rpx;
  426. height: 70rpx;
  427. z-index: 1000;
  428. position: absolute;
  429. bottom: -120rpx;
  430. left: calc(50% - 70rpx / 2);
  431. }
  432. .content-button {
  433. text-align: center;
  434. flex: 1;
  435. font-size: 30rpx;
  436. font-weight: 400;
  437. color: #FFFFFF;
  438. height: 80rpx;
  439. line-height: 80rpx;
  440. background: url(../static/button_bg.png) no-repeat center/contain;
  441. }
  442. .flex-column {
  443. display: flex;
  444. flex-direction: column;
  445. align-items: center;
  446. }
  447. </style>