upgrade-popup.vue 12 KB

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