index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <view class="versionUpgrade" v-if="isShow && show" @touchmove.stop.prevent="() => {}">
  3. <view class="upgrade-wrapper">
  4. <view class="oa-version-upgrade" @tap.stop="discard">
  5. <view class="upgrade-top-img"><image :src="upgradeTop"></image></view>
  6. <view class="versionUpgradeViewV2">
  7. <view class="upgrade-info">
  8. <view class="content-title">{{ upgradeInfo.title }} {{ upgradeInfo.version }}</view>
  9. <rich-text class="content-text" :nodes="upgradeInfo.content.replace(/\n/g, '<br/>')"></rich-text>
  10. </view>
  11. <button
  12. class="confirm-btn"
  13. :class="'bg-' + themeColor.name"
  14. @click="downUpdate()"
  15. v-if="!is_force && !start"
  16. >
  17. 立即更新
  18. </button>
  19. <view
  20. class="versionUpgradeViewIs_force0"
  21. v-if="is_force && start"
  22. >
  23. <text class="downloadIs_force1" @click="downUpdate()">立即更新</text>
  24. </view>
  25. <view class="versionUpgradeViewStartTrue" v-if="start">
  26. <text class="title">下载进度</text>
  27. <progress
  28. :percent="start"
  29. :activeColor="themeColor.color"
  30. :show-info="true"
  31. stroke-width="6"
  32. ></progress>
  33. </view>
  34. <view class="versionUpgradeViewStartTrue" v-if="start >= 100">
  35. <button
  36. class="confirm-btn"
  37. :class="'bg-' + themeColor.name"
  38. @click="install()"
  39. >立即安装</button
  40. >
  41. </view>
  42. </view>
  43. </view>
  44. <view class="close-upgrade">
  45. <view class="line">
  46. <view class="close-upgrade-line"></view>
  47. </view>
  48. <text class="iconfont iconguanbi1" @tap="close"></text>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. /* eslint-disable */
  55. import { versionsIndex } from '@/api/basic';
  56. export default {
  57. name: 'oa-version-upgrade',
  58. props: {
  59. versionType: {
  60. type: String,
  61. default() {
  62. return 'android';
  63. }
  64. },
  65. isShow: {
  66. type: Boolean,
  67. default: true
  68. },
  69. is_force: {
  70. type: Boolean,
  71. default() {
  72. return false;
  73. }
  74. },
  75. isTipsShow: {
  76. type: Boolean,
  77. default() {
  78. return false;
  79. }
  80. }
  81. },
  82. data() {
  83. return {
  84. upgradeTop: this.$mAssetsPath.upgradeTop,
  85. url: '',
  86. show: false,
  87. start: 0,
  88. installPath: '',
  89. upgradeInfo: {}
  90. };
  91. },
  92. created() {
  93. this.getVersionsInfo(plus.runtime.version);
  94. },
  95. computed: {
  96. versionTypes() {
  97. return this.getPlatform(); //版本获取
  98. }
  99. },
  100. methods: {
  101. /* 版本比较是否需要更新 */
  102. async getVersionsInfo(version) {
  103. const type = this.versionType == 'android' && this.versionTypes == 'android' ? 1 : 2;
  104. await this.$http
  105. .get(`${versionsIndex}`, { type, version })
  106. .then(r => {
  107. this.upgradeInfo = r.data;
  108. if (!r.data && this.isTipsShow) {
  109. this.$emit('tip', true);
  110. return;
  111. }
  112. this.version = r.data.version;
  113. this.url = r.data.download_url;
  114. if (this.versionType == 'android' && this.versionTypes == 'android') {
  115. this.show = true;
  116. uni.hideTabBar();
  117. this.update();
  118. return;
  119. } else {
  120. if (!this.isTipsShow) return;
  121. uni.showModal({
  122. content: `发现新版本 [${this.upgradeInfo.version}], 是否跳转至App Store更新?`,
  123. success: confirmRes => {
  124. if (confirmRes.confirm) {
  125. let appleId = 1533769070;
  126. // eslint-disable-next-line
  127. plus.runtime.launchApplication({
  128. action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
  129. }, function(e) {
  130. this.$mHelper.log('Open system default browser failed: ' + e.message);
  131. });
  132. }
  133. }
  134. });
  135. }
  136. });
  137. },
  138. discard() {},
  139. // 升级检测
  140. update() {
  141. let self = this;
  142. /* 5+环境锁定屏幕方向 */
  143. plus.screen.lockOrientation('portrait-primary'); //锁定
  144. //客户端信息
  145. plus.runtime.getProperty(plus.runtime.appid, function(res) {
  146. let isupdate = self.isUpdate(res.version, self.version);
  147. if (isupdate) {
  148. self.versionShow();
  149. return;
  150. }
  151. });
  152. },
  153. /**
  154. * 比较版本大小,如果新版本nv大于旧版本ov则返回true,否则返回false
  155. * @param {String} ov
  156. * @param {String} nv
  157. * @return {Boolean}
  158. */
  159. isUpdate(ov, nv) {
  160. if (!ov || !nv || ov == '' || nv == '') {
  161. return false;
  162. }
  163. var b = false,
  164. ova = ov
  165. .replace('V', '')
  166. .replace('v', '')
  167. .replace(' ', '')
  168. .split('.', 4),
  169. nva = nv
  170. .replace('V', '')
  171. .replace('v', '')
  172. .replace(' ', '')
  173. .split('.', 4);
  174. for (var i = 0; i < ova.length && i < nva.length; i++) {
  175. var so = ova[i],
  176. no = parseInt(so),
  177. sn = nva[i],
  178. nn = parseInt(sn);
  179. if (nn > no || sn.length > so.length) {
  180. return true;
  181. } else if (nn < no) {
  182. return false;
  183. }
  184. }
  185. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  186. return true;
  187. } else {
  188. return false;
  189. }
  190. },
  191. /* 获取版本类别 android/ios */
  192. getPlatform() {
  193. return uni.getSystemInfoSync().platform.toLowerCase();
  194. },
  195. /* 开启升级 */
  196. versionShow() {
  197. this.show = true;
  198. },
  199. /* 取消升级 */
  200. close() {
  201. if (this.upgradeInfo.is_enforce.toString() === '1') {
  202. this.$mHelper.toast('本次更新无法取消');
  203. } else {
  204. uni.showTabBar();
  205. if (this.isTipsShow) {
  206. this.$emit('close');
  207. } else {
  208. this.show = false;
  209. }
  210. }
  211. },
  212. downUpdate() {
  213. //平台判断
  214. if (this.versionTypes == 'ios') {
  215. plus.nativeUI.alert('无法进行在线升级,请到AppStore进行升级..', '提示');
  216. this.close();
  217. return;
  218. }
  219. //检测是否下载过更新
  220. let self = this;
  221. self.installPath = uni.getStorageSync('update_path');
  222. plus.io.resolveLocalFileSystemURL(
  223. self.installPath,
  224. function(entry) {
  225. self.install();
  226. return;
  227. },
  228. function(err) {
  229. const dtask = plus.downloader.createDownload(self.url, {
  230. filename: '_downloads/'
  231. });
  232. dtask.addEventListener('statechanged', onStateChanged, false);
  233. dtask.start();
  234. }
  235. );
  236. //下载状态
  237. function onStateChanged(download, status) {
  238. let totalSize = download.totalSize;
  239. let useSize = download.downloadedSize;
  240. self.start = ((useSize / totalSize) * 100).toFixed(0);
  241. // 下载完成
  242. if (download.state == 4 && status == 200) {
  243. self.installPath = download.filename;
  244. uni.setStorageSync('update_path', self.installPath);
  245. //需要用户点击立即安装才能安装去掉注释掉self.install();
  246. //self.install();
  247. }
  248. }
  249. },
  250. //安装提示
  251. install() {
  252. this.close();
  253. plus.nativeUI.showWaiting('安装中,请稍后...');
  254. plus.runtime.install(
  255. this.installPath,
  256. {},
  257. res => {
  258. plus.nativeUI.closeWaiting();
  259. plus.runtime.restart();
  260. uni.removeStorageSync('update_path');
  261. },
  262. err => {
  263. plus.nativeUI.closeWaiting();
  264. plus.nativeUI.alert('error ' + err.code, '', '提示');
  265. }
  266. );
  267. }
  268. }
  269. };
  270. </script>
  271. <style lang="scss" scoped>
  272. .versionUpgrade {
  273. width: 100%;
  274. height: 100%;
  275. position: fixed;
  276. top: 0;
  277. left: 0;
  278. right: 0;
  279. bottom: 0;
  280. z-index: 999999;
  281. background: rgba(0, 0, 0, 0.4);
  282. display: flex;
  283. justify-content: center;
  284. align-items: center;
  285. }
  286. .upgrade-wrapper {
  287. margin-top: -80upx;
  288. width: 500upx;
  289. min-height: 500upx;
  290. max-height: 800upx;
  291. }
  292. .oa-version-upgrade {
  293. z-index: 9999999;
  294. width: 100%;
  295. border-radius: 12upx;
  296. background: #ffffff;
  297. .upgrade-top-img {
  298. image {
  299. width: 100%;
  300. height: 240upx;
  301. }
  302. }
  303. .versionUpgradeViewV2 {
  304. display: flex;
  305. flex-direction: column !important;
  306. padding: 30upx;
  307. .upgrade-info {
  308. .content-title {
  309. font-size: $font-base;
  310. color: $font-color-dark;
  311. margin-bottom: 10upx;
  312. }
  313. .content-text {
  314. font-size: $font-sm;
  315. color: $font-color-light;
  316. }
  317. }
  318. .confirm-btn {
  319. width: 90%;
  320. height: 60upx;
  321. line-height: 60upx;
  322. margin-top: 20upx;
  323. }
  324. .versionUpgradeViewIs_force0 {
  325. display: flex;
  326. flex-direction: row !important;
  327. justify-content: space-between;
  328. .text {
  329. padding: 10upx 70upx;
  330. border-radius: 30upx;
  331. font-size: 16px;
  332. color: #ffffff;
  333. }
  334. .butClose {
  335. background: #e2e2e2;
  336. }
  337. .download {
  338. background: #ff5722;
  339. }
  340. .downloadIs_force1 {
  341. width: 100%;
  342. display: flex;
  343. justify-content: center;
  344. align-items: center;
  345. padding: 10upx 0;
  346. border-radius: 30upx;
  347. font-size: 16px;
  348. color: #ffffff;
  349. background: #ff5722;
  350. }
  351. }
  352. .versionUpgradeViewStartTrue {
  353. display: flex;
  354. flex-direction: column !important;
  355. .title {
  356. font-size: 14px;
  357. color: #626262;
  358. padding: 10upx 0;
  359. }
  360. .btn-text {
  361. width: 100%;
  362. display: flex;
  363. justify-content: center;
  364. align-items: center;
  365. padding: 10upx 0;
  366. border-radius: 30upx;
  367. font-size: 16px;
  368. color: #ffffff;
  369. }
  370. .downloadStartTrue {
  371. background: #ff5722;
  372. }
  373. .downloadStartFalse {
  374. background: #e2e2e2;
  375. }
  376. }
  377. }
  378. }
  379. .close-upgrade {
  380. width: 100%;
  381. text-align: center;
  382. color: $color-white;
  383. .line {
  384. display: flex;
  385. justify-content: center;
  386. text-align: center;
  387. .close-upgrade-line {
  388. width: 1upx;
  389. height: 72upx;
  390. background: $color-white;
  391. opacity: 0.6;
  392. }
  393. }
  394. .iconfont {
  395. font-size: $font-lg;
  396. margin-top: -12upx;
  397. display: inline-block;
  398. }
  399. }
  400. </style>