WithdrawService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\UserModel;
  4. use app\common\model\WithDrawLogModel;
  5. use utils\RedisCache;
  6. /**
  7. * 提现服务 by wes
  8. * Class WithdrawService
  9. * @package app\common\service
  10. */
  11. class WithdrawService
  12. {
  13. protected static $instance = null;
  14. protected $model = null;
  15. public function __construct()
  16. {
  17. $this->model = new WithDrawLogModel();
  18. }
  19. /**
  20. * 静态化入口
  21. * @return static|null
  22. */
  23. public static function make()
  24. {
  25. if(!self::$instance){
  26. self::$instance = new static();
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. * 当日是否提现过
  32. * @param $uid
  33. * @return array|int|mixed
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function checkWithdrawByDay($uid)
  37. {
  38. $cacheKey = "caches:withdraw:check:{$uid}";
  39. $data = RedisCache::get($cacheKey);
  40. if($data){
  41. return $data;
  42. }
  43. $data = $this->model->where(['uid'=> $uid])
  44. ->whereDay('create_at', 'today')
  45. ->count();
  46. if($data){
  47. RedisCache::set($cacheKey, $data, rand(10, 20));
  48. }
  49. return $data;
  50. }
  51. /**
  52. * 提现处理
  53. * @param $uid
  54. * @param $money
  55. * @param $type
  56. * @param $params
  57. * @return bool
  58. * @throws Exception
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function withdraw($uid, $money, $type, $params)
  64. {
  65. if (WithdrawService::make()->checkWithdrawByDay($uid)){
  66. sr_throw('每天只能提现一次');
  67. }
  68. if ($type == 1 && (empty($params['name']) || empty($params['number']))){
  69. sr_throw('参数错误');
  70. }
  71. if ($type == 2 && (empty($params['name']) || empty($params['number']) || empty($params['bank_subname']))){
  72. sr_throw('参数错误');
  73. }
  74. if ($money<=0){
  75. sr_throw('参数错误');
  76. }
  77. if (!preg_match("/^[1-9][0-9]*$/" ,$money)){
  78. sr_throw('输入非法');
  79. }
  80. $user = UserModel::alias('a')
  81. ->join('user_data b', 'a.id=b.uid')
  82. ->field('money,status,path,pay_pass,alipay,real_name,is_auth,level,user_type,b.alipay,b.uid')
  83. ->where('a.id', $uid)
  84. ->find();
  85. if (!$user || $user['status'] != 1){
  86. sr_throw('账号已被禁用');
  87. }
  88. // 验证安全密码
  89. // $password = AESjiemi($params['password']);
  90. if(md5($params['security_pass']) != $user['pay_pass']){
  91. sr_throw('安全密码不正确');
  92. }
  93. $config = SystemConfigService::make()->getConfigByNames(['withdraw_min_money','withdraw_max_money','withdraw_service_rate'],1,'withdraw');
  94. $minMoney = isset($config['withdraw_min_money'])? $config['withdraw_min_money'] : env('WITHDRAW.ONE_COUNT_MONEY',10);
  95. $maxMoney = isset($config['withdraw_max_money'])? $config['withdraw_max_money'] : env('WITHDRAW.MAX_MONEY',100000);
  96. $serviceRate = isset($config['withdraw_service_rate'])? max(0,$config['withdraw_service_rate']) : env('WITHDRAW.APP_WITHDRAW_SCALE',5);
  97. if ($money < $minMoney){
  98. sr_throw('单次提现金额不低于'.$minMoney);
  99. }
  100. if ($money % 10 > 0)
  101. {
  102. sr_throw('提现失败,金额必须是10的整数倍');
  103. }
  104. if ($money > $maxMoney)
  105. {
  106. sr_throw('单笔金额最大'.$maxMoney);
  107. }
  108. if ($money > $user['money'])
  109. {
  110. sr_throw('账户余额不足');
  111. }
  112. $serviceRate = $serviceRate<100?$serviceRate : 5;
  113. $serviceMoney = $money * $serviceRate/100;
  114. $practicalMoney = $money - $serviceMoney; // 最终金额
  115. if ($money > 5000 && $type == 1)
  116. {
  117. sr_throw('单笔提现金额大于5000只能选择银行卡');
  118. }
  119. // 流水处理
  120. $orderSn = ('sd'. date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999)));
  121. $insert_log = [
  122. 'uid' => $uid,
  123. 'apply_money' => $money,
  124. 'status' => 0,
  125. 'zfb_number' => $type==1?$params['number']:'',
  126. 'zfb_name' => $type==1?$params['name']:'',
  127. 'bank_no'=>$type==2?$params['number']:'',
  128. 'bank_name'=>$type==2?$params['name']:'',
  129. 'bank_subname'=>$type==2?$params['bank_subname']:'',
  130. 'real_name'=>'',
  131. 'channel' => intval($type),
  132. 'service_money' => $serviceMoney,
  133. 'transfer_type' => 1,
  134. 'practical_money' => $practicalMoney,
  135. 'ip' => get_client_ip(),
  136. 'mchid' => '111', // 商户号
  137. 'w_ordersn'=> $orderSn
  138. ];
  139. if(!WithDrawLogModel::insertGetId($insert_log)){
  140. sr_throw('提现失败');
  141. }
  142. if(!UserModel::where('id', $uid)->dec('money', $money)->update()){
  143. sr_throw('提现处理失败');
  144. }
  145. $data = [
  146. 'uid'=>$uid,
  147. 'type'=>$type,
  148. 'money'=>$money,
  149. 'create_at'=>sr_getcurtime(time()),
  150. 'state'=> 2,
  151. 'before_money'=> $user['money'],
  152. 'after_money'=> max(0,$user['money'] - $money),
  153. 'from_id'=> 0,
  154. 'uid2'=> 0,
  155. 'free_type'=> 0,
  156. 'remark'=> '余额提现'
  157. ];
  158. if(!MoneyLogModel::insertGetId($data)){
  159. sr_throw('提现处理失败');
  160. }
  161. $checkAccount = WithdrawAccountModel::where('uid', $uid)
  162. ->where('type', $type)
  163. ->where('number', $params['number'])
  164. ->value('id');
  165. if (!$checkAccount){
  166. WithdrawAccountModel::insert([
  167. 'type'=>$type,
  168. 'name'=>$params['name'],
  169. 'number'=>$params['number'],
  170. 'bank_subname'=>$type==1?'':$params['bank_subname'],
  171. 'create_time'=>sr_getcurtime(time()),
  172. 'uid'=>$uid
  173. ]);
  174. }
  175. if(!UserModel::where('id', $uid)->inc('total_withdraw', $money)->update()){
  176. sr_throw('提现处理失败');
  177. }
  178. return true;
  179. }
  180. }