feat(Dashboard): 添加小程序版本更新检查功能
在 Dashboard 页面添加微信小程序版本更新检查逻辑,使用 Taro 的 getUpdateManager API 实现自动检测、下载和提示用户更新
Showing
1 changed file
with
43 additions
and
2 deletions
| ... | @@ -194,8 +194,8 @@ | ... | @@ -194,8 +194,8 @@ |
| 194 | 194 | ||
| 195 | <script setup> | 195 | <script setup> |
| 196 | import { ref, computed, onMounted } from 'vue'; | 196 | import { ref, computed, onMounted } from 'vue'; |
| 197 | -import Taro, { useDidShow } from '@tarojs/taro'; | 197 | +import Taro, { useDidShow, useReady } from '@tarojs/taro'; |
| 198 | -import { Setting, Photograph, Right, Close, Category } from '@nutui/icons-vue-taro'; | 198 | +import { Setting, Photograph, Close, Category } from '@nutui/icons-vue-taro'; |
| 199 | import BottomNav from '../../components/BottomNav.vue'; | 199 | import BottomNav from '../../components/BottomNav.vue'; |
| 200 | import TotalPointsDisplay from '@/components/TotalPointsDisplay.vue'; | 200 | import TotalPointsDisplay from '@/components/TotalPointsDisplay.vue'; |
| 201 | import PointsCollector from '@/components/PointsCollector.vue' | 201 | import PointsCollector from '@/components/PointsCollector.vue' |
| ... | @@ -395,6 +395,47 @@ useDidShow(() => { | ... | @@ -395,6 +395,47 @@ useDidShow(() => { |
| 395 | // 总积分数量也要从接口获取传进去 | 395 | // 总积分数量也要从接口获取传进去 |
| 396 | finalTotalPoints.value = 10086; | 396 | finalTotalPoints.value = 10086; |
| 397 | }) | 397 | }) |
| 398 | + | ||
| 399 | +useReady(async () => { | ||
| 400 | + // 版本更新检查 | ||
| 401 | + if (!Taro.canIUse("getUpdateManager")) { | ||
| 402 | + Taro.showModal({ | ||
| 403 | + title: "提示", | ||
| 404 | + content: "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试", | ||
| 405 | + showCancel: false, | ||
| 406 | + }); | ||
| 407 | + return; | ||
| 408 | + } | ||
| 409 | + | ||
| 410 | + // https://developers.weixin.qq.com/miniprogram/dev/api/base/update/UpdateManager.html | ||
| 411 | + const updateManager = Taro.getUpdateManager(); | ||
| 412 | + | ||
| 413 | + updateManager.onCheckForUpdate((res) => { | ||
| 414 | + // 请求完新版本信息的回调 | ||
| 415 | + if (res.hasUpdate) { | ||
| 416 | + updateManager.onUpdateReady(function () { | ||
| 417 | + Taro.showModal({ | ||
| 418 | + title: "更新提示", | ||
| 419 | + content: "新版本已经准备好,是否重启应用?", | ||
| 420 | + success: function (res) { | ||
| 421 | + if (res.confirm) { | ||
| 422 | + // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 | ||
| 423 | + updateManager.applyUpdate(); | ||
| 424 | + } | ||
| 425 | + }, | ||
| 426 | + }); | ||
| 427 | + }); | ||
| 428 | + | ||
| 429 | + updateManager.onUpdateFailed(function () { | ||
| 430 | + // 新版本下载失败 | ||
| 431 | + Taro.showModal({ | ||
| 432 | + title: "更新提示", | ||
| 433 | + content: "新版本已上线,请删除当前小程序,重新搜索打开", | ||
| 434 | + }); | ||
| 435 | + }); | ||
| 436 | + } | ||
| 437 | + }); | ||
| 438 | +}) | ||
| 398 | </script> | 439 | </script> |
| 399 | 440 | ||
| 400 | <style lang="less"> | 441 | <style lang="less"> | ... | ... |
-
Please register or login to post a comment