Commit 234783a4 by 叶明星

话费充值

parent 3b96b390
...@@ -6,8 +6,5 @@ use Illuminate\Support\Facades\DB; ...@@ -6,8 +6,5 @@ use Illuminate\Support\Facades\DB;
class ExchangeController extends Controller class ExchangeController extends Controller
{ {
//用于抢兑换名额
public function create(Request $request){
$Field = ['user_id',''];
}
} }
\ No newline at end of file
<?php
namespace App\Http\Controllers;
use App\Models\Recharge;
use App\Models\UserExchange;
use Common\Model\RedisModel;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use DB;
class ExchangesTask extends Task
{
private $data;
private $Exchange;
public function __construct($data,$Exchange)
{
$this->data = $data;
$this->Exchange = $Exchange;
}
// 处理任务的逻辑,运行在Task进程中,不能投递任务
public function handle()
{
//金额大于等于20走审核流程
if($this->Exchange['amount'] >= 20)
return true;
//微信红包暂时不能对接,走人工
if($this->data['type'] == 2 )
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 = [
'status' => 1,
'audit_id' => 1,
'audit_reason' => '系统自动审核'
];
$result = $UserExchangModel->SaveRecord($this->data['id'] , $Exchang);
if(!$result)
throw new \Exception('提现记录状态修改失败#'.$this->data['id']);
$RechargeModel = new Recharge();
//验证手机号能不能充值
$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();
}catch (\Exception $e){
DB::rollBack();
return SendErrMsg($e);
}
}
public function finish()
{
}
}
...@@ -5,7 +5,11 @@ namespace App\Http\Controllers; ...@@ -5,7 +5,11 @@ namespace App\Http\Controllers;
use App\Http\Filters\UserExchangeFilter; use App\Http\Filters\UserExchangeFilter;
use App\Models\ExchangeSetting;
use App\Models\UserExchange; use App\Models\UserExchange;
use App\Models\UserIntegral;
use Common\Model\RedisModel;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
...@@ -57,4 +61,80 @@ class UserExchangesController extends Controller ...@@ -57,4 +61,80 @@ class UserExchangesController extends Controller
return $this->Export(0, 'ok'); return $this->Export(0, 'ok');
// return $this->Export(033, '批量修改用户积分失败'); // return $this->Export(033, '批量修改用户积分失败');
} }
//用于抢兑换名额(接口自己需要先验证是否能)
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']);
if($Record)
return $this->Export(ErrorCode(100,1),'今天已经兑换过来,请明天再来吧');
//消费名额
$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'] , 1);
$Redis->expire($Pre , 60*60*24);
try{
DB::beginTransaction();
//扣减用户金额
$UserIntrgralModel = new UserIntegral();
$result = $UserIntrgralModel->Deduction($collert['user_id'] , $Cahce['amount']);
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' => $Cahce['type'],
'exchange_id' => $Cahce['id'],
];
$result = $UserExchangeModel->addRecord($data);
if(!$result){
$Errinfo = '红包提现记录添加失败'.$collert['user_id'];
ErrorLog(ErrorCode(1,9),$Errinfo);
throw new \Exception($Errinfo);
}
//提前提交事务
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']);
return $this->Export(0,'ok',['data'=>$data['id']]);
}catch (\Exception $e){
DB::rollBack();
SendErrMsg($e);
return $this->Export(ErrorCode(100,5),'提现失败');
}
}
} }
\ No newline at end of file
<?php <?php
namespace App\Models; namespace App\Models;
use App\Http\Filters\QueryFilter; use App\Http\Filters\QueryFilter;
use Common\Model\RedisModel;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use DB;
class ExchangeSetting extends Model class ExchangeSetting extends Model
{ {
...@@ -20,4 +20,13 @@ class ExchangeSetting extends Model ...@@ -20,4 +20,13 @@ class ExchangeSetting extends Model
{ {
return $filters->apply($query); return $filters->apply($query);
} }
//抢占兑换名额
public function QuotaSnatching($data){
}
} }
\ No newline at end of file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Recharge extends Model
{
protected $connection = false;
private $key = '';
private $OpenID = 'JH3f55c02fc5aaf8e1bca362b9de5051ba';
//话费充值
public function Recharge($mobile , $amount , $OrderId){
$Url = 'http://op.juhe.cn/ofpay/mobile/onlineorder';
$data = [
'phoneno' => $mobile,
'cardnum' => $amount,
'orderid' => $OrderId,
'key' => $this->key,
'sign' => $this->OpenID.$this->key.$mobile.$amount.$OrderId
];
$result = curl($Url , $data , true);
$result = json_decode($result ,true);
if(!$result || !isset($result['error_code']) || !$result['error_code'] == 10014){
$message = '话费充值订单异常,需要联系聚合客服确认'."\r";
$message .= '订单号:'.$OrderId."\r";
$message .= json_encode($result);
SendDingTalk($message);
return false;
}
if($result['error_code'] !== 0 ){
$message = '充值失败'."\r";
$message .= '订单号:'.$OrderId."\r";
$message .= json_encode($result);
SendDingTalk($message);
return false;
}
return true;
}
//检测订单状态
public function OrderStatus($OrderId){
$Url = 'http://op.juhe.cn/ofpay/mobile/ordersta';
$data = [
'orderid' => $OrderId,
'key' => $this->key
];
$result = curl($Url , $data , true);
$result = json_decode($result ,true);
if(!$result || !isset($result['error_code']) || !$result['error_code'] == 10014){
SendDingTalk('话费充值订单状态检查异常');
return false;
}
}
//查询手机是否能充值
public function TelCheck($mobile , $amount){
$Url = 'http://op.juhe.cn/ofpay/mobile/telcheck';
$data = [
'phoneno' => $mobile,
'cardnum' => $amount,
'key' => $this->key
];
$result = curl($Url , $data , true);
$result = json_decode($result ,true);
if(!$result || !isset($result['error_code'])){
$message = '话费充值检测接口异常'."\r";
$message .= '手机号:'.$mobile."\r";
$message .= '金额:'.$amount."\r";
$message .= json_encode($result);
SendDingTalk($message);
return false;
}
if($result['error_code'] !== 0 ){
$message = '用户手机及金额不可以充值'."\r";
$message .= '手机号:'.$mobile."\r";
$message .= '金额:'.$amount."\r";
$message .= json_encode($result);
SendDingTalk($message);
return false;
}
return true;
}
}
\ No newline at end of file
...@@ -20,4 +20,20 @@ class UserExchange extends Model ...@@ -20,4 +20,20 @@ class UserExchange extends Model
{ {
return $filters->apply($query); return $filters->apply($query);
} }
//添加提现记录
public function addRecord(&$data){
$data['add_time'] = time();
$data['update_time'] = $data['add_time'];
$ID = $this->insertGetId($data);
if(!$ID)
return false;
$data['id'] = $ID;
return true;
}
public function SaveRecord($id,$data){
$data['update_time'] = time();
return $this->where('id','=',$id)->update($data);
}
} }
\ No newline at end of file
...@@ -20,4 +20,9 @@ class UserIntegral extends Model ...@@ -20,4 +20,9 @@ class UserIntegral extends Model
{ {
return $filters->apply($query); return $filters->apply($query);
} }
//扣减红包,需要制定扣减数量
public function Deduction($user_id , $integral){
return $this->where('user_id','=',$user_id)->decrement('integral',$integral,['update_time'=>time()]);
}
} }
\ No newline at end of file
...@@ -2,4 +2,12 @@ ...@@ -2,4 +2,12 @@
function ErrorCode($code = 0, $level = 1){ function ErrorCode($code = 0, $level = 1){
$systemcode = config('system.code'); $systemcode = config('system.code');
return errCode($code , $level , $systemcode); return errCode($code , $level , $systemcode);
}
function SendErrMsg($e){
return SendAlarm($e,config('system.RobotUrl'),[config('system.at')],config('system.SystemName'));
}
function SendDingTalk($message){
return SendRobot(config('website.RobotUrl'),$message,[config('website.at')],config('website.SystemName'));
} }
\ No newline at end of file
...@@ -2,4 +2,7 @@ ...@@ -2,4 +2,7 @@
return [ return [
'code' => env('SYSTEM_CODE'), 'code' => env('SYSTEM_CODE'),
'RobotUrl' => 'https://oapi.dingtalk.com/robot/send?access_token=245f39f3b18bfcb8d739068327a32327f5c0c868529b5d5ba357f919567ef1fd',
'SystemName' => env('SYSTEM_NAME','这家伙没设置系统'),//系统名称
"at" => env('AT'),
]; ];
\ 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