index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //index.js
  2. //获取应用实例
  3. const getReq = require('./../../config.js').getReq
  4. const app = getApp()
  5. Page({
  6. data: {
  7. motto: 'Hello World',
  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. getReq({
  20. goods_id: '4565',
  21. act: 'goods_common',
  22. op: 'index',
  23. }, function (res) {
  24. console.log(res)
  25. })
  26. if (app.globalData.userInfo) {
  27. this.setData({
  28. userInfo: app.globalData.userInfo,
  29. hasUserInfo: true
  30. })
  31. } else if (this.data.canIUse) {
  32. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  33. // 所以此处加入 callback 以防止这种情况
  34. app.userInfoReadyCallback = res => {
  35. this.setData({
  36. userInfo: res.userInfo,
  37. hasUserInfo: true
  38. })
  39. }
  40. } else {
  41. // 在没有 open-type=getUserInfo 版本的兼容处理
  42. wx.getUserInfo({
  43. success: res => {
  44. app.globalData.userInfo = res.userInfo
  45. this.setData({
  46. userInfo: res.userInfo,
  47. hasUserInfo: true
  48. })
  49. }
  50. })
  51. }
  52. },
  53. getUserInfo: function (e) {
  54. console.log(e)
  55. app.globalData.userInfo = e.detail.userInfo
  56. this.setData({
  57. userInfo: e.detail.userInfo,
  58. hasUserInfo: true
  59. })
  60. }
  61. })