app.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  9. wx.getSystemInfo({
  10. success: res => {
  11. let statusBarHeight = res.statusBarHeight,
  12. navTop = menuButtonObject.top,//胶囊按钮与顶部的距离
  13. navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight)*2;//导航高度
  14. this.globalData.statusBarHeight = statusBarHeight;
  15. this.globalData.navHeight = navHeight;
  16. this.globalData.navTop = navTop;
  17. this.globalData.windowHeight = res.windowHeight;
  18. },
  19. fail(err) {
  20. console.log(err);
  21. },
  22. })
  23. // 登录
  24. wx.login({
  25. success: res => {
  26. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  27. }
  28. })
  29. // 获取用户信息
  30. wx.getSetting({
  31. success: res => {
  32. if (res.authSetting['scope.userInfo']) {
  33. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  34. wx.getUserInfo({
  35. success: res => {
  36. // 可以将 res 发送给后台解码出 unionId
  37. this.globalData.userInfo = res.userInfo
  38. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  39. // 所以此处加入 callback 以防止这种情况
  40. if (this.userInfoReadyCallback) {
  41. this.userInfoReadyCallback(res)
  42. }
  43. }
  44. })
  45. }else{
  46. // 未授权,跳转到授权页面
  47. wx.reLaunch({
  48. url: '/pages/auth/auth',
  49. })
  50. }
  51. }
  52. })
  53. },
  54. globalData: {
  55. userInfo: null
  56. }
  57. })