user.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. is_login: 'false',
  8. nikename: '',
  9. avatar: '',
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function(options) {
  15. },
  16. /**
  17. * 生命周期函数--监听页面初次渲染完成
  18. */
  19. onReady: function() {
  20. },
  21. /**
  22. * 生命周期函数--监听页面显示
  23. */
  24. onShow: function() {
  25. var that = this
  26. if (wx.getStorageSync('user')) {
  27. that.setData({
  28. is_login: false,
  29. nikename: wx.getStorageSync('user').username,
  30. avatar: wx.getStorageSync('user').avatar
  31. })
  32. } else {
  33. that.setData({
  34. is_login: true
  35. })
  36. }
  37. },
  38. /**
  39. * 生命周期函数--监听页面隐藏
  40. */
  41. onHide: function() {
  42. },
  43. /**
  44. * 生命周期函数--监听页面卸载
  45. */
  46. onUnload: function() {
  47. },
  48. /**
  49. * 页面相关事件处理函数--监听用户下拉动作
  50. */
  51. onPullDownRefresh: function() {
  52. },
  53. /**
  54. * 页面上拉触底事件的处理函数
  55. */
  56. onReachBottom: function() {
  57. },
  58. /**
  59. * 用户点击右上角分享
  60. */
  61. onShareAppMessage: function() {
  62. },
  63. close_login: function() {
  64. this.setData({
  65. is_login: false
  66. })
  67. },
  68. userLogin: function(e) {
  69. this.setData({
  70. is_login: false
  71. })
  72. wx.showLoading({
  73. title: '登录中',
  74. })
  75. console.log(e.detail)
  76. var un = JSON.parse(e.detail.rawData)
  77. var that = this
  78. try {
  79. wx.login({
  80. success: function(res) {
  81. let code = res.code
  82. console.log(code)
  83. let tx = un.avatarUrl
  84. let nn = un.nickName
  85. app.request({
  86. url: 'https://t6.9026.com/api/furniture/service_login',
  87. method: 'POST',
  88. data: {
  89. code: code,
  90. avatar: tx,
  91. nickName: nn,
  92. store_id: app.globalData.store_id
  93. },
  94. success: function(res) {
  95. console.log(res.data)
  96. wx.setStorageSync("token", res.data.data.token)
  97. wx.setStorageSync('key', res.data.data.session_key)
  98. wx.setStorageSync("user", res.data.data.user);
  99. wx.setStorageSync('ma', 0)
  100. wx.setStorageSync('pa', 0)
  101. wx.setStorageSync('us', 0)
  102. wx.hideLoading();
  103. wx.showToast({
  104. title: '登录成功',
  105. icon: 'success',
  106. duration: 1000,
  107. success: () => {
  108. that.setData({
  109. nikename: wx.getStorageSync('user').username,
  110. avatar: wx.getStorageSync('user').avatar
  111. })
  112. }
  113. })
  114. }
  115. })
  116. }
  117. })
  118. } catch (e) {
  119. console.log(e.message)
  120. }
  121. },
  122. })