app-update.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <view class="wrap" v-if="popup_show">
  3. <view class="popup-bg" :style="getHeight">
  4. <view class="popup-content" :class="{'popup-content-show' : popup_show}">
  5. <view class="update-wrap">
  6. <image src="./images/img.png" class="top-img"></image>
  7. <view class="content">
  8. <text class="title">发现新版本V{{update_info.versionno}}</text>
  9. <!-- 标题 -->
  10. <view class="title-sub" v-html="update_info.title" style="font-weight: bold;"></view>
  11. <!-- 升级描述 -->
  12. <view class="title-sub" v-html="update_info.content" style="padding-top: 8rpx;"></view>
  13. <!-- 升级按钮 -->
  14. <button class="btn" v-if="downstatus < 1" @click="nowUpdate()">立即升级</button>
  15. <!-- 下载进度 -->
  16. <view class="sche-wrap" v-else>
  17. <!-- 更新包下载中 -->
  18. <view style="display: flex;align-items: center;justify-content: space-between;">
  19. <view class="sche-bg">
  20. <view class="sche-bg-jindu" :style="lengthWidth"></view>
  21. </view>
  22. <text style="color: #999999;margin-left: 4rpx;">{{width}}%</text>
  23. </view>
  24. <text
  25. class="down-text">下载进度:{{(downSize/1024/1024 ).toFixed(2)}}M/{{(fileSize/1024/1024).toFixed(2)}}M</text>
  26. </view>
  27. </view>
  28. </view>
  29. <image src="./images/close.png" class="close-ioc" @click="closeUpdate()"></image>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. var vm;
  36. export default {
  37. name: "appUpdate",
  38. //@是否强制更新
  39. props: {
  40. },
  41. data() {
  42. return {
  43. popup_show: false, //弹窗是否显示
  44. platform: "", //ios or android
  45. version: "", //当前软件版本
  46. wgtLatestVersion: "", //最新资源
  47. apkLatestVersion: "", //最新整包
  48. need_update: false, // 是否更新
  49. downing: false, //是否下载中
  50. downstatus: 0, //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  51. update_info: {},
  52. fileSize: 0, //文件大小
  53. downSize: 0, //已下载大小
  54. viewObj: null, //原生遮罩view
  55. force: true,
  56. width: "",
  57. wgtInfo: {},
  58. apkInfo: {},
  59. };
  60. },
  61. created() {
  62. vm = this;
  63. },
  64. computed: {
  65. // 下载进度计算
  66. lengthWidth: function() {
  67. let w = this.downSize / this.fileSize * 100;
  68. let result = w;
  69. this.width = Math.floor(result)
  70. if (!w) {
  71. w = 0
  72. } else {
  73. w = w.toFixed(2)
  74. }
  75. return {
  76. width: w + "%" //return 宽度百分比
  77. }
  78. },
  79. getHeight: function() {
  80. let bottom = 0;
  81. return {
  82. "bottom": bottom + 'px',
  83. "height": "auto"
  84. }
  85. }
  86. },
  87. methods: {
  88. // 检查更新
  89. update() {
  90. // #ifdef APP-PLUS
  91. // 获取手机系统信息
  92. uni.getSystemInfo({
  93. success: function(res) {
  94. vm.platform = res.platform //ios or android
  95. }
  96. });
  97. // 获取当前版本号
  98. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  99. vm.version = inf.version
  100. });
  101. vm.getUpdateInfo(); //获取更新信息
  102. // #endif
  103. },
  104. // 获取线上版本信息
  105. async getUpdateInfo() {
  106. //向后台发起请求,获取最新版本号
  107. let apkquote = await this.$http.post('/sysVersion/findPage', {
  108. pageNum: 1,
  109. pageSize: 50,
  110. columnFilters: {
  111. apptype: {
  112. name: "apptype",
  113. value: "apk",
  114. },
  115. appid: {
  116. name: "appid",
  117. value: "__UNI__3FA05C4",
  118. }
  119. }
  120. });
  121. vm.apkInfo = apkquote.data.content[0];
  122. vm.apkLatestVersion = apkquote.data.content[0].versionno;
  123. let wgtquote = await this.$http.post('/sysVersion/findPage', {
  124. pageNum: 1,
  125. pageSize: 50,
  126. columnFilters: {
  127. apptype: {
  128. name: "apptype",
  129. value: "wgt",
  130. },
  131. appid: {
  132. name: "appid",
  133. value: "__UNI__3FA05C4",
  134. }
  135. }
  136. });
  137. vm.wgtInfo = wgtquote.data.content[0];
  138. vm.wgtLatestVersion = wgtquote.data.content[0].versionno;
  139. vm.checkUpdate(); //检查是否更新
  140. // var data = {
  141. // appid: "__UNI__D4FE29A"
  142. // }
  143. // let res = await this.$http.get('/sysVersion/queryMaxNum', data);
  144. // vm.update_info = res.data;
  145. // if (vm.update_info.updateflag == '1') {
  146. // vm.force = true;
  147. // } else {
  148. // vm.force = false;
  149. // }
  150. // if (!vm.update_info.platform) {
  151. // // 后台未配置当前系统的升级数据
  152. // } else {
  153. //
  154. // }
  155. },
  156. // 检查是否更新
  157. checkUpdate() {
  158. let apkneed = vm.compareVersion(vm.version, vm.apkLatestVersion); // 检查包版本
  159. let wgtneed = vm.compareVersion(vm.version, vm.wgtLatestVersion); // 检查资源版本
  160. if (apkneed) {
  161. vm.update_info = vm.apkInfo;
  162. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  163. //页面是否有原生tabbar组件
  164. // 创建原生view用来遮罩tabbar的点击事件 (如果是没有用原生的tabbar这一步可以取消)
  165. vm.viewObj = new plus.nativeObj.View('viewObj', {
  166. bottom: '0px',
  167. left: '0px',
  168. height: '10px',
  169. width: '100%',
  170. position: "fixed",
  171. backgroundColor: "rgba(0,0,0,.6)"
  172. });
  173. vm.viewObj.show() //显示原生遮罩
  174. } else {
  175. if (wgtneed) {
  176. vm.update_info = vm.wgtInfo;
  177. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  178. //页面是否有原生tabbar组件
  179. // 创建原生view用来遮罩tabbar的点击事件 (如果是没有用原生的tabbar这一步可以取消)
  180. vm.viewObj = new plus.nativeObj.View('viewObj', {
  181. bottom: '0px',
  182. left: '0px',
  183. height: '10px',
  184. width: '100%',
  185. position: "fixed",
  186. backgroundColor: "rgba(0,0,0,.6)"
  187. });
  188. vm.viewObj.show() //显示原生遮罩
  189. }
  190. }
  191. },
  192. // 取消更新
  193. closeUpdate() {
  194. if (vm.force) {
  195. // 强制更新,取消退出app
  196. plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication()
  197. .performSelector(
  198. "exit");
  199. } else {
  200. vm.popup_show = false; //关闭升级弹窗
  201. if (vm.viewObj) vm.viewObj.hide() //隐藏原生遮罩
  202. }
  203. },
  204. // 立即更新
  205. nowUpdate() {
  206. if (vm.downing) return false; //如果正在下载就停止操作
  207. vm.downing = true; //状态改变 正在下载中
  208. if (/\.apk$/.test(vm.update_info.path)) {
  209. // 如果是apk地址
  210. vm.download_wgt() // 安装包/升级包更新
  211. } else if (/\.wgt$/.test(vm.update_info.path)) {
  212. // 如果是更新包
  213. vm.download_wgt() // 安装包/升级包更新
  214. } else {
  215. plus.runtime.openURL(vm.update_info.path, function() { //调用外部浏览器打开更新地址
  216. plus.nativeUI.toast("打开错误");
  217. });
  218. }
  219. },
  220. // 下载升级资源包
  221. download_wgt() {
  222. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  223. let options = {
  224. method: "get"
  225. };
  226. let dtask = plus.downloader.createDownload(vm.update_info.path, options, function(d, status) {
  227. });
  228. dtask.addEventListener("statechanged", function(task, status) {
  229. if (status === null) {} else if (status == 200) {
  230. vm.downstatus = task.state;
  231. switch (task.state) {
  232. case 3: // 已接收到数据
  233. vm.downSize = task.downloadedSize;
  234. if (task.totalSize) {
  235. vm.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  236. }
  237. break;
  238. case 4:
  239. vm.installWgt(task.filename); // 安装wgt包
  240. break;
  241. }
  242. } else {
  243. plus.nativeUI.closeWaiting();
  244. plus.nativeUI.toast("下载出错");
  245. vm.downing = false;
  246. vm.downstatus = 0;
  247. }
  248. });
  249. dtask.start();
  250. },
  251. // 安装文件
  252. installWgt(path) {
  253. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  254. plus.runtime.install(path, {}, function() {
  255. plus.nativeUI.closeWaiting();
  256. // 应用资源下载完成!
  257. plus.nativeUI.alert("应用资源下载完成!", function() {
  258. plus.runtime.restart();
  259. });
  260. }, function(e) {
  261. plus.nativeUI.closeWaiting();
  262. // 安装更新文件失败
  263. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  264. });
  265. },
  266. // 对比版本号
  267. compareVersion(ov, nv) {
  268. if (!ov || !nv || ov == "" || nv == "") {
  269. return false;
  270. }
  271. let b = false,
  272. ova = ov.split(".", 4),
  273. nva = nv.split(".", 4);
  274. for (let i = 0; i < ova.length && i < nva.length; i++) {
  275. let so = ova[i],
  276. no = parseInt(so),
  277. sn = nva[i],
  278. nn = parseInt(sn);
  279. if (nn > no || sn.length > so.length) {
  280. return true;
  281. } else if (nn < no) {
  282. return false;
  283. }
  284. }
  285. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. },
  291. }
  292. }
  293. </script>
  294. <style lang="scss" scoped>
  295. .popup-bg {
  296. display: flex;
  297. flex-direction: column;
  298. align-items: center;
  299. justify-content: center;
  300. position: fixed;
  301. top: 0;
  302. left: 0;
  303. right: 0;
  304. bottom: var(--window-bottom);
  305. width: 750rpx;
  306. background-color: rgba(0, 0, 0, 0.6);
  307. z-index: 99999999;
  308. }
  309. .popup-content {
  310. display: flex;
  311. flex-direction: column;
  312. align-items: center;
  313. }
  314. .popup-content-show {
  315. animation: mymove 500ms;
  316. transform: scale(1);
  317. }
  318. @keyframes mymove {
  319. 0% {
  320. transform: scale(0);
  321. /*开始为原始大小*/
  322. }
  323. 100% {
  324. transform: scale(1);
  325. }
  326. }
  327. .update-wrap {
  328. width: 580rpx;
  329. border-radius: 18rpx;
  330. position: relative;
  331. display: flex;
  332. flex-direction: column;
  333. background-color: #ffffff;
  334. padding: 170rpx 30rpx 0;
  335. .top-img {
  336. position: absolute;
  337. left: 0;
  338. width: 100%;
  339. height: 256rpx;
  340. top: -128rpx;
  341. }
  342. .content {
  343. display: flex;
  344. flex-direction: column;
  345. align-items: center;
  346. padding-bottom: 40rpx;
  347. .title {
  348. font-size: 32rpx;
  349. font-weight: bold;
  350. color: #3a63f6;
  351. }
  352. .title-sub {
  353. text-align: center;
  354. font-size: 24rpx;
  355. color: #666666;
  356. padding-top: 20rpx;
  357. }
  358. .btn {
  359. width: 460rpx;
  360. display: flex;
  361. align-items: center;
  362. justify-content: center;
  363. color: #ffffff;
  364. font-size: 30rpx;
  365. height: 80rpx;
  366. line-height: 80rpx;
  367. border-radius: 100px;
  368. background-color: #3a63f6;
  369. margin-top: 20rpx;
  370. }
  371. }
  372. }
  373. .close-ioc {
  374. width: 70rpx;
  375. height: 70rpx;
  376. margin-top: 30rpx;
  377. }
  378. .sche-wrap {
  379. display: flex;
  380. flex-direction: column;
  381. align-items: center;
  382. justify-content: flex-end;
  383. padding: 10rpx 50rpx 0;
  384. .sche-wrap-text {
  385. font-size: 24rpx;
  386. color: #666;
  387. margin-bottom: 20rpx;
  388. }
  389. .sche-bg {
  390. position: relative;
  391. background-color: #cccccc;
  392. height: 30rpx;
  393. border-radius: 100px;
  394. width: 480rpx;
  395. display: flex;
  396. align-items: center;
  397. .sche-bg-jindu {
  398. position: absolute;
  399. left: 0;
  400. top: 0;
  401. height: 30rpx;
  402. min-width: 40rpx;
  403. border-radius: 100px;
  404. background: url(images/round.png) #5775e7 center right 4rpx no-repeat;
  405. background-size: 26rpx 26rpx;
  406. }
  407. }
  408. .down-text {
  409. font-size: 24rpx;
  410. color: #5674e5;
  411. margin-top: 16rpx;
  412. }
  413. }
  414. </style>