UserExtract.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/3/3
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\workerman\ChannelService;
  10. use crmeb\traits\ModelTrait;
  11. /**
  12. * TODO 用户提现
  13. * Class UserExtract
  14. * @package app\models\user
  15. */
  16. class UserExtract extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'user_extract';
  28. use ModelTrait;
  29. //审核中
  30. const AUDIT_STATUS = 0;
  31. //未通过
  32. const FAIL_STATUS = -1;
  33. //已提现
  34. const SUCCESS_STATUS = 1;
  35. protected static $extractType = ['alipay','bank','weixin'];
  36. protected static $extractTypeMsg = ['alipay'=>'支付宝','bank'=>'银行卡','weixin'=>'微信'];
  37. protected static $status = array(
  38. -1=>'未通过',
  39. 0 =>'审核中',
  40. 1 =>'已提现'
  41. );
  42. /**
  43. * 用户自主提现记录提现记录,后台执行审核
  44. * @param array $userInfo 用户个人信息
  45. * @param array $data 提现详细信息
  46. * @return bool
  47. */
  48. public static function userExtract($userInfo,$data){
  49. if(!in_array($data['extract_type'],self::$extractType))
  50. return self::setErrorInfo('提现方式不存在');
  51. $userInfo = User::get($userInfo['uid']);
  52. $extractPrice = $userInfo['brokerage_price'];
  53. if($extractPrice < 0) return self::setErrorInfo('提现佣金不足'.$data['money']);
  54. if($data['money'] > $extractPrice) return self::setErrorInfo('提现佣金不足'.$data['money']);
  55. if($data['money'] <= 0) return self::setErrorInfo('提现佣金大于0');
  56. $balance = bcsub($userInfo['brokerage_price'],$data['money'],2);
  57. if($balance < 0) $balance=0;
  58. $insertData = [
  59. 'uid' => $userInfo['uid'],
  60. 'extract_type' => $data['extract_type'],
  61. 'extract_price' => $data['money']*0.97,
  62. 'add_time' => time(),
  63. 'balance' => $balance,
  64. 'status' => self::AUDIT_STATUS,
  65. 'money' => $data['money']
  66. ];
  67. if(isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  68. else $insertData['real_name'] = $userInfo['nickname'];
  69. if(isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  70. else $insertData['bank_code'] = '';
  71. if(isset($data['bankname'])) $insertData['bank_address']=$data['bankname'];
  72. else $insertData['bank_address']='';
  73. if(isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  74. else $insertData['wechat'] = $userInfo['nickname'];
  75. if($data['extract_type'] == 'alipay'){
  76. if(!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
  77. $insertData['alipay_code'] = $data['alipay_code'];
  78. $mark = '使用支付宝提现'.$insertData['extract_price'].'元';
  79. }else if($data['extract_type'] == 'bank'){
  80. if(!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
  81. if(!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
  82. $mark = '使用银联卡'.$insertData['bank_code'].'提现'.$insertData['extract_price'].'元';
  83. }else if($data['extract_type'] == 'weixin'){
  84. if(!$data['weixin']) return self::setErrorInfo('请输入微信账号');
  85. $mark = '使用微信提现'.$insertData['extract_price'].'元';
  86. }
  87. self::beginTrans();
  88. try{
  89. $res1 = self::create($insertData);
  90. if(!$res1) return self::setErrorInfo('提现失败');
  91. $res2 = User::edit(['brokerage_price'=>$balance],$userInfo['uid'],'uid');
  92. $res3 = UserBill::expend('余额提现',$userInfo['uid'],'now_money','extract',$data['money'],$res1['id'],$balance,$mark);
  93. $res = $res2 && $res3;
  94. if($res){
  95. self::commitTrans();
  96. try{
  97. ChannelService::instance()->send('WITHDRAW', ['id'=>$res1->id]);
  98. }catch (\Exception $e){}
  99. event('AdminNewPush');
  100. //发送模板消息
  101. return true;
  102. }else return self::setErrorInfo('提现失败!');
  103. }catch (\Exception $e){
  104. self::rollbackTrans();
  105. return self::setErrorInfo('提现失败!');
  106. }
  107. }
  108. /**
  109. * 获得用户最后一次提现信息
  110. * @param $openid
  111. * @return mixed
  112. */
  113. public static function userLastInfo($uid)
  114. {
  115. return self::where(compact('uid'))->order('add_time DESC')->find();
  116. }
  117. /**
  118. * 获得用户提现总金额
  119. * @param $uid
  120. * @return mixed
  121. */
  122. public static function userExtractTotalPrice($uid,$status=self::SUCCESS_STATUS)
  123. {
  124. return self::where('uid',$uid)->where('status',$status)->value('SUM(extract_price)')?:0;
  125. }
  126. /**
  127. * 用户提现记录列表
  128. * @param int $uid 用户uid
  129. * @param int $first 截取行数
  130. * @param int $limit 截取数
  131. * @return \think\Collection
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public static function extractList($uid,$first = 0,$limit = 8)
  137. {
  138. $list=UserExtract::where('uid',$uid)->order('add_time desc')->limit($first,$limit)->select();
  139. foreach($list as &$v){
  140. $v['add_time']=date('Y/m/d',$v['add_time']);
  141. }
  142. return $list;
  143. }
  144. /**
  145. * 获取累计已提现佣金
  146. * @param $uid
  147. * @return float
  148. */
  149. public static function extractSum($uid)
  150. {
  151. return self::where('uid',$uid)->where('status',1)->sum('extract_price');
  152. }
  153. }