user.js 3.5 KB

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