Browse Source

代理上分下分接口

master
wanghongjun 3 years ago
parent
commit
de312d1597
  1. 117
      app/controller/AgentTeam.php
  2. 2
      app/logic/Zone.php
  3. 34
      app/model/AgentDownScoresRecords.php
  4. 34
      app/model/AgentUpScoresRecords.php
  5. 86
      app/model/AgentUser.php
  6. 37
      app/model/RechargeRecords.php
  7. 22
      app/model/User.php
  8. 36
      app/model/WithdrawalRecords.php
  9. 32
      app/validate/Agent.php
  10. 4
      route/app.php

117
app/controller/AgentTeam.php

@ -0,0 +1,117 @@
<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\model\AgentDownScoresRecords;
use app\model\AgentUpScoresRecords;
use app\model\AgentUser;
use app\model\RechargeRecords;
use app\model\User;
use app\model\WithdrawalRecords;
use app\validate\Agent;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
class AgentTeam extends BaseController
{
/**
* 上分
* @return array
*/
public function upScores()
{
$request = Request::param();
$connection = Db::connect();
try {
$agentUser = Session::get('login_agent_user_data');
$agentUser['id'] = 1000;
validate(Agent::class)->scene('scores')->check($request);
$user_id = $request['user_id']; # 用户id
$quota = $request['quota']; # 额度
# 检测代理余额是否足够
$inspectRes = AgentUser::inspectBalance($agentUser['id'],$quota);
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
# 开启事务
$connection->startTrans();
# 扣减代理余额
$agentBalance = AgentUser::decrBalance($agentUser['id'],$quota);
# 用户上分记录
AgentUpScoresRecords::createRecords($agentUser['id'],$user_id,$quota,$agentBalance);
# 用户上分
$userBalance = User::incrBalance($user_id,$quota);
# 用户充值记录-上分
RechargeRecords::createRecords($user_id,$quota,$userBalance,1);
$connection->commit();
return $this->renderSuccess('上分成功');
} catch (ValidateException $e) {
return $this->renderError($e->getMessage());
} catch (\Exception $exception) {
$connection->rollback();
return $this->renderError($exception->getMessage());
}
}
/**
* 下分
* @return array|void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function downScores()
{
$request = Request::param();
$connection = Db::connect();
try {
$agentUser = Session::get('login_agent_user_data');
$agentUser['id'] = 1000;
validate(Agent::class)->scene('scores')->check($request);
$user_id = $request['user_id']; # 用户id
$quota = $request['quota']; # 额度
# 检测代理余额是否足够
$inspectRes = User::inspectUserBalance($user_id,$quota);
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
# 开启事务
$connection->startTrans();
# 增加代理余额
$agentBalance = AgentUser::incrWithdrawalBalance($agentUser['id'],$quota);
# 用户下分记录
AgentDownScoresRecords::createRecords($agentUser['id'],$user_id,$quota,$agentBalance);
# 用户下分
$userBalance = User::decrWithdrawalBalance($user_id,$quota);
# 用户提现记录-下分
WithdrawalRecords::createRecords($user_id,$quota,$userBalance,1);
$connection->commit();
return $this->renderSuccess('下分成功');
} catch (ValidateException $e) {
return $this->renderError($e->getMessage());
} catch (\Exception $exception) {
$connection->rollback();
return $this->renderError($exception->getMessage());
}
}
}

2
app/logic/Zone.php

@ -113,7 +113,7 @@ class Zone
if ($awards_amount > 0) {
# 修改用户余额
$balance = User::IncrWithdrawalBalance($user_id,$awards_amount);
$balance = User::incrWithdrawalBalance($user_id,$awards_amount);
# 中奖做记录
AwardsRecords::createRecords($user_id,$c_r_id,$awards_amount,$balance);

34
app/model/AgentDownScoresRecords.php

@ -0,0 +1,34 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class AgentDownScoresRecords extends Model
{
/**
* 创建下分记录
* @param $aid
* @param $user_id
* @param $withdrawal_amount
* @param $withdrawal_balance
* @return mixed
*/
public static function createRecords($aid,$user_id,$withdrawal_amount,$withdrawal_balance)
{
$Records = new AgentDownScoresRecords();
$Records->aid = $aid;
$Records->user_id = $user_id;
$Records->withdrawal_amount = $withdrawal_amount;
$Records->withdrawal_balance = $withdrawal_balance;
$Records->create_time = date("Y-m-d H:i:s",time());
$Records->save();
return $Records->id;
}
}

34
app/model/AgentUpScoresRecords.php

@ -0,0 +1,34 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class AgentUpScoresRecords extends Model
{
/**
* 创建上分记录
* @param $aid
* @param $user_id
* @param $withdrawal_amount
* @param $withdrawal_balance
* @return mixed
*/
public static function createRecords($aid,$user_id,$balance,$residue_amount)
{
$Records = new AgentUpScoresRecords();
$Records->aid = $aid;
$Records->user_id = $user_id;
$Records->balance = $balance;
$Records->residue_amount = $residue_amount;
$Records->create_time = date("Y-m-d H:i:s",time());
$Records->save();
return $Records->id;
}
}

86
app/model/AgentUser.php

@ -52,6 +52,92 @@ class AgentUser extends Model
}
}
/**
* 扣减余额(给用户上分)
* @param $aid
* @param $balance
* @return float|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function decrBalance($aid,$balance)
{
$user = self::find($aid);
$user->balance = round($user->balance - $balance,2);
$user->save();
return $user->balance;
}
/**
* 增加余额(充值、总后台上分)
* @param $aid
* @param $balance
* @return float|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function incrBalance($aid,$balance)
{
$user = self::find($aid);
$user->balance = round($user->balance + $balance,2);
$user->save();
return $user->balance;
}
/**
* 扣除可提余额 (提现)
* @param $aid
* @param $balance
* @return float|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function decrWithdrawalBalance($aid,$balance)
{
$user = self::find($aid);
$user->withdrawal_balance = round($user->withdrawal_balance - $balance,2);
$user->save();
return $user->withdrawal_balance;
}
/**
* 增加可提余额(给用户下分)
* @param $aid
* @param $balance
* @return float|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function incrWithdrawalBalance($aid,$balance)
{
$user = self::find($aid);
$user->withdrawal_balance = round($user->withdrawal_balance + $balance,2);
$user->save();
return $user->withdrawal_balance;
}
/**
* 检测代理余额
* @param $aid
* @param $balance
* @return array|int[]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function inspectBalance($aid,$balance)
{
$user = self::find($aid);
if ($user->balance < $balance) {
return ['status' => 0, 'msg' => '代理余额不足'];
}
return ['status' => 1];
}
/**
* 生成密码
* @param $password

37
app/model/RechargeRecords.php

@ -0,0 +1,37 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class RechargeRecords extends Model
{
/**
* 创建充值记录
* @param $user_id
* @param $recharge_amount
* @param $residue_amount
* @param $status
* @param $trade_type
* @return mixed
*/
public static function createRecords($user_id,$recharge_amount,$residue_amount,$status = 0,$trade_type = 1)
{
$RechargeRecords = new RechargeRecords();
$RechargeRecords->user_id = $user_id;
$RechargeRecords->recharge_amount = $recharge_amount;
$RechargeRecords->residue_amount = $residue_amount;
$RechargeRecords->recharge_time = date("Y-m-d H:i:s",time());
$RechargeRecords->trade_type = $trade_type;
$RechargeRecords->status = $status;
$RechargeRecords->save();
return $RechargeRecords->id;
}
}

22
app/model/User.php

@ -187,7 +187,7 @@ class User extends Model
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function IncrBalance($user_id,$balance)
public static function incrBalance($user_id,$balance)
{
$user = self::find($user_id);
$user->balance = round($user->balance + $balance,2);
@ -221,7 +221,7 @@ class User extends Model
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function IncrWithdrawalBalance($user_id,$balance)
public static function incrWithdrawalBalance($user_id,$balance)
{
$user = self::find($user_id);
$user->withdrawal_balance = round($user->withdrawal_balance + $balance,2);
@ -229,6 +229,24 @@ class User extends Model
return $user->withdrawal_balance;
}
/**
* 检查用户余额是否足够
* @param $user_id
* @param $balance
* @return array|int[]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function inspectUserBalance($user_id,$balance)
{
$user = self::find($user_id);
if ($user->withdrawal_balance < $balance) {
return ['status' => 0, 'msg' => '用户可提余额不足'];
}
return ['status' => 1];
}
/**
* 生成盐值
* @return string

36
app/model/WithdrawalRecords.php

@ -0,0 +1,36 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class WithdrawalRecords extends Model
{
/**
* 用户提现记录
* @param $user_id
* @param $withdrawal_amount
* @param $withdrawal_balance
* @param $status
* @param $trade_type
* @return mixed
*/
public static function createRecords($user_id,$withdrawal_amount,$withdrawal_balance,$status = 0,$trade_type = 1)
{
$Records = new WithdrawalRecords();
$Records->user_id = $user_id;
$Records->withdrawal_amount = $withdrawal_amount;
$Records->withdrawal_balance = $withdrawal_balance;
$Records->status = $status;
$Records->trade_type = $trade_type;
$Records->create_time = date("Y-m-d H:i:s",time());
$Records->save();
return $Records->id;
}
}

32
app/validate/Agent.php

@ -0,0 +1,32 @@
<?php
declare (strict_types = 1);
namespace app\validate;
use think\Validate;
class Agent extends Validate
{
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'user_id|用户id' => 'require|number',
'quota|额度' => 'require|number'
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [];
protected $scene = [
'scores' => ['user_id','quota']
];
}

4
route/app.php

@ -45,6 +45,10 @@ Route::group('zone',function(){
});
# 代理
Route::group('agentTeam',function(){
Route::get('upScores','agentTeam/upScores');
Route::get('downScores','agentTeam/downScores');
});

Loading…
Cancel
Save