app-update.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <view class="page-height">
  3. <view class="page-content">
  4. <view class="wrap" v-if="popup_show">
  5. <view class="popup-bg">
  6. <view class="popup-content" :class="{'popup-content-show' : popup_show}">
  7. <view class="update-wrap">
  8. <image :src="`${$IMG_URL}/static/common/img.png`" class="top-img"></image>
  9. <view class="content">
  10. <text class="title">发现新版本V{{update_info.version}}</text>
  11. <!-- 升级描述 -->
  12. <view class="title-sub" v-html="update_info.note"></view>
  13. <!-- 升级按钮 -->
  14. <button class="btn" v-if="downstatus < 1" @click="onUpdate()">立即升级</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/{{(fileSize/1024/1024).toFixed(2)}}M</text>
  22. </view>
  23. </view>
  24. </view>
  25. <image :src="`${$IMG_URL}/static/common/close.png`" class="close-ioc" @click="closeUpdate()" v-if="downstatus < 1 && update_info.force == 0"></image>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. popup_show: true,
  37. update_info: null, //上一页面传过来的升级参数
  38. note: [], //升级说明数组格式
  39. fileSize: 0, //文件大小
  40. downSize: 0, //已下载大小
  41. downing: false, //是否下载中
  42. downstatus: 0, //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  43. }
  44. },
  45. onLoad() {
  46. const option = this.$Route.query
  47. if (option.updata_info) {
  48. this.update_info = option.updata_info.version ? option.updata_info : JSON.parse(option.updata_info)
  49. this.note = this.update_info.note.split("\n"); //版本说明
  50. } else {
  51. plus.nativeUI.toast("参数出错");
  52. setTimeout(() => {
  53. uni.navigateBack()
  54. }, 500)
  55. }
  56. },
  57. onBackPress(e) {
  58. if (e.from == "backbutton") return true; //APP安卓物理返回键逻辑
  59. },
  60. computed: {
  61. // 下载进度计算
  62. lengthWidth: function() {
  63. let w = this.downSize / this.fileSize * 100;
  64. if (!w) {
  65. w = 0
  66. } else {
  67. w = w.toFixed(2)
  68. }
  69. return {
  70. width: w + "%" //return 宽度半分比
  71. }
  72. },
  73. getHeight: function() {
  74. let bottom = 0;
  75. if (this.tabbar) {
  76. bottom = 50;
  77. }
  78. return {
  79. "bottom": bottom + 'px',
  80. "height": "auto"
  81. }
  82. }
  83. },
  84. methods: {
  85. // 当点击更新时
  86. onUpdate() {
  87. //判断是否为WIFI网络 并且是非强制更新
  88. if (this.update_info.net_check == 1 && this.update_info.net_check == 0) {
  89. //判断是否为wifi模式
  90. uni.getNetworkType({
  91. success: (res) => {
  92. if (res.networkType == "wifi") {
  93. this.startUpdate(); //开始更新
  94. } else {
  95. uni.showModal({
  96. title: '提示',
  97. content: '当前网络非WIFI,继续更新可能会产生流量,确认要更新吗?',
  98. success: (modal_res) => {
  99. if (modal_res.confirm) {
  100. this.startUpdate(); //开始更新
  101. }
  102. }
  103. });
  104. }
  105. }
  106. });
  107. } else {
  108. this.startUpdate(); //开始更新
  109. }
  110. },
  111. //开始更新
  112. startUpdate() {
  113. if (this.downing) return false; //如果正在下载就停止操作
  114. this.downing = true; //状态改变 正在下载中
  115. if (/\.apk$/.test(this.update_info.now_url)) {
  116. // 如果是apk地址
  117. this.download_wgt() // 安装包/升级包更新
  118. } else if (/\.wgt$/.test(this.update_info.now_url)) {
  119. // 如果是更新包
  120. this.download_wgt() // 安装包/升级包更新
  121. } else {
  122. plus.runtime.openURL(this.update_info.now_url, function() { //调用外部浏览器打开更新地址
  123. plus.nativeUI.toast("打开错误");
  124. });
  125. }
  126. },
  127. // 下载升级资源包
  128. download_wgt() {
  129. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  130. let options = {
  131. method: "get"
  132. };
  133. let dtask = plus.downloader.createDownload(this.update_info.now_url, options);
  134. dtask.addEventListener("statechanged", (task, status) => {
  135. if (status === null) {} else if (status == 200) {
  136. //在这里打印会不停的执行,请注意,正式上线切记不要在这里打印东西!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  137. this.downstatus = task.state;
  138. switch (task.state) {
  139. case 3: // 已接收到数据
  140. plus.nativeUI.closeWaiting();
  141. this.downSize = task.downloadedSize;
  142. if (task.totalSize) {
  143. this.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  144. }
  145. break;
  146. case 4:
  147. this.installWgt(task.filename); // 安装
  148. break;
  149. }
  150. } else {
  151. plus.nativeUI.closeWaiting();
  152. plus.nativeUI.toast("下载出错");
  153. this.downing = false;
  154. this.downstatus = 0;
  155. }
  156. });
  157. dtask.start();
  158. },
  159. // 安装文件
  160. installWgt(path) {
  161. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  162. plus.runtime.install(path, {}, function() {
  163. plus.nativeUI.closeWaiting();
  164. // 应用资源下载完成!
  165. plus.nativeUI.alert("更新完成,请重启APP!", function() {
  166. plus.runtime.restart(); //重启APP
  167. });
  168. }, function(e) {
  169. plus.nativeUI.closeWaiting();
  170. // 安装更新文件失败
  171. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  172. });
  173. },
  174. // 取消更新
  175. closeUpdate() {
  176. uni.setStorageSync("update_ignore", this.update_info.version);
  177. uni.navigateBack()
  178. },
  179. }
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. page {
  184. background: transparent;
  185. }
  186. .page-height {
  187. height: 100vh;
  188. display: flex;
  189. flex-direction: column;
  190. justify-content: center;
  191. align-items: center;
  192. background-color: rgba($color: #000000, $alpha: .7);
  193. }
  194. .popup-bg {
  195. display: flex;
  196. flex-direction: column;
  197. align-items: center;
  198. justify-content: center;
  199. position: fixed;
  200. top: 0;
  201. left: 0;
  202. right: 0;
  203. bottom: 0;
  204. width: 750rpx;
  205. }
  206. .popup-content {
  207. display: flex;
  208. flex-direction: column;
  209. align-items: center;
  210. }
  211. .popup-content-show {
  212. animation: mymove 300ms;
  213. transform: scale(1);
  214. }
  215. @keyframes mymove {
  216. 0% {
  217. transform: scale(0);
  218. /*开始为原始大小*/
  219. }
  220. 100% {
  221. transform: scale(1);
  222. }
  223. }
  224. .update-wrap {
  225. width: 580rpx;
  226. border-radius: 18rpx;
  227. position: relative;
  228. display: flex;
  229. flex-direction: column;
  230. background-color: #ffffff;
  231. padding: 170rpx 30rpx 0;
  232. .top-img {
  233. position: absolute;
  234. left: 0;
  235. width: 100%;
  236. height: 256rpx;
  237. top: -128rpx;
  238. }
  239. .content {
  240. display: flex;
  241. flex-direction: column;
  242. align-items: center;
  243. padding-bottom: 40rpx;
  244. .title {
  245. font-size: 32rpx;
  246. font-weight: bold;
  247. color: #6526f3;
  248. }
  249. .title-sub {
  250. text-align: center;
  251. font-size: 24rpx;
  252. color: #666666;
  253. padding: 30rpx 0;
  254. }
  255. .btn {
  256. width: 460rpx;
  257. display: flex;
  258. align-items: center;
  259. justify-content: center;
  260. color: #ffffff;
  261. font-size: 30rpx;
  262. height: 80rpx;
  263. line-height: 80rpx;
  264. border-radius: 100px;
  265. background-color: #6526f3;
  266. margin-top: 20rpx;
  267. }
  268. }
  269. }
  270. .close-ioc {
  271. width: 70rpx;
  272. height: 70rpx;
  273. margin-top: 30rpx;
  274. }
  275. .sche-wrap {
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. justify-content: flex-end;
  280. padding: 10rpx 50rpx 0;
  281. .sche-wrap-text {
  282. font-size: 24rpx;
  283. color: #666;
  284. margin-bottom: 20rpx;
  285. }
  286. .sche-bg {
  287. position: relative;
  288. background-color: #cccccc;
  289. height: 30rpx;
  290. border-radius: 100px;
  291. width: 480rpx;
  292. display: flex;
  293. align-items: center;
  294. .sche-bg-jindu {
  295. position: absolute;
  296. left: 0;
  297. top: 0;
  298. height: 30rpx;
  299. min-width: 40rpx;
  300. border-radius: 100px;
  301. background: url($IMG_URL+'/static/common/round.png') #5775e7 center right 4rpx no-repeat;
  302. background-size: 26rpx 26rpx;
  303. }
  304. }
  305. .down-text {
  306. font-size: 24rpx;
  307. color: #5674e5;
  308. margin-top: 16rpx;
  309. }
  310. }
  311. </style>