app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.checkLogin(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. }
  39. }
  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) || (/iphone/i.test(model) && /unknown/.test(model))|| /iphone\s11/i.test(model)){
  47. that.globalData.isFixed = true;
  48. } else {
  49. that.globalData.isFixed = false;
  50. }
  51. }, fail(err) {}
  52. });
  53. const updateManager = wx.getUpdateManager();
  54. updateManager.onCheckForUpdate(function (res) {
  55. // 请求完新版本信息的回调
  56. })
  57. updateManager.onUpdateReady(function () {
  58. wx.showModal({
  59. title: '更新提示',
  60. content: '新版本已经准备好,是否重启应用?',
  61. success: function (res) {
  62. if (res.confirm) {
  63. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  64. updateManager.applyUpdate()
  65. }
  66. }
  67. })
  68. });
  69. updateManager.onUpdateFailed(function () {
  70. return that.Tips({title:'新版本下载失败'});
  71. })
  72. //实例化聊天服务
  73. this.$chat = new Server(this);
  74. },
  75. $chat:null,
  76. globalData: {
  77. navHeight: 0,
  78. routineStyle: '#ffffff',
  79. openPages: '',
  80. spid: 0,
  81. code:0,
  82. urlImages: '',
  83. url: HTTP_REQUEST_URL,
  84. token: '',
  85. isLog:false, // 是否已登录
  86. expiresTime:0,
  87. MyMenus:[],
  88. userInfo:{},
  89. loginType:'routine',
  90. isFixed: false,
  91. unread: 0, // 未读消息条数
  92. },
  93. /**
  94. * 聊天事件快捷注册
  95. *
  96. */
  97. $on: function (name, action){
  98. this.$chat.$on(name,action);
  99. },
  100. /*
  101. * 信息提示 + 跳转
  102. * @param object opt {title:'提示语',icon:''} | url
  103. * @param object to_url 跳转url 有5种跳转方式 {tab:1-5,url:跳转地址}
  104. */
  105. Tips: function (opt, to_url) {
  106. return util.Tips(opt, to_url);
  107. },
  108. /**
  109. * 快捷调取助手函数
  110. */
  111. help:function()
  112. {
  113. return util.$h;
  114. },
  115. /*
  116. * 合并数组
  117. * @param array list 请求返回数据
  118. * @param array sp 原始数组
  119. * @return array
  120. */
  121. SplitArray: function (list, sp) { return util.SplitArray(list, sp)},
  122. })