app-update.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. tabbar: {
  41. type: Boolean,
  42. default: false, //是否有原生tabbar组件
  43. }
  44. },
  45. data() {
  46. return {
  47. popup_show: false, //弹窗是否显示
  48. platform: "", //ios or android
  49. version: "", //当前软件版本
  50. need_update: false, // 是否更新
  51. downing: false, //是否下载中
  52. downstatus: 0, //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  53. update_info: {},
  54. fileSize: 0, //文件大小
  55. downSize: 0, //已下载大小
  56. viewObj: null, //原生遮罩view
  57. force: false,
  58. width: "",
  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. if (this.tabbar) {
  82. bottom = 10;
  83. }
  84. return {
  85. "bottom": bottom + 'px',
  86. "height": "auto"
  87. }
  88. }
  89. },
  90. methods: {
  91. // 检查更新
  92. update() {
  93. // #ifdef APP-PLUS
  94. // 获取手机系统信息
  95. uni.getSystemInfo({
  96. success: function(res) {
  97. vm.platform = res.platform //ios or android
  98. }
  99. });
  100. // 获取当前版本号
  101. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  102. vm.version = inf.version
  103. });
  104. vm.getUpdateInfo(); //获取更新信息
  105. // #endif
  106. },
  107. // 获取线上版本信息
  108. async getUpdateInfo() {
  109. //向后台发起请求,获取最新版本号
  110. var data = {
  111. appid: "__UNI__D4FE29A"
  112. }
  113. let res = await this.$http.get('/sysVersion/queryMaxNum', data);
  114. vm.update_info = res.data;
  115. if (vm.update_info.updateflag == '1') {
  116. vm.force = true;
  117. } else {
  118. vm.force = false;
  119. }
  120. if (!vm.update_info.platform) {
  121. // 后台未配置当前系统的升级数据
  122. } else {
  123. vm.checkUpdate(); //检查是否更新
  124. }
  125. // uni.request({
  126. // url: 'http://test.baoxianzhanggui.com:8080/sysVersion/queryMaxNum', //仅为示例,并非真实接口地址。
  127. // method: 'GET',
  128. // data:{
  129. // },
  130. // success: (res) => {
  131. // console.log(res)
  132. // // 这里的返回的数据跟后台约定
  133. // let data = {
  134. // os: 'android',
  135. // version: '1.0.116', //最新版本
  136. // note: '我是安卓升级说明<br>我是安卓升级说明我是安卓升级说明',
  137. // download_url: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-75149e67-2f9b-4973-bdb4-b3781786fe84/3bec1254-6778-4d91-83dd-1ac206adbf74.apk', //下载地址 (微信测试)
  138. // };
  139. // vm.update_info = data;
  140. // if (!vm.update_info.os) {
  141. // // 后台未配置当前系统的升级数据
  142. // } else {
  143. // console.log('后台返回的当前系统的升级参数', vm.update_info);
  144. // vm.checkUpdate(); ///检查是否更新
  145. // }
  146. // }
  147. // });
  148. },
  149. // 检查是否更新
  150. checkUpdate() {
  151. vm.need_update = vm.compareVersion(vm.version, vm.update_info.versionno); // 检查是否需要升级
  152. if (vm.need_update) {
  153. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  154. if (vm.tabbar) {
  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. }
  167. }
  168. },
  169. // 取消更新
  170. closeUpdate() {
  171. if (vm.force) {
  172. // 强制更新,取消退出app
  173. plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication()
  174. .performSelector(
  175. "exit");
  176. } else {
  177. vm.popup_show = false; //关闭升级弹窗
  178. if (vm.viewObj) vm.viewObj.hide() //隐藏原生遮罩
  179. }
  180. },
  181. // 立即更新
  182. nowUpdate() {
  183. if (vm.downing) return false; //如果正在下载就停止操作
  184. vm.downing = true; //状态改变 正在下载中
  185. if (/\.apk$/.test(vm.update_info.path)) {
  186. // 如果是apk地址
  187. vm.download_wgt() // 安装包/升级包更新
  188. } else if (/\.wgt$/.test(vm.update_info.path)) {
  189. // 如果是更新包
  190. vm.download_wgt() // 安装包/升级包更新
  191. } else {
  192. plus.runtime.openURL(vm.update_info.path, function() { //调用外部浏览器打开更新地址
  193. plus.nativeUI.toast("打开错误");
  194. });
  195. }
  196. },
  197. // 下载升级资源包
  198. download_wgt() {
  199. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  200. let options = {
  201. method: "get"
  202. };
  203. let dtask = plus.downloader.createDownload(vm.update_info.path, options, function(d, status) {
  204. });
  205. dtask.addEventListener("statechanged", function(task, status) {
  206. if (status === null) {} else if (status == 200) {
  207. vm.downstatus = task.state;
  208. switch (task.state) {
  209. case 3: // 已接收到数据
  210. vm.downSize = task.downloadedSize;
  211. if (task.totalSize) {
  212. vm.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  213. }
  214. break;
  215. case 4:
  216. vm.installWgt(task.filename); // 安装wgt包
  217. break;
  218. }
  219. } else {
  220. plus.nativeUI.closeWaiting();
  221. plus.nativeUI.toast("下载出错");
  222. vm.downing = false;
  223. vm.downstatus = 0;
  224. }
  225. });
  226. dtask.start();
  227. },
  228. // 安装文件
  229. installWgt(path) {
  230. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  231. plus.runtime.install(path, {}, function() {
  232. plus.nativeUI.closeWaiting();
  233. // 应用资源下载完成!
  234. plus.nativeUI.alert("应用资源下载完成!", function() {
  235. plus.runtime.restart();
  236. });
  237. }, function(e) {
  238. plus.nativeUI.closeWaiting();
  239. // 安装更新文件失败
  240. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  241. });
  242. },
  243. // 对比版本号
  244. compareVersion(ov, nv) {
  245. if (!ov || !nv || ov == "" || nv == "") {
  246. return false;
  247. }
  248. let b = false,
  249. ova = ov.split(".", 4),
  250. nva = nv.split(".", 4);
  251. for (let i = 0; i < ova.length && i < nva.length; i++) {
  252. let so = ova[i],
  253. no = parseInt(so),
  254. sn = nva[i],
  255. nn = parseInt(sn);
  256. if (nn > no || sn.length > so.length) {
  257. return true;
  258. } else if (nn < no) {
  259. return false;
  260. }
  261. }
  262. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  263. return true;
  264. } else {
  265. return false;
  266. }
  267. },
  268. }
  269. }
  270. </script>
  271. <style lang="scss" scoped>
  272. .popup-bg {
  273. display: flex;
  274. flex-direction: column;
  275. align-items: center;
  276. justify-content: center;
  277. position: fixed;
  278. top: 0;
  279. left: 0;
  280. right: 0;
  281. bottom: var(--window-bottom);
  282. width: 750rpx;
  283. background-color: rgba(0, 0, 0, 0.6);
  284. z-index: 99999999;
  285. }
  286. .popup-content {
  287. display: flex;
  288. flex-direction: column;
  289. align-items: center;
  290. }
  291. .popup-content-show {
  292. animation: mymove 500ms;
  293. transform: scale(1);
  294. }
  295. @keyframes mymove {
  296. 0% {
  297. transform: scale(0);
  298. /*开始为原始大小*/
  299. }
  300. 100% {
  301. transform: scale(1);
  302. }
  303. }
  304. .update-wrap {
  305. width: 580rpx;
  306. border-radius: 18rpx;
  307. position: relative;
  308. display: flex;
  309. flex-direction: column;
  310. background-color: #ffffff;
  311. padding: 170rpx 30rpx 0;
  312. .top-img {
  313. position: absolute;
  314. left: 0;
  315. width: 100%;
  316. height: 256rpx;
  317. top: -128rpx;
  318. }
  319. .content {
  320. display: flex;
  321. flex-direction: column;
  322. align-items: center;
  323. padding-bottom: 40rpx;
  324. .title {
  325. font-size: 32rpx;
  326. font-weight: bold;
  327. color: #6526f3;
  328. }
  329. .title-sub {
  330. text-align: center;
  331. font-size: 24rpx;
  332. color: #666666;
  333. padding-top: 20rpx;
  334. }
  335. .btn {
  336. width: 460rpx;
  337. display: flex;
  338. align-items: center;
  339. justify-content: center;
  340. color: #ffffff;
  341. font-size: 30rpx;
  342. height: 80rpx;
  343. line-height: 80rpx;
  344. border-radius: 100px;
  345. background-color: #6526f3;
  346. margin-top: 20rpx;
  347. }
  348. }
  349. }
  350. .close-ioc {
  351. width: 70rpx;
  352. height: 70rpx;
  353. margin-top: 30rpx;
  354. }
  355. .sche-wrap {
  356. display: flex;
  357. flex-direction: column;
  358. align-items: center;
  359. justify-content: flex-end;
  360. padding: 10rpx 50rpx 0;
  361. .sche-wrap-text {
  362. font-size: 24rpx;
  363. color: #666;
  364. margin-bottom: 20rpx;
  365. }
  366. .sche-bg {
  367. position: relative;
  368. background-color: #cccccc;
  369. height: 30rpx;
  370. border-radius: 100px;
  371. width: 480rpx;
  372. display: flex;
  373. align-items: center;
  374. .sche-bg-jindu {
  375. position: absolute;
  376. left: 0;
  377. top: 0;
  378. height: 30rpx;
  379. min-width: 40rpx;
  380. border-radius: 100px;
  381. background: url(images/round.png) #5775e7 center right 4rpx no-repeat;
  382. background-size: 26rpx 26rpx;
  383. }
  384. }
  385. .down-text {
  386. font-size: 24rpx;
  387. color: #5674e5;
  388. margin-top: 16rpx;
  389. }
  390. }
  391. </style>