user.js 4.1 KB

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