upgrade-popup.uvue 15 KB

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