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