app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { HTTP_REQUEST_URL, CACHE_USERINFO, CACHE_TOKEN, CACHE_EXPIRES_TIME } from './config.js';
  2. import Server from './utils/Server.js';
  3. import util from './utils/util.js';
  4. App({
  5. onLaunch: function (option) {
  6. if (HTTP_REQUEST_URL=='') {
  7. console.error("请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret");
  8. return false;
  9. }
  10. let that = this;
  11. let token = wx.getStorageSync(CACHE_TOKEN);
  12. let expiresTime = wx.getStorageSync(CACHE_EXPIRES_TIME);
  13. let userInfo = wx.getStorageSync(CACHE_USERINFO);
  14. this.globalData.isLog = !!userInfo && util.isLoginValid(token, expiresTime, true);
  15. if (this.globalData.isLog) {
  16. this.globalData.token = token;
  17. this.globalData.expiresTime = expiresTime;
  18. this.globalData.userInfo = userInfo ? JSON.parse(userInfo) : {};
  19. }
  20. if (option.query.hasOwnProperty('scene')){
  21. switch (option.scene) {
  22. //扫描小程序码
  23. case 1047:
  24. that.globalData.code = option.query.scene;
  25. break;
  26. //长按图片识别小程序码
  27. case 1048:
  28. that.globalData.code = option.query.scene;
  29. break;
  30. //手机相册选取小程序码
  31. case 1049:
  32. that.globalData.code = option.query.scene;
  33. break;
  34. //直接进入小程序
  35. case 1001:
  36. that.globalData.spid = option.query.scene;
  37. break;
  38. } // switch
  39. } // if
  40. // 获取导航高度;
  41. wx.getSystemInfo({
  42. success: res => {
  43. //导航高度
  44. this.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 97;
  45. var model = res.model;
  46. if (/iphone\sx/i.test(model) ||
  47. (/iphone/i.test(model) && /unknown/.test(model)) ||
  48. /iphone\s11/i.test(model)) {
  49. that.globalData.isFixed = true;
  50. } else {
  51. that.globalData.isFixed = false;
  52. }
  53. },
  54. fail(err) {}
  55. });
  56. const updateManager = wx.getUpdateManager();
  57. updateManager.onCheckForUpdate(function (res) {
  58. // 请求完新版本信息的回调
  59. })
  60. updateManager.onUpdateReady(function () {
  61. wx.showModal({
  62. title: '更新提示',
  63. content: '新版本已经准备好,是否重启应用?',
  64. success: function (res) {
  65. if (res.confirm) {
  66. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  67. updateManager.applyUpdate()
  68. }
  69. }
  70. })
  71. });
  72. updateManager.onUpdateFailed(function () {
  73. return that.Tips({title:'新版本下载失败'});
  74. })
  75. //实例化聊天服务
  76. this.$chat = new Server(this);
  77. }, // onLaunch
  78. $chat:null,
  79. globalData: {
  80. navHeight: 0,
  81. routineStyle: '#ffffff',
  82. openPages: '',
  83. spid: 0,
  84. code:0,
  85. urlImages: '',
  86. url: HTTP_REQUEST_URL,
  87. token: '',
  88. isLog:false, // 是否已登录
  89. expiresTime:0,
  90. MyMenus:[],
  91. userInfo:{},
  92. loginType:'routine',
  93. isFixed: false,
  94. unread: 0, // 未读消息条数
  95. },
  96. /**
  97. * 聊天事件快捷注册
  98. */
  99. $on: function (name, action){
  100. this.$chat.$on(name,action);
  101. },
  102. /**
  103. * 信息提示 + 跳转
  104. * @param object opt {title:'提示语',icon:''} | url
  105. * @param object to_url 跳转url 有5种跳转方式 {tab:1-5,url:跳转地址}
  106. */
  107. Tips: function (opt, to_url) {
  108. return util.Tips(opt, to_url);
  109. },
  110. /**
  111. * 快捷调取助手函数
  112. */
  113. help:function() {
  114. return util.$h;
  115. },
  116. setUnread: function(newVal) {
  117. this.globalData.unread = newVal
  118. this.updateUnread();
  119. },
  120. /**
  121. * update unread message tip
  122. * @param {int} newval
  123. */
  124. updateUnread: function() {
  125. if (this.globalData.unread != undefined && this.globalData.unread > 0) {
  126. wx.setTabBarBadge({
  127. index: 2,
  128. text: this.globalData.unread.toString(),
  129. });
  130. } else {
  131. wx.removeTabBarBadge({
  132. index: 2,
  133. });
  134. }
  135. }, // updateUnread
  136. /**
  137. * 合并数组
  138. * @param array list 请求返回数据
  139. * @param array sp 原始数组
  140. * @return array
  141. */
  142. SplitArray: function (list, sp) {
  143. return util.SplitArray(list, sp)
  144. },
  145. }) // App