App.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <script>
  2. export default {
  3. data(){
  4. return {
  5. curVersion: 100
  6. }
  7. },
  8. onLaunch: function() {
  9. //console.log('App Launch')
  10. // #ifdef APP-PLUS
  11. // token标志来判断
  12. let token= uni.getStorageSync('userInfo');
  13. if (token) {
  14. //存在则关闭启动页进入首页
  15. plus.navigator.closeSplashscreen();
  16. } else {
  17. //不存在则跳转至登录页
  18. // uni.reLaunch({
  19. // url: "/pages/login/login",
  20. // success: () => {
  21. // plus.navigator.closeSplashscreen();
  22. // }
  23. // })
  24. }
  25. // #endif
  26. let baseUrl= uni.getStorageSync('BasrUrl');
  27. if(baseUrl) {
  28. uni.setStorage({
  29. key: 'BasrUrl',
  30. data: baseUrl
  31. });
  32. }
  33. else {
  34. uni.setStorage({
  35. key: 'BasrUrl',
  36. data: 'http://192.168.150.100:8888'
  37. });
  38. }
  39. },
  40. onShow: function() {
  41. this.checkUpdate();
  42. //console.log('App Show')
  43. },
  44. onHide: function() {
  45. //console.log('App Hide')
  46. },
  47. globalData: {
  48. baseUrl: 'http://192.168.150.100:8888',
  49. userName: '',
  50. password: ''
  51. },
  52. methods: {
  53. // 版本升级
  54. async checkUpdate(){
  55. // 获取manifest.json 的信息
  56. plus.runtime.getProperty( plus.runtime.appid, async function ( wgtinfo ) {
  57. this.curVersion = parseInt(wgtinfo.versionCode);
  58. // console.log( "this.curVersion:" + this.curVersion );
  59. let parm = {
  60. url : '/GetPdaLatestmsg',
  61. data: {},
  62. method: 'GET',
  63. isHaveToken : false
  64. }
  65. let retData = await getApp().RequestData(parm)
  66. if(retData.isSuccess) {
  67. if(retData.data.code == 200 && parseInt(wgtinfo.versionCode) < retData.data.data.versionNo) {
  68. uni.showModal({
  69. title: '版本更新:' + retData.data.data.versionName,
  70. content: retData.data.data.updateDetails,
  71. confirmText: "更新",
  72. showCancel: false,
  73. success: function(e) {
  74. if (e.confirm) {
  75. let baseUrl= uni.getStorageSync('BasrUrl');
  76. // console.log(baseUrl + '/GetPdaPackage')
  77. getApp().createDownload(baseUrl + '/GetPdaPackage?fileName=' + retData.data.data.packageName);
  78. } else {
  79. //取消
  80. }
  81. }
  82. });
  83. }
  84. else {
  85. uni.showModal({
  86. title: '提示',
  87. content: '已是最新版本',
  88. showCancel: false
  89. });
  90. }
  91. }
  92. else {
  93. uni.showModal({
  94. title: '提示',
  95. content: '自动更新接口异常!!',
  96. showCancel: false
  97. });
  98. }
  99. });
  100. },
  101. createDownload(url){
  102. var dtask = plus.downloader.createDownload(url, {},
  103. function(d, status) {
  104. uni.showToast({
  105. title: '下载完成',
  106. mask: false,
  107. duration: 1000
  108. });
  109. //console.log(dtask);
  110. // 下载完成
  111. // console.log('status: '+status);
  112. if (status == 200) {
  113. // console.log('下载成功:'+d.filename);
  114. // console.log('plus.io.convertLocalFileSystemURL(d.filename): '+plus.io.convertLocalFileSystemURL(d.filename))
  115. plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, function(success) {
  116. uni.showToast({
  117. title: '安装成功',
  118. mask: false,
  119. duration: 1500
  120. });
  121. }, function(error) {
  122. uni.showToast({
  123. title: '安装失败-01',
  124. mask: false,
  125. duration: 1500
  126. });
  127. })
  128. } else {
  129. uni.showToast({
  130. title: '更新失败-02',
  131. mask: false,
  132. duration: 1500
  133. });
  134. }
  135. });
  136. try {
  137. dtask.start(); // 开启下载的任务
  138. var prg = 0;
  139. var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象
  140. dtask.addEventListener('statechanged', function(task,status) {
  141. // 给下载任务设置一个监听 并根据状态 做操作
  142. switch (task.state) {
  143. case 1:
  144. showLoading.setTitle("正在下载");
  145. break;
  146. case 2:
  147. showLoading.setTitle("已连接到服务器");
  148. break;
  149. case 3:
  150. prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100 );
  151. showLoading.setTitle(" 正在下载" + prg + "% ");
  152. break;
  153. case 4:
  154. plus.nativeUI.closeWaiting();
  155. //下载完成
  156. break;
  157. }
  158. });
  159. }
  160. catch (err) {
  161. plus.nativeUI.closeWaiting();
  162. uni.showToast({
  163. title: '更新失败-03',
  164. mask: false,
  165. duration: 1500
  166. });
  167. }
  168. },
  169. AppExit() {
  170. uni.showModal({
  171. //计算删除的数量
  172. title: '提示',
  173. content: '是否要退出应用?', //文章内容
  174. cancelText: '取消', //取消按钮的文字
  175. confirmText: '确定', //确定文本的文字
  176. cancelColor: '#007aff', //取消按钮文本颜色
  177. confirmColor: '#007aff', //确定按钮文本颜色
  178. success: function(res) {
  179. if (res.confirm) {
  180. //console.log('用户点击了确定')
  181. let userMsg = uni.getStorageSync('userInfo')
  182. if(userMsg) {
  183. let baseUrlStr = uni.getStorageSync('BasrUrl')
  184. uni.request({
  185. url: baseUrlStr + '/logout',
  186. //自定义请求头信息
  187. header: {
  188. Authorization: 'Bearer ' + userMsg.tokenStr + '',
  189. 'content-type': 'application/json'
  190. },
  191. method: 'POST',
  192. })
  193. //指定移除登录后缓存的user用户信息
  194. uni.removeStorage({
  195. key: 'userInfo',
  196. success: function(res) {
  197. //console.log('清空了用户缓存信息', );
  198. uni.showToast({
  199. title: '退出应用成功',
  200. icon: 'success',
  201. duration: 500
  202. })
  203. }
  204. })
  205. }
  206. setTimeout(() => {
  207. // main.finish();
  208. plus.runtime.quit();
  209. }, 600)
  210. } else if (res.cancel) {
  211. //console.log('用户点击了取消')
  212. }
  213. }
  214. })
  215. },
  216. RequestData(parm) {
  217. return new Promise((resolve,reject) => {
  218. let userMsg = uni.getStorageSync('userInfo')
  219. let baseUrlStr = uni.getStorageSync('BasrUrl')
  220. // console.log(parm.data)
  221. uni.request({
  222. url: baseUrlStr + parm.url,
  223. data: parm.data,
  224. header: { //自定义请求头信息
  225. Authorization: parm.isHaveToken? 'Bearer ' + userMsg.tokenStr + '' : '',
  226. 'content-type': 'application/json'
  227. },
  228. timeout: 20000,
  229. method: parm.method,
  230. success: (res) => {
  231. resolve({
  232. isSuccess : true,
  233. data : res.data,
  234. retMsg : '请求成功!'
  235. })
  236. },
  237. fail: (res) => {
  238. reject({
  239. isSuccess : false,
  240. data : {},
  241. retMsg : '请求失败!' + res.errMsg
  242. })
  243. }
  244. });
  245. })
  246. }
  247. }
  248. }
  249. </script>
  250. <style>
  251. /* #ifndef APP-PLUS-NVUE */
  252. /* uni.css - 通用组件、模板样式库,可以当作一套ui库应用 */
  253. @import './common/uni.css';
  254. @import '@/static/customicons.css';
  255. /* H5 兼容 pc 所需 */
  256. /* #ifdef H5 */
  257. @media screen and (min-width: 768px) {
  258. body {
  259. overflow-y: scroll;
  260. }
  261. }
  262. /* 顶栏通栏样式 */
  263. /* .uni-top-window {
  264. left: 0;
  265. right: 0;
  266. } */
  267. uni-page-body {
  268. background-color: #F5F5F5 !important;
  269. min-height: 100% !important;
  270. height: auto !important;
  271. }
  272. .uni-top-window uni-tabbar .uni-tabbar {
  273. background-color: #fff !important;
  274. }
  275. .uni-app--showleftwindow .hideOnPc {
  276. display: none !important;
  277. }
  278. /* #endif */
  279. /* 以下样式用于 hello uni-app 演示所需 */
  280. page {
  281. background-color: #efeff4;
  282. height: 100%;
  283. font-size: 28rpx;
  284. /* line-height: 1.8; */
  285. }
  286. .fix-pc-padding {
  287. padding: 0 50px;
  288. }
  289. .uni-header-logo {
  290. padding: 30rpx;
  291. flex-direction: column;
  292. justify-content: center;
  293. align-items: center;
  294. margin-top: 10rpx;
  295. }
  296. .uni-header-image {
  297. width: 100px;
  298. height: 100px;
  299. }
  300. .uni-hello-text {
  301. color: #7A7E83;
  302. }
  303. .uni-hello-addfile {
  304. text-align: center;
  305. line-height: 300rpx;
  306. background: #FFF;
  307. padding: 50rpx;
  308. margin-top: 10px;
  309. font-size: 38rpx;
  310. color: #808080;
  311. }
  312. /* #endif*/
  313. </style>