version.nvue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <view class="page">
  3. <!-- 状态栏占位 -->
  4. <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
  5. <!-- 自定义导航栏 -->
  6. <view class="nav-bar">
  7. <image src="/static/public/backBlack.png" mode="aspectFill" class="back-img" @click="goBack"></image>
  8. <text class="nav-title">版本信息</text>
  9. <view class="nav-right"></view>
  10. </view>
  11. <!-- 版本信息区 -->
  12. <view class="version-content">
  13. <image class="logo" src="/static/144x144.png" mode="aspectFill"></image>
  14. <text class="app-name">研年健康平台</text>
  15. <text class="version-code">V{{localVersion}}</text>
  16. <text class="check-btn" @click="checkUpdate">检查更新</text>
  17. </view>
  18. <!-- 更新弹窗 -->
  19. <view class="update-modal" v-if="showUpdateDialog">
  20. <view class="update-mask" @click="closeUpdateDialog"></view>
  21. <view class="update-dialog">
  22. <text class="update-title">发现新版本</text>
  23. <text class="update-version">V{{updateInfo.versionCode}}</text>
  24. <text class="update-size" v-if="updateInfo.fileSize">{{updateInfo.fileSize}}</text>
  25. <view class="update-content-box">
  26. <text class="update-content">{{updateInfo.updateContent || '更新了一些功能和体验优化'}}</text>
  27. </view>
  28. <!-- 下载进度条 -->
  29. <view class="progress-box" v-if="downloading">
  30. <view class="progress-bar">
  31. <view class="progress-fill" :style="{width: downloadProgress + '%'}"></view>
  32. </view>
  33. <text class="progress-text">{{downloadProgress}}%</text>
  34. </view>
  35. <text class="update-btn" @click="doUpdate" v-if="!downloading">立即更新</text>
  36. <text class="update-btn update-btn-disabled" v-if="downloading">下载中...</text>
  37. <text class="update-later" v-if="updateInfo.updateType !== 1 && !downloading" @click="closeUpdateDialog">稍后再说</text>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import { checkUpdate } from '@/utils/api/version.js'
  44. export default {
  45. data() {
  46. return {
  47. statusBarHeight: 44,
  48. localVersion: '1.0.0',
  49. platform: 'android',
  50. showUpdateDialog: false,
  51. updateInfo: {},
  52. downloading: false,
  53. downloadProgress: 0
  54. }
  55. },
  56. onLoad() {
  57. const sysInfo = uni.getSystemInfoSync()
  58. this.statusBarHeight = sysInfo.statusBarHeight || 44
  59. this.platform = sysInfo.platform || 'android'
  60. // 获取本地版本号
  61. const appBase = uni.getAppBaseInfo()
  62. if (appBase && appBase.appVersion) {
  63. this.localVersion = appBase.appVersion
  64. }
  65. },
  66. methods: {
  67. goBack() {
  68. uni.navigateBack()
  69. },
  70. async checkUpdate() {
  71. const platformCode = this.platform === 'ios' ? 2 : 1
  72. try {
  73. const res = await checkUpdate(platformCode, this.localVersion)
  74. console.log(res)
  75. if (res && res.hasUpdate) {
  76. this.updateInfo = res
  77. this.showUpdateDialog = true
  78. this.downloading = false
  79. this.downloadProgress = 0
  80. } else {
  81. uni.showToast({
  82. title: '已是最新版本',
  83. icon: 'none'
  84. })
  85. }
  86. } catch (e) {
  87. uni.showToast({
  88. title: '检查更新失败',
  89. icon: 'none'
  90. })
  91. }
  92. },
  93. closeUpdateDialog() {
  94. // 强制更新时不允许关闭
  95. if (this.updateInfo.updateType === 1) {
  96. return
  97. }
  98. this.showUpdateDialog = false
  99. },
  100. doUpdate() {
  101. if (this.platform === 'ios') {
  102. this.openAppStore()
  103. } else {
  104. this.downloadAndInstall()
  105. }
  106. },
  107. // iOS: 跳转App Store
  108. openAppStore() {
  109. const url = this.updateInfo.downloadUrl
  110. if (!url) {
  111. uni.showToast({ title: '下载地址无效', icon: 'none' })
  112. return
  113. }
  114. // #ifdef APP-PLUS
  115. plus.runtime.openURL(url, (err) => {
  116. uni.showToast({ title: '打开App Store失败', icon: 'none' })
  117. })
  118. // #endif
  119. },
  120. // Android: 下载APK并安装
  121. downloadAndInstall() {
  122. console.log("下载地址",this.updateInfo.downloadUrl)
  123. const url = this.updateInfo.downloadUrl
  124. if (!url) {
  125. uni.showToast({ title: '下载地址无效', icon: 'none' })
  126. return
  127. }
  128. this.downloading = true
  129. this.downloadProgress = 0
  130. // #ifdef APP-PLUS
  131. const dtask = plus.downloader.createDownload(url, {
  132. filename: '_downloads/update.apk'
  133. }, (d, status) => {
  134. if (status === 200) {
  135. // 下载完成,安装APK
  136. plus.runtime.install(d.filename, {
  137. force: true
  138. }, () => {
  139. // 安装成功
  140. plus.runtime.restart()
  141. }, (err) => {
  142. this.downloading = false
  143. uni.showToast({ title: '安装失败,请重试', icon: 'none' })
  144. })
  145. } else {
  146. this.downloading = false
  147. uni.showToast({ title: '下载失败,请重试', icon: 'none' })
  148. }
  149. })
  150. dtask.addEventListener('statechanged', (task, status) => {
  151. // state=3 表示正在接收数据
  152. if (task.state === 3 && task.totalSize > 0) {
  153. this.downloadProgress = Math.round((task.downloadedSize / task.totalSize) * 100)
  154. }
  155. }, false)
  156. dtask.start()
  157. // #endif
  158. }
  159. }
  160. }
  161. </script>
  162. <style>
  163. .page {
  164. flex: 1;
  165. background-color: #FFFFFF;
  166. }
  167. .status-bar {
  168. background-color: #FFFFFF;
  169. }
  170. .nav-bar {
  171. flex-direction: row;
  172. align-items: center;
  173. justify-content: space-between;
  174. height: 88rpx;
  175. padding-left: 54rpx;
  176. padding-right: 54rpx;
  177. background-color: #FFFFFF;
  178. }
  179. .back-img {
  180. width: 50rpx;
  181. height: 50rpx;
  182. }
  183. .nav-title {
  184. font-weight: bold;
  185. font-size: 32rpx;
  186. color: #545F71;
  187. }
  188. .nav-right {
  189. width: 50rpx;
  190. height: 50rpx;
  191. }
  192. /* 版本信息区 */
  193. .version-content {
  194. align-items: center;
  195. padding-top: 100rpx;
  196. }
  197. .logo {
  198. width: 208rpx;
  199. height: 208rpx;
  200. border-radius: 120rpx;
  201. margin-bottom: 16rpx;
  202. }
  203. .app-name {
  204. font-size: 32rpx;
  205. color: #545F71;
  206. line-height: 60rpx;
  207. text-align: center;
  208. }
  209. .version-code {
  210. font-size: 32rpx;
  211. color: #BAC0CA;
  212. line-height: 60rpx;
  213. text-align: center;
  214. }
  215. .check-btn {
  216. margin-top: 78rpx;
  217. width: 644rpx;
  218. height: 88rpx;
  219. background-color: #389588;
  220. border-radius: 20rpx;
  221. color: #FFFFFF;
  222. font-size: 32rpx;
  223. line-height: 88rpx;
  224. text-align: center;
  225. }
  226. /* 更新弹窗 */
  227. .update-modal {
  228. position: fixed;
  229. top: 0;
  230. left: 0;
  231. right: 0;
  232. bottom: 0;
  233. justify-content: center;
  234. align-items: center;
  235. }
  236. .update-mask {
  237. position: absolute;
  238. top: 0;
  239. left: 0;
  240. right: 0;
  241. bottom: 0;
  242. background-color: rgba(0, 0, 0, 0.5);
  243. }
  244. .update-dialog {
  245. width: 600rpx;
  246. background-color: #FFFFFF;
  247. border-radius: 24rpx;
  248. padding: 48rpx 40rpx;
  249. align-items: center;
  250. }
  251. .update-title {
  252. font-size: 36rpx;
  253. font-weight: bold;
  254. color: #333333;
  255. margin-bottom: 12rpx;
  256. }
  257. .update-version {
  258. font-size: 28rpx;
  259. color: #999999;
  260. margin-bottom: 8rpx;
  261. }
  262. .update-size {
  263. font-size: 24rpx;
  264. color: #BBBBBB;
  265. margin-bottom: 24rpx;
  266. }
  267. .update-content-box {
  268. width: 520rpx;
  269. max-height: 300rpx;
  270. margin-bottom: 36rpx;
  271. }
  272. .update-content {
  273. font-size: 28rpx;
  274. color: #666666;
  275. line-height: 44rpx;
  276. lines: 0;
  277. }
  278. .progress-box {
  279. width: 520rpx;
  280. align-items: center;
  281. margin-bottom: 24rpx;
  282. }
  283. .progress-bar {
  284. width: 520rpx;
  285. height: 12rpx;
  286. background-color: #EEEEEE;
  287. border-radius: 6rpx;
  288. overflow: hidden;
  289. }
  290. .progress-fill {
  291. height: 12rpx;
  292. background-color: #389588;
  293. border-radius: 6rpx;
  294. }
  295. .progress-text {
  296. font-size: 24rpx;
  297. color: #389588;
  298. margin-top: 8rpx;
  299. }
  300. .update-btn {
  301. width: 520rpx;
  302. height: 80rpx;
  303. background-color: #389588;
  304. border-radius: 16rpx;
  305. color: #FFFFFF;
  306. font-size: 30rpx;
  307. line-height: 80rpx;
  308. text-align: center;
  309. }
  310. .update-btn-disabled {
  311. background-color: #AAAAAA;
  312. }
  313. .update-later {
  314. margin-top: 20rpx;
  315. font-size: 26rpx;
  316. color: #999999;
  317. }
  318. </style>