作者 何书鹏

提现到账

@@ -4,12 +4,18 @@ @@ -4,12 +4,18 @@
4 namespace app\api\controller; 4 namespace app\api\controller;
5 5
6 6
  7 +use addons\epay\library\Service;
7 use app\api\model\GoodsComment; 8 use app\api\model\GoodsComment;
8 use app\api\model\RiderOrder; 9 use app\api\model\RiderOrder;
  10 +use app\api\model\Third;
9 use app\api\model\UserMoneyLog; 11 use app\api\model\UserMoneyLog;
  12 +use app\api\model\Withdraw;
10 use app\common\controller\Api; 13 use app\common\controller\Api;
  14 +use app\common\model\User;
11 use think\Config; 15 use think\Config;
12 use think\Db; 16 use think\Db;
  17 +use think\Exception;
  18 +use think\exception\PDOException;
13 19
14 /** 20 /**
15 * 骑手 21 * 骑手
@@ -356,15 +362,52 @@ class Rider extends Api @@ -356,15 +362,52 @@ class Rider extends Api
356 $money = $this->request->post('money'); 362 $money = $this->request->post('money');
357 $user = $this->auth->getUser(); 363 $user = $this->auth->getUser();
358 if ($user['money'] < $money) $this->error('可提现金额不足'); 364 if ($user['money'] < $money) $this->error('可提现金额不足');
359 - $data = [  
360 - 'user_id' => $this->auth->id,  
361 - 'money' => -$money,  
362 - 'before' => $user->money,  
363 - 'after' => $user->money-$money,  
364 - 'memo' => '提现'  
365 - ];  
366 - UserMoneyLog::create($data);  
367 - $user->setDec('money',$money); 365 + // 禁止连点
  366 + !empty(cache('withdraw_'.$user['id'])) && $this->error('提现频繁,请稍后再试');
  367 + cache('withdraw_'.$user['id'],'123',5);
  368 +
  369 + Db::startTrans();
  370 + try{
  371 + $money = 0.01;
  372 + $order_sn = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  373 + $withdraw = Withdraw::create([
  374 + 'user_id' => $user['id'],
  375 + 'order_sn' => $order_sn,
  376 + 'money' => $money,
  377 + ]);
  378 + // 变更会员余额
  379 + User::money(-$money,$user['id'],'提现');
  380 +
  381 + $config = Service::getConfig('wechat');
  382 + $config['app_id'] = $config['miniapp_id'];
  383 + $app = Factory::payment($config);
  384 + $result = $app->transfer->toBalance([
  385 + 'partner_trade_no' => $order_sn, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  386 + 'openid' => Third::where('user_id',$this->auth->id)->value('openid'),
  387 + 'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  388 +// 're_user_name' => '王小帅', // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  389 + 'amount' => $money * 100, // 企业付款金额,单位为分
  390 + 'desc' => '骑手提现', // 企业付款操作说明信息。必填
  391 + ]);
  392 + \think\Log::write('企业付款到返回数据:'.json_encode($result,JSON_UNESCAPED_UNICODE));
  393 + if($result['result_code'] != 'SUCCESS'){
  394 + $this->error($result['err_code_des']);
  395 + }
  396 +
  397 + // 记录企业付款单号
  398 + $withdraw->payment_no = $result['payment_no'];
  399 + $withdraw->payment_time = $result['payment_time'];
  400 + $withdraw->status = '1';
  401 + $withdraw->save();
  402 +
  403 + Db::commit();
  404 + } catch (PDOException $e) {
  405 + Db::rollback();
  406 + $this->error($e->getMessage());
  407 + } catch (Exception $e) {
  408 + Db::rollback();
  409 + $this->error($e->getMessage());
  410 + }
368 $this->success('提现成功'); 411 $this->success('提现成功');
369 } 412 }
370 413