updatepage.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <view :class="[AppTheme]">
  3. <view class="download">
  4. <uni-popup ref="popup" type="center">
  5. <view class="upgrade">
  6. <view class="logo">
  7. <image src="./static/upgrade.png" mode="widthFix" />
  8. </view>
  9. <view class="content">
  10. <view class="title">
  11. <text>{{ upgrading ? '正在升级' : '发现新版本' + versionStr }}</text>
  12. </view>
  13. <view class="container">
  14. <view class="descriptions">
  15. <text>{{ upgrading ? '正在为您下载,请耐心等待' : '本次版本更新描述内容:' }}</text>
  16. </view>
  17. <view class="details" v-if="!upgrading">
  18. <view class="item">
  19. <u-parse :content="versions_desc"></u-parse>
  20. </view>
  21. </view>
  22. <view v-else class="prpgroess"><progress :percent="downloadTime" active-mode="forwards"
  23. activeColor="red" active stroke-width="4" show-info /></view>
  24. </view>
  25. <view v-if="!upgrading" class="btn-group">
  26. <view class="cancel" @click="hiddenUppop"><text>取 消</text></view>
  27. <view class="confirm bg-primary" @click="upgradeEvent"><text>确 定</text></view>
  28. </view>
  29. <view v-else class="btn-group">
  30. <view v-if="!isForceUpgrade" class="cancel" @click="abortDownload"><text>取消下载</text></view>
  31. <view v-if="!isForceUpgrade" class="confirm bg-primary" @click="hiddenUpgradeEvent">
  32. <text>后台下载</text>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </uni-popup>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import Vue from 'vue';
  43. import uniPopup from './uni-popup/uni-popup.vue';
  44. import uniPopupMessage from './uni-popup/uni-popup-message.vue';
  45. import uniPopupDialog from './uni-popup/uni-popup-dialog.vue';
  46. import update from "@/api/update/index.js"
  47. export default {
  48. props: {
  49. isUpdate: false
  50. },
  51. data: () => ({
  52. tempFilePaths: '',
  53. // 版本代码
  54. versionCode: 28,
  55. // 版本字符串
  56. versionStr: '1.0.0530',
  57. // 是否更新
  58. upgrading: false,
  59. // 下载时间
  60. downloadTime: 0,
  61. // 定时器
  62. timer: null,
  63. // 是否强制更新
  64. isForceUpgrade: false,
  65. // 下载任务
  66. downloadTask: null,
  67. // 下载地址
  68. downloadUrl: '',
  69. //版本更新内容
  70. versions_desc: '系统优化已知问题',
  71. settingFile: getApp().globalData.siteinfo,
  72. }),
  73. // 使用的组件
  74. components: {
  75. uniPopup,
  76. uniPopupMessage,
  77. uniPopupDialog
  78. },
  79. mounted() {
  80. let _this = this;
  81. _this.init(_this.isUpdate)
  82. },
  83. methods: {
  84. init(_isUpdate, fun) {
  85. let _this = this;
  86. const res = uni.getSystemInfoSync();
  87. if (res.platform == 'android') {
  88. let _this = this;
  89. if (_isUpdate) {
  90. update.download_link({
  91. platform: 'android'
  92. }).then(res => {
  93. if (res.status == 1) {
  94. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  95. if (inf.version !== res.data.version_number) {
  96. _this.$refs.popup.open();
  97. _this.downloadUrl = res.data.link;
  98. _this.versionStr = res.data.version_number;
  99. _this.versions_desc = res.data.content
  100. _this.versions_desc = _this.versions_desc.replace(/\<img src="/gi,
  101. `<img style='width:100%;display:block' src="${_this.settingFile.root_rich_img}`
  102. );
  103. } else {
  104. if (fun) {
  105. fun();
  106. }
  107. }
  108. });
  109. }
  110. })
  111. .catch(error => {
  112. console.log(error, "失败了")
  113. });
  114. }
  115. } else if (res.platform == 'ios') {}
  116. },
  117. // 取消更新
  118. hiddenUppop() {
  119. this.$store.commit('changeUpdate', false);
  120. this.$refs.popup.close();
  121. },
  122. // 检测更新
  123. upgrade() {
  124. let _this = this;
  125. _this.init(true, function() {
  126. uni.showToast({
  127. title: '恭喜您的应用是最新版本!',
  128. duration: 2000,
  129. icon: 'none'
  130. });
  131. })
  132. },
  133. // 点击更新
  134. upgradeEvent() {
  135. this.$store.commit('changeUpdate', false);
  136. if (!this.upgrading) {
  137. let versionInfo = this.getVersionAndPlatform();
  138. // 如果是正式更新需要打开下面这行注释
  139. // 这里只是做了简单的判断
  140. // 这里可以更据平台来下载
  141. // -------------------------
  142. console.log('versionInfo',versionInfo)
  143. if (versionInfo) {
  144. this.upgrading = true;
  145. // 如果是强制更新
  146. this.isForceUpgrade = true;
  147. // 正式下载请打开这里的注释
  148. this.downloadApplications();
  149. }
  150. // -----------------------
  151. }
  152. },
  153. // 后台下载
  154. hiddenUpgradeEvent() {
  155. this.$store.commit('changeUpdate', false);
  156. this.$refs.popup.close();
  157. },
  158. // 检测平台和当前app版本号
  159. getVersionAndPlatform() {
  160. // 这里try-catch是因为,当前代码是在浏览器上调试的
  161. // 由于浏览器不支持所以加了try-catch
  162. // 你也可以加这个玩意
  163. // #ifdef APP-PLUS
  164. // #endif
  165. // 你可以删除
  166. try {
  167. // 获取版本号
  168. let version = plus.runtime.version;
  169. // 获取当前平台
  170. // - Android
  171. // - Ios
  172. // 两种平台
  173. let platform = plus.os.name;
  174. return {
  175. version: version,
  176. platform: platform
  177. };
  178. } catch {
  179. console.error('当前平台不支持5+SDK!');
  180. }
  181. },
  182. // 下载
  183. downloadApplications() {
  184. let that = this;
  185. // 建立下载任务
  186. that.downloadTask = uni.downloadFile({
  187. // 下载地址
  188. url: that.downloadUrl,
  189. success: res => {
  190. console.log('res',res)
  191. if (res.statusCode === 200) {
  192. // 把当前app保存下载
  193. let tempFilePaths = res.tempFilePath
  194. uni.saveFile({
  195. tempFilePath: tempFilePaths,
  196. success: resp => {
  197. // 保存成功
  198. var savedFilePath = resp.savedFilePath;
  199. const installPath = plus.io.convertLocalFileSystemURL(
  200. savedFilePath);
  201. // 安装
  202. that.installApplications({
  203. filePath: installPath,
  204. success: res => {
  205. that.$store.commit('changeUpdate', false);
  206. that.$refs.popup.close();
  207. plus.nativeUI.alert("应用资源更新完成!",
  208. function() {
  209. plus.runtime.restart();
  210. });
  211. },
  212. error: err => {
  213. that.$refs.popup.close();
  214. that.upgrading = false;
  215. that.$api.msg(err);
  216. }
  217. });
  218. },
  219. fail: err => {
  220. // 保存失败
  221. that.upgrading = false;
  222. that.$refs.popup.close();
  223. that.$api.msg(err);
  224. }
  225. });
  226. }
  227. }
  228. });
  229. that.downloadTask.onProgressUpdate(res => {
  230. // 下载进度
  231. that.downloadTime = res.progress;
  232. });
  233. },
  234. // 取消下载
  235. abortDownload() {
  236. let that = this;
  237. that.downloadTime = 0;
  238. if (that.$refs.popup) {
  239. that.$refs.popup.close();
  240. setTimeout(() => {
  241. that.upgrading = false;
  242. }, 200)
  243. }
  244. clearInterval(that.timer);
  245. if (that.downloadTask) {
  246. that.downloadTask.abort();
  247. }
  248. },
  249. //安装
  250. installApplications({
  251. filePath,
  252. success,
  253. error
  254. }) {
  255. let that = this;
  256. plus.runtime.install(filePath, {
  257. force: that.isForceUpgrade
  258. }, success, error);
  259. }
  260. },
  261. computed: {},
  262. watch: {}
  263. };
  264. </script>
  265. <style>
  266. .scroll-view_H {
  267. hight: 400rpx;
  268. }
  269. .download .logo {
  270. width: 208rpx;
  271. }
  272. .cancel {
  273. background-color: #cbcbcb;
  274. }
  275. .download .upgrade {
  276. position: relative;
  277. background: #fff;
  278. width: 680rpx;
  279. min-height: 400rpx;
  280. border-radius: 20rpx;
  281. }
  282. .download .logo image {
  283. width: 208rpx;
  284. position: absolute;
  285. top: -80rpx;
  286. left: 0;
  287. right: 0;
  288. margin: 0 auto;
  289. }
  290. .download .content {
  291. padding-top: 80rpx;
  292. }
  293. .download .content .title {
  294. text-align: center;
  295. font-size: 30rpx;
  296. font-weight: bold;
  297. }
  298. .download .content .container {
  299. color: #666;
  300. overflow-y: scroll;
  301. min-height: 200rpx;
  302. }
  303. .download .content .container .descriptions {
  304. padding: 15rpx 30rpx;
  305. text-align: center;
  306. font-size: 28rpx;
  307. }
  308. .download .content .container .details {
  309. /* max-height:300rpx; */
  310. /* overflow: auto; */
  311. /* word-wrap:break-word; */
  312. }
  313. ,
  314. .download .content .container .details .item {
  315. padding: 20rpx;
  316. max-height: 500rpx;
  317. overflow-y: scroll;
  318. font-size: 30rpx;
  319. }
  320. ,
  321. .download .content .prpgroess {
  322. padding: 16rpx 46rpx;
  323. box-sizing: border-box;
  324. font-size: 24rpx;
  325. }
  326. .download .content .prpgroess {
  327. padding: 16rpx 22rpx;
  328. margin: 20rpx 0;
  329. }
  330. .download .content .btn-group {
  331. display: flex;
  332. justify-content: center;
  333. align-items: center;
  334. padding-bottom: 25rpx;
  335. }
  336. .download .content .btn-group view {
  337. width: 200rpx;
  338. height: 70rpx;
  339. display: flex;
  340. justify-content: center;
  341. align-items: center;
  342. margin: 14rpx;
  343. font-size: 28rpx;
  344. border-radius: 42rpx;
  345. line-height: 70rpx;
  346. }
  347. .download .content .btn-group view:last-child {
  348. /* background: #ef5656; */
  349. color: #fff;
  350. }
  351. </style>