user.js 4.1 KB

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