user.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if(res.data.code != 0){
  96. wx.showToast({
  97. icon: 'none',
  98. title: res.data.message,
  99. })
  100. wx.hideLoading();
  101. return;
  102. }
  103. data.openid=res.data.data.openid
  104. wx.request({
  105. url: host + '/api/wx/login',
  106. method:'POST',
  107. data: data,
  108. success(res){
  109. //登陆成功
  110. console.log(res)
  111. wx.hideLoading();
  112. if(res.data.code != 0){
  113. wx.showToast({
  114. icon: 'none',
  115. title: res.data.message,
  116. })
  117. return;
  118. }
  119. _this.setData({
  120. userInfo : res.data.data
  121. })
  122. wx.setStorage({
  123. key: 'userInfo',
  124. data: res.data.data,
  125. })
  126. },
  127. fail(error){
  128. console.log(error)
  129. wx.hideLoading();
  130. }
  131. })
  132. },
  133. fail() {
  134. wx.hideLoading();
  135. }
  136. })
  137. }
  138. })
  139. }
  140. })
  141. },
  142. /**
  143. * 生命周期函数--监听页面初次渲染完成
  144. */
  145. onReady: function () {
  146. },
  147. /**
  148. * 生命周期函数--监听页面显示
  149. */
  150. onShow: function () {
  151. },
  152. /**
  153. * 生命周期函数--监听页面隐藏
  154. */
  155. onHide: function () {
  156. },
  157. /**
  158. * 生命周期函数--监听页面卸载
  159. */
  160. onUnload: function () {
  161. },
  162. /**
  163. * 页面相关事件处理函数--监听用户下拉动作
  164. */
  165. onPullDownRefresh: function () {
  166. },
  167. /**
  168. * 页面上拉触底事件的处理函数
  169. */
  170. onReachBottom: function () {
  171. },
  172. /**
  173. * 用户点击右上角分享
  174. */
  175. onShareAppMessage: function () {
  176. }
  177. })