| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace app\common\service;
- use app\common\model\UserModel;
- use app\common\model\WithDrawLogModel;
- use utils\RedisCache;
- /**
- * 提现服务 by wes
- * Class WithdrawService
- * @package app\common\service
- */
- class WithdrawService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new WithDrawLogModel();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 当日是否提现过
- * @param $uid
- * @return array|int|mixed
- * @throws \think\db\exception\DbException
- */
- public function checkWithdrawByDay($uid)
- {
- $cacheKey = "caches:withdraw:check:{$uid}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['uid'=> $uid])
- ->whereDay('create_at', 'today')
- ->count();
- if($data){
- RedisCache::set($cacheKey, $data, rand(10, 20));
- }
- return $data;
- }
- /**
- * 提现处理
- * @param $uid
- * @param $money
- * @param $type
- * @param $params
- * @return bool
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function withdraw($uid, $money, $type, $params)
- {
- if (WithdrawService::make()->checkWithdrawByDay($uid)){
- sr_throw('每天只能提现一次');
- }
- if ($type == 1 && (empty($params['name']) || empty($params['number']))){
- sr_throw('参数错误');
- }
- if ($type == 2 && (empty($params['name']) || empty($params['number']) || empty($params['bank_subname']))){
- sr_throw('参数错误');
- }
- if ($money<=0){
- sr_throw('参数错误');
- }
- if (!preg_match("/^[1-9][0-9]*$/" ,$money)){
- sr_throw('输入非法');
- }
- $user = UserModel::alias('a')
- ->join('user_data b', 'a.id=b.uid')
- ->field('money,status,path,pay_pass,alipay,real_name,is_auth,level,user_type,b.alipay,b.uid')
- ->where('a.id', $uid)
- ->find();
- if (!$user || $user['status'] != 1){
- sr_throw('账号已被禁用');
- }
- // 验证安全密码
- // $password = AESjiemi($params['password']);
- if(md5($params['security_pass']) != $user['pay_pass']){
- sr_throw('安全密码不正确');
- }
- $config = SystemConfigService::make()->getConfigByNames(['withdraw_min_money','withdraw_max_money','withdraw_service_rate'],1,'withdraw');
- $minMoney = isset($config['withdraw_min_money'])? $config['withdraw_min_money'] : env('WITHDRAW.ONE_COUNT_MONEY',10);
- $maxMoney = isset($config['withdraw_max_money'])? $config['withdraw_max_money'] : env('WITHDRAW.MAX_MONEY',100000);
- $serviceRate = isset($config['withdraw_service_rate'])? max(0,$config['withdraw_service_rate']) : env('WITHDRAW.APP_WITHDRAW_SCALE',5);
- if ($money < $minMoney){
- sr_throw('单次提现金额不低于'.$minMoney);
- }
- if ($money % 10 > 0)
- {
- sr_throw('提现失败,金额必须是10的整数倍');
- }
- if ($money > $maxMoney)
- {
- sr_throw('单笔金额最大'.$maxMoney);
- }
- if ($money > $user['money'])
- {
- sr_throw('账户余额不足');
- }
- $serviceRate = $serviceRate<100?$serviceRate : 5;
- $serviceMoney = $money * $serviceRate/100;
- $practicalMoney = $money - $serviceMoney; // 最终金额
- if ($money > 5000 && $type == 1)
- {
- sr_throw('单笔提现金额大于5000只能选择银行卡');
- }
- // 流水处理
- $orderSn = ('sd'. date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999)));
- $insert_log = [
- 'uid' => $uid,
- 'apply_money' => $money,
- 'status' => 0,
- 'zfb_number' => $type==1?$params['number']:'',
- 'zfb_name' => $type==1?$params['name']:'',
- 'bank_no'=>$type==2?$params['number']:'',
- 'bank_name'=>$type==2?$params['name']:'',
- 'bank_subname'=>$type==2?$params['bank_subname']:'',
- 'real_name'=>'',
- 'channel' => intval($type),
- 'service_money' => $serviceMoney,
- 'transfer_type' => 1,
- 'practical_money' => $practicalMoney,
- 'ip' => get_client_ip(),
- 'mchid' => '111', // 商户号
- 'w_ordersn'=> $orderSn
- ];
- if(!WithDrawLogModel::insertGetId($insert_log)){
- sr_throw('提现失败');
- }
- if(!UserModel::where('id', $uid)->dec('money', $money)->update()){
- sr_throw('提现处理失败');
- }
- $data = [
- 'uid'=>$uid,
- 'type'=>$type,
- 'money'=>$money,
- 'create_at'=>sr_getcurtime(time()),
- 'state'=> 2,
- 'before_money'=> $user['money'],
- 'after_money'=> max(0,$user['money'] - $money),
- 'from_id'=> 0,
- 'uid2'=> 0,
- 'free_type'=> 0,
- 'remark'=> '余额提现'
- ];
- if(!MoneyLogModel::insertGetId($data)){
- sr_throw('提现处理失败');
- }
- $checkAccount = WithdrawAccountModel::where('uid', $uid)
- ->where('type', $type)
- ->where('number', $params['number'])
- ->value('id');
- if (!$checkAccount){
- WithdrawAccountModel::insert([
- 'type'=>$type,
- 'name'=>$params['name'],
- 'number'=>$params['number'],
- 'bank_subname'=>$type==1?'':$params['bank_subname'],
- 'create_time'=>sr_getcurtime(time()),
- 'uid'=>$uid
- ]);
- }
- if(!UserModel::where('id', $uid)->inc('total_withdraw', $money)->update()){
- sr_throw('提现处理失败');
- }
- return true;
- }
- }
|