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