upgrade-popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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="../images/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="../images/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. }
  272. } else {
  273. const localFilePathRecord = uni.getStorageSync(localFilePathKey)
  274. uni.setStorageSync(localFilePathKey, {
  275. ...localFilePathRecord,
  276. installed: true
  277. })
  278. }
  279. }, async err => {
  280. // 如果是安装之前的包,安装失败后删除之前的包
  281. if (this.installForBeforeFilePath) {
  282. await this.deleteSavedFile(this.installForBeforeFilePath)
  283. this.installForBeforeFilePath = '';
  284. }
  285. // 安装失败需要重新下载安装包
  286. this.installing = false;
  287. this.installed = false;
  288. uni.showModal({
  289. title: `更新失败${this.isWGT ? '' : ',APK文件不存在'},请重新下载`,
  290. content: err.message,
  291. showCancel: false
  292. });
  293. });
  294. // 非wgt包,安装跳出覆盖安装,此处直接返回上一页
  295. if (!this.isWGT) {
  296. uni.navigateBack()
  297. }
  298. // #endif
  299. },
  300. restart() {
  301. this.installed = false;
  302. // #ifdef APP-PLUS
  303. //更新完重启app
  304. plus.runtime.restart();
  305. // #endif
  306. },
  307. async saveFile(tempFilePath, version) {
  308. const [err, res] = await uni.saveFile({
  309. tempFilePath
  310. })
  311. if (err) {
  312. return;
  313. }
  314. uni.setStorageSync(localFilePathKey, {
  315. version,
  316. savedFilePath: res.savedFilePath
  317. })
  318. },
  319. deleteSavedFile(filePath) {
  320. uni.removeStorageSync(localFilePathKey)
  321. return uni.removeSavedFile({
  322. filePath
  323. })
  324. },
  325. jumpToAppStore() {
  326. plus.runtime.openURL(this.url);
  327. }
  328. }
  329. }
  330. </script>
  331. <style>
  332. page {
  333. background: transparent;
  334. }
  335. .flex-center {
  336. /* #ifndef APP-NVUE */
  337. display: flex;
  338. /* #endif */
  339. justify-content: center;
  340. align-items: center;
  341. }
  342. .mask {
  343. position: fixed;
  344. left: 0;
  345. top: 0;
  346. right: 0;
  347. bottom: 0;
  348. background-color: rgba(0, 0, 0, .65);
  349. }
  350. .botton-radius {
  351. border-bottom-left-radius: 30rpx;
  352. border-bottom-right-radius: 30rpx;
  353. }
  354. .content {
  355. position: relative;
  356. top: 0;
  357. width: 600rpx;
  358. background-color: #fff;
  359. box-sizing: border-box;
  360. padding: 0 50rpx;
  361. font-family: Source Han Sans CN;
  362. }
  363. .text {
  364. /* #ifndef APP-NVUE */
  365. display: block;
  366. /* #endif */
  367. line-height: 200px;
  368. text-align: center;
  369. color: #FFFFFF;
  370. }
  371. .content-top {
  372. position: absolute;
  373. top: -195rpx;
  374. left: 0;
  375. width: 600rpx;
  376. height: 270rpx;
  377. }
  378. .content-top-text {
  379. font-size: 45rpx;
  380. font-weight: bold;
  381. color: #F8F8FA;
  382. position: absolute;
  383. top: 120rpx;
  384. left: 50rpx;
  385. z-index: 1;
  386. }
  387. .content-header {
  388. height: 70rpx;
  389. }
  390. .title {
  391. font-size: 33rpx;
  392. font-weight: bold;
  393. color: #3DA7FF;
  394. line-height: 38px;
  395. }
  396. .footer {
  397. height: 150rpx;
  398. display: flex;
  399. align-items: center;
  400. justify-content: space-around;
  401. }
  402. .box-des-scroll {
  403. box-sizing: border-box;
  404. padding: 0 40rpx;
  405. height: 200rpx;
  406. text-align: left;
  407. }
  408. .box-des {
  409. font-size: 26rpx;
  410. color: #000000;
  411. line-height: 50rpx;
  412. }
  413. .progress-box {
  414. width: 100%;
  415. }
  416. .progress {
  417. width: 90%;
  418. height: 40rpx;
  419. border-radius: 35px;
  420. }
  421. .close-img {
  422. width: 70rpx;
  423. height: 70rpx;
  424. z-index: 1000;
  425. position: absolute;
  426. bottom: -120rpx;
  427. left: calc(50% - 70rpx / 2);
  428. }
  429. .content-button {
  430. text-align: center;
  431. flex: 1;
  432. font-size: 30rpx;
  433. font-weight: 400;
  434. color: #FFFFFF;
  435. border-radius: 40rpx;
  436. margin: 0 18rpx;
  437. height: 80rpx;
  438. line-height: 80rpx;
  439. background: linear-gradient(to right, #1785ff, #3DA7FF);
  440. }
  441. .flex-column {
  442. display: flex;
  443. flex-direction: column;
  444. align-items: center;
  445. }
  446. </style>