index.js 1.3 KB

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