index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. motto: 'Hello World',
  7. userInfo: {},
  8. hasUserInfo: false,
  9. canIUse: wx.canIUse('button.open-type.getUserInfo')
  10. },
  11. //事件处理函数
  12. bindViewTap: function () {
  13. wx.redirectTo({
  14. url: '../home/home'
  15. })
  16. },
  17. onLoad: function () {
  18. if (app.globalData.userInfo) {
  19. this.setData({
  20. userInfo: app.globalData.userInfo,
  21. hasUserInfo: true
  22. })
  23. } else if (this.data.canIUse) {
  24. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  25. // 所以此处加入 callback 以防止这种情况
  26. app.userInfoReadyCallback = res => {
  27. this.setData({
  28. userInfo: res.userInfo,
  29. hasUserInfo: true
  30. })
  31. }
  32. } else {
  33. // 在没有 open-type=getUserInfo 版本的兼容处理
  34. wx.getUserInfo({
  35. success: res => {
  36. app.globalData.userInfo = res.userInfo
  37. this.setData({
  38. userInfo: res.userInfo,
  39. hasUserInfo: true
  40. })
  41. }
  42. })
  43. }
  44. },
  45. getUserInfo: function (e) {
  46. console.log(e)
  47. app.globalData.userInfo = e.detail.userInfo
  48. this.setData({
  49. userInfo: e.detail.userInfo,
  50. hasUserInfo: true
  51. })
  52. this.bindViewTap()
  53. }
  54. })