index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // 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. bindGetUserInfo: 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. },
  53. login: function() {
  54. // 获取userInfo
  55. wx.getUserInfo({
  56. success: res => {
  57. app.globalData.userInfo = res.userInfo,
  58. app.globalData.rawData = res.rawData,
  59. app.globalData.signature = res.signature,
  60. app.globalData.encryptedData = res.encryptedData,
  61. app.globalData.iv = res.iv
  62. }
  63. }),
  64. wx.login({
  65. success(res) {
  66. if (res.code) {
  67. //发起网络请求
  68. wx.request({
  69. url: app.globalData.host + "/wx/user/login/" + res.code,
  70. data: app.globalData.userInfo,
  71. header: {
  72. 'content-type': 'application/json' // 默认值
  73. },
  74. dataType: "json",
  75. method: "POST",
  76. success(res) {
  77. console.log(res.data)
  78. }
  79. })
  80. } else {
  81. console.log('登录失败!' + res.errMsg)
  82. }
  83. }
  84. })
  85. }
  86. })