appUpdate.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <view class="appUpdate">
  3. <view>
  4. <u-overlay :show="showDownloadState" :opacity="0.7">
  5. <view class="warp">
  6. <view class="warp_content">
  7. <u-line-progress :percentage="parseInt(progressVal)" activeColor="#ff0000"></u-line-progress>
  8. <view class="warp_font">{{textLoading2}}</view>
  9. </view>
  10. </view>
  11. </u-overlay>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import * as API_item from '@/api/item.js';
  17. export default {
  18. props:{
  19. },
  20. data() {
  21. return {
  22. showProgress:false,
  23. showDownloadState:false,
  24. textLoading2:'版本更新中,请稍等...',
  25. progressVal: 0, //进度值
  26. }
  27. },
  28. created() {
  29. console.log('页面是读到了');
  30. setTimeout(()=>{
  31. this.checkUpdate();
  32. },20)
  33. },
  34. methods: {
  35. // 版本号对比
  36. versionCompare(a, b) {
  37. //a>b 返回true
  38. console.log('对比版本号了');
  39. let res = false;
  40. let arrA = a.split(".");
  41. let arrB = b.split(".");
  42. arrA[0] = arrA[0]*1
  43. arrA[1] = arrA[1]*1
  44. arrA[2] = arrA[2]*1
  45. arrB[0] = arrB[0]*1
  46. arrB[1] = arrB[1]*1
  47. arrB[2] = arrB[2]*1
  48. if (arrA[0] > arrB[0]) {
  49. res = true;
  50. } else if (arrA[0] == arrB[0] && arrA[1] > arrB[1]) {
  51. res = true;
  52. }else if(arrA[0] == arrB[0] && arrA[1] == arrB[1] && arrA[2] > arrB[2]){
  53. res = true;
  54. }
  55. return res;
  56. },
  57. // 检查更新
  58. checkUpdate(){
  59. console.log('开始检查更新了');
  60. let _this = this
  61. let sys = plus.os.name.toLowerCase();
  62. var VERSION
  63. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  64. console.log('获取版本号',inf);
  65. VERSION = inf.version;
  66. })
  67. API_item.getUpdateJson().then(function(res){
  68. console.log('获取到结果了吗',res);
  69. let data = res;
  70. //安装式更新
  71. // if (sys == "ios") {
  72. // //版本判断,提示更新
  73. // this.$http.jsonp("http://itunes.apple.com/cn/lookup?id=1232107347")
  74. // .then(
  75. // function(Res) {
  76. // _this.versionCompare(Res.body.results[0].version, VERSION)? _this.showNeedUpdate('ios',data.isMustUpdate) : void 0
  77. // }
  78. // )
  79. // } else
  80. console.log('获取终端机类型',sys);
  81. if (sys == "android") {
  82. console.log('分别是参数',data.version, VERSION,_this.versionCompare(data.version, VERSION));
  83. _this.versionCompare(data.version, VERSION)? _this.showNeedUpdate('android',data.isMustUpdate,data.packageDown) : void 0
  84. }
  85. })
  86. },
  87. // 强制更新
  88. showNeedUpdate(func,isMust,downUrl){ //isMust 是否必须更新
  89. console.log('得强制更新了')
  90. let _this = this
  91. let connectionStatus = plus.networkinfo.getCurrentType(); //在Ios上获取网络类型正常,但是只能获取app打开之前的网络状态,
  92. // 强制退出
  93. uni.showModal({
  94. title: '更新提示',
  95. content: func==='android'?'有新版本,点击更新':'有新版本,点击更新<br><span style="font-size:12px;">(确定后5s自动退出)</span>',
  96. confirmText:"立即更新",
  97. cancelText:isMust==1?"退出应用":"取消",
  98. success:function(res){
  99. if (res.confirm) { //点击了确定更新
  100. if(func==='hot'){
  101. if(connectionStatus == 2 || connectionStatus == 3){
  102. _this.downWgt()
  103. }else{
  104. _this.$vux.confirm.show({
  105. title: '网络提示',
  106. content:'您正在使用移动流量,更新大约需20M',
  107. confirmText:"打开wifi(免流量)",
  108. cancelText:"继续",
  109. onCancel () {
  110. _this.downWgt()
  111. },
  112. onConfirm (msg) {
  113. _this.openSysSet()
  114. }
  115. })
  116. }
  117. if(isMust == 1){
  118. _this.afterQuit(func) //退出app
  119. }
  120. }else if(func==='ios'){
  121. plus.runtime.openURL("https://itunes.apple.com/cn/app/id1232107347")
  122. if(isMust == 1){
  123. _this.afterQuit(func) //退出app
  124. }
  125. }else if(func==='android'){
  126. _this.downWgt(downUrl,isMust)
  127. }
  128. } else if (res.cancel) { //点击了取消
  129. if(isMust == 1){
  130. if(func === 'ios'){
  131. const threadClass = plus.ios.importClass("NSThread");
  132. const mainThread = plus.ios.invoke(threadClass, "mainThread");
  133. plus.ios.invoke(mainThread, "exit");
  134. }else if(func === 'android'){
  135. plus.runtime.quit()
  136. }
  137. }else{
  138. void 0
  139. }
  140. }
  141. }
  142. })
  143. },
  144. // 下载wgt文件
  145. downWgt (downUrl,isMust){
  146. console.log('转到下载文件',downUrl,isMust);
  147. let _this = this
  148. let timer = (new Date).valueOf()
  149. let changeTime = 300
  150. if(downUrl){
  151. // _this.showProgress = true
  152. _this.showDownloadState = true
  153. var downloader = plus.downloader.createDownload(downUrl)
  154. downloader.addEventListener("statechanged", function(dtask,status){
  155. console.log('下载得一些状态',dtask,status);
  156. if(dtask.state == 4 && status == 200){
  157. _this.textLoading2 = "恭喜您,下载成功"
  158. uni.showModal({
  159. title: '安装提示',
  160. content: "下载已完成,是否安装?",
  161. confirmText:"立即安装",
  162. cancelText:isMust==1?"退出应用":"取消",
  163. success:function(res){
  164. if (res.confirm) { //点击了确定更新
  165. plus.runtime.openFile(dtask.getFileName());
  166. }else if(res.cancel){
  167. if(isMust == 1){
  168. plus.runtime.quit()
  169. }else{
  170. plus.runtime.quit()
  171. }
  172. }
  173. }
  174. })
  175. }else{
  176. switch(dtask.state) {
  177. case 1: // 开始
  178. _this.textLoading2 = "开始下载..."
  179. break;
  180. case 2: // 已连接到服务器
  181. _this.textLoading2 = "连接到服务器.."
  182. break;
  183. case 3: // 已接收到数据
  184. let _time = (new Date).valueOf()
  185. if(_time-timer>changeTime){
  186. timer = _time
  187. // _this.showLoading = true
  188. _this.progressVal = ((dtask.downloadedSize/dtask.totalSize).toFixed(2)*(100))
  189. _this.textLoading2 = "下载更新文件..."
  190. }
  191. break;
  192. case 4: // 下载完成
  193. break;
  194. }
  195. }
  196. })
  197. downloader.start();
  198. }else if(_this.wgtUrl){
  199. var downloader = plus.downloader.createDownload( _this.wgtUrl, {filename:"_doc/update/"}
  200. , function(d,status){ //下载完成执行的回调函数
  201. if ( status == 200 ) {
  202. // alert("下载wgt成功:"+d.filename);
  203. _this.installWgt(d.filename); // 安装wgt包
  204. } else {
  205. // console.log("下载wgt失败!");
  206. plus.nativeUI.alert("下载wgt失败!");
  207. }
  208. }
  209. )
  210. downloader.addEventListener("statechanged", function(dtask,status){
  211. if(!dtask){return;}
  212. switch(dtask.state) {
  213. case 1: // 开始
  214. _this.showLoading = true
  215. _this.textLoading = "开始下载..."
  216. break;
  217. case 2: // 已连接到服务器
  218. _this.showLoading = true
  219. _this.textLoading = "连接到服务器.."
  220. break;
  221. case 3: // 已接收到数据
  222. let _time = (new Date).valueOf()
  223. if(_time-timer>changeTime){
  224. timer = _time
  225. _this.showLoading = true
  226. _this.progressVal = ((dtask.downloadedSize/dtask.totalSize).toFixed(2)*(100))
  227. _this.textLoading = "下载更新文件 "+progressVal.substr(0,4)+'%'
  228. }
  229. break;
  230. case 4: // 下载完成
  231. _this.showLoading = false
  232. break;
  233. }
  234. })
  235. downloader.start();
  236. }else{
  237. return false
  238. }
  239. },
  240. //打开wifi,用WiFi下载
  241. openSysSet(){
  242. let sys = plus.os.name.toLowerCase();
  243. this.backFromSetWIFI = true
  244. if(sys == "ios"){ //操作苹果手机
  245. plus.runtime.launchApplication({action:'App-Prefs:root=WIFI'}, function(e){
  246. console.log(JSON.stringify(e));
  247. }); //WIFI
  248. } else { //操作安卓收集
  249. var main = plus.android.runtimeMainActivity();
  250. var Intent = plus.android.importClass("android.content.Intent");
  251. var mIntent = new Intent('android.settings.WIFI_SETTINGS');
  252. main.startActivity(mIntent);
  253. }
  254. },
  255. // 避免旧版本使用退出
  256. afterQuit(func){
  257. setTimeout(()=>{
  258. if(func==='ios'){
  259. const threadClass = plus.ios.importClass("NSThread");
  260. const mainThread = plus.ios.invoke(threadClass, "mainThread");
  261. plus.ios.invoke(mainThread, "exit");
  262. }else if(func==='android'){
  263. plus.runtime.quit()
  264. }else if(func==='hot'){
  265. plus.runtime.quit();
  266. const threadClass = plus.ios.importClass("NSThread");
  267. const mainThread = plus.ios.invoke(threadClass, "mainThread");
  268. plus.ios.invoke(mainThread, "exit");
  269. }
  270. },5000)
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="scss" scoped>
  276. .appUpdate {
  277. .warp {
  278. display: flex;
  279. align-items: center;
  280. justify-content: center;
  281. height: 100%;
  282. .warp_content{
  283. width: 90%; margin: 0 auto; text-align: center;
  284. .warp_font{
  285. margin-top: 30rpx; color: #f9f9f9;
  286. }
  287. }
  288. }
  289. }
  290. </style>