user.js 5.8 KB

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