app-update.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <view class="wrap">
  3. <view class="popup-bg" :class="{'popup-show' : popup_show}" :style="getHeight">
  4. <view class="update-wrap">
  5. <view class="update-content">
  6. <view class="update-con-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="downstatus < 1">更新内容:<br>{{update_info.note}}</text>
  12. <text class="current-version" v-if="downstatus < 1">当前版本:V{{version}}</text>
  13. <view class="update-btn" v-if="downstatus < 1">
  14. <view class="btn-item" @click="closeUpdate" v-if="update_info.minimum_Version === version">
  15. <!-- 残忍拒绝 -->
  16. <text class="item-text ">残忍拒绝</text>
  17. </view>
  18. <view class="btn-item" @click="nowUpdate">
  19. <!-- 立即升级 -->
  20. <text class="item-text text-bule">立即升级</text>
  21. </view>
  22. </view>
  23. <!-- 下载进度 -->
  24. <view class="sche-wrap" v-else>
  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. <view class="updata-bg-img"></view>
  38. <view class="updata-bg-color"></view>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. let vm;
  45. export default {
  46. name: "appUpdate",
  47. //@是否强制更新
  48. props: {
  49. force: {
  50. type: Boolean,
  51. default: true, //true强制更新 false非强制更新
  52. },
  53. tabbar: {
  54. type: Boolean,
  55. default: false, //是否有原生tabbar组件
  56. }
  57. },
  58. data() {
  59. return {
  60. popup_show: false, //弹窗是否显示
  61. platform: "", //ios or android
  62. version: "1.0.0", //当前软件版本
  63. need_update: false, // 是否更新
  64. downing: false, //是否下载中
  65. downstatus: 0, //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  66. update_info: {
  67. os: '', //设备系统
  68. version: '', //最新版本
  69. note: '', //升级说明
  70. },
  71. schedule: 0, //下载进度宽度
  72. downloadedSize: 0, //下载大小
  73. viewObj: null, //原生遮罩view
  74. };
  75. },
  76. created() {
  77. vm = this;
  78. },
  79. computed: {
  80. // 下载进度计算
  81. lengthWidth: function() {
  82. return {
  83. width: vm.schedule * 480 / 100 + "rpx"
  84. }
  85. },
  86. getHeight: function() {
  87. let bottom = 0;
  88. if (vm.tabbar) {
  89. bottom = 50;
  90. }
  91. return {
  92. "bottom": bottom + 'px'
  93. }
  94. }
  95. },
  96. methods: {
  97. // 检查更新
  98. update() {
  99. console.log(111)
  100. if (vm.viewObj) vm.viewObj.hide() //隐藏原生遮罩
  101. // console.log('父组件触发方法');
  102. // #ifdef APP-PLUS
  103. // 获取手机系统信息
  104. uni.getSystemInfo({
  105. success: function(res) {
  106. vm.platform = res.platform //ios or android
  107. // console.log('手机系统信息', vm.platform);
  108. }
  109. });
  110. // 获取版本号
  111. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  112. vm.version = inf.version
  113. });
  114. // console.log('当前版本', vm.version);
  115. vm.getUpdateInfo(); //获取更新信息
  116. // #endif
  117. },
  118. // 获取线上版本信息
  119. getUpdateInfo() {
  120. //向后台发起请求,获取最新版本号
  121. let integer = ''
  122. switch (uni.getSystemInfoSync().platform) {
  123. case 'android':
  124. integer = '1'
  125. break;
  126. case 'ios':
  127. integer = '2'
  128. break;
  129. default:
  130. console.log('运行在其他设备上')
  131. break;
  132. }
  133. vm.$u.get('/appVersion/version/getAppVersion', {
  134. integer: integer
  135. }).then(res => {
  136. // console.log(res)
  137. // 这里的返回的数据跟后台约定
  138. vm.update_info = {
  139. os: uni.getSystemInfoSync().platform,
  140. version: res.data.version, //最新版本
  141. note: res.data.plus_msg, //升级提示
  142. download_url: res.data.url, //下载地址
  143. minimum_Version: res.data.minimum_Version //最低版本
  144. };
  145. // 循环获取当前设备对应的更新数据
  146. // for (let i = 0; i < data.length; i++) {
  147. // if (vm.platform == data[i]['os']) {
  148. // vm.update_info = data[i];
  149. // console.log(data[i])
  150. // break;
  151. // }
  152. // }
  153. if (!vm.update_info.os) {
  154. // 后台未配置当前系统的升级数据
  155. } else {
  156. // console.log('后台返回的当前系统的升级参数', vm.update_info);
  157. vm.checkUpdate(); ///检查是否更新
  158. }
  159. })
  160. },
  161. // 检查是否更新
  162. checkUpdate() {
  163. vm.need_update = vm.compareVersion(vm.version, vm.update_info.version); // 检查是否需要升级
  164. // console.log(vm.need_update)
  165. if (vm.need_update) {
  166. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  167. if (vm.tabbar) {
  168. //页面是否有原生tabbar组件
  169. // 创建原生view用来遮罩tabbar的点击事件 (如果是没有用原生的tabbar这一步可以取消)
  170. vm.viewObj = new plus.nativeObj.View('viewObj', {
  171. bottom: '0px',
  172. left: '0px',
  173. height: '50px',
  174. width: '100%',
  175. backgroundColor: "rgba(0,0,0,.6)"
  176. });
  177. vm.viewObj.show() //显示原生遮罩
  178. }
  179. }
  180. },
  181. // 取消更新
  182. closeUpdate() {
  183. if (vm.force) {
  184. // 强制更新,取消退出app
  185. plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication()
  186. .performSelector(
  187. "exit");
  188. } else {
  189. vm.popup_show = false; //关闭升级弹窗
  190. if (vm.tabbar) vm.viewObj.hide() //隐藏原生遮罩
  191. }
  192. },
  193. // 立即更新
  194. nowUpdate() {
  195. if (vm.downing) {
  196. return false //如果正在下载就停止操作
  197. } else {
  198. vm.downing = true; //状态改变 正在下载中
  199. }
  200. if (/\.apk$/.test(vm.update_info.download_url)) {
  201. // 如果是apk地址
  202. vm.download_wgt() // 安装包/升级包更新
  203. } else if (/\.wgt$/.test(vm.update_info.download_url)) {
  204. // 如果是更新包
  205. vm.download_wgt() // 安装包/升级包更新
  206. } else {
  207. plus.runtime.openURL(vm.update_info.download_url, 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.download_url, options, function(d, status) {
  219. });
  220. dtask.addEventListener("statechanged", function(task, status) {
  221. if (status === null) {} else if (status == 200) {
  222. //在这里打印会不停的执行,请注意,正式上线切记不要在这里打印东西///////////////////////////////////////////////////
  223. vm.downstatus = task.state;
  224. switch (task.state) {
  225. case 3: // 已接收到数据
  226. vm.downloadedSize = task.downloadedSize;
  227. let totalSize = 0;
  228. if (task.totalSize) {
  229. totalSize = task.totalSize //服务器须返回正确的content-length才会有长度
  230. }
  231. vm.schedule = parseInt(100 * task.downloadedSize / totalSize);
  232. break;
  233. case 4:
  234. vm.installWgt(task.filename); // 安装wgt包
  235. break;
  236. }
  237. } else {
  238. plus.nativeUI.closeWaiting();
  239. plus.nativeUI.toast("下载出错");
  240. vm.downing = false;
  241. vm.downstatus = 0;
  242. }
  243. });
  244. dtask.start();
  245. },
  246. // 安装文件
  247. installWgt(path) {
  248. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  249. plus.runtime.install(path, {}, function() {
  250. plus.nativeUI.closeWaiting();
  251. // 应用资源下载完成!
  252. plus.nativeUI.alert("应用资源下载完成!", function() {
  253. plus.runtime.restart();
  254. });
  255. }, function(e) {
  256. plus.nativeUI.closeWaiting();
  257. // 安装更新文件失败
  258. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  259. });
  260. },
  261. // 对比版本号
  262. compareVersion(ov, nv) {
  263. if (!ov || !nv || ov == "" || nv == "") {
  264. return false;
  265. }
  266. let b = false,
  267. ova = ov.split(".", 4),
  268. nva = nv.split(".", 4);
  269. for (let i = 0; i < ova.length && i < nva.length; i++) {
  270. let so = ova[i],
  271. no = parseInt(so),
  272. sn = nva[i],
  273. nn = parseInt(sn);
  274. if (nn > no || sn.length > so.length) {
  275. return true;
  276. } else if (nn < no) {
  277. return false;
  278. }
  279. }
  280. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  281. return true;
  282. } else {
  283. return false;
  284. }
  285. },
  286. }
  287. }
  288. </script>
  289. <style lang="scss" scoped>
  290. .popup-bg {
  291. display: flex;
  292. align-items: center;
  293. justify-content: center;
  294. position: fixed;
  295. z-index: -9999;
  296. opacity: 0;
  297. top: 0;
  298. left: 0;
  299. right: 0;
  300. bottom: 0;
  301. height: 100%;
  302. width: 100%;
  303. background-color: rgba(0, 0, 0, .6);
  304. transition: opacity 300ms;
  305. }
  306. .popup-show {
  307. z-index: 9999;
  308. opacity: 1;
  309. }
  310. .update-wrap {
  311. width: 580rpx;
  312. border-radius: 10rpx;
  313. position: relative;
  314. display: flex;
  315. flex-direction: column;
  316. overflow: hidden;
  317. .updata-bg-img {
  318. position: absolute;
  319. top: 0vw;
  320. left: 0px;
  321. width: 580rpx;
  322. height: 440rpx;
  323. background: url(images/update-img.png) no-repeat;
  324. background-size: 100% 100%;
  325. z-index: 1;
  326. }
  327. .updata-bg-color {
  328. position: absolute;
  329. width: 580rpx;
  330. top: 10vw;
  331. height: calc(100% - 10vw);
  332. background-color: #ffffff;
  333. z-index: 0;
  334. }
  335. .update-content {
  336. position: relative;
  337. z-index: 2;
  338. display: flex;
  339. flex-direction: column;
  340. }
  341. .update-con-top {
  342. padding: 70rpx 50rpx;
  343. }
  344. .update-btn {
  345. height: 80rpx;
  346. display: flex;
  347. align-items: center;
  348. border-top: 1px solid #e7e7e7;
  349. margin-top: 20rpx;
  350. .btn-item {
  351. display: flex;
  352. align-items: center;
  353. justify-content: center;
  354. flex: 1;
  355. .item-text {
  356. text-align: center;
  357. font-size: 28rpx;
  358. color: #666;
  359. }
  360. .text-bule {
  361. color: #045FCF;
  362. }
  363. }
  364. .btn-item:first-child {
  365. border-right: 1px solid #e7e7e7
  366. }
  367. }
  368. .update-top-title {
  369. color: #fff;
  370. font-size: 34rpx;
  371. font-weight: bold;
  372. }
  373. .update-top-version {
  374. color: #fff;
  375. font-size: 28rpx;
  376. margin-top: 10rpx;
  377. }
  378. .uodate-content {
  379. color: #333;
  380. font-size: 30rpx;
  381. padding: 0px 50rpx;
  382. margin-top: 120rpx;
  383. }
  384. .current-version {
  385. text-align: center;
  386. margin-top: 20rpx;
  387. font-size: 24rpx;
  388. color: #666;
  389. margin-bottom: 16rpx;
  390. }
  391. }
  392. .sche-wrap {
  393. display: flex;
  394. flex-direction: column;
  395. align-items: center;
  396. justify-content: flex-end;
  397. padding: 160rpx 50rpx 40rpx;
  398. .sche-wrap-text {
  399. font-size: 24rpx;
  400. color: #666;
  401. margin-bottom: 20rpx;
  402. }
  403. .sche-bg {
  404. position: relative;
  405. background-color: #ccc;
  406. height: 20rpx;
  407. border-radius: 100px;
  408. width: 480rpx;
  409. display: flex;
  410. align-items: center;
  411. margin-bottom: 30rpx;
  412. }
  413. .sche-bg-jindu {
  414. position: absolute;
  415. left: 0;
  416. top: 0;
  417. height: 20rpx;
  418. background-color: #5775e7;
  419. border-radius: 100px;
  420. .sche-bg-round {
  421. position: absolute;
  422. left: 100%;
  423. height: 24rpx;
  424. width: 24rpx;
  425. background-color: #fff;
  426. border-color: #fff;
  427. border-style: solid;
  428. border-width: 10rpx;
  429. border-radius: 100px;
  430. transform: translateX(-20rpx) translateY(-10rpx);
  431. box-shadow: 0 0 4px #eaeaea;
  432. .sche-bg-round-text {
  433. position: relative;
  434. top: 24rpx;
  435. font-size: 24rpx;
  436. text-align: center;
  437. color: #5775e7;
  438. }
  439. }
  440. }
  441. }
  442. </style>