app.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. console.log(res);
  27. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  28. }
  29. })
  30. // 获取用户信息
  31. wx.getSetting({
  32. success: res => {
  33. if (res.authSetting['scope.userInfo']) {
  34. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  35. wx.getUserInfo({
  36. success: res => {
  37. // 可以将 res 发送给后台解码出 unionId
  38. this.globalData.userInfo = res.userInfo
  39. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  40. // 所以此处加入 callback 以防止这种情况
  41. if (this.userInfoReadyCallback) {
  42. this.userInfoReadyCallback(res)
  43. }
  44. }
  45. })
  46. }else{
  47. // 未授权,跳转到授权页面
  48. wx.reLaunch({
  49. url: '/pages/auth/auth',
  50. })
  51. }
  52. }
  53. })
  54. },
  55. globalData: {
  56. userInfo: null
  57. }
  58. })