user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.showLoading({
  65. title: '正在登陆',
  66. })
  67. wx.getUserInfo({
  68. success(res){
  69. //获取基本信息
  70. var data={
  71. nickName: res.userInfo.nickName,
  72. avatarUrl: res.userInfo.avatarUrl,
  73. usercode: _this.data.usercode
  74. }
  75. wx.login({
  76. success(res) {
  77. //获取code
  78. wx.request({
  79. url: host + '/api/wx/openid',
  80. data: {
  81. code: res.code
  82. },
  83. success(res) {
  84. //获取openid
  85. data.openid=res.data.data.openid
  86. wx.request({
  87. url: host + '/api/wx/login',
  88. method:'POST',
  89. data: data,
  90. success(res){
  91. //登陆成功
  92. console.log(res)
  93. wx.hideLoading();
  94. if(res.data.code != 0){
  95. wx.showToast({
  96. icon: 'none',
  97. title: res.data.message,
  98. })
  99. return;
  100. }
  101. _this.setData({
  102. userInfo : res.data.data
  103. })
  104. wx.setStorage({
  105. key: 'userInfo',
  106. data: res.data.data,
  107. })
  108. },
  109. fail(error){
  110. console.log(error)
  111. wx.hideLoading();
  112. }
  113. })
  114. },
  115. fail() {
  116. wx.hideLoading();
  117. }
  118. })
  119. }
  120. })
  121. }
  122. })
  123. },
  124. /**
  125. * 生命周期函数--监听页面初次渲染完成
  126. */
  127. onReady: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面显示
  131. */
  132. onShow: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面隐藏
  136. */
  137. onHide: function () {
  138. },
  139. /**
  140. * 生命周期函数--监听页面卸载
  141. */
  142. onUnload: function () {
  143. },
  144. /**
  145. * 页面相关事件处理函数--监听用户下拉动作
  146. */
  147. onPullDownRefresh: function () {
  148. },
  149. /**
  150. * 页面上拉触底事件的处理函数
  151. */
  152. onReachBottom: function () {
  153. },
  154. /**
  155. * 用户点击右上角分享
  156. */
  157. onShareAppMessage: function () {
  158. }
  159. })