app.js 1.4 KB

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