Usercenter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 会员中心
  7. */
  8. class Usercenter extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //提现账号添加
  13. public function bankadd() {
  14. $realname = input('realname', '', 'trim'); //账户真实姓名
  15. $banknumber = input('banknumber', '', 'trim'); //卡号或账号
  16. $bankname = input('bankname', '', 'trim'); //银行名称
  17. if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
  18. $this->error('账号姓名1-30位');
  19. }
  20. if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
  21. $this->error('账号1-50位');
  22. }
  23. if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
  24. $this->error('银行名称1-50位');
  25. }
  26. //添加
  27. $count = Db::name('user_bank')->where(['user_id' => $this->auth->id])->count('id');
  28. if ($count) {
  29. $this->error('您已经拥有该类型账号,不能再添加');
  30. }
  31. $data['user_id'] = $this->auth->id;
  32. $data['realname'] = $realname;
  33. $data['banknumber'] = $banknumber;
  34. $data['bankname'] = $bankname;
  35. $data['createtime'] = time();
  36. $rs = Db::name('user_bank')->insertGetId($data);
  37. if (!$rs) {
  38. $this->error('添加失败');
  39. }
  40. $this->success('添加成功');
  41. }
  42. //提现账号编辑
  43. public function bankedit(){
  44. $id = input('id', 0, 'intval'); //账号id
  45. $realname = input('realname', '', 'trim'); //账户真实姓名
  46. $banknumber = input('banknumber', '', 'trim'); //卡号或账号
  47. $bankname = input('bankname', '', 'trim'); //银行名称
  48. if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
  49. $this->error('账号姓名1-30位');
  50. }
  51. if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
  52. $this->error('账号1-50位');
  53. }
  54. if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
  55. $this->error('银行名称1-50位');
  56. }
  57. //查询账号是否存在
  58. $info = Db::name('user_bank')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  59. if (!$info) {
  60. $this->error('账号不存在');
  61. }
  62. $data['realname'] = $realname;
  63. $data['banknumber'] = $banknumber;
  64. $data['bankname'] = $bankname;
  65. $data['updatetime'] = time();
  66. $rs = Db::name('user_bank')->where(['id' => $id])->update($data);
  67. if ($rs === false) {
  68. $this->error('修改失败');
  69. }
  70. $this->success('修改成功');
  71. }
  72. //提现账号信息
  73. public function bankinfo() {
  74. $info = Db::name('user_bank')->where(['user_id' => $this->auth->id])->find();
  75. if (!$info) {
  76. $info = (object)[];
  77. }
  78. $this->success('账户信息', $info);
  79. }
  80. //提现
  81. public function moneywithdraw() {
  82. $id = input('id', 0, 'intval'); //提现账号id
  83. $money = input('money', '', 'trim');
  84. if (!$id) {
  85. $this->error('请选择提现账号');
  86. }
  87. $bankinfo = Db::name('user_bank')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  88. if (!$bankinfo) {
  89. $this->error('账号不存在');
  90. }
  91. if (!preg_match('/^[0-9]+(.[0-9]{1,8})?$/', $money) || $money <= 0) {
  92. $this->error('请输入正确提现金额');
  93. }
  94. //余额查询
  95. $user_money = model('wallet')->getWallet($this->auth->id,'money');
  96. if ($user_money < $money) {
  97. $this->error('余额不足');
  98. }
  99. //查询最低最高提现金额
  100. $min_withdrawal_money = config('site.min_withdrawal_money') ? config('site.min_withdrawal_money') : 1;
  101. $max_withdrawal_money = config('site.max_withdrawal_money') ? config('site.max_withdrawal_money') : 50000;
  102. if ($money < $min_withdrawal_money) {
  103. $this->error('最低提现金额' . $min_withdrawal_money . '元');
  104. }
  105. if ($money > $max_withdrawal_money) {
  106. $this->error('最高提现金额' . $max_withdrawal_money . '元');
  107. }
  108. //查询提现手续费百分比
  109. $withdrawal_service_fee = (int)config('site.withdrawal_service_fee') ? : 0;
  110. if ($withdrawal_service_fee < 0 || $withdrawal_service_fee >= 100) {
  111. $this->error('提现手续费异常');
  112. }
  113. $real_money = bcdiv(bcmul($money,bcsub(100,$withdrawal_service_fee)),100);
  114. $data['order_no'] = createUniqueNo('T',$this->auth->id);
  115. $data['user_id'] = $this->auth->id;
  116. $data['amount'] = $money;
  117. $data['money'] = $real_money;
  118. $data['user_bank_id'] = $id;
  119. $data['realname'] = $bankinfo['realname'];
  120. $data['banknumber'] = $bankinfo['banknumber'];
  121. $data['bankname'] = $bankinfo['bankname'];
  122. $data['status'] = 0;
  123. $data['createtime'] = time();
  124. //开启事务
  125. Db::startTrans();
  126. //添加提现记录
  127. $log_id = Db::name('user_withdraw')->insertGetId($data);
  128. if (!$log_id) {
  129. Db::rollback();
  130. $this->error('申请提现失败');
  131. }
  132. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'money',$money,4,'提现','user_withdraw',$log_id);
  133. if ($rs_wallet['status'] == false) {
  134. $this->error($rs_wallet['msg']);
  135. Db::rollback();
  136. }
  137. Db::commit();
  138. $this->success('申请提现成功,请等待审核');
  139. }
  140. //用户钱包流水
  141. public function moneylog(){
  142. $list = Db::name('user_money_log')->field('id,change_value,log_type,createtime')->where('user_id',$this->auth->id)->page($this->page,$this->listrow)->select();
  143. foreach($list as $key => &$val){
  144. $val['log_type_text'] = model('wallet')->getlogtype($val['log_type']);
  145. }
  146. $this->success('success',$list);
  147. }
  148. }