app.js 2.2 KB

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