Commit 197aed22 by 杨树贤

修复逻辑上的问题

parent 0c6a0e6d
......@@ -11,6 +11,15 @@ DB_USERNAME=ic_welfare
DB_PASSWORD=ic_welfare#zsyM
DB_PREFIX=ic_
#ic_data数据库的配置
IC_DATA_DB_HOST_R=192.168.2.232
IC_DATA_DB_HOST_W=192.168.2.232
IC_DATA_DB_PORT=3306
IC_DATA_DB_DATABASE=ic_data
IC_DATA_DB_USERNAME=ic_data
IC_DATA_DB_PASSWORD=ic_data#zsyM
IC_DATA_DB_PREFIX=ic_
CACHE_DRIVER=file
QUEUE_DRIVER=sync
......
......@@ -105,73 +105,79 @@ class UserExchangesController extends Controller
}
//用于抢兑换名额
public function create(Request $request){
$Field = ['user_id','exchange_id'];
public function create(Request $request)
{
$Field = ['user_id', 'exchange_id'];
$collert = $request->only($Field);
$Redis = new RedisModel();
//先判断客户今天是否抢到名额
$time = date('Ymd');
$Pre = 'ic_exchanges_quota_'.$time;
$Record = $Redis->hget($Pre , $collert['user_id']);
$Pre = 'ic_exchanges_quota_' . $time;
$Record = $Redis->hget($Pre, $collert['user_id']);
if($Record)
return $this->Export(ErrorCode(100,1),'今天已经兑换过来,请明天再来吧');
if ($Record) {
return $this->Export(ErrorCode(100, 1), '今天已经兑换过来,请明天再来吧');
}
//获取用户红包
$UserInfo = $Redis->hget('ic_user' , $collert['user_id']);
$UserInfo = $Redis->hget('ic_user', $collert['user_id']);
$UserInfo = json_decode( $UserInfo,true );
if(empty($UserInfo['integral']))
return $this->Export(ErrorCode(102,1) , '红包余额不足');
$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);
$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 || $ic_exchange_settings['status'] != 1) {
return $this->Export(ErrorCode(103, 1), '当前提现不可用');
}
//判断用余额
if($ic_exchange_settings['amount'] > $UserInfo['integral'])
return $this->Export(ErrorCode(102,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),'名额被抢光了');
$Cahce = json_decode($Cahce,true);
$Cahce = $Redis->rpop('ic_exchange_settings_' . $collert['exchange_id']);
if (!$Cahce) {
return $this->Export(ErrorCode(101, 1), '名额被抢光了');
}
$Cahce = json_decode($Cahce, true);
//记录当前消费的名额,自动过期
$Redis->hset($Pre , $collert['user_id'] , $collert['exchange_id']);
$Redis->expire($Pre , 60*60*24);
$Redis->hset($Pre, $collert['user_id'], $collert['exchange_id']);
$Redis->expire($Pre, 60 * 60 * 24);
try{
try {
DB::beginTransaction();
//扣减用户金额
$UserIntrgralModel = new UserIntegral();
$result = $UserIntrgralModel->Deduction($collert['user_id'] , $ic_exchange_settings['amount']);
$result = $UserIntrgralModel->Deduction($collert['user_id'], $ic_exchange_settings['amount']);
if(!$result){
ErrorLog(ErrorCode(1,9),'扣减用户红包失败,用户ID:'.$collert['user_id']);
throw new \Exception('扣减用户红包失败,用户ID:'.$collert['user_id']);
if (!$result) {
ErrorLog(ErrorCode(1, 9), '扣减用户红包失败,用户ID:' . $collert['user_id']);
throw new \Exception('扣减用户红包失败,用户ID:' . $collert['user_id']);
}
//添加红包记录
$UserExchangeModel = new UserExchange();
$data = [
'user_id' => $collert['user_id'],
'type' => $ic_exchange_settings['type'],
'user_id' => $collert['user_id'],
'type' => $ic_exchange_settings['type'],
'exchange_id' => $ic_exchange_settings['id'],
'amount' => $ic_exchange_settings['amount']
'amount' => $ic_exchange_settings['amount'],
];
$result = $UserExchangeModel->addRecord($data);
if(!$result){
$Errinfo = '红包提现记录添加失败'.$collert['user_id'];
ErrorLog(ErrorCode(1,9),$Errinfo);
if (!$result) {
$Errinfo = '红包提现记录添加失败' . $collert['user_id'];
ErrorLog(ErrorCode(1, 9), $Errinfo);
throw new \Exception($Errinfo);
}
......@@ -180,23 +186,25 @@ class UserExchangesController extends Controller
//扣减缓存里面的红包金额
$UserInfo['integral'] = $UserInfo['integral'] - $ic_exchange_settings['amount'];
$Redis->hset('ic_user' ,$UserInfo['user_id'] , json_encode($UserInfo));
$Redis->hset('ic_user', $UserInfo['user_id'], json_encode($UserInfo));
//金额小于20元时,推送一个异步任务(用来提现或者充值),延时10秒,防止主从同步不及时
if($data['amount'] < 20 && $data['type'] == 1){
$Task = new ExchangesTask($data,$ic_exchange_settings);
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']);
if (!$result) {
ErrorLog(ErrorCode(1, 9), '提现任务推送失败,提现ID:' . $data['id']);
}
}
return $this->Export(0,'ok',['data'=>$data['id']]);
return $this->Export(0, 'ok', ['data' => $data['id']]);
}catch (\Exception $e){
} catch (\Exception $e) {
DB::rollBack();
SendErrMsg($e);
return $this->Export(ErrorCode(100,5),'提现失败');
return $this->Export(ErrorCode(100, 5), '提现失败');
}
}
}
\ No newline at end of file
......@@ -24,6 +24,10 @@ class Code extends Model
$exist = $redis->sismember('ic_welfare_code_limit', $userId);
//如果是存在的话,代表这个人已经输入过红包码,就返回false
if ($exist) {
//需要记录一下这种奇怪的请求,因为如果兑换过一次之后,前端那边是不会有界面显示兑换的了
//所以如果出现这个已经兑换过还过来兑换的情况,可能是有人用http请求想刷红包
Log::error("疑似刷红包码兑换,用户Id是$userId,红包码是$code");
return false;
} else {
//还要去判断这个code的主人今天的被兑换次数是否超过限额
......
......@@ -46,6 +46,7 @@ class IntegralBill extends Model
}
//这是一个比较重要的方法,这个方法关系着需不需要插入红包获取流水
//data里面必须有user_id,integral_id和add_time三个键值
public function createIntegralBill($data = [])
{
$integralId = $data['integral_id'];
......
......@@ -15,9 +15,6 @@ use App\Tasks\IntegralBillTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
$router->get('/', function () use ($router) {
$task = new \App\Tasks\TestTask();
$task->delay(30);
$result = Task::deliver($task);
return $router->app->version();
});
......@@ -60,6 +57,7 @@ $router->post('/exchange_settings/batchUpdateStatus', 'ExchangeSettingsControlle
//用户兑换记录
$router->post('/user_exchanges/info', 'UserExchangesController@show');
$router->post('/user_exchanges/add', 'UserExchangesController@store');
$router->post('/user_exchanges/audit', 'UserExchangesController@audit');
$router->post('/user_exchanges/list', 'UserExchangesController@index');
$router->post('/user_exchanges/update', 'UserExchangesController@update');
......
16308
\ No newline at end of file
2364
\ No newline at end of file
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