index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.navigateTo({
  14. url: '../logs/logs'
  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. console.log("page.userInfoReadyCallback" + res.iv)
  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. bindGetUserInfo: function(e) {
  47. console.log("page.bindGetUserInfo" + e.detail.iv)
  48. console.log(e)
  49. app.globalData.userInfo = e.detail.userInfo
  50. this.setData({
  51. userInfo: e.detail.userInfo,
  52. hasUserInfo: true
  53. })
  54. },
  55. login: function() {
  56. // 获取userInfo
  57. wx.getUserInfo({
  58. success: res => {
  59. app.globalData.rawData = res.rawData
  60. app.globalData.signature = res.signature
  61. console.log(app.globalData.rawData)
  62. console.log(app.globalData.signature)
  63. }
  64. }),
  65. wx.login({
  66. success(res) {
  67. if (res.code) {
  68. //发起网络请求
  69. wx.request({
  70. url: app.globalData.host + "/register",
  71. data: {
  72. jsCode: res.code,
  73. rawData: app.globalData.rawData,
  74. signature: app.globalData.signature
  75. },
  76. header: {
  77. 'content-type': 'application/json' // 默认值
  78. },
  79. dataType: "json",
  80. method: "POST",
  81. success(res) {
  82. console.log(res.data)
  83. }
  84. })
  85. } else {
  86. console.log('登录失败!' + res.errMsg)
  87. }
  88. }
  89. })
  90. }
  91. })