user.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // pages/user/user.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. const $api = require('../../utils/api.js').API;
  5. var logindata={}
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. usercode: '',
  12. phone:'',
  13. phcode:'',
  14. openid: '',
  15. info: {},
  16. userinfo:{},
  17. todayinfo:{},
  18. todayMoney:'',
  19. status:0,
  20. text:'获取验证码'
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. this.getUserData()
  27. },
  28. zx(){
  29. wx.navigateTo({
  30. url: '../message/message',
  31. })
  32. },
  33. getcode(){
  34. if(this.data.phone.length>=11){
  35. $api.sendcode({phone:this.data.phone}).then(res=>{
  36. this.countdown()
  37. })
  38. }else{
  39. wx.showToast({
  40. icon: 'none',
  41. title: '请输入正确的手机号',
  42. })
  43. }
  44. },
  45. countdown(){
  46. var count=60,_this=this;
  47. var timer=setInterval(c,1000)
  48. function c(){
  49. if(count>0){
  50. count--;
  51. _this.setData({
  52. text:count+"s重新发送"
  53. })
  54. }else{
  55. _this.setData({
  56. text:'获取验证码'
  57. })
  58. clearInterval(timer)
  59. }
  60. }
  61. },
  62. getUserData() {
  63. $api.getAuthinfo().then(res=>{
  64. this.setData({
  65. usercode: res.data.data.usercode,
  66. userinfo: res.data.data,
  67. status:3
  68. })
  69. })
  70. .catch(err=>{
  71. this.setData({
  72. userinfo: {}
  73. })
  74. })
  75. },
  76. getTodayData() {
  77. $api.getMineLatest().then(res=>{
  78. this.setData({
  79. todayinfo: res.data.data,
  80. todayMoney:((res.data.data.today_fund - res.data.data.yesterday_fund)*10000).toFixed(2)
  81. })
  82. })
  83. .catch(err=>{
  84. this.setData({
  85. todayinfo: {},
  86. status:0
  87. })
  88. })
  89. },
  90. inputchange(e) {
  91. this.setData({
  92. phone: e.detail.value
  93. })
  94. },
  95. codechange(e) {
  96. this.setData({
  97. phcode: e.detail.value
  98. })
  99. },
  100. logout() {
  101. wx.removeStorage({
  102. key: 'userInfo'
  103. })
  104. this.setData({
  105. userinfo: {},
  106. usercode: ''
  107. })
  108. wx.navigateTo({
  109. url: '../login/user',
  110. })
  111. },
  112. /**登陆 */
  113. login: function () {
  114. var _this=this;
  115. // if (!this.data.usercode) {
  116. // wx.showToast({
  117. // icon: 'none',
  118. // title: '请输入用户代码',
  119. // })
  120. // return;
  121. // }
  122. wx.getUserProfile({
  123. desc: '用于完善会员资料',
  124. success: res=> {
  125. //获取基本信息
  126. var data = {
  127. nickName: res.userInfo.nickName,
  128. avatarUrl: res.userInfo.avatarUrl,
  129. usercode: this.data.usercode
  130. }
  131. wx.showLoading({
  132. title: '正在登陆',
  133. })
  134. wx.login({
  135. success(res) {
  136. //获取code
  137. $api.getOpenid({ code: res.code})
  138. .then(res => {
  139. //获取openid
  140. if (res.data.code != 0) {
  141. wx.showToast({
  142. icon: 'none',
  143. title: res.data.message,
  144. })
  145. wx.hideLoading();
  146. return;
  147. }
  148. data.openid = res.data.data.openid;
  149. logindata=data
  150. $api.login(data)
  151. .then(res=>{
  152. //登录成功
  153. _this.setData({
  154. status:res.data.data.status
  155. })
  156. wx.hideLoading();
  157. wx.setNavigationBarTitle({
  158. title: '',
  159. })
  160. if (res.data.code != 0) {
  161. wx.showToast({
  162. icon: 'none',
  163. title: res.data.message,
  164. })
  165. return;
  166. }
  167. wx.setStorage({
  168. key: 'userInfo',
  169. data: res.data.data,
  170. })
  171. if(res.data.data.status == 3){
  172. _this.getUserData()
  173. _this.getTodayData()
  174. // wx.switchTab({
  175. // url: '../index/index',
  176. // })
  177. }
  178. })
  179. .catch(err=>{
  180. wx.hideLoading();
  181. })
  182. })
  183. .catch(err => {
  184. //请求失败
  185. wx.hideLoading();
  186. })
  187. }
  188. })
  189. }
  190. })
  191. },
  192. login1(){
  193. logindata.phone=this.data.phone
  194. logindata.phcode=this.data.phcode
  195. $api.login(logindata).then(res=>{
  196. if(res.data.code == 0){
  197. this.getUserData()
  198. this.getTodayData()
  199. }
  200. })
  201. },
  202. login2(){
  203. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  204. if(res.data.code == 0){
  205. this.getUserData()
  206. this.getTodayData()
  207. }
  208. })
  209. },
  210. onCustomerService(){
  211. wx.openCustomerServiceChat({
  212. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  213. corpId: 'wwc4de479c81db4706',
  214. success(res) {}
  215. })
  216. },
  217. /**
  218. * 生命周期函数--监听页面初次渲染完成
  219. */
  220. onReady: function () {
  221. },
  222. /**
  223. * 生命周期函数--监听页面显示
  224. */
  225. onShow: function () {
  226. this.getUserData()
  227. this.getTodayData()
  228. },
  229. /**
  230. * 生命周期函数--监听页面隐藏
  231. */
  232. onHide: function () {
  233. },
  234. /**
  235. * 生命周期函数--监听页面卸载
  236. */
  237. onUnload: function () {
  238. },
  239. /**
  240. * 页面相关事件处理函数--监听用户下拉动作
  241. */
  242. onPullDownRefresh: function () {
  243. },
  244. /**
  245. * 页面上拉触底事件的处理函数
  246. */
  247. onReachBottom: function () {
  248. },
  249. /**
  250. * 用户点击右上角分享
  251. */
  252. onShareAppMessage: function () {
  253. }
  254. })