user.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // pages/user/user.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. const $api = require('../../utils/api.js').API;
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. usercode: '',
  11. openid: '',
  12. info: {},
  13. userinfo:{}
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function (options) {
  19. this.getUserData()
  20. },
  21. getUserData() {
  22. $api.getAuthinfo().then(res=>{
  23. wx.setNavigationBarTitle({
  24. title: '',
  25. })
  26. this.setData({
  27. usercode: res.data.data.usercode,
  28. userinfo: res.data.data
  29. })
  30. })
  31. .catch(err=>{
  32. this.setData({
  33. userinfo: {}
  34. })
  35. })
  36. },
  37. inputchange(e) {
  38. this.setData({
  39. usercode: e.detail.value
  40. })
  41. },
  42. logout() {
  43. wx.removeStorage({
  44. key: 'userInfo'
  45. })
  46. wx.setNavigationBarTitle({
  47. title: '登录',
  48. })
  49. this.setData({
  50. userinfo: {},
  51. usercode: ''
  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. $api.getOpenid({ code: res.code})
  80. .then(res => {
  81. //获取openid
  82. if (res.data.code != 0) {
  83. wx.showToast({
  84. icon: 'none',
  85. title: res.data.message,
  86. })
  87. wx.hideLoading();
  88. return;
  89. }
  90. data.openid = res.data.data.openid;
  91. $api.login(data)
  92. .then(res=>{
  93. //登录成功
  94. wx.hideLoading();
  95. wx.setNavigationBarTitle({
  96. title: '',
  97. })
  98. if (res.data.code != 0) {
  99. wx.showToast({
  100. icon: 'none',
  101. title: res.data.message,
  102. })
  103. return;
  104. }
  105. wx.setStorage({
  106. key: 'userInfo',
  107. data: res.data.data,
  108. })
  109. _this.getUserData()
  110. })
  111. .catch(err=>{
  112. wx.hideLoading();
  113. })
  114. })
  115. .catch(err => {
  116. //请求失败
  117. wx.hideLoading();
  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. })