luo-version-upgrade.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <view class="versionUpgrade" v-if="show">
  3. <view class="versionUpgradeView">
  4. <view class="versionUpgradeViewV1">
  5. <image class="img" src="./icon/upgrade.png" mode="aspectFill"></image>
  6. <text class="v-text">发现新版本啦!</text>
  7. </view>
  8. <view class="versionUpgradeViewV2">
  9. <text class="content-text">{{describe}}</text>
  10. <view class="versionUpgradeViewIs_force0" v-if="is_force==false&&start==false">
  11. <text class="text butClose" @click="close">下次在说</text>
  12. <text class="text download" @tap="$u.throttle(downUpdate, 2500)">立即更新</text>
  13. </view>
  14. <view class="versionUpgradeViewIs_force0" v-if="is_force==true&&start==false">
  15. <text class="downloadIs_force1" @tap="$u.throttle(downUpdate, 2500)">立即更新</text>
  16. </view>
  17. <view class="versionUpgradeViewStartTrue" v-if="start!=false">
  18. <text class="title">下载进度</text>
  19. <progress :percent="start" activeColor="#0094FE" :show-info="true" stroke-width="6"></progress>
  20. </view>
  21. <view class="versionUpgradeViewStartTrue" v-if="start!=false">
  22. <text class="btn-text downloadStartTrue" v-if="start>=100" @click="install()">立即安装</text>
  23. <text class="btn-text downloadStartFalse" v-else>立即安装</text>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. name: 'luo-version-upgrade',
  32. props: {
  33. version: {
  34. type: String,
  35. default () {
  36. return '1.0.0';
  37. }
  38. },
  39. versionType: {
  40. type: String,
  41. default () {
  42. return 'android';
  43. }
  44. },
  45. url: {
  46. type: String,
  47. default () {
  48. return '';
  49. }
  50. },
  51. describe: {
  52. type: String,
  53. default () {
  54. return `
  55. 1. 修复badge组件的size参数无效问题
  56. 2. 新增Modal模态框组件
  57. 3. 新增压窗屏组件,可以在APP上以弹窗的形式遮盖导航栏和底部tabbar
  58. 4. 修复键盘组件在微信小程序上遮罩无效的问题`
  59. }
  60. },
  61. is_force: {
  62. type: Boolean,
  63. default () {
  64. return false;
  65. }
  66. }
  67. },
  68. data() {
  69. return {
  70. show: false,
  71. start: false,
  72. };
  73. },
  74. watch: {
  75. 'version': {
  76. handler(newVal) {
  77. if (newVal != '1.0.0') {
  78. this.getVerison();
  79. }
  80. },
  81. deep: true,
  82. immediate: true,
  83. }
  84. },
  85. computed: {
  86. versionTypes() {
  87. return this.getPlatform(); //版本获取
  88. }
  89. },
  90. methods: {
  91. /* 版本比较是否需要更新 */
  92. getVerison: function() {
  93. if (this.version == '1.0.0' || this.url == '') {
  94. return;
  95. }
  96. //ios端判断
  97. if (this.versionType == 'ios' || this.versionTypes == 'ios') {
  98. uni.showModal({
  99. title: "更新提醒",
  100. content: "无法进行在线升级,请到AppStore进行升级..",
  101. showCancel: false,
  102. confirmText: "立即更新",
  103. success: function(res) {
  104. if (res.confirm) {
  105. } else if (res.cancel) {
  106. }
  107. }
  108. })
  109. // plus.nativeUI.alert('无法进行在线升级,请到AppStore进行升级..', '提示');
  110. return;
  111. }
  112. if (this.versionType == 'android' && this.versionTypes == 'android') {
  113. this.update();
  114. }
  115. },
  116. // 升级检测
  117. update: function() {
  118. let self = this;
  119. /* 5+环境锁定屏幕方向 */
  120. plus.screen.lockOrientation('portrait-primary'); //锁定
  121. //客户端信息
  122. plus.runtime.getProperty(plus.runtime.appid, function(res) {
  123. let isupdate = self.isUpdate(res.version, self.version);
  124. if (isupdate) {
  125. self.versionShow();
  126. return;
  127. }
  128. })
  129. },
  130. // * 比较版本大小,如果新版本nv大于旧版本ov则返回true,否则返回false
  131. // * @param {String} ov
  132. // * @param {String} nv
  133. // * @return {Boolean}
  134. isUpdate: function(ov, nv) {
  135. if (!ov || !nv || ov == "" || nv == "") {
  136. return false;
  137. }
  138. var b = false,
  139. ova = ov.replace("V", "").replace("v", "").replace(" ", '').split(".", 4),
  140. nva = nv.replace("V", "").replace("v", "").replace(" ", '').split(".", 4);
  141. for (var i = 0; i < ova.length && i < nva.length; i++) {
  142. var so = ova[i],
  143. no = parseInt(so),
  144. sn = nva[i],
  145. nn = parseInt(sn);
  146. if (nn > no || sn.length > so.length) {
  147. return true;
  148. } else if (nn < no) {
  149. return false;
  150. }
  151. }
  152. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. },
  158. /* 获取版本类别 android/ios */
  159. getPlatform: function() {
  160. return uni.getSystemInfoSync().platform.toLowerCase();
  161. },
  162. /* 开启升级 */
  163. versionShow: function() {
  164. this.show = true;
  165. uni.hideTabBar();
  166. },
  167. /* 取消升级 */
  168. close: function() {
  169. this.show = false;
  170. uni.showTabBar();
  171. },
  172. downUpdate: function() {
  173. //平台判断
  174. if (this.versionTypes == 'ios') {
  175. plus.nativeUI.alert('无法进行在线升级,请到AppStore进行升级..', '提示');
  176. this.close();
  177. return;
  178. }
  179. //检测是否下载过更新
  180. let self = this;
  181. self.installPath = uni.getStorageSync('update_path');
  182. plus.io.resolveLocalFileSystemURL(
  183. self.installPath,
  184. function(entry) {
  185. self.install();
  186. return;
  187. },
  188. function(err) {
  189. const dtask = plus.downloader.createDownload(self.url, {
  190. filename: '_downloads/'
  191. });
  192. dtask.addEventListener("statechanged", onStateChanged, false);
  193. dtask.start();
  194. }
  195. );
  196. //下载状态
  197. function onStateChanged(download, status) {
  198. let totalSize = download.totalSize;
  199. let useSize = download.downloadedSize;
  200. if (totalSize) {
  201. self.start = (useSize / totalSize * 100).toFixed(0);
  202. }
  203. // 下载完成
  204. if (download.state == 4 && status == 200) {
  205. self.installPath = download.filename;
  206. uni.setStorageSync('update_path', self.installPath);
  207. //需要用户点击立即安装才能安装去掉注释掉self.install();
  208. self.install();
  209. }
  210. }
  211. },
  212. //安装提示
  213. install: function() {
  214. this.close();
  215. plus.nativeUI.showWaiting('安装中,请稍后...');
  216. plus.runtime.install(this.installPath, {}, (res) => {
  217. plus.nativeUI.closeWaiting();
  218. plus.runtime.restart();
  219. uni.removeStorageSync("update_path");
  220. }, (err) => {
  221. plus.nativeUI.closeWaiting();
  222. plus.nativeUI.alert('error ' + err.code, '', '提示');
  223. });
  224. }
  225. }
  226. }
  227. </script>
  228. <style lang="scss" scoped>
  229. .versionUpgrade {
  230. width: 100%;
  231. height: 100%;
  232. position: fixed;
  233. top: 0;
  234. left: 0;
  235. right: 0;
  236. bottom: 0;
  237. z-index: 999999;
  238. background: rgba(0, 0, 0, 0.3);
  239. .versionUpgradeView {
  240. z-index: 9999999;
  241. margin: 500rpx 60rpx 0 60rpx;
  242. min-height: 200rpx;
  243. max-height: 800rpx;
  244. background: #FFFFFF;
  245. border-radius: 30rpx;
  246. display: flex;
  247. flex-direction: column !important;
  248. //padding: 30rpx;
  249. overflow: hidden;
  250. .versionUpgradeViewV1 {
  251. display: flex;
  252. flex-direction: row !important;
  253. justify-content: center;
  254. align-items: center;
  255. background: #0094FE;
  256. padding: 10rpx 0;
  257. color: #FFFFFF;
  258. .img {
  259. width: 60rpx;
  260. height: 60rpx;
  261. }
  262. .v-text {
  263. font-size: 16px;
  264. margin-left: 10rpx;
  265. }
  266. }
  267. .versionUpgradeViewV2 {
  268. display: flex;
  269. flex-direction: column !important;
  270. padding: 30upx;
  271. .content-text {
  272. font-size: 14px;
  273. color: #626262;
  274. }
  275. .versionUpgradeViewIs_force0 {
  276. display: flex;
  277. flex-direction: row !important;
  278. justify-content: space-between;
  279. padding: 30rpx 0 0 0;
  280. .text {
  281. padding: 10rpx 70rpx;
  282. border-radius: 30rpx;
  283. font-size: 16px;
  284. color: #FFFFFF;
  285. }
  286. .butClose {
  287. background: #e2e2e2;
  288. }
  289. .download {
  290. background: #0094FE;
  291. }
  292. .downloadIs_force1 {
  293. width: 100%;
  294. display: flex;
  295. justify-content: center;
  296. align-items: center;
  297. padding: 10rpx 0;
  298. border-radius: 30rpx;
  299. font-size: 16px;
  300. color: #FFFFFF;
  301. background: #0094FE;
  302. }
  303. }
  304. .versionUpgradeViewStartTrue {
  305. display: flex;
  306. flex-direction: column !important;
  307. padding: 30rpx 0 0 0;
  308. .title {
  309. font-size: 14px;
  310. color: #626262;
  311. padding: 10rpx 0;
  312. }
  313. .btn-text {
  314. width: 100%;
  315. display: flex;
  316. justify-content: center;
  317. align-items: center;
  318. padding: 10rpx 0;
  319. border-radius: 30rpx;
  320. font-size: 16px;
  321. color: #FFFFFF;
  322. }
  323. .downloadStartTrue {
  324. background: #0094FE;
  325. }
  326. .downloadStartFalse {
  327. background: #e2e2e2;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. </style>