user.js 3.7 KB

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