App.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <script>
  2. import config from './config'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/auth'
  5. import {updateUserProfile,findVersion} from "@/api/system/user.js"
  6. const baseUrl = config.baseUrl
  7. export default {
  8. data(){
  9. return{
  10. platform:'Android'
  11. }
  12. },
  13. onLaunch: function() {
  14. // #ifdef APP
  15. this.onlineWgt(); //版本更新
  16. // #endif
  17. this.initApp()
  18. },
  19. methods: {
  20. //极光推送
  21. // this.getJGtuifn()
  22. // 在线跟新
  23. onlineWgt() {
  24. let that = this;
  25. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  26. that.comparisonVersionNo(widgetInfo.versionCode);
  27. });
  28. // that.comparisonVersionNo()
  29. },
  30. comparisonVersionNo(versionCode) {
  31. let that = this;
  32. // console.log("aaaaaaa===versionCode", versionCode);
  33. const parmas = {
  34. model: that.platform
  35. }
  36. findVersion(parmas).then(res => {
  37. console.log(res)
  38. if (res.code == "200") {
  39. let versionNos = res.data.code;
  40. // console.log(versionNos,'versionNos')
  41. let filePath = res.data.path;
  42. if (Number(versionNos) > Number(versionCode)) { //服务器返回1更新,0则不更新
  43. uni.showModal({
  44. title: '提示',
  45. content: '发现新版本,是否升级',
  46. success: function(res) {
  47. // console.log(widgetInfo.version);
  48. if (res.confirm) {
  49. console.log('用户点击确定');
  50. that.getProperty(filePath);
  51. } else if (res.cancel) {
  52. console.log('用户点击取消');
  53. }
  54. }
  55. });
  56. }
  57. }
  58. }).catch(error=>{
  59. uni.hideLoading()
  60. console.log(error)
  61. })
  62. },
  63. getProperty(getProperty) {
  64. // console.log("bbbbbbbbb----------->", getProperty)
  65. let that = this;
  66. // console.log(baseUrl + getProperty)
  67. // 在线升级app/热更新
  68. that.showwri = true;
  69. const downloadTask = uni.downloadFile({ //下载文件
  70. url: baseUrl + getProperty,
  71. success: (downloadResult) => {
  72. // console.log(downloadResult);
  73. if (downloadResult.statusCode == 200) {
  74. // 更新升级
  75. plus.runtime.install(downloadResult.tempFilePath, {
  76. force: true
  77. }, function() {
  78. // console.log('install success...');
  79. that.showwri = false;
  80. plus.nativeUI.alert("应用资源更新完成!", function() {
  81. plus.runtime.restart();
  82. });
  83. }, function(e) {
  84. that.showwri = false;
  85. plus.nativeUI.alert("更新失败,请稍后再试");
  86. });
  87. }
  88. }
  89. });
  90. downloadTask.onProgressUpdate((res) => { //下载文件的进度
  91. that.totalBytesWritten = res.totalBytesWritten; //当前下载大小
  92. that.progress = res.progress; //当前下载比例
  93. that.totalBytesExpectedToWrite = res.totalBytesExpectedToWrite; //
  94. });
  95. },
  96. // 极光推送
  97. getJGtuifn(){
  98. //#ifdef APP-PLUS
  99. var jpushModule = uni.requireNativePlugin("JG-JPush");
  100. jpushModule.setLoggerEnable(true);
  101. // 初始化函数
  102. jpushModule.initJPushService();
  103. jpushModule.addConnectEventListener(result=>{
  104. let connectEnable = result.connectEnable
  105. // console.log("jpush连接", connectEnable)
  106. })
  107. jpushModule.getRegistrationID(result => {
  108. // console.log("注册ID.....",result)
  109. this.registerID = result.registerID;
  110. if(getToken()){
  111. var params={
  112. jgId:this.registerID,
  113. }
  114. updateUserProfile(params).then(res=>{
  115. if(rtes.code==200){
  116. console.log(res,9)
  117. }
  118. })
  119. }
  120. })
  121. jpushModule.isPushStopped(result=>{
  122. let code = result.code
  123. console.log('连接状态回调',result)
  124. });
  125. // 设置别名
  126. jpushModule.setAlias({
  127. 'alias' : 'coder',
  128. 'sequence': 1
  129. })
  130. jpushModule.addNotificationListener(result=>{
  131. let notificationEventType = result.notificationEventType
  132. let messageID = result.messageID
  133. let title = result.title
  134. let content = result.content
  135. let extras = result.extras
  136. console.log('通知事件回调',result)
  137. // 推送一个本地通知
  138. jpushModule.addLocalNotification({
  139. messageID,
  140. title,
  141. content,
  142. extras
  143. })
  144. })
  145. jpushModule.addNotificationListener(result => {
  146. console.log(result)
  147. if (result.notificationEventType == "notificationOpened") {
  148. //点击窗口通知栏推送的消息 跳转指定页面
  149. uni.reLaunch({
  150. url:'/pages/login'
  151. })
  152. }
  153. });
  154. //#endif
  155. },
  156. // 初始化应用
  157. initApp() {
  158. // 初始化应用配置
  159. this.initConfig()
  160. // 检查用户登录状态
  161. //#ifdef H5
  162. // this.checkLogin()
  163. //#endif
  164. },
  165. initConfig() {
  166. this.globalData.config = config
  167. },
  168. checkLogin() {
  169. if (!getToken()) {
  170. this.$tab.reLaunch('/pages/login')
  171. }
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss">
  177. @import "@/static/style.css";
  178. @import '@/static/scss/index.scss'
  179. </style>