luo-version-upgrade.vue 8.5 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" @click="downUpdate()">立即更新</text>
  13. </view>
  14. <view class="versionUpgradeViewIs_force0" v-if="is_force==true&&start==false">
  15. <text class="downloadIs_force1" @click="downUpdate()">立即更新</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. console.log('用户点击确定');
  106. } else if (res.cancel) {
  107. console.log('用户点击取消');
  108. }
  109. }
  110. })
  111. // plus.nativeUI.alert('无法进行在线升级,请到AppStore进行升级..', '提示');
  112. return;
  113. }
  114. if (this.versionType == 'android' && this.versionTypes == 'android') {
  115. this.update();
  116. }
  117. },
  118. // 升级检测
  119. update: function() {
  120. let self = this;
  121. /* 5+环境锁定屏幕方向 */
  122. plus.screen.lockOrientation('portrait-primary'); //锁定
  123. //客户端信息
  124. plus.runtime.getProperty(plus.runtime.appid, function(res) {
  125. let isupdate = self.isUpdate(res.version, self.version);
  126. if (isupdate) {
  127. self.versionShow();
  128. return;
  129. }
  130. })
  131. },
  132. // * 比较版本大小,如果新版本nv大于旧版本ov则返回true,否则返回false
  133. // * @param {String} ov
  134. // * @param {String} nv
  135. // * @return {Boolean}
  136. isUpdate: function(ov, nv) {
  137. if (!ov || !nv || ov == "" || nv == "") {
  138. return false;
  139. }
  140. var b = false,
  141. ova = ov.replace("V", "").replace("v", "").replace(" ", '').split(".", 4),
  142. nva = nv.replace("V", "").replace("v", "").replace(" ", '').split(".", 4);
  143. for (var i = 0; i < ova.length && i < nva.length; i++) {
  144. var so = ova[i],
  145. no = parseInt(so),
  146. sn = nva[i],
  147. nn = parseInt(sn);
  148. if (nn > no || sn.length > so.length) {
  149. return true;
  150. } else if (nn < no) {
  151. return false;
  152. }
  153. }
  154. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. },
  160. /* 获取版本类别 android/ios */
  161. getPlatform: function() {
  162. return uni.getSystemInfoSync().platform.toLowerCase();
  163. },
  164. /* 开启升级 */
  165. versionShow: function() {
  166. this.show = true;
  167. uni.hideTabBar();
  168. },
  169. /* 取消升级 */
  170. close: function() {
  171. this.show = false;
  172. uni.showTabBar();
  173. },
  174. downUpdate: function() {
  175. //平台判断
  176. if (this.versionTypes == 'ios') {
  177. plus.nativeUI.alert('无法进行在线升级,请到AppStore进行升级..', '提示');
  178. this.close();
  179. return;
  180. }
  181. //检测是否下载过更新
  182. let self = this;
  183. self.installPath = uni.getStorageSync('update_path');
  184. plus.io.resolveLocalFileSystemURL(
  185. self.installPath,
  186. function(entry) {
  187. self.install();
  188. return;
  189. },
  190. function(err) {
  191. const dtask = plus.downloader.createDownload(self.url, {
  192. filename: '_downloads/'
  193. });
  194. dtask.addEventListener("statechanged", onStateChanged, false);
  195. dtask.start();
  196. }
  197. );
  198. //下载状态
  199. function onStateChanged(download, status) {
  200. let totalSize = download.totalSize;
  201. let useSize = download.downloadedSize;
  202. if(totalSize){
  203. self.start = (useSize / totalSize * 100).toFixed(0);
  204. }
  205. // 下载完成
  206. if (download.state == 4 && status == 200) {
  207. self.installPath = download.filename;
  208. uni.setStorageSync('update_path', self.installPath);
  209. //需要用户点击立即安装才能安装去掉注释掉self.install();
  210. self.install();
  211. }
  212. }
  213. },
  214. //安装提示
  215. install: function() {
  216. this.close();
  217. plus.nativeUI.showWaiting('安装中,请稍后...');
  218. plus.runtime.install(this.installPath, {}, (res) => {
  219. plus.nativeUI.closeWaiting();
  220. plus.runtime.restart();
  221. uni.removeStorageSync("update_path");
  222. }, (err) => {
  223. plus.nativeUI.closeWaiting();
  224. plus.nativeUI.alert('error ' + err.code, '', '提示');
  225. });
  226. }
  227. }
  228. }
  229. </script>
  230. <style lang="scss" scoped>
  231. .versionUpgrade {
  232. width: 100%;
  233. height: 100%;
  234. position: fixed;
  235. top: 0;
  236. left: 0;
  237. right: 0;
  238. bottom: 0;
  239. z-index: 999999;
  240. background: rgba(0, 0, 0, 0.3);
  241. .versionUpgradeView {
  242. z-index: 9999999;
  243. margin: 500rpx 60rpx 0 60rpx;
  244. min-height: 200rpx;
  245. max-height: 800rpx;
  246. background: #FFFFFF;
  247. border-radius: 30rpx;
  248. display: flex;
  249. flex-direction: column !important;
  250. //padding: 30rpx;
  251. overflow: hidden;
  252. .versionUpgradeViewV1 {
  253. display: flex;
  254. flex-direction: row !important;
  255. justify-content: center;
  256. align-items: center;
  257. background: #0094FE;
  258. padding: 10rpx 0;
  259. color: #FFFFFF;
  260. .img {
  261. width: 60rpx;
  262. height: 60rpx;
  263. }
  264. .v-text {
  265. font-size: 16px;
  266. margin-left: 10rpx;
  267. }
  268. }
  269. .versionUpgradeViewV2 {
  270. display: flex;
  271. flex-direction: column !important;
  272. padding: 30upx;
  273. .content-text {
  274. font-size: 14px;
  275. color: #626262;
  276. }
  277. .versionUpgradeViewIs_force0 {
  278. display: flex;
  279. flex-direction: row !important;
  280. justify-content: space-between;
  281. padding: 30rpx 0 0 0;
  282. .text {
  283. padding: 10rpx 70rpx;
  284. border-radius: 30rpx;
  285. font-size: 16px;
  286. color: #FFFFFF;
  287. }
  288. .butClose {
  289. background: #e2e2e2;
  290. }
  291. .download {
  292. background: #0094FE;
  293. }
  294. .downloadIs_force1 {
  295. width: 100%;
  296. display: flex;
  297. justify-content: center;
  298. align-items: center;
  299. padding: 10rpx 0;
  300. border-radius: 30rpx;
  301. font-size: 16px;
  302. color: #FFFFFF;
  303. background: #0094FE;
  304. }
  305. }
  306. .versionUpgradeViewStartTrue {
  307. display: flex;
  308. flex-direction: column !important;
  309. padding: 30rpx 0 0 0;
  310. .title {
  311. font-size: 14px;
  312. color: #626262;
  313. padding: 10rpx 0;
  314. }
  315. .btn-text {
  316. width: 100%;
  317. display: flex;
  318. justify-content: center;
  319. align-items: center;
  320. padding: 10rpx 0;
  321. border-radius: 30rpx;
  322. font-size: 16px;
  323. color: #FFFFFF;
  324. }
  325. .downloadStartTrue {
  326. background: #0094FE;
  327. }
  328. .downloadStartFalse {
  329. background: #e2e2e2;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. </style>