app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. var userinfo = wx.getStorageSync('sg-userinfo')
  5. if(!userinfo) {
  6. wx.redirectTo({
  7. url: '/pages/login/index',
  8. })
  9. } else {
  10. this.globalData.userInfo = userinfo
  11. }
  12. // 展示本地存储能力
  13. // var logs = wx.getStorageSync('logs') || []
  14. // logs.unshift(Date.now())
  15. // wx.setStorageSync('logs', logs)
  16. // 登录
  17. // wx.login({
  18. // success: res => {
  19. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  20. // }
  21. // })
  22. // 获取用户信息
  23. // wx.getSetting({
  24. // success: res => {
  25. // if (res.authSetting['scope.userInfo']) {
  26. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  27. // wx.getUserInfo({
  28. // success: res => {
  29. // // 可以将 res 发送给后台解码出 unionId
  30. // this.globalData.userInfo = res.userInfo
  31. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  32. // // 所以此处加入 callback 以防止这种情况
  33. // if (this.userInfoReadyCallback) {
  34. // this.userInfoReadyCallback(res)
  35. // }
  36. // }
  37. // })
  38. // }
  39. // }
  40. // })
  41. },
  42. globalData: {
  43. userInfo: null
  44. },
  45. loginCallback: function(data) {
  46. this.updateUserInfo(data)
  47. wx.redirectTo({
  48. url: '/pages/index/index',
  49. })
  50. },
  51. updateUserInfo: function(info) {
  52. this.globalData.userInfo = info
  53. wx.setStorageSync('sg-userinfo', info)
  54. }
  55. })