app-update.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. }
  116. });
  117. vm.apkInfo = apkquote.data.content[0];
  118. vm.apkLatestVersion = apkquote.data.content[0].versionno;
  119. let wgtquote = await this.$http.post('/sysVersion/findPage', {
  120. pageNum: 1,
  121. pageSize: 50,
  122. columnFilters: {
  123. apptype: {
  124. name: "apptype",
  125. value: "wgt",
  126. }
  127. }
  128. });
  129. vm.wgtInfo = wgtquote.data.content[0];
  130. vm.wgtLatestVersion = wgtquote.data.content[0].versionno;
  131. vm.checkUpdate(); //检查是否更新
  132. // var data = {
  133. // appid: "__UNI__D4FE29A"
  134. // }
  135. // let res = await this.$http.get('/sysVersion/queryMaxNum', data);
  136. // vm.update_info = res.data;
  137. // if (vm.update_info.updateflag == '1') {
  138. // vm.force = true;
  139. // } else {
  140. // vm.force = false;
  141. // }
  142. // if (!vm.update_info.platform) {
  143. // // 后台未配置当前系统的升级数据
  144. // } else {
  145. //
  146. // }
  147. },
  148. // 检查是否更新
  149. checkUpdate() {
  150. let apkneed = vm.compareVersion(vm.version, vm.apkLatestVersion); // 检查包版本
  151. let wgtneed = vm.compareVersion(vm.version, vm.wgtLatestVersion); // 检查资源版本
  152. if (apkneed) {
  153. vm.update_info = vm.apkInfo;
  154. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  155. //页面是否有原生tabbar组件
  156. // 创建原生view用来遮罩tabbar的点击事件 (如果是没有用原生的tabbar这一步可以取消)
  157. vm.viewObj = new plus.nativeObj.View('viewObj', {
  158. bottom: '0px',
  159. left: '0px',
  160. height: '10px',
  161. width: '100%',
  162. position: "fixed",
  163. backgroundColor: "rgba(0,0,0,.6)"
  164. });
  165. vm.viewObj.show() //显示原生遮罩
  166. } else {
  167. if (wgtneed) {
  168. vm.update_info = vm.wgtInfo;
  169. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  170. //页面是否有原生tabbar组件
  171. // 创建原生view用来遮罩tabbar的点击事件 (如果是没有用原生的tabbar这一步可以取消)
  172. vm.viewObj = new plus.nativeObj.View('viewObj', {
  173. bottom: '0px',
  174. left: '0px',
  175. height: '10px',
  176. width: '100%',
  177. position: "fixed",
  178. backgroundColor: "rgba(0,0,0,.6)"
  179. });
  180. vm.viewObj.show() //显示原生遮罩
  181. }
  182. }
  183. },
  184. // 取消更新
  185. closeUpdate() {
  186. if (vm.force) {
  187. // 强制更新,取消退出app
  188. plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication()
  189. .performSelector(
  190. "exit");
  191. } else {
  192. vm.popup_show = false; //关闭升级弹窗
  193. if (vm.viewObj) vm.viewObj.hide() //隐藏原生遮罩
  194. }
  195. },
  196. // 立即更新
  197. nowUpdate() {
  198. if (vm.downing) return false; //如果正在下载就停止操作
  199. vm.downing = true; //状态改变 正在下载中
  200. if (/\.apk$/.test(vm.update_info.path)) {
  201. // 如果是apk地址
  202. vm.download_wgt() // 安装包/升级包更新
  203. } else if (/\.wgt$/.test(vm.update_info.path)) {
  204. // 如果是更新包
  205. vm.download_wgt() // 安装包/升级包更新
  206. } else {
  207. plus.runtime.openURL(vm.update_info.path, function() { //调用外部浏览器打开更新地址
  208. plus.nativeUI.toast("打开错误");
  209. });
  210. }
  211. },
  212. // 下载升级资源包
  213. download_wgt() {
  214. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  215. let options = {
  216. method: "get"
  217. };
  218. let dtask = plus.downloader.createDownload(vm.update_info.path, options, function(d, status) {
  219. });
  220. dtask.addEventListener("statechanged", function(task, status) {
  221. if (status === null) {} else if (status == 200) {
  222. vm.downstatus = task.state;
  223. switch (task.state) {
  224. case 3: // 已接收到数据
  225. vm.downSize = task.downloadedSize;
  226. if (task.totalSize) {
  227. vm.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  228. }
  229. break;
  230. case 4:
  231. vm.installWgt(task.filename); // 安装wgt包
  232. break;
  233. }
  234. } else {
  235. plus.nativeUI.closeWaiting();
  236. plus.nativeUI.toast("下载出错");
  237. vm.downing = false;
  238. vm.downstatus = 0;
  239. }
  240. });
  241. dtask.start();
  242. },
  243. // 安装文件
  244. installWgt(path) {
  245. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  246. plus.runtime.install(path, {}, function() {
  247. plus.nativeUI.closeWaiting();
  248. // 应用资源下载完成!
  249. plus.nativeUI.alert("应用资源下载完成!", function() {
  250. plus.runtime.restart();
  251. });
  252. }, function(e) {
  253. plus.nativeUI.closeWaiting();
  254. // 安装更新文件失败
  255. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  256. });
  257. },
  258. // 对比版本号
  259. compareVersion(ov, nv) {
  260. if (!ov || !nv || ov == "" || nv == "") {
  261. return false;
  262. }
  263. let b = false,
  264. ova = ov.split(".", 4),
  265. nva = nv.split(".", 4);
  266. for (let i = 0; i < ova.length && i < nva.length; i++) {
  267. let so = ova[i],
  268. no = parseInt(so),
  269. sn = nva[i],
  270. nn = parseInt(sn);
  271. if (nn > no || sn.length > so.length) {
  272. return true;
  273. } else if (nn < no) {
  274. return false;
  275. }
  276. }
  277. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  278. return true;
  279. } else {
  280. return false;
  281. }
  282. },
  283. }
  284. }
  285. </script>
  286. <style lang="scss" scoped>
  287. .popup-bg {
  288. display: flex;
  289. flex-direction: column;
  290. align-items: center;
  291. justify-content: center;
  292. position: fixed;
  293. top: 0;
  294. left: 0;
  295. right: 0;
  296. bottom: var(--window-bottom);
  297. width: 750rpx;
  298. background-color: rgba(0, 0, 0, 0.6);
  299. z-index: 99999999;
  300. }
  301. .popup-content {
  302. display: flex;
  303. flex-direction: column;
  304. align-items: center;
  305. }
  306. .popup-content-show {
  307. animation: mymove 500ms;
  308. transform: scale(1);
  309. }
  310. @keyframes mymove {
  311. 0% {
  312. transform: scale(0);
  313. /*开始为原始大小*/
  314. }
  315. 100% {
  316. transform: scale(1);
  317. }
  318. }
  319. .update-wrap {
  320. width: 580rpx;
  321. border-radius: 18rpx;
  322. position: relative;
  323. display: flex;
  324. flex-direction: column;
  325. background-color: #ffffff;
  326. padding: 170rpx 30rpx 0;
  327. .top-img {
  328. position: absolute;
  329. left: 0;
  330. width: 100%;
  331. height: 256rpx;
  332. top: -128rpx;
  333. }
  334. .content {
  335. display: flex;
  336. flex-direction: column;
  337. align-items: center;
  338. padding-bottom: 40rpx;
  339. .title {
  340. font-size: 32rpx;
  341. font-weight: bold;
  342. color: #6526f3;
  343. }
  344. .title-sub {
  345. text-align: center;
  346. font-size: 24rpx;
  347. color: #666666;
  348. padding-top: 20rpx;
  349. }
  350. .btn {
  351. width: 460rpx;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. color: #ffffff;
  356. font-size: 30rpx;
  357. height: 80rpx;
  358. line-height: 80rpx;
  359. border-radius: 100px;
  360. background-color: #6526f3;
  361. margin-top: 20rpx;
  362. }
  363. }
  364. }
  365. .close-ioc {
  366. width: 70rpx;
  367. height: 70rpx;
  368. margin-top: 30rpx;
  369. }
  370. .sche-wrap {
  371. display: flex;
  372. flex-direction: column;
  373. align-items: center;
  374. justify-content: flex-end;
  375. padding: 10rpx 50rpx 0;
  376. .sche-wrap-text {
  377. font-size: 24rpx;
  378. color: #666;
  379. margin-bottom: 20rpx;
  380. }
  381. .sche-bg {
  382. position: relative;
  383. background-color: #cccccc;
  384. height: 30rpx;
  385. border-radius: 100px;
  386. width: 480rpx;
  387. display: flex;
  388. align-items: center;
  389. .sche-bg-jindu {
  390. position: absolute;
  391. left: 0;
  392. top: 0;
  393. height: 30rpx;
  394. min-width: 40rpx;
  395. border-radius: 100px;
  396. background: url(images/round.png) #5775e7 center right 4rpx no-repeat;
  397. background-size: 26rpx 26rpx;
  398. }
  399. }
  400. .down-text {
  401. font-size: 24rpx;
  402. color: #5674e5;
  403. margin-top: 16rpx;
  404. }
  405. }
  406. </style>