Commit 63b85851 by 叶明星

更新代码

parent 7f1a9930
......@@ -20,24 +20,15 @@ class ExchangesTask extends Task
// 处理任务的逻辑,运行在Task进程中,不能投递任务
public function handle()
{
//金额大于等于20走审核流程
if($this->Exchange['amount'] >= 20)
return true;
//微信红包暂时不能对接,走人工
if($this->data['type'] == 2 )
//验证这个订单是否充值,防止重复提现
$Redis = new RedisModel();
$Cache = $Redis->hsetnx('ic_user_exchanges' , $this->data['id'].'_'.$this->data['user_id'] , 1);
if(!$Cache)
return true;
try{
DB::beginTransaction();
//获取用户手机号
$Redis = new RedisModel(false);
$UserInfo = json_decode($Redis->hget('ic_user',$this->data['user_id']),true);
if(!$UserInfo || empty($UserInfo['mobile']))
throw new \Exception('用户手机号获取失败#'.$this->data['id']);
//先修改提现记录状态
$UserExchangModel = new UserExchange();
$Exchang = [
......@@ -49,23 +40,57 @@ class ExchangesTask extends Task
if(!$result)
throw new \Exception('提现记录状态修改失败#'.$this->data['id']);
$RechargeModel = new Recharge();
if($this->data['type'] == 1 )
$this->Recharge();
elseif($this->data['type'] == 2)
$this->Integrals();
//验证手机号能不能充值
$RechargeModel->TelCheck($UserInfo['mobile'] , $this->Exchange['amount']);
//开始充值
$time = date('Ymd');
$OrderId = $this->data['user_id'].$time;//用户ID拼接时间
$result = $RechargeModel->Recharge($UserInfo['mobile'] , $this->Exchange['amount'] , $OrderId);
if(!$result)
throw new \Exception('充值失败#'.$this->data['id']);
DB::commit();
SendDingTalk('充值完成');
return true;
}catch (\Exception $e){
DB::rollBack();
//强制删除充值标记
$Redis->hdel('ic_user_exchanges' , $this->data['id'].'_'.$this->data['user_id']);
return SendErrMsg($e);
}
}
//红包提现(后期需要对接微信平台)
public function Integrals(){
//目前只改审核状态
return true;
}
//话费充值
public function Recharge(){
//获取用户手机号
$Redis = new RedisModel(false);
$UserInfo = json_decode( $Redis->hget('ic_user' , $this->data['user_id']) ,true );
if(!$UserInfo || empty($UserInfo['mobile']))
throw new \Exception('用户手机号获取失败#'.$this->data['id']);
$RechargeModel = new Recharge();
//验证手机号能不能充值
$result = $RechargeModel->TelCheck($UserInfo['mobile'] , $this->Exchange['amount']);
if(!$result)
throw new \Exception('手机号与金额无法充值');
//开始充值
$time = date('Ymd');
$OrderId = $this->data['user_id'].$time.'2';//用户ID拼接时间
$result = $RechargeModel->Recharge($UserInfo['mobile'] , $this->Exchange['amount'] , $OrderId);
if(!$result)
throw new \Exception('充值失败#'.$this->data['id']);
return true;
}
public function finish()
{
......
......@@ -104,7 +104,7 @@ class UserExchangesController extends Controller
}
}
//用于抢兑换名额(接口自己需要先验证是否能)
//用于抢兑换名额
public function create(Request $request){
$Field = ['user_id','exchange_id'];
$collert = $request->only($Field);
......@@ -119,14 +119,32 @@ class UserExchangesController extends Controller
if($Record)
return $this->Export(ErrorCode(100,1),'今天已经兑换过来,请明天再来吧');
//获取用户红包
$UserInfo = $Redis->hget('ic_user' , $collert['user_id']);
$UserInfo = json_decode( $UserInfo,true );
if(empty($UserInfo['integral']))
return $this->Export(ErrorCode(102,1) , '红包余额不足');
//获取红包配置
$ic_exchange_settings = json_decode( $Redis->hget('ic_exchange_settings' , $collert['exchange_id']) , true);
//判断红包提现设置是否可用
if(!$ic_exchange_settings || $ic_exchange_settings['status'] != 1)
return $this->Export(ErrorCode(103,1) , '当前提现不可用');
//判断用余额
if($ic_exchange_settings['amount'] > $UserInfo['integral'])
return $this->Export(ErrorCode(102,1) , '红包余额不足');
//消费名额
$Cahce = $Redis->rpop('ic_exchange_settings_'.$collert['exchange_id']);
if(!$Cahce)
return $this->Export(ErrorCode(101,1),'已经被兑换完了');
return $this->Export(ErrorCode(101,1),'名额被抢光了');
$Cahce = json_decode($Cahce,true);
//记录当前消费的名额,自动过期
$Redis->hset($Pre , $collert['user_id'] , 1);
$Redis->hset($Pre , $collert['user_id'] , $collert['exchange_id']);
$Redis->expire($Pre , 60*60*24);
try{
......@@ -134,7 +152,7 @@ class UserExchangesController extends Controller
//扣减用户金额
$UserIntrgralModel = new UserIntegral();
$result = $UserIntrgralModel->Deduction($collert['user_id'] , $Cahce['amount']);
$result = $UserIntrgralModel->Deduction($collert['user_id'] , $ic_exchange_settings['amount']);
if(!$result){
ErrorLog(ErrorCode(1,9),'扣减用户红包失败,用户ID:'.$collert['user_id']);
......@@ -145,8 +163,9 @@ class UserExchangesController extends Controller
$UserExchangeModel = new UserExchange();
$data = [
'user_id' => $collert['user_id'],
'type' => $Cahce['type'],
'exchange_id' => $Cahce['id'],
'type' => $ic_exchange_settings['type'],
'exchange_id' => $ic_exchange_settings['id'],
'amount' => $ic_exchange_settings['amount']
];
$result = $UserExchangeModel->addRecord($data);
......@@ -160,16 +179,18 @@ class UserExchangesController extends Controller
DB::commit();
//扣减缓存里面的红包金额
$UserInfo = json_decode($Redis->hget('ic_user' , $collert['user_id']),true);
$UserInfo['integral'] = $UserInfo['integral'] - $Cahce['amount'];
$Redis->hset('ic_user' , json_encode($UserInfo));
//推送一个异步任务(用来提现或者充值),延时10秒,防止主从同步不及时
$Task = new ExchangesTask($data,$Cahce);
$Task->delay(10);
$result = Task::deliver($Task);
if(!$result)
ErrorLog(ErrorCode(1,9),'提现任务推送失败,提现ID:'.$data['id']);
$UserInfo['integral'] = $UserInfo['integral'] - $ic_exchange_settings['amount'];
$Redis->hset('ic_user' ,$UserInfo['user_id'] , json_encode($UserInfo));
//金额小于20元时,推送一个异步任务(用来提现或者充值),延时10秒,防止主从同步不及时
if($data['amount'] < 20){
$Task = new ExchangesTask($data,$ic_exchange_settings);
$Task->delay(10);
$result = Task::deliver($Task);
if(!$result)
ErrorLog(ErrorCode(1,9),'提现任务推送失败,提现ID:'.$data['id']);
}
return $this->Export(0,'ok',['data'=>$data['id']]);
}catch (\Exception $e){
......@@ -177,6 +198,5 @@ class UserExchangesController extends Controller
SendErrMsg($e);
return $this->Export(ErrorCode(100,5),'提现失败');
}
}
}
\ No newline at end of file
......@@ -12,16 +12,19 @@ class Recharge extends Model
//话费充值
public function Recharge($mobile , $amount , $OrderId){
$Url = 'http://op.juhe.cn/ofpay/mobile/onlineorder';
$amount = (int)$amount;
$sign = $this->OpenID.$this->key.$mobile.$amount.$OrderId;
$data = [
'phoneno' => $mobile,
'cardnum' => $amount,
'orderid' => $OrderId,
'key' => $this->key,
'sign' => $this->OpenID.$this->key.$mobile.$amount.$OrderId
'sign' => md5($sign)
];
$result = curl($Url , $data , true);
$result = json_decode($result ,true);
if(!$result || !isset($result['error_code']) || !$result['error_code'] == 10014){
if(!$result || !isset($result['error_code']) || $result['error_code'] == 10014){
$message = '话费充值订单异常,需要联系聚合客服确认'."\r";
$message .= '订单号:'.$OrderId."\r";
$message .= json_encode($result);
......
......@@ -9,5 +9,5 @@ function SendErrMsg($e){
}
function SendDingTalk($message){
return SendRobot(config('website.RobotUrl'),$message,[config('website.at')],config('website.SystemName'));
return SendRobot(config('system.RobotUrl'),$message,[config('system.at')],config('system.SystemName'));
}
\ No newline at end of file
......@@ -143,13 +143,13 @@ return [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2
'database' => 0
],
'read' =>[
'host' => env('REDIS_READ_HOST', ''),
'password' => env('REDIS_READ_PASSWORD', null),
'port' => env('REDIS_READ_PORT', 6379),
'database' => 2
'database' => 0
]
],
......
......@@ -62,7 +62,7 @@ return [
'dispatch_mode' => 2,
'reactor_num' => function_exists('swoole_cpu_num') ? swoole_cpu_num() * 2 : 4,
'worker_num' => function_exists('swoole_cpu_num') ? swoole_cpu_num() * 2 : 8,
//'task_worker_num' => function_exists('swoole_cpu_num') ? swoole_cpu_num() * 2 : 8,
'task_worker_num' => function_exists('swoole_cpu_num') ? swoole_cpu_num() * 2 : 8,
'task_ipc_mode' => 1,
'task_max_request' => 8000,
'task_tmpdir' => @is_writable('/dev/shm/') ? '/dev/shm' : '/tmp',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment