shouquan.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. openid: '',
  11. message: '',
  12. },
  13. //事件处理函数
  14. bindViewTap: function() {
  15. wx.navigateTo({
  16. url: '../logs/logs'
  17. })
  18. },
  19. onLoad: function () {
  20. var that = this
  21. console.log("孙");
  22. console.log("-"+app.globalData.quanju);
  23. if (app.globalData.userInfo) {
  24. this.setData({
  25. userInfo: app.globalData.userInfo,
  26. hasUserInfo: true
  27. })
  28. } else if (this.data.canIUse){
  29. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  30. // 所以此处加入 callback 以防止这种情况
  31. app.userInfoReadyCallback = res => {
  32. this.setData({
  33. userInfo: res.userInfo,
  34. hasUserInfo: true
  35. })
  36. }
  37. } else {
  38. // 在没有 open-type=getUserInfo 版本的兼容处理
  39. wx.getUserInfo({
  40. success: res => {
  41. app.globalData.userInfo = res.userInfo
  42. this.setData({
  43. userInfo: res.userInfo,
  44. hasUserInfo: true
  45. })
  46. }
  47. })
  48. }
  49. // 获取openid
  50. wx.login({
  51. success: function (res) {
  52. if (res.code) {
  53. wx.getUserInfo({
  54. success: function (res) {
  55. console.log("存在code")
  56. }
  57. });
  58. var appid = "wxc55158faad3e366a"
  59. var secret = "f38f654411e153673ff4a473f1ccefd0"
  60. var openid = ""
  61. var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
  62. wx.request({
  63. url: l,
  64. data: {},
  65. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  66. // header: {}, // 设置请求的 header
  67. success: function (res) {
  68. var obj = {};
  69. obj.openid = res.data.openid;
  70. console.log("取得的openid==" + res.data.openid);
  71. that.setData({
  72. openid: obj.openid
  73. })
  74. obj.expires_in = Date.now() + res.data.expires_in;
  75. // console.log(obj);
  76. // wx.setStorageSync('user', obj);//存储openid
  77. }
  78. });
  79. } else {
  80. console.log('获取用户登录态失败!' + res.errMsg)
  81. }
  82. }
  83. });
  84. //获取登录用户的code
  85. // wx.login({
  86. // success(res){
  87. // if(res.code){
  88. // console.log("当前code存在")
  89. // //发送code
  90. // wx.request({
  91. // url: 'http://localhost:8080/sendOpenid',
  92. // data:{
  93. // code:res.code
  94. // }
  95. // })
  96. // }
  97. // else{
  98. // console.log('登录失败!' + res.errMsg)
  99. // }
  100. // }
  101. // })
  102. },
  103. getUserInfo: function(e) {
  104. console.log(e)
  105. console.log("---");
  106. app.globalData.userInfo = e.detail.userInfo
  107. this.setData({
  108. userInfo: e.detail.userInfo,
  109. hasUserInfo: true
  110. })
  111. }
  112. })