user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (res.data){
  22. this.setData({
  23. userInfo: res.data
  24. })
  25. this.getUserData();
  26. }
  27. }
  28. })
  29. },
  30. getUserData(){
  31. wx.request({
  32. url: host+'/api/wx/authinfo',
  33. header:{
  34. 'Authorization':this.data.userInfo.token
  35. },
  36. success:res=>{
  37. this.setData({
  38. usercode: res.data.data.usercode
  39. })
  40. },
  41. fail:error=>{
  42. //失败重新登陆
  43. this.setData({
  44. userInfo: {}
  45. })
  46. }
  47. })
  48. },
  49. inputchange(e){
  50. this.setData({
  51. usercode:e.detail.value
  52. })
  53. },
  54. /**登陆 */
  55. login:function(){
  56. var _this=this;
  57. if(!this.data.usercode){
  58. wx.showToast({
  59. icon:'none',
  60. title: '请输入用户代码',
  61. })
  62. return;
  63. }
  64. wx.getUserProfile({
  65. desc: '用于完善会员资料',
  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.showLoading({
  74. title: '正在登陆',
  75. })
  76. wx.login({
  77. success(res) {
  78. //获取code
  79. wx.request({
  80. url: host + '/api/wx/openid',
  81. data: {
  82. code: res.code
  83. },
  84. success(res) {
  85. //获取openid
  86. data.openid=res.data.data.openid
  87. wx.request({
  88. url: host + '/api/wx/login',
  89. method:'POST',
  90. data: data,
  91. success(res){
  92. //登陆成功
  93. console.log(res)
  94. wx.hideLoading();
  95. if(res.data.code != 0){
  96. wx.showToast({
  97. icon: 'none',
  98. title: res.data.message,
  99. })
  100. return;
  101. }
  102. _this.setData({
  103. userInfo : res.data.data
  104. })
  105. wx.setStorage({
  106. key: 'userInfo',
  107. data: res.data.data,
  108. })
  109. },
  110. fail(error){
  111. console.log(error)
  112. wx.hideLoading();
  113. }
  114. })
  115. },
  116. fail() {
  117. wx.hideLoading();
  118. }
  119. })
  120. }
  121. })
  122. }
  123. })
  124. },
  125. /**
  126. * 生命周期函数--监听页面初次渲染完成
  127. */
  128. onReady: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面显示
  132. */
  133. onShow: function () {
  134. },
  135. /**
  136. * 生命周期函数--监听页面隐藏
  137. */
  138. onHide: function () {
  139. },
  140. /**
  141. * 生命周期函数--监听页面卸载
  142. */
  143. onUnload: function () {
  144. },
  145. /**
  146. * 页面相关事件处理函数--监听用户下拉动作
  147. */
  148. onPullDownRefresh: function () {
  149. },
  150. /**
  151. * 页面上拉触底事件的处理函数
  152. */
  153. onReachBottom: function () {
  154. },
  155. /**
  156. * 用户点击右上角分享
  157. */
  158. onShareAppMessage: function () {
  159. }
  160. })