usercenter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. motto: 'Hello World',
  8. userInfo: {},
  9. hasUserInfo: false,
  10. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  11. openid: '',
  12. message: '',
  13. resturl: app.data.resturl + '/userinfo.action',
  14. uinfo:''
  15. },
  16. //事件处理函数
  17. bindViewTap: function() {
  18. wx.navigateTo({
  19. url: '../logs/logs'
  20. })
  21. },
  22. onLoad: function () {
  23. var that = this
  24. console.log("孙");
  25. console.log("-"+app.globalData.quanju);
  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. // 获取openid
  53. wx.login({
  54. success: function (res) {
  55. if (res.code) {
  56. wx.getUserInfo({
  57. success: function (res) {
  58. console.log("存在code")
  59. }
  60. });
  61. var appid = "wxc55158faad3e366a"
  62. var secret = "f38f654411e153673ff4a473f1ccefd0"
  63. var openid = ""
  64. var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
  65. wx.request({
  66. url: l,
  67. data: {},
  68. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  69. // header: {}, // 设置请求的 header
  70. success: function (res) {
  71. var obj = {};
  72. obj.openid = res.data.openid;
  73. console.log("取得的openid==" + res.data.openid);
  74. that.setData({
  75. openid: obj.openid
  76. })
  77. obj.expires_in = Date.now() + res.data.expires_in;
  78. // console.log(obj);
  79. // wx.setStorageSync('user', obj);//存储openid
  80. }
  81. });
  82. } else {
  83. console.log('获取用户登录态失败!' + res.errMsg)
  84. }
  85. }
  86. });
  87. //获取登录用户的code
  88. // wx.login({
  89. // success(res){
  90. // if(res.code){
  91. // console.log("当前code存在")
  92. // //发送code
  93. // wx.request({
  94. // url: 'http://localhost:8080/sendOpenid',
  95. // data:{
  96. // code:res.code
  97. // }
  98. // })
  99. // }
  100. // else{
  101. // console.log('登录失败!' + res.errMsg)
  102. // }
  103. // }
  104. // })
  105. },
  106. getUserInfo: function(e) {
  107. console.log(e)
  108. console.log("---");
  109. app.globalData.userInfo = e.detail.userInfo
  110. this.setData({
  111. userInfo: e.detail.userInfo,
  112. hasUserInfo: true
  113. })
  114. },
  115. /**
  116. * 生命周期函数--监听页面加载
  117. */
  118. onLoad: function (options) {
  119. },
  120. /**
  121. * 生命周期函数--监听页面初次渲染完成
  122. */
  123. onReady: function () {
  124. },
  125. /**
  126. * 生命周期函数--监听页面显示
  127. */
  128. onShow: function () {
  129. this.getUserinfo();
  130. },
  131. loginout:function(){
  132. app.globalData.openid = "";
  133. wx.switchTab({
  134. url: '../../pages/index/index'
  135. })
  136. },
  137. getUserinfo: function () {
  138. let openid = app.globalData.openid;
  139. let that = this;
  140. if (openid == '') {
  141. wx.redirectTo({
  142. url: '../../pages/login/login',
  143. })
  144. return;
  145. }
  146. let param = "openid=" + app.globalData.openid;
  147. wx.request({
  148. url: app.data.resturl + '/uinfo.action',
  149. data: {
  150. uid: openid
  151. },
  152. method: 'post',
  153. header: {
  154. 'content-type': 'application/x-www-form-urlencoded'
  155. },
  156. success: (res) => {
  157. console.log(res);
  158. that.setData({
  159. dinfo: res.data.uinfo
  160. });
  161. }
  162. })
  163. },
  164. })