app-update.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <template>
  2. <uni-popup ref="popup" type="center" :maskClick="false">
  3. <view class="update-wrap">
  4. <view class="white-bg"></view>
  5. <image src="@/static/images/top_2.png" class="updateCon-img"></image>
  6. <view class="updateCon-top">
  7. <!-- 发现新版本 -->
  8. <text class="update-top-title">发现新版本</text>
  9. <text class="update-top-version">V{{update_info.version}}</text>
  10. </view>
  11. <text class="uodate-content" v-if="!update_ing">更新内容:{{update_info.version_note}}</text>
  12. <text class="current-version" v-if="!update_ing">当前版本:V{{version}}</text>
  13. <view class="update-btn" v-if="!update_ing">
  14. <view class="update-btn-item update-btn-left" @click="up_close">
  15. <!-- 残忍拒绝 -->
  16. <text class="update-btn-item-text ">残忍拒绝</text>
  17. </view>
  18. <view class="update-btn-item" @click="nowUpdate">
  19. <!-- 立即升级 -->
  20. <text class="update-btn-item-text text-bule">立即升级</text>
  21. </view>
  22. </view>
  23. <!-- 下载进度 -->
  24. <view class="sche-wrap" v-if="update_ing">
  25. <!-- 更新包下载中 -->
  26. <text class="sche-wrap-text">更新包下载中...</text>
  27. <view class="sche-bg">
  28. <view class="sche-bg-jindu" :style="lengthWidth">
  29. <view class="sche-bg-round">
  30. <text class="sche-bg-round-text" v-if="schedule">{{schedule}}%</text>
  31. <text class="sche-bg-round-text" v-else>{{(downloadedSize/1024/1024 ).toFixed(2)}}M</text>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </uni-popup>
  38. </template>
  39. <script>
  40. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  41. import { creatRequest } from '../../utils/request.js';
  42. export default {
  43. // props:['url'],
  44. data() {
  45. return {
  46. platform: "", //ios or android
  47. version: "0.0.0", //当前软件版本
  48. is_update: false, // 是否更新
  49. is_reques: false, //是否请求中
  50. update_info: {
  51. "type": "2", //1分发平台更新 2安装包/升级包更新
  52. "version_note": "", //ios更新备注
  53. "version": "0.0.0", //ios版本号
  54. "download_url": "", //ios更新新链接
  55. },
  56. ifCheckAppUpdate:false,
  57. is_update: false,
  58. schedule: 0,
  59. update_ing: false, //点击升级
  60. is_down: false,
  61. downloadedSize: 0,
  62. };
  63. },
  64. components:{uniPopup},
  65. mounted() {
  66. const updateRejectStart = uni.getStorageSync('updateRejectStart');
  67. const updateRejectCurrent = new Date().getTime();
  68. const timeLeft = updateRejectCurrent-(updateRejectStart?updateRejectStart:9999999999999999999999999999);
  69. console.log({updateRejectStart,updateRejectCurrent,timeLeft});
  70. if(timeLeft>=600000){
  71. this.getSystemInfo() //获取系统新
  72. }
  73. },
  74. watch: {
  75. ifCheckAppUpdate: function(newVal, oldVal) {
  76. //其他业务代码
  77. console.log({newVal, oldVal});
  78. }
  79. },
  80. computed: {
  81. // 下载进度计算
  82. lengthWidth: function() {
  83. return {
  84. width: this.schedule * 480 / 100 + "rpx"
  85. }
  86. }
  87. },
  88. methods: {
  89. // 获取系统信息
  90. getSystemInfo() {
  91. let vm = this;
  92. // 获取手机系统信息
  93. uni.getSystemInfo({
  94. success: function(res) {
  95. vm.platform = res.platform //ios or android
  96. }
  97. });
  98. // 获取版本号
  99. // #ifdef APP-PLUS
  100. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  101. vm.version = inf.version
  102. });
  103. // #endif
  104. // #ifdef APP-PLUS
  105. vm.getUpdateInfo(); //获取更新信息
  106. // #endif
  107. // // #ifdef APP-PLUS
  108. // plus.runtime.getProperty( plus.runtime.appid, function ( wgtinfo ) {
  109. // //appid属性
  110. // var wgtStr = "appid:"+wgtinfo.appid;
  111. // //version属性
  112. // wgtStr += "<br/>version:"+wgtinfo.version;
  113. // //name属性
  114. // wgtStr += "<br/>name:"+wgtinfo.name;
  115. // //description属性
  116. // wgtStr += "<br/>description:"+wgtinfo.description;
  117. // //author属性
  118. // wgtStr += "<br/>author:"+wgtinfo.author;
  119. // //email属性
  120. // wgtStr += "<br/>email:"+wgtinfo.email;
  121. // //features 属性
  122. // wgtStr += "<br/>features:"+wgtinfo.features;
  123. // console.log( wgtStr );
  124. // } );
  125. // // #endif
  126. },
  127. // 获取线上版本信息
  128. getUpdateInfo() {
  129. let vm = this;
  130. const obj = {
  131. method: 'GET',
  132. url: 'versionInfo/getVersionData'
  133. }
  134. creatRequest(obj,{versionNo:vm.version}).then(res=>{
  135. console.log({res});
  136. if(res.isNew){
  137. // vm.update_info.type = "1";
  138. vm.update_info.version_note = res.versionContent;
  139. vm.update_info.version = res.versionNo;
  140. vm.update_info.download_url = res.versionUrl;
  141. }else {
  142. }
  143. vm.checkUpdate(); ///检查是否更新
  144. })
  145. },
  146. // 检查是否更新
  147. checkUpdate() {
  148. let vm = this;
  149. console.log(vm.version,vm.update_info.version);
  150. vm.is_update = vm.compareVersion(vm.version, vm.update_info.version); // 检查是否升级
  151. if (vm.is_update) {
  152. vm.$refs.popup.open() //显示升级弹窗
  153. } else {
  154. // 不更新,根据需要做处理
  155. }
  156. },
  157. // 取消更新
  158. up_close() {
  159. console.log('点击取消');
  160. // this.$store.commit('setAppUpdateStatus',false);
  161. uni.setStorageSync('updateRejectStart',new Date().getTime());
  162. this.$refs.popup.close();
  163. // plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication().performSelector("exit");
  164. },
  165. // 立即更新
  166. nowUpdate() {
  167. let vm = this;
  168. if(vm.is_reques){
  169. return false //如果正在下载就停止操作
  170. }else{
  171. vm.is_reques = true
  172. }
  173. console.log(111111);
  174. if (vm.update_info.type == 1) {
  175. // 分发平台更新 //调用浏览器打开链接或者分发平台
  176. plus.runtime.openURL(vm.update_info.download_url, function() {
  177. plus.nativeUI.toast("打开错误");
  178. });
  179. setTimeout(function() {
  180. plus.runtime.quit(); //五秒后关闭app
  181. }, 5000)
  182. } else if (vm.update_info.type == 2) {
  183. // 安装包/升级包更新
  184. vm.download_wgt()
  185. }
  186. },
  187. // 下载升级资源包
  188. download_wgt() {
  189. let vm = this;
  190. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  191. // const token = uni.getStorageSync('token');
  192. let options = {
  193. method: "GET",
  194. };
  195. let dtask = plus.downloader.createDownload(vm.update_info.download_url, options);
  196. dtask.addEventListener("statechanged", function(task, status) {
  197. switch (task.state) {
  198. case 1: // 开始
  199. break;
  200. case 2: //已连接到服务器
  201. vm.update_ing = true;
  202. break;
  203. case 3: // 已接收到数据
  204. vm.downloadedSize = task.downloadedSize;
  205. let totalSize = 0;
  206. if (task.totalSize) {
  207. totalSize = task.totalSize //服务器须返回正确的content-length才会有长度
  208. }
  209. vm.schedule = parseInt(100 * task.downloadedSize / totalSize);
  210. break;
  211. case 4:
  212. vm.installWgt(task.filename); // 安装wgt包
  213. break;
  214. }
  215. });
  216. dtask.start();
  217. },
  218. // 安装文件
  219. installWgt(path) {
  220. let vm = this;
  221. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  222. plus.runtime.install(path, {}, function() {
  223. plus.nativeUI.closeWaiting();
  224. // 应用资源更新完成!
  225. plus.nativeUI.alert("应用资源更新完成!", function() {
  226. plus.runtime.restart();
  227. });
  228. }, function(e) {
  229. plus.nativeUI.closeWaiting();
  230. // 安装更新文件失败
  231. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  232. });
  233. },
  234. // 对比版本号
  235. compareVersion(ov, nv) {
  236. if (!ov || !nv || ov == "" || nv == "") {
  237. return false;
  238. }
  239. let b = false,
  240. ova = ov.split(".", 4),
  241. nva = nv.split(".", 4);
  242. for (let i = 0; i < ova.length && i < nva.length; i++) {
  243. let so = ova[i],
  244. no = parseInt(so),
  245. sn = nva[i],
  246. nn = parseInt(sn);
  247. if (nn > no || sn.length > so.length) {
  248. return true;
  249. } else if (nn < no) {
  250. return false;
  251. }
  252. }
  253. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  254. return true;
  255. } else {
  256. return false;
  257. }
  258. },
  259. }
  260. }
  261. </script>
  262. <style scoped>
  263. /*#ifndef APP-NVUE*/
  264. view {
  265. display: flex;
  266. flex-direction: column;
  267. box-sizing: border-box;
  268. }
  269. /*#endif*/
  270. .updateBox {
  271. background-color: rgba(0, 0, 0, 0.6);
  272. z-index: 1000;
  273. position: fixed;
  274. right: 0px;
  275. top: 0;
  276. left: 0;
  277. bottom: 0;
  278. align-items: center;
  279. justify-content: center;
  280. }
  281. .update-wrap {
  282. width: 580rpx;
  283. height: 500rpx;
  284. border-radius: 10px;
  285. padding-bottom: 0px;
  286. position: relative;
  287. }
  288. .white-bg {
  289. position: absolute;
  290. top: 60rpx;
  291. left: 0px;
  292. width: 580rpx;
  293. height: 440rpx;
  294. background-color: #ffffff;
  295. border-bottom-left-radius: 30rpx;
  296. border-bottom-right-radius: 30rpx;
  297. }
  298. .updateCon-img {
  299. position: absolute;
  300. top: 0rpx;
  301. left: 0px;
  302. width: 580rpx;
  303. height: 440rpx;
  304. }
  305. .updateCon-top {
  306. padding: 70rpx 50rpx;
  307. /*#ifndef APP-NVUE*/
  308. z-index: 1;
  309. /*#endif*/
  310. }
  311. .update-top-title {
  312. color: #fff;
  313. font-size: 36rpx;
  314. font-weight: bold;
  315. }
  316. .update-top-version {
  317. color: #fff;
  318. font-size: 28rpx;
  319. margin-top: 10rpx;
  320. }
  321. .uodate-content {
  322. color: #333;
  323. font-size: 18px;
  324. padding: 0px 50rpx;
  325. margin-top: 80rpx;
  326. /*#ifndef APP-NVUE*/
  327. z-index: 1;
  328. /*#endif*/
  329. }
  330. .current-version {
  331. text-align: center;
  332. margin-top: 10rpx;
  333. font-size: 24rpx;
  334. color: #666;
  335. /*#ifndef APP-NVUE*/
  336. z-index: 1;
  337. /*#endif*/
  338. }
  339. .update-btn {
  340. position: absolute;
  341. left: 0px;
  342. bottom: 0px;
  343. width: 580rpx;
  344. height: 80rpx;
  345. padding: 0px 50rpx;
  346. align-items: center;
  347. flex-direction: row;
  348. border-top-color: #e7e7e7;
  349. border-top-style: solid;
  350. border-top-width: 1px;
  351. background-color: #fff;
  352. border-bottom-left-radius: 30rpx;
  353. border-bottom-right-radius: 30rpx;
  354. }
  355. .update-btn-item {
  356. width: 240rpx;
  357. height: 80rpx;
  358. justify-content: center;
  359. align-items: center;
  360. }
  361. .update-btn-left {
  362. border-right-color: #e7e7e7;
  363. border-right-style: solid;
  364. border-right-width: 1px;
  365. }
  366. .update-btn-item-text {
  367. text-align: center;
  368. font-size: 28rpx;
  369. color: #666;
  370. }
  371. .text-bule {
  372. color: #045FCF;
  373. }
  374. .sche-wrap {
  375. padding: 0px 50rpx 0rpx;
  376. height: 100rpx;
  377. align-items: center;
  378. border-bottom-left-radius: 30rpx;
  379. border-bottom-right-radius: 30rpx;
  380. flex: 1;
  381. justify-content: flex-end;
  382. }
  383. .sche-wrap-text {
  384. font-size: 24rpx;
  385. color: #666;
  386. margin-bottom: 20rpx;
  387. }
  388. .sche-bg {
  389. position: relative;
  390. background-color: #ccc;
  391. height: 20rpx;
  392. border-radius: 100px;
  393. width: 480rpx;
  394. margin-bottom: 30rpx;
  395. }
  396. .sche-bg-jindu {
  397. position: absolute;
  398. left: 0px;
  399. top: 0px;
  400. height: 20rpx;
  401. background-color: #5775e7;
  402. border-radius: 100px;
  403. }
  404. .sche-bg-round {
  405. position: absolute;
  406. left: 100%;
  407. height: 24rpx;
  408. width: 24rpx;
  409. background-color: #fff;
  410. border-color: #fff;
  411. border-style: solid;
  412. border-width: 10px;
  413. border-radius: 100px;
  414. transform: translateX(-20rpx) translateY(-10rpx);
  415. }
  416. .sche-bg-round-text {
  417. font-size: 24rpx;
  418. width: 120rpx;
  419. text-align: center;
  420. transform: translateX(-50rpx) translateY(-40rpx);
  421. color: #5775e7;
  422. }
  423. .uodate-close {
  424. /* display: flex; */
  425. flex-direction: row;
  426. justify-content: center;
  427. padding-top: 50rpx;
  428. }
  429. .uodate-close-img {
  430. width: 80rpx;
  431. height: 80rpx;
  432. }
  433. </style>