diff --git a/app/controller/AdminUserTeam.php b/app/controller/AdminUserTeam.php index d00f87c..927bf79 100644 --- a/app/controller/AdminUserTeam.php +++ b/app/controller/AdminUserTeam.php @@ -245,12 +245,9 @@ class AdminUserTeam extends BaseController try { validate()->rule(['aid' => 'require|number'])->check($request); $aid = $request['aid']; - $User = new User(); - $UserRes = $User->find($aid); - if (empty($UserRes)) throw new ValidateException('用户不存在'); - if ($UserRes['identity'] != 3) { - throw new ValidateException('当前用户已是'.$User->identityArr[$UserRes['identity']]); - } + + $validateUserIdentity = validate(UserValidate::class)->validateUserIdentity($aid); + if ($validateUserIdentity !== true) return $this->renderError($validateUserIdentity); # 开启事务 $connection->startTrans(); diff --git a/app/controller/AgentTeam.php b/app/controller/AgentTeam.php index bdcc309..06e57f1 100644 --- a/app/controller/AgentTeam.php +++ b/app/controller/AgentTeam.php @@ -15,6 +15,7 @@ use app\model\User as UserModel; use app\model\CustomerService as CustomerServiceModel; use app\model\WithdrawalRecords; use app\validate\Agent; +use app\validate\User as UserValidate; use think\exception\ValidateException; use think\facade\Db; use think\facade\Request; @@ -127,8 +128,8 @@ class AgentTeam extends BaseController $agentUser = $this->request->userInfo; validate(Agent::class)->scene('scores')->check($request); - $checkCodeRes = validate(Agent::class)->validateUserInfo($agentUser['id'],$request['user_id']); - if ($checkCodeRes !== true) return $this->renderError($checkCodeRes); + $validateUserInfo = validate(Agent::class)->validateUserInfo($agentUser['id'],$request['user_id']); + if ($validateUserInfo !== true) return $this->renderError($validateUserInfo); $user_id = $request['user_id']; # 用户id $quota = $request['quota']; # 额度 @@ -177,8 +178,8 @@ class AgentTeam extends BaseController try { $agentUser = $this->request->userInfo; validate(Agent::class)->scene('scores')->check($request); - $checkCodeRes = validate(Agent::class)->validateUserInfo($agentUser['id'],$request['user_id']); - if ($checkCodeRes !== true) return $this->renderError($checkCodeRes); + $validateUserInfo = validate(Agent::class)->validateUserInfo($agentUser['id'],$request['user_id']); + if ($validateUserInfo !== true) return $this->renderError($validateUserInfo); $user_id = $request['user_id']; # 用户id $quota = $request['quota']; # 额度 @@ -346,6 +347,79 @@ class AgentTeam extends BaseController ]); } + /** + * 代理升级用户为代理 + */ + public function upGradationAgent() + { + $param = Request::param(); + + $connection = Db::connect(); + try { + + $agentData = $this->request->userInfo; + validate(Agent::class)->scene('promotion')->check($param); + + $user_id = $param['user_id']; + $rebate_ratio = $param['rebate_ratio'] ?? 0; + + $validateUserInfo = validate(Agent::class)->validateUserInfo($agentData['id'],$user_id); + if ($validateUserInfo !== true) throw new ValidateException($validateUserInfo); + + $validateUserIdentity = validate(UserValidate::class)->validateUserIdentity($user_id); + if ($validateUserIdentity !== true) throw new ValidateException($validateUserIdentity); + + $validateRebateRatio = validate(Agent::class)->validateRebateRatio($agentData['id'],$rebate_ratio); + if ($validateRebateRatio !== true) throw new ValidateException($validateRebateRatio); + + # 开启事务 + $connection->startTrans(); + # 创建代理信息 + AgentInfo::createInfo($user_id,$rebate_ratio); + # 升级为代理 + User::changeIdentity($user_id,2,$agentData['id']); + + $connection->commit(); + return $this->renderSuccess('成功'); + } catch (ValidateException $validateException) { + return $this->renderError($validateException->getMessage()); + } catch (\Exception $e) { + $connection->rollback(); + return $this->renderError($e->getMessage()); + } + } + + /** + * 修改代理占比 + */ + public function settingRebateRatio() + { + $param = Request::param(); + try { + + $agentData = $this->request->userInfo; + validate()->rule([ + 'user_id|用户id' => 'require|number', + 'rebate_ratio|返点占比' => 'require|float' + ])->check($param); + + $user_id = $param['user_id']; + $rebate_ratio = $param['rebate_ratio'] ?? 0; + + $validateUserInfo = validate(Agent::class)->validateUserInfo($agentData['id'],$user_id); + if ($validateUserInfo !== true) throw new ValidateException($validateUserInfo); + + $validateRebateRatio = validate(Agent::class)->validateRebateRatio($agentData['id'],$rebate_ratio); + if ($validateRebateRatio !== true) throw new ValidateException($validateRebateRatio); + + AgentInfo::updateRebateRatio($user_id,$rebate_ratio); + + return $this->renderSuccess('成功'); + } catch (ValidateException $validateException) { + return $this->renderError($validateException->getMessage()); + } + } + /** * 返回用户联系客服信息 * @return array diff --git a/app/model/User.php b/app/model/User.php index 507efc4..aa84aa3 100644 --- a/app/model/User.php +++ b/app/model/User.php @@ -318,15 +318,17 @@ class User extends Model * 修改身份 * @param $user_id * @param $identity + * @param $aid * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public static function changeIdentity($user_id,$identity = 2) + public static function changeIdentity($user_id,$identity = 2,$aid = 0) { $user = new User(); $userRes = $user->find($user_id); $userRes->identity = $identity; + $userRes->aid = $aid; $userRes->save(); } @@ -339,20 +341,26 @@ class User extends Model * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public static function addRebateRatioAmount($user_id,$amount) + public static function addRebateRatioAmount($user_id,$amount,$superior_rebate_ratio = 0) { + # 获取用户代理 $aid = User::where('id',$user_id)->value('aid'); if (empty($aid)) return false; - + # 获取代理返点比率 $rebate_ratio = AgentInfo::where('aid',$aid)->value('rebate_ratio'); if (!$rebate_ratio || !($rebate_ratio * 1)) return false; - + # 减去下级返点比率 + $rebate_ratio -= $superior_rebate_ratio; + # 计算反比金额 $rebateRatioAmount = bcmul($amount,$rebate_ratio,2); - - $residue_amount = self::incrBalance($aid,$rebateRatioAmount); + # 代理可提余额增加 + $residue_amount = self::incrWithdrawalBalance($aid,$rebateRatioAmount); # 代理充值返点金额 RechargeRecords::createRecords($aid,$rebateRatioAmount,$residue_amount,1,4,$user_id); + + # 获取用户代理上级代理 + self::addRebateRatioAmount($aid,$amount,$rebate_ratio); } /** diff --git a/app/validate/Agent.php b/app/validate/Agent.php index 1131677..a23ef95 100644 --- a/app/validate/Agent.php +++ b/app/validate/Agent.php @@ -3,6 +3,7 @@ declare (strict_types = 1); namespace app\validate; +use app\model\AgentInfo; use think\Validate; class Agent extends Validate @@ -37,12 +38,42 @@ class Agent extends Validate 'del' => ['aid'], 'register' => ['phone','password','rebate_ratio'], 'modifyPassword' => ['password','repassword'], + 'promotion' => ['user_id','rebate_ratio'] ]; + /** + * 验证用户 + * @param $aid + * @param $user_id + * @return bool|string + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ public function validateUserInfo($aid,$user_id) { $query = \app\model\User::where('aid',$aid)->where('id',$user_id)->find(); if (!$query) return '当前用户不在你管理之下'; return true; } + + /** + * 验证 返点比率 不得超出上级代理 + * @param $aid + * @param $rebate_ratio + * @return bool|string + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function validateRebateRatio($aid,$rebate_ratio) + { + $agentInfo = AgentInfo::where('aid',$aid)->field('rebate_ratio')->find(); + if (empty($agentInfo['rebate_ratio'])) return '当前代理账号未有返点权限,未能升级用户为代理'; + $rebate_ratio = $rebate_ratio / 100; + if ($rebate_ratio >= $agentInfo['rebate_ratio']) { + return '填写返点占比不能超出代理本身的返点占比,代理返点比率为:'.($agentInfo['rebate_ratio']*100).'%'; + } + return true; + } } diff --git a/app/validate/User.php b/app/validate/User.php index 62f1ea4..5a098bc 100644 --- a/app/validate/User.php +++ b/app/validate/User.php @@ -14,12 +14,13 @@ class User extends Validate * * @var array */ - protected $rule = [ - 'password|密码' => 'require|min:6|max:20', - 'repassword|确认密码' => 'require|confirm:password', - 'phone|手机号' => 'require|mobile', - 'user_id|用户id' => 'require|number' - ]; + protected $rule + = [ + 'password|密码' => 'require|min:6|max:20', + 'repassword|确认密码' => 'require|confirm:password', + 'phone|手机号' => 'require|mobile', + 'user_id|用户id' => 'require|number' + ]; /** * 定义错误信息 @@ -29,12 +30,13 @@ class User extends Validate */ protected $message = []; - protected $scene = [ - 'modifyPassword' => ['password','repassword'], - 'editUser' => ['user_id','password'], - 'register' => ['phone','password'], - 'delUser' => ['user_id'], - ]; + protected $scene + = [ + 'modifyPassword' => ['password', 'repassword'], + 'editUser' => ['user_id', 'password'], + 'register' => ['phone', 'password'], + 'delUser' => ['user_id'], + ]; /** * 手机号短信验证 @@ -45,9 +47,9 @@ class User extends Validate * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function checkCode($mobile,$code) + public function checkCode($mobile, $code) { - $pin_info = Db::name('pincode')->where('mobile',$mobile)->field('code,time')->find(); + $pin_info = Db::name('pincode')->where('mobile', $mobile)->field('code,time')->find(); if (empty($pin_info)) { return '短信验证码错误'; } @@ -59,4 +61,15 @@ class User extends Validate } return true; } + + public function validateUserIdentity($user_id) + { + $User = new \app\model\User(); + $UserRes = $User->find($user_id); + if (empty($UserRes)) return '用户不存在'; + if ($UserRes['identity'] != 3) { + return '当前用户已是'.$User->identityArr[$UserRes['identity']]; + } + return true; + } }