upgrade-popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. installed
  177. } = localFilePathRecord
  178. // 比对版本
  179. if (!installed && compare(version, this.version) === 0) {
  180. this.downloadSuccess = true;
  181. this.installForBeforeFilePath = savedFilePath;
  182. this.tempFilePath = savedFilePath
  183. } else {
  184. // 如果保存的包版本小 或 已安装过,则直接删除
  185. this.deleteSavedFile(savedFilePath)
  186. }
  187. }
  188. },
  189. async closeUpdate() {
  190. if (this.downloading) {
  191. if (this.is_mandatory) {
  192. return uni.showToast({
  193. title: '下载中,请稍后……',
  194. icon: 'none',
  195. duration: 500
  196. })
  197. }
  198. uni.showModal({
  199. title: '是否取消下载?',
  200. cancelText: '否',
  201. confirmText: '是',
  202. success: res => {
  203. if (res.confirm) {
  204. downloadTask && downloadTask.abort()
  205. uni.navigateBack()
  206. }
  207. }
  208. });
  209. return;
  210. }
  211. if (this.downloadSuccess && this.tempFilePath) {
  212. // 包已经下载完毕,稍后安装,将包保存在本地
  213. await this.saveFile(this.tempFilePath, this.version)
  214. uni.navigateBack()
  215. return;
  216. }
  217. uni.navigateBack()
  218. },
  219. downloadPackage() {
  220. this.downloading = true;
  221. //下载包
  222. downloadTask = uni.downloadFile({
  223. url: this.url,
  224. success: res => {
  225. if (res.statusCode == 200) {
  226. this.downloadSuccess = true;
  227. this.tempFilePath = res.tempFilePath
  228. // 强制更新,直接安装
  229. if (this.is_mandatory) {
  230. this.installPackage();
  231. }
  232. }
  233. },
  234. complete: () => {
  235. this.downloading = false;
  236. this.downLoadPercent = 0
  237. this.downloadedSize = 0
  238. this.packageFileSize = 0
  239. downloadTask = null;
  240. }
  241. });
  242. downloadTask.onProgressUpdate(res => {
  243. this.downLoadPercent = res.progress;
  244. this.downloadedSize = (res.totalBytesWritten / Math.pow(1024, 2)).toFixed(2);
  245. this.packageFileSize = (res.totalBytesExpectedToWrite / Math.pow(1024, 2)).toFixed(2);
  246. });
  247. },
  248. installPackage() {
  249. // #ifdef APP-PLUS
  250. // wgt资源包安装
  251. if (this.isWGT) {
  252. this.installing = true;
  253. }
  254. plus.runtime.install(this.tempFilePath, {
  255. force: false
  256. }, async res => {
  257. this.installing = false;
  258. this.installed = true;
  259. // wgt包,安装后会提示 安装成功,是否重启
  260. if (this.isWGT) {
  261. // 强制更新安装完成重启
  262. if (this.is_mandatory) {
  263. uni.showLoading({
  264. icon: 'none',
  265. title: '安装成功,正在重启……'
  266. })
  267. setTimeout(() => {
  268. uni.hideLoading()
  269. this.restart();
  270. }, 1000)
  271. } else {
  272. uni.showLoading({
  273. icon: 'none',
  274. title: '安装成功,重启应用体验新版',
  275. duration: 1000
  276. })
  277. }
  278. } else {
  279. const localFilePathRecord = uni.getStorageSync(localFilePathKey)
  280. uni.setStorageSync(localFilePathKey, {
  281. ...localFilePathRecord,
  282. installed: true
  283. })
  284. }
  285. }, async err => {
  286. // 如果是安装之前的包,安装失败后删除之前的包
  287. if (this.installForBeforeFilePath) {
  288. await this.deleteSavedFile(this.installForBeforeFilePath)
  289. this.installForBeforeFilePath = '';
  290. }
  291. // 安装失败需要重新下载安装包
  292. this.installed = false;
  293. uni.showModal({
  294. title: `更新失败${this.isWGT ? '' : ',APK文件不存在'},请重新下载`,
  295. content: err.message,
  296. showCancel: false
  297. });
  298. });
  299. // 非wgt包,安装跳出覆盖安装,此处直接返回上一页
  300. if (!this.isWGT) {
  301. uni.navigateBack()
  302. }
  303. // #endif
  304. },
  305. restart() {
  306. this.installed = false;
  307. // #ifdef APP-PLUS
  308. //更新完重启app
  309. plus.runtime.restart();
  310. // #endif
  311. },
  312. async saveFile(tempFilePath, version) {
  313. const [err, res] = await uni.saveFile({
  314. tempFilePath
  315. })
  316. if (err) {
  317. return;
  318. }
  319. uni.setStorageSync(localFilePathKey, {
  320. version,
  321. savedFilePath: res.savedFilePath
  322. })
  323. },
  324. deleteSavedFile(filePath) {
  325. uni.removeStorageSync(localFilePathKey)
  326. return uni.removeSavedFile({
  327. filePath
  328. })
  329. },
  330. jumpToAppStore() {
  331. plus.runtime.openURL(this.url);
  332. }
  333. }
  334. }
  335. </script>
  336. <style>
  337. page {
  338. background: transparent;
  339. }
  340. .flex-center {
  341. /* #ifndef APP-NVUE */
  342. display: flex;
  343. /* #endif */
  344. justify-content: center;
  345. align-items: center;
  346. }
  347. .mask {
  348. position: fixed;
  349. left: 0;
  350. top: 0;
  351. right: 0;
  352. bottom: 0;
  353. background-color: rgba(0, 0, 0, .65);
  354. }
  355. .botton-radius {
  356. border-bottom-left-radius: 30rpx;
  357. border-bottom-right-radius: 30rpx;
  358. }
  359. .content {
  360. position: relative;
  361. top: 0;
  362. width: 600rpx;
  363. background-color: #fff;
  364. box-sizing: border-box;
  365. padding: 0 50rpx;
  366. font-family: Source Han Sans CN;
  367. }
  368. .text {
  369. /* #ifndef APP-NVUE */
  370. display: block;
  371. /* #endif */
  372. line-height: 200px;
  373. text-align: center;
  374. color: #FFFFFF;
  375. }
  376. .content-top {
  377. position: absolute;
  378. top: -195rpx;
  379. left: 0;
  380. width: 600rpx;
  381. height: 270rpx;
  382. }
  383. .content-top-text {
  384. font-size: 45rpx;
  385. font-weight: bold;
  386. color: #F8F8FA;
  387. position: absolute;
  388. top: 120rpx;
  389. left: 50rpx;
  390. z-index: 1;
  391. }
  392. .content-header {
  393. height: 70rpx;
  394. }
  395. .title {
  396. font-size: 33rpx;
  397. font-weight: bold;
  398. color: #3DA7FF;
  399. line-height: 38px;
  400. }
  401. .footer {
  402. height: 150rpx;
  403. display: flex;
  404. align-items: center;
  405. justify-content: space-around;
  406. }
  407. .box-des-scroll {
  408. box-sizing: border-box;
  409. padding: 0 40rpx;
  410. height: 200rpx;
  411. text-align: left;
  412. }
  413. .box-des {
  414. font-size: 26rpx;
  415. color: #000000;
  416. line-height: 50rpx;
  417. }
  418. .progress-box {
  419. width: 100%;
  420. }
  421. .progress {
  422. width: 90%;
  423. height: 40rpx;
  424. border-radius: 35px;
  425. }
  426. .close-img {
  427. width: 70rpx;
  428. height: 70rpx;
  429. z-index: 1000;
  430. position: absolute;
  431. bottom: -120rpx;
  432. left: calc(50% - 70rpx / 2);
  433. }
  434. .content-button {
  435. text-align: center;
  436. flex: 1;
  437. font-size: 30rpx;
  438. font-weight: 400;
  439. color: #FFFFFF;
  440. border-radius: 40rpx;
  441. margin: 0 18rpx;
  442. height: 80rpx;
  443. line-height: 80rpx;
  444. background: linear-gradient(to right, #1785ff, #3DA7FF);
  445. }
  446. .flex-column {
  447. display: flex;
  448. flex-direction: column;
  449. align-items: center;
  450. }
  451. </style>