App.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <script>
  2. /* eslint-disable */
  3. import Vue from 'vue';
  4. import {
  5. configList,
  6. bindingEquipment
  7. } from '@/api/basic';
  8. import {
  9. verifyAccessToken
  10. } from '@/api/login'
  11. import {
  12. mapMutations
  13. } from 'vuex';
  14. export default {
  15. onLaunch() {
  16. this.initData();
  17. this.checkApk();
  18. },
  19. onHide() {},
  20. methods: {
  21. //...mapMutations(['setNotifyNum']),
  22. // 数据初始化
  23. initData() {
  24. // 获取页面设置配置
  25. const _this = this;
  26. const token = uni.getStorageSync('accessToken');
  27. // #ifdef APP-PLUS
  28. // 5+ push 消息推送 ps:使用:H5+的方式监听,实现推送
  29. plus.push.addEventListener(
  30. 'click',
  31. function(msg) {
  32. _this.navTo(JSON.parse(msg.content));
  33. },
  34. false
  35. );
  36. // 监听在线消息事件
  37. plus.push.addEventListener(
  38. 'receive',
  39. function(msg) {
  40. _this.navTo(JSON.parse(msg.content));
  41. },
  42. false
  43. );
  44. // #endif
  45. // 获取系统title高度
  46. this.initSystemInfo();
  47. if (token) {
  48. this.handleVerifyAccessToken(token);
  49. }
  50. if (this.$mStore.getters.hasLogin) {
  51. // 初始化Websocket
  52. // await this.$mWebsocket.initWebsocket();
  53. // 初始化数量
  54. //this.setNotifyNum(uni.getStorageSync('notifyNum') || 0);
  55. // #ifdef APP-PLUS
  56. const info = plus.push.getClientInfo();
  57. //await this.handleBindingEquipment(info.clientid, token);
  58. // #endif
  59. this.getTabBarBadge();
  60. }
  61. },
  62. // 初始化系统信息
  63. initSystemInfo() {
  64. uni.getSystemInfo({
  65. success(e) {
  66. // #ifndef MP
  67. Vue.prototype.StatusBar = e.statusBarHeight;
  68. if (e.platform === 'android') {
  69. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  70. } else {
  71. Vue.prototype.CustomBar = e.statusBarHeight + 43;
  72. }
  73. // #endif
  74. // #ifdef MP-WEIXIN
  75. Vue.prototype.StatusBar = e.statusBarHeight;
  76. const custom = wx.getMenuButtonBoundingClientRect();
  77. Vue.prototype.Custom = custom;
  78. Vue.prototype.CustomBar = custom.top - e.statusBarHeight;
  79. // #endif
  80. // #ifdef MP-ALIPAY
  81. Vue.prototype.StatusBar = e.statusBarHeight;
  82. Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
  83. // #endif
  84. }
  85. });
  86. },
  87. // 设备绑定(app推送)
  88. async handleBindingEquipment(id, token) {
  89. const oauth_client = uni.getSystemInfoSync().platform;
  90. await this.$http.post(bindingEquipment, {
  91. token,
  92. oauth_client,
  93. oauth_client_user_id: id
  94. });
  95. },
  96. async handleVerifyAccessToken(token) {
  97. var url = this.$mConfig.baseUrl + '/fw/Refresh';
  98. this.$http.request({
  99. url: url,
  100. method: "POST",
  101. header: {
  102. 'content-type': 'application/json',
  103. 'Authorization': token
  104. }
  105. }).then(r => {
  106. if (!r.loginUser.token) {
  107. this.$mStore.commit('logout');
  108. } else {
  109. this.$store.commit('login', r);
  110. }
  111. })
  112. },
  113. // 推送消息跳转
  114. async navTo(item) {
  115. let route;
  116. const id = item.target_id;
  117. const type = item.target_type;
  118. switch (type) {
  119. default:
  120. route = '/pages/index/index';
  121. break;
  122. }
  123. if (route) this.$mRouter.push({
  124. route
  125. });
  126. },
  127. //检查更新
  128. checkApk() {
  129. let url = this.$mConfig.baseUrl + '/appinterface/ip/version';
  130. this.$http.request({
  131. url: url,
  132. header: {
  133. 'content-type': 'application/json'
  134. },
  135. }).then(res => {
  136. //获取到的app最新版本号
  137. //获取url
  138. let url = res.data.dataUrl;
  139. let app_last_version = res.data.lastVersion;
  140. // let nativeVersion = plus.runtime.version;
  141. console.log("服务器版本:" + app_last_version)
  142. // console.log("客户端版本:" + nativeVersion)
  143. //修改版本判断
  144. let that = this
  145. var result = null
  146. var ss = null
  147. // #ifdef APP-PLUS
  148. plus.runtime.getProperty( plus.runtime.appid, async function ( wgtinfo ) {
  149. console.log("当前app版本"+wgtinfo.version)
  150. result = that.compareVersion(wgtinfo.version, app_last_version);
  151. that.checkAndInstall(res,result)
  152. } );
  153. // #endif
  154. })
  155. },
  156. //版本号对比:当且仅当本地版本号小于请求的版本号则返回 true
  157. compareVersion(nativeVersion, lastVersion) {
  158. nativeVersion = nativeVersion.split('.');
  159. lastVersion = lastVersion.split('.');
  160. let n = Math.max(nativeVersion.length, lastVersion.length)
  161. for (let i = 0; i < n; i++) {
  162. let code1 = (nativeVersion[i] === undefined) ? 0 : parseInt(nativeVersion[i]);
  163. let code2 = (lastVersion[i] === undefined) ? 0 : parseInt(lastVersion[i]);
  164. if (code1 > code2) {
  165. this.$needUpdate = false;
  166. return false
  167. } else if (code1 < code2) {
  168. return true
  169. }
  170. }
  171. this.$needUpdate = false;
  172. return false;
  173. },
  174. //下载和安装apk
  175. downloadApk(url) {
  176. let that = this;
  177. let watiting = plus.nativeUI.showWaiting("下载文件...");
  178. let dtask = plus.downloader.createDownload(url, {
  179. method: "GET",
  180. retry: 0,
  181. });
  182. dtask.addEventListener(
  183. "statechanged",
  184. function(task, status) {
  185. if (!dtask) {
  186. return;
  187. }
  188. switch (task.state) {
  189. case 1:
  190. break;
  191. case 2:
  192. break;
  193. case 3:
  194. var nowData = Math.floor(
  195. (task.downloadedSize * 100) / task.totalSize
  196. );
  197. watiting.setTitle("已下载:" + nowData + "%");
  198. if (nowData === 100) {
  199. watiting.toast("正在准备环境,请稍后!");
  200. watiting.close();
  201. }
  202. break;
  203. case 4:
  204. // 安装apk资源包
  205. plus.runtime.install(
  206. dtask.filename, {},
  207. function() {
  208. plus.nativeUI.closeWaiting();
  209. this.$needUpdate = false;
  210. plus.runtime.restart();
  211. // plus.nativeUI.alert("更新完成!", function () {
  212. // // 更新完成后重启应用
  213. // plus.runtime.restart();
  214. // });
  215. },
  216. function(e) {
  217. plus.nativeUI.closeWaiting();
  218. plus.nativeUI.toast("安装更新失败!");
  219. this.$needUpdate = true;
  220. }
  221. );
  222. break;
  223. default:
  224. break;
  225. }
  226. },
  227. false
  228. );
  229. dtask.setRequestHeader("Access-Control-Allow-Origin", "*");
  230. dtask.start();
  231. },
  232. //安装wgt
  233. downloadWgt(url){
  234. uni.downloadFile({
  235. url: url,
  236. success: (downloadResult) => {
  237. plus.nativeUI.showWaiting("下载wgt文件...");
  238. // console.log(plus.runtime.version)
  239. // console.log(url)
  240. // console.log(downloadResult.tempFilePath+'==========='+downloadResult.statusCode)
  241. if (downloadResult.statusCode === 200) {
  242. plus.nativeUI.toast("正在准备环境");
  243. plus.runtime.install(downloadResult.tempFilePath, {
  244. force: true
  245. }, function() {
  246. plus.nativeUI.closeWaiting();
  247. plus.nativeUI.toast("更新成功,正在重启应用!");
  248. this.$needUpdate = false;
  249. plus.runtime.restart();
  250. }, function(e) {
  251. plus.nativeUI.closeWaiting();
  252. plus.nativeUI.toast("安装更新失败!");
  253. this.$needUpdate = true;
  254. });
  255. }
  256. }
  257. });
  258. },
  259. checkAndInstall(res,result){
  260. if (result) {
  261. uni.showModal({
  262. title: "有新的版本",
  263. content: "请确认是否更新?",
  264. success: (info) => {
  265. if (info.confirm) {
  266. // 因为App.vue中不能直接使用组件,这里用到了vuex来调用下载进度的组件
  267. // 显示下载进度弹窗
  268. // this.$store.commit('setHide',true)
  269. if (plus.os.name.toLowerCase() == 'ios') {
  270. // ios直接跳转到下载页面
  271. let downloadUrl = this.$mConfig.baseUrl + res.data.dataUrl
  272. plus.runtime.openURL(downloadUrl)
  273. } else {
  274. let downloadUrl = this.$mConfig.baseUrl + res.data.dataUrl
  275. if(downloadUrl.substring(downloadUrl.lastIndexOf(".")+1) == "apk"){
  276. return this.downloadApk(downloadUrl)
  277. }else if(downloadUrl.substring(downloadUrl.lastIndexOf(".")+1) == "wgt"){
  278. return this.downloadWgt(downloadUrl)
  279. }
  280. }
  281. }
  282. }
  283. })
  284. } else {
  285. this.$needUpdate = false;
  286. return;
  287. }
  288. }
  289. }
  290. };
  291. </script>
  292. <style lang="scss">
  293. // 导入colorUI
  294. @import '/static/css/colorui/main.css';
  295. @import '/static/css/colorui/icon.css';
  296. @import '/static/css/colorui/animation.css';
  297. // 导入阿里巴巴矢量图标库
  298. /*#ifdef MP*/
  299. @import './static/css/iconfont/iconfont.css';
  300. /*#endif*/
  301. /*#ifndef MP*/
  302. @import url('https://at.alicdn.com/t/font_1823374_98c45zxwb3c.css');
  303. /*#endif*/
  304. @import './static/css/reset.scss';
  305. @import './static/css/uni.scss';
  306. </style>