upgrade-popup.uvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <template>
  2. <view class="mask flex-center">
  3. <view class="content">
  4. <view class="content-top">
  5. <text class="content-top-text">{{title}}</text>
  6. <image class="content-top-image" mode="widthFix"
  7. src="/uni_modules/uni-upgrade-center-app/static/app/bg_top.png"></image>
  8. </view>
  9. <view class="content-space"></view>
  10. <view class="content-body">
  11. <view class="content-body-title">
  12. <text class="text title">{{subTitle}}</text>
  13. <text class="text version">v{{version}}</text>
  14. </view>
  15. <view class="body">
  16. <scroll-view class="box-des-scroll" scroll-y="true">
  17. <text class="text box-des">
  18. {{contents}}
  19. </text>
  20. </scroll-view>
  21. </view>
  22. <view class="footer flex-center">
  23. <template v-if="isiOS || isHarmony">
  24. <button class="content-button" style="border: none;color: #fff;" type="primary" 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" :percent="downLoadPercent" activeColor="#3DA7FF"
  32. :show-info="true" :stroke-width="10" />
  33. <view
  34. style="width:100%;display: flex;justify-content: space-around;flex-direction: row;">
  35. <text class="text" style="font-size: 14px;">{{downLoadingText}}</text>
  36. <text class="text"
  37. style="font-size: 14px;">({{downloadedSize}}/{{packageFileSize}}M)</text>
  38. </view>
  39. </view>
  40. <button v-else class="content-button" @click="updateApp">
  41. {{downLoadBtnText}}
  42. </button>
  43. </template>
  44. <button v-else-if="downloadSuccess && !installed" class="content-button" :loading="installing"
  45. :disabled="installing" @click="installPackage">
  46. {{installing ? '正在安装……' : '下载完成,立即安装'}}
  47. </button>
  48. <button v-else-if="installed" class="content-button" @click="installPackage">
  49. 安装未完成,点击安装
  50. </button>
  51. </template>
  52. </view>
  53. </view>
  54. <view class="content-bottom">
  55. <image v-if="!is_mandatory" class="close-img" mode="widthFix"
  56. src="/uni_modules/uni-upgrade-center-app/static/app/app_update_close.png" @click="closeUpdate">
  57. </image>
  58. </view>
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. import { openSchema as utsOpenSchema } from '@/uni_modules/uts-openSchema'
  64. import { UniUpgradeCenterResult, StoreListItem } from '../../utils/call-check-version'
  65. import { platform_iOS, platform_Android, platform_Harmony } from '../../utils/utils'
  66. // #ifdef APP-ANDROID
  67. import { createNotificationProgress, cancelNotificationProgress, finishNotificationProgress } from '@/uni_modules/uts-progressNotification'
  68. import { type CreateNotificationProgressOptions, type FinishNotificationProgressOptions } from '@/uni_modules/uts-progressNotification/utssdk/interface.uts'
  69. // #endif
  70. const requiredKey = ['version', 'url', 'type']
  71. let downloadTask : DownloadTask | null = null;
  72. let openSchemePromise : Promise<boolean> | null = null;
  73. const openSchema = (url : string) : Promise<boolean> => new Promise<boolean>((resolve, reject) => {
  74. try {
  75. utsOpenSchema(url)
  76. resolve(true)
  77. } catch (e) {
  78. reject(false)
  79. }
  80. })
  81. export default {
  82. data() {
  83. return {
  84. // 从之前下载安装
  85. installForBeforeFilePath: '',
  86. // 安装
  87. installed: false,
  88. installing: false,
  89. // 下载
  90. downloadSuccess: false,
  91. downloading: false,
  92. downLoadPercent: 0,
  93. downloadedSize: 0,
  94. packageFileSize: 0,
  95. tempFilePath: '', // 要安装的本地包地址
  96. // 默认安装包信息
  97. title: '更新日志',
  98. contents: '',
  99. version: '',
  100. is_mandatory: false,
  101. url: "",
  102. platform: [] as string[],
  103. store_list: null as StoreListItem[] | null,
  104. // 可自定义属性
  105. subTitle: '发现新版本',
  106. downLoadBtnTextiOS: '立即跳转更新',
  107. downLoadBtnText: '立即下载更新',
  108. downLoadingText: '安装包下载中,请稍后'
  109. }
  110. },
  111. computed: {
  112. isiOS() : boolean {
  113. return this.platform.includes(platform_iOS);
  114. },
  115. isHarmony() : boolean {
  116. return this.platform.includes(platform_Harmony);
  117. },
  118. isAndroid() : boolean {
  119. return this.platform.includes(platform_Android);
  120. },
  121. needNotificationProgress() : boolean {
  122. return this.isAndroid && !this.is_mandatory
  123. }
  124. },
  125. onUnload() {
  126. if (this.needNotificationProgress) {
  127. // #ifdef APP-ANDROID
  128. cancelNotificationProgress()
  129. // #endif
  130. }
  131. },
  132. onLoad(onLoadOptions: OnLoadOptions) {
  133. const local_storage_key: string | null = onLoadOptions['local_storage_key']
  134. if (local_storage_key == null) {
  135. console.error('local_storage_key为空,请检查后重试')
  136. this.closePopup()
  137. return;
  138. };
  139. const localPackageInfo = uni.getStorageSync(local_storage_key);
  140. if (localPackageInfo == null) {
  141. console.error('安装包信息为空,请检查后重试')
  142. this.closePopup()
  143. return;
  144. };
  145. this.show(JSON.parse<UniUpgradeCenterResult>(JSON.stringify(localPackageInfo)) as UniUpgradeCenterResult)
  146. },
  147. onBackPress(options : OnBackPressOptions) : boolean | null {
  148. if (this.is_mandatory) return true
  149. if (!this.needNotificationProgress) {
  150. if (downloadTask !== null) {
  151. downloadTask!.abort()
  152. }
  153. }
  154. return false
  155. },
  156. methods: {
  157. jumpToAppStore() {
  158. openSchema(this.url)
  159. },
  160. show(localPackageInfo : UniUpgradeCenterResult | null) {
  161. if (localPackageInfo === null) return;
  162. for (let key in localPackageInfo) {
  163. if (requiredKey.indexOf(key) != -1 && localPackageInfo[key] === null) {
  164. console.error(`参数 ${key} 必填,请检查后重试`)
  165. this.closePopup()
  166. return;
  167. }
  168. }
  169. this.title = localPackageInfo.title
  170. this.url = localPackageInfo.url
  171. this.contents = localPackageInfo.contents
  172. this.is_mandatory = localPackageInfo.is_mandatory
  173. this.platform = localPackageInfo.platform
  174. this.version = localPackageInfo.version
  175. this.store_list = localPackageInfo.store_list
  176. },
  177. askAbortDownload() {
  178. uni.showModal({
  179. title: '是否取消下载?',
  180. cancelText: '否',
  181. confirmText: '是',
  182. success: res => {
  183. if (res.confirm) {
  184. if (downloadTask !== null) downloadTask!.abort()
  185. if (this.needNotificationProgress) {
  186. // #ifdef APP-ANDROID
  187. cancelNotificationProgress();
  188. // #endif
  189. }
  190. this.closePopup()
  191. }
  192. }
  193. });
  194. },
  195. closeUpdate() {
  196. if (this.downloading && !this.needNotificationProgress) {
  197. this.askAbortDownload()
  198. return;
  199. }
  200. this.closePopup()
  201. },
  202. closePopup() {
  203. this.downloadSuccess = false
  204. this.downloading = false
  205. this.downLoadPercent = 0
  206. this.downloadedSize = 0
  207. this.packageFileSize = 0
  208. this.tempFilePath = ''
  209. this.installing = false
  210. this.installed = false
  211. uni.closeDialogPage({
  212. dialogPage: this.$page
  213. })
  214. },
  215. updateApp() {
  216. const checkStoreScheme = this.checkStoreScheme()
  217. if (checkStoreScheme !== null) {
  218. checkStoreScheme
  219. .then(_ => { })
  220. .catch(() => { this.downloadPackage() })
  221. .finally(() => {
  222. openSchemePromise = null
  223. })
  224. } else { this.downloadPackage() }
  225. },
  226. // 跳转应用商店
  227. checkStoreScheme() : Promise<boolean> | null {
  228. if (this.store_list !== null) {
  229. const storeList : StoreListItem[] = this.store_list!.filter((item : StoreListItem) : boolean => item.enable)
  230. if (storeList.length > 0) {
  231. if (openSchemePromise === null) {
  232. openSchemePromise = Promise.reject() as Promise<boolean>
  233. }
  234. storeList
  235. .sort((cur : StoreListItem, next : StoreListItem) : number => next.priority - cur.priority)
  236. .map((item : StoreListItem) : string => item.scheme)
  237. .reduce((promise : Promise<boolean>, cur : string) : Promise<boolean> => {
  238. openSchemePromise = promise.catch<boolean>(() : Promise<boolean> => openSchema(cur))
  239. return openSchemePromise!
  240. }, openSchemePromise!)
  241. return openSchemePromise!
  242. }
  243. }
  244. return null
  245. },
  246. downloadPackage() {
  247. //下载包
  248. downloadTask = uni.downloadFile({
  249. url: this.url,
  250. success: res => {
  251. if (res.statusCode == 200) {
  252. this.tempFilePath = res.tempFilePath
  253. this.downLoadComplete()
  254. } else {
  255. console.log('downloadFile err: ', res);
  256. this.downloadFail()
  257. }
  258. },
  259. fail: err => {
  260. console.log('downloadFile err: ', err);
  261. this.downloadFail()
  262. }
  263. });
  264. if (downloadTask !== null) {
  265. this.downloading = true;
  266. if (this.needNotificationProgress) {
  267. this.closePopup()
  268. }
  269. downloadTask!.onProgressUpdate(res => {
  270. this.downLoadPercent = parseFloat(res.progress.toFixed(0));
  271. this.downloadedSize = parseFloat((res.totalBytesWritten / Math.pow(1024, 2)).toFixed(2));
  272. this.packageFileSize = parseFloat((res.totalBytesExpectedToWrite / Math.pow(1024, 2)).toFixed(2));
  273. if (this.needNotificationProgress) {
  274. // #ifdef APP-ANDROID
  275. createNotificationProgress({
  276. title: "升级中心正在下载安装包……",
  277. content: `${this.downLoadPercent}%`,
  278. progress: this.downLoadPercent,
  279. onClick: () => {
  280. if (!this.downloadSuccess) {
  281. this.askAbortDownload()
  282. }
  283. }
  284. } as CreateNotificationProgressOptions)
  285. // #endif
  286. }
  287. });
  288. }
  289. },
  290. downloadFail() {
  291. const errMsg = '下载失败,请点击重试'
  292. this.downloadSuccess = false;
  293. this.downloading = false;
  294. this.downLoadPercent = 0;
  295. this.downloadedSize = 0;
  296. this.packageFileSize = 0;
  297. this.downLoadBtnText = errMsg
  298. downloadTask = null;
  299. if (this.needNotificationProgress) {
  300. // #ifdef APP-ANDROID
  301. finishNotificationProgress({
  302. title: '升级包下载失败',
  303. content: '请重新检查更新',
  304. onClick() { }
  305. } as FinishNotificationProgressOptions);
  306. // #endif
  307. }
  308. },
  309. downLoadComplete() {
  310. this.downloadSuccess = true;
  311. this.downloading = false;
  312. this.downLoadPercent = 0
  313. this.downloadedSize = 0
  314. this.packageFileSize = 0
  315. downloadTask = null;
  316. if (this.needNotificationProgress) {
  317. // #ifdef APP-ANDROID
  318. finishNotificationProgress({
  319. title: "安装升级包",
  320. content: "下载完成",
  321. onClick() { }
  322. } as FinishNotificationProgressOptions)
  323. this.installPackage();
  324. // #endif
  325. return
  326. }
  327. // 强制更新,直接安装
  328. if (this.is_mandatory) {
  329. this.installPackage();
  330. }
  331. },
  332. installPackage() {
  333. this.installing = true;
  334. // #ifdef APP
  335. uni.installApk({
  336. filePath: this.tempFilePath,
  337. success: _ => {
  338. this.installing = false;
  339. this.installed = true;
  340. },
  341. fail: err => {
  342. console.error('installApk fail', err);
  343. // 安装失败需要重新下载安装包
  344. this.installing = false;
  345. this.installed = false;
  346. uni.showModal({
  347. title: '更新失败,请重新下载',
  348. content: `uni.installApk 错误码 ${err.errCode}`,
  349. showCancel: false
  350. });
  351. }
  352. });
  353. // 安装跳出覆盖安装,此处直接返回上一页
  354. if (!this.is_mandatory) {
  355. uni.navigateBack()
  356. }
  357. // #endif
  358. }
  359. }
  360. }
  361. </script>
  362. <style>
  363. .flex-center {
  364. /* #ifndef APP-NVUE | UNI-APP-X */
  365. display: flex;
  366. /* #endif */
  367. justify-content: center;
  368. align-items: center;
  369. }
  370. .mask {
  371. position: fixed;
  372. left: 0;
  373. top: 0;
  374. right: 0;
  375. bottom: 0;
  376. background-color: rgba(0, 0, 0, .65);
  377. }
  378. .content {
  379. position: relative;
  380. top: 0;
  381. width: 600rpx;
  382. background-color: transparent;
  383. }
  384. .text {
  385. font-family: Source Han Sans CN;
  386. }
  387. .content-top {
  388. width: 100%;
  389. border-bottom-color: #fff;
  390. border-bottom-width: 15px;
  391. border-bottom-style: solid;
  392. }
  393. .content-space {
  394. width: 100%;
  395. height: 120px;
  396. background-color: #fff;
  397. position: absolute;
  398. top: 30%;
  399. z-index: -1;
  400. }
  401. .content-top-image {
  402. width: 100%;
  403. position: relative;
  404. bottom: -10%;
  405. }
  406. .content-top-text {
  407. font-size: 22px;
  408. font-weight: bold;
  409. color: #F8F8FA;
  410. position: absolute;
  411. width: 65%;
  412. top: 50%;
  413. left: 25px;
  414. z-index: 1;
  415. }
  416. .content-body {
  417. box-sizing: border-box;
  418. padding: 0 25px;
  419. width: 100%;
  420. background-color: #fff;
  421. border-bottom-left-radius: 15px;
  422. border-bottom-right-radius: 15px;
  423. }
  424. .content-body-title {
  425. flex-direction: row;
  426. align-items: center;
  427. }
  428. .content-body-title .version {
  429. padding-left: 10px;
  430. color: #fff;
  431. font-size: 10px;
  432. margin-left: 5px;
  433. padding: 2px 4px;
  434. border-radius: 10px;
  435. background: #50aefd;
  436. }
  437. .title {
  438. font-size: 16px;
  439. font-weight: bold;
  440. color: #3DA7FF;
  441. line-height: 38px;
  442. }
  443. .footer {
  444. height: 75px;
  445. display: flex;
  446. align-items: center;
  447. justify-content: space-around;
  448. }
  449. .box-des-scroll {
  450. box-sizing: border-box;
  451. padding: 0 15px;
  452. height: 100px;
  453. }
  454. .box-des {
  455. font-size: 13px;
  456. color: #000000;
  457. line-height: 25px;
  458. }
  459. .progress-box {
  460. width: 100%;
  461. }
  462. .progress {
  463. width: 90%;
  464. height: 20px;
  465. }
  466. .content-bottom {
  467. height: 75px;
  468. }
  469. .close-img {
  470. width: 35px;
  471. height: 35px;
  472. z-index: 1000;
  473. position: relative;
  474. bottom: -30%;
  475. left: 50%;
  476. margin-left: -17px;
  477. }
  478. .content-button {
  479. width: 100%;
  480. height: 40px;
  481. line-height: 40px;
  482. font-size: 15px;
  483. font-weight: 400;
  484. border-radius: 20px;
  485. border: none;
  486. color: #fff;
  487. text-align: center;
  488. background-color: #1785ff;
  489. }
  490. .flex-column {
  491. display: flex;
  492. flex-direction: column;
  493. align-items: center;
  494. }
  495. </style>