app-update.vue 9.9 KB

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