user.js 5.9 KB

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