user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // pages/user/user.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. usercode:'',
  10. openid:'',
  11. info:{},
  12. userInfo:{}, //登陆成功的信息
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. wx.getStorage({
  19. key: 'userInfo',
  20. success:res=>{
  21. this.setData({
  22. userInfo:res.data
  23. })
  24. this.getUserData();
  25. }
  26. })
  27. },
  28. getUserData(){
  29. wx.request({
  30. url: host+'/api/wx/authinfo',
  31. header:{
  32. 'Authorization':this.data.userInfo.token
  33. },
  34. success:res=>{
  35. this.setData({
  36. usercode: res.data.data.usercode
  37. })
  38. },
  39. fail:error=>{
  40. //失败重新登陆
  41. this.setData({
  42. userInfo: {}
  43. })
  44. }
  45. })
  46. },
  47. inputchange(e){
  48. this.setData({
  49. usercode:e.detail.value
  50. })
  51. },
  52. /**登陆 */
  53. login:function(){
  54. var _this=this;
  55. if(!this.data.usercode){
  56. wx.showToast({
  57. icon:'none',
  58. title: '请输入用户代码',
  59. })
  60. return;
  61. }
  62. wx.showLoading({
  63. title: '正在登陆',
  64. })
  65. wx.getUserInfo({
  66. success(res){
  67. //获取基本信息
  68. var data={
  69. nickName: res.userInfo.nickName,
  70. avatarUrl: res.userInfo.avatarUrl,
  71. usercode: _this.data.usercode
  72. }
  73. wx.login({
  74. success(res) {
  75. //获取code
  76. wx.request({
  77. url: host + '/api/wx/openid',
  78. data: {
  79. code: res.code
  80. },
  81. success(res) {
  82. //获取openid
  83. data.openid=res.data.data.openid
  84. wx.request({
  85. url: host + '/api/wx/login',
  86. method:'POST',
  87. data: data,
  88. success(res){
  89. //登陆成功
  90. wx.hideLoading();
  91. console.log(res.data.data)
  92. _this.setData({
  93. userInfo : res.data.data
  94. })
  95. wx.setStorage({
  96. key: 'userInfo',
  97. data: res.data.data,
  98. })
  99. },
  100. fail(){
  101. wx.hideLoading();
  102. }
  103. })
  104. },
  105. fail() {
  106. wx.hideLoading();
  107. }
  108. })
  109. }
  110. })
  111. }
  112. })
  113. },
  114. /**
  115. * 生命周期函数--监听页面初次渲染完成
  116. */
  117. onReady: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面显示
  121. */
  122. onShow: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面隐藏
  126. */
  127. onHide: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面卸载
  131. */
  132. onUnload: function () {
  133. },
  134. /**
  135. * 页面相关事件处理函数--监听用户下拉动作
  136. */
  137. onPullDownRefresh: function () {
  138. },
  139. /**
  140. * 页面上拉触底事件的处理函数
  141. */
  142. onReachBottom: function () {
  143. },
  144. /**
  145. * 用户点击右上角分享
  146. */
  147. onShareAppMessage: function () {
  148. }
  149. })