app.js 3.5 KB

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