app-update.vue 13 KB

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