Browse Source

总后台代理管理优化

master
wanghongjun 3 years ago
parent
commit
c3afeb3f11
  1. 91
      app/controller/AdminAgentTeam.php
  2. 4
      app/listener/LogoutAgent.php
  3. 8
      app/middleware/CheckAgent.php
  4. 75
      app/model/AgentInfo.php
  5. 40
      app/model/AgentRechargeRecords.php
  6. 248
      app/model/AgentUser.php
  7. 6
      app/model/RechargeRecords.php
  8. 65
      app/model/User.php
  9. 4
      app/model/WithdrawalRecords.php
  10. 5
      app/validate/Agent.php

91
app/controller/AdminAgentTeam.php

@ -8,8 +8,10 @@ use app\event\AgentPasswordChange;
use app\middleware\CheckAdmin; use app\middleware\CheckAdmin;
use app\model\AdminDownScoresRecords; use app\model\AdminDownScoresRecords;
use app\model\AdminUpScoresRecords; use app\model\AdminUpScoresRecords;
use app\model\AgentRechargeRecords; use app\model\AgentInfo;
use app\model\AgentUser; use app\model\RechargeRecords;
use app\model\User;
use app\model\WithdrawalRecords;
use app\validate\Admin; use app\validate\Admin;
use app\validate\Agent; use app\validate\Agent;
use think\exception\ValidateException; use think\exception\ValidateException;
@ -35,9 +37,9 @@ class AdminAgentTeam extends BaseController
$limit = $request['limit'] ?? 10; $limit = $request['limit'] ?? 10;
$where = []; $where = ['identity' => 2];
$UserModel = new AgentUser(); $UserModel = new User();
# 用户id / 手机号码查询 # 用户id / 手机号码查询
if (isset($request['title']) && !empty($request['title'])) { if (isset($request['title']) && !empty($request['title'])) {
@ -56,17 +58,20 @@ class AdminAgentTeam extends BaseController
} }
# 查询用户列表 # 查询用户列表
$field = 'id,phone,avatar,balance,withdrawal_balance'; $field = 'id,phone,avatar,balance,withdrawal_balance,status';
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit); $userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
$list = $userRes->items(); $list = $userRes->items();
$total = $userRes->total(); $total = $userRes->total();
$AgentInfo = new AgentInfo();
foreach ($list as &$item) { foreach ($list as &$item) {
#$item['phone'] = format_phone_number($item['phone']);
$item['avatar'] = get_image_url($item['avatar']); $item['avatar'] = get_image_url($item['avatar']);
$item['status'] = $item['status'] == 1 ? '正常' : '停用'; $item['status'] = $item['status'] == 1 ? '正常' : '停用';
# 获取返点占比
$rebate_ratio = $AgentInfo->where('aid',$item['id'])->value('rebate_ratio');
$item['rebate_ratio'] = $rebate_ratio * 100;
} }
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]); return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]);
@ -93,11 +98,11 @@ class AdminAgentTeam extends BaseController
$connection->startTrans(); $connection->startTrans();
# 增加代理余额 # 增加代理余额
$agentBalance = AgentUser::incrBalance($aid,$quota); $agentBalance = User::incrBalance($aid,$quota);
# 代理上分记录 # 代理上分记录
AdminUpScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2); AdminUpScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2);
# 代理充值记录 # 代理充值记录
AgentRechargeRecords::createRecords($aid,$quota,$agentBalance,1); RechargeRecords::createRecords($aid,$quota,$agentBalance,1,2,$adminUser['id']);
$connection->commit(); $connection->commit();
return $this->renderSuccess('上分成功'); return $this->renderSuccess('上分成功');
@ -105,7 +110,7 @@ class AdminAgentTeam extends BaseController
return $this->renderError($e->getMessage()); return $this->renderError($e->getMessage());
} catch (\Exception $exception) { } catch (\Exception $exception) {
$connection->rollback(); $connection->rollback();
return $this->renderError($exception->getMessage()); return $this->renderError('操作失败');
} }
} }
@ -129,25 +134,28 @@ class AdminAgentTeam extends BaseController
$quota = $request['quota']; # 额度 $quota = $request['quota']; # 额度
# 检测代理可提余额是否足够 # 检测代理可提余额是否足够
$inspectRes = AgentUser::inspectWithdrawalBalance($aid,$quota); $inspectRes = User::inspectUserBalance($aid,$quota);
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']); if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
# 开启事务 # 开启事务
$connection->startTrans(); $connection->startTrans();
# 扣减代理可提余额 # 扣减代理可提余额
$agentBalance = AgentUser::decrWithdrawalBalance($aid,$quota); $agentBalance = User::decrWithdrawalBalance($aid,$quota);
# 代理下分记录 # 代理下分记录
AdminDownScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2); AdminDownScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2);
# 用户提现记录-下分
WithdrawalRecords::createRecords($aid,$quota,$agentBalance,1,2,$adminUser['id']);
$connection->commit(); $connection->commit();
return $this->renderSuccess('下分成功'); return $this->renderSuccess('下分成功');
} catch (ValidateException $e) { } catch (ValidateException $e) {
return $this->renderError($e->getMessage()); return $this->renderError($e->getMessage());
} catch (\Exception $exception) { } catch (\Exception $exception) {
$connection->rollback(); $connection->rollback();
return $this->renderError($exception->getMessage()); return $this->renderError('操作失败');
} }
} }
@ -158,21 +166,46 @@ class AdminAgentTeam extends BaseController
{ {
$param = Request::param(); $param = Request::param();
$connection = Db::connect();
try { try {
validate(Agent::class)->scene('edit')->check($param); validate()->rule([
'aid|代理id' => 'require|number',
'password|密码' => 'min:6|max:20',
'rebate_ratio|返点占比' => 'float|egt:1'
])->check($param);
if (empty($param['password']) && empty($param['rebate_ratio'])) {
throw new ValidateException('请填写修改的密码或返点占比');
}
# 开启事务
$connection->startTrans();
$UserModel = new AgentUser(); # 保存密码
if (!empty($param['password'])) {
$param['user_id'] = $param['aid'];
$UserModel = new User();
$result = $UserModel->retrieve($param); $result = $UserModel->retrieve($param);
if (!$result['status']) throw new ValidateException($result['msg']); if (!$result['status']) throw new ValidateException($result['msg']);
# 清除登录记录
$event = new AgentPasswordChange($param['aid']); $event = new AgentPasswordChange($param['aid']);
Event::trigger(AgentPasswordChange::class,$event); Event::trigger(AgentPasswordChange::class,$event);
}
# 保存返点占比
if (!empty($param['rebate_ratio'])) {
AgentInfo::updateRebateRatio($param['aid'],$param['rebate_ratio']);
}
return $this->renderSuccess($result['msg']); $connection->commit();
return $this->renderSuccess('成功');
} catch (ValidateException $validateException) { } catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage()); return $this->renderError($validateException->getMessage());
} catch (\Exception $exception) {
$connection->rollback();
return $this->renderError('操作失败');
} }
} }
@ -184,18 +217,27 @@ class AdminAgentTeam extends BaseController
{ {
$param = Request::param(); $param = Request::param();
$connection = Db::connect();
try { try {
validate(Agent::class)->scene('del')->check($param); validate(Agent::class)->scene('del')->check($param);
$result = AgentUser::destroy($param['aid']); $connection->startTrans();
$result = User::destroy($param['aid']);
if (!$result) throw new ValidateException('删除失败'); if (!$result) throw new ValidateException('删除失败');
$agent_info_id = AgentInfo::where('aid',$param['aid'])->value('id');
$resInfo = AgentInfo::destroy($agent_info_id);
if (!$resInfo) throw new ValidateException('删除失败');
$connection->commit();
return $this->renderSuccess('已删除'); return $this->renderSuccess('已删除');
} catch (ValidateException $validateException) { } catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage()); return $this->renderError($validateException->getMessage());
} catch (\Exception $e) { } catch (\Exception $e) {
$connection->rollback();
return $this->renderError('操作失败'); return $this->renderError('操作失败');
} }
} }
@ -211,19 +253,28 @@ class AdminAgentTeam extends BaseController
{ {
$param = Request::param(); $param = Request::param();
$connection = Db::connect();
try { try {
validate(Agent::class)->scene('register')->check($param); validate(Agent::class)->scene('register')->check($param);
$AgentUser = new AgentUser(); # 开启事务
$result = $AgentUser->register($param); $connection->startTrans();
$AgentUser = new User();
$param['aid'] = 0;
$aid = $AgentUser->register($param,2);
if (!$result) throw new ValidateException('代理已存在'); if (!$aid) throw new ValidateException('代理已存在');
AgentInfo::createInfo($aid,$param['rebate_ratio']);
$connection->commit();
return $this->renderSuccess('添加成功'); return $this->renderSuccess('添加成功');
} catch (ValidateException $validateException) { } catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage()); return $this->renderError($validateException->getMessage());
} catch (\Exception $e) { } catch (\Exception $e) {
$connection->rollback();
return $this->renderError('操作失败'); return $this->renderError('操作失败');
} }
} }

4
app/listener/LogoutAgent.php

@ -17,11 +17,11 @@ class LogoutAgent
{ {
$aid = $event->aid; $aid = $event->aid;
$agentData = Cache::store('redis')->get('login_agent_user_data'); $agentData = Cache::store('redis')->get('login_user_data');
if ($agentData) { if ($agentData) {
$agentData = unserialize($agentData); $agentData = unserialize($agentData);
if ($agentData['id'] == $aid) { if ($agentData['id'] == $aid) {
Session::delete('login_agent_user_data'); Session::delete('login_user_data');
} }
} }
} }

8
app/middleware/CheckAgent.php

@ -3,8 +3,8 @@ declare (strict_types = 1);
namespace app\middleware; namespace app\middleware;
use app\model\User;
use think\Exception; use think\Exception;
use think\facade\Cache;
use think\facade\Request; use think\facade\Request;
use think\facade\Session; use think\facade\Session;
@ -26,9 +26,13 @@ class CheckAgent
if($userinfo['code'] != 200) if($userinfo['code'] != 200)
throw new Exception('当前登录已失效,请重新登录',403); throw new Exception('当前登录已失效,请重新登录',403);
$request->userInfo = $userinfo['data']; $request->userInfo = $userinfo['data'];
if (!Session::get('login_agent_user_data')) { if (!Session::get('login_user_data')) {
throw new Exception('代理未登陆,请先登陆后操作',201); throw new Exception('代理未登陆,请先登陆后操作',201);
} }
$user = User::find($userinfo['data']['id']);
if ($user['identity'] != 2) {
throw new Exception('非代理身份无法操作',400);
}
} }
catch (\Exception $err){ catch (\Exception $err){
return json(['code'=>$err->getCode(),'msg'=>$err->getMessage()]); return json(['code'=>$err->getCode(),'msg'=>$err->getMessage()]);

75
app/model/AgentInfo.php

@ -0,0 +1,75 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin \think\Model
*/
class AgentInfo extends Model
{
//
use SoftDelete;
protected $deleteTime = 'delete_time';
/**
* 升级为代理
* @param $aid
* @param $rebate_ratio
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function createInfo($aid,$rebate_ratio = 0)
{
$invite_code = self::returnInviteCode();
$model = new AgentInfo();
$save = [
'aid' => $aid,
'invite_code' => $invite_code,
'create_time' => date("Y-m-d H:i:s",time())
];
if (!empty($rebate_ratio)) {
$save['rebate_ratio'] = $rebate_ratio / 100;
}
$model->save($save);
}
/**
* 递归获取验证码(不重复)
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function returnInviteCode()
{
$invite_code = generate_random_str(6);
$query = AgentInfo::withTrashed()->where('invite_code',$invite_code)->field('id')->find();
if ($query) {
return self::returnInviteCode();
}
return $invite_code;
}
/**
* 修改返点占比
* @param $aid
* @param $rebate_ratio
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function updateRebateRatio($aid,$rebate_ratio)
{
$AgentInfo = new AgentInfo();
$AgentInfoRes = $AgentInfo->where('aid',$aid)->find();
$AgentInfoRes->rebate_ratio = $rebate_ratio / 100;
$AgentInfoRes->update_time = date("Y-m-d H:i:s",time());
$AgentInfoRes->save();
}
}

40
app/model/AgentRechargeRecords.php

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

248
app/model/AgentUser.php

@ -1,248 +0,0 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\facade\Cache;
use think\facade\Session;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin \think\Model
*/
class AgentUser extends Model
{
use SoftDelete;
protected $deleteTime = 'delete_time';
/**
* 代理登陆
* @param $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function login($data)
{
// 根据用户名查询用户信息
$AgentUser = new AgentUser();
$user = $AgentUser
->where('phone', $data['phone'])
->field('id,phone,avatar,password,salt,status')
->find();
try {
if (!$user) throw new \Exception('代理账号不存在');
if ($user->status != 1) throw new \Exception('账号已被停用');
// 使用相同的盐值对输入密码进行哈希验证
$hashedPassword = $AgentUser->generateHashedPassword($data['password'], $user->salt);
if ($user->password !== $hashedPassword) throw new \Exception('密码错误');
# 缓存用户信息
$login_user_data = $user->toArray();
unset($login_user_data['password'],$login_user_data['salt'],$login_user_data['status']);
Session::set('login_agent_user_data',$login_user_data);
Cache::store('redis')->set('login_agent_user_data',serialize($login_user_data),7200);
return ['status' => 1, 'msg' => '登陆成功', 'data' => $login_user_data];
} catch (\Exception $e) {
return ['status' => 0, 'msg' => $e->getMessage()];
}
}
/**
* 注册、创建代理
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function register($data)
{
$user = $this->where('phone', $data['phone'])->find();
if ($user) return false;
// 生成盐值
$salt = generate_random_str(6);
// 密码加盐值后哈希存储
$password = $this->generateHashedPassword($data['password'], $salt);
$this->save([
// 随机头像
'avatar' => rand_avatar(),
'password' => $password,
'salt' => $salt,
'phone' => $data['phone'],
'invite_code' => generate_random_str(6),
'create_time' => date("Y-m-d H:i:s",time())
]);
return true;
}
/**
* 扣减余额(给用户上分)
* @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)
{
$userModel = new AgentUser();
$user = $userModel->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)
{
$userModel = new AgentUser();
$user = $userModel->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)
{
$userModel = new AgentUser();
$user = $userModel->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)
{
$userModel = new AgentUser();
$user = $userModel->find($aid);
if ($user->balance < $balance) {
return ['status' => 0, 'msg' => '代理余额不足'];
}
return ['status' => 1];
}
/**
* 检测代理余额
* @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 inspectWithdrawalBalance($aid,$balance)
{
$userModel = new AgentUser();
$user = $userModel->find($aid);
if ($user->withdrawal_balance < $balance) {
return ['status' => 0, 'msg' => '代理可提余额不足'];
}
return ['status' => 1];
}
/**
* 找回密码
* @param $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function retrieve($data)
{
// 根据代理手机号和代理id修改密码
$user = [];
$errorMsg = '';
if (isset($data['phone'])) {
$errorMsg = '手机号';
$user = $this->where('phone', $data['phone'])->find();
} elseif ($data['aid']) {
$errorMsg = '代理账号';
$user = $this->find($data['aid']);
}
if ($user) {
// 生成盐值
$salt = generate_random_str(6);
$password = $this->generateHashedPassword($data['password'], $salt);
// 密码加盐值后哈希存储
$user->password = $password;
$user->salt = $salt;
$user->update_time = date("Y-m-d H:i:s",time());
$user->save();
return ['status' => true, 'msg' => '密码重制成功'];
}
return ['status' => false, 'msg' => $errorMsg.'未注册'];
}
/**
* 生成密码
* @param $password
* @param $salt
* @return string
*/
private function generateHashedPassword($password,$salt)
{
return md5(md5($password) . md5($salt));
}
}

6
app/model/RechargeRecords.php

@ -10,6 +10,8 @@ use think\Model;
*/ */
class RechargeRecords extends Model class RechargeRecords extends Model
{ {
public $tradeType = [1 => '代理上分', 2 => '管理员上分', 3 => '支付宝'];
/** /**
* 创建充值记录 * 创建充值记录
* @param $user_id * @param $user_id
@ -17,9 +19,10 @@ class RechargeRecords extends Model
* @param $residue_amount * @param $residue_amount
* @param $status * @param $status
* @param $trade_type * @param $trade_type
* @param $trade_id
* @return mixed * @return mixed
*/ */
public static function createRecords($user_id,$recharge_amount,$residue_amount,$status = 0,$trade_type = 1) public static function createRecords($user_id,$recharge_amount,$residue_amount,$status = 0,$trade_type = 1,$trade_id = 0)
{ {
$RechargeRecords = new RechargeRecords(); $RechargeRecords = new RechargeRecords();
@ -28,6 +31,7 @@ class RechargeRecords extends Model
$RechargeRecords->residue_amount = $residue_amount; $RechargeRecords->residue_amount = $residue_amount;
$RechargeRecords->recharge_time = date("Y-m-d H:i:s",time()); $RechargeRecords->recharge_time = date("Y-m-d H:i:s",time());
$RechargeRecords->trade_type = $trade_type; $RechargeRecords->trade_type = $trade_type;
$RechargeRecords->trade_id = $trade_id;
$RechargeRecords->status = $status; $RechargeRecords->status = $status;
if ($status == 1) { if ($status == 1) {
$RechargeRecords->pay_time = date("Y-m-d H:i:s",time()); $RechargeRecords->pay_time = date("Y-m-d H:i:s",time());

65
app/model/User.php

@ -13,36 +13,40 @@ class User extends Model
protected $deleteTime = 'delete_time'; protected $deleteTime = 'delete_time';
protected $defaultSoftDelete = 0; protected $defaultSoftDelete = 0;
public $identityArr = [2 => '代理', 3 => '用户'];
/** /**
* 注册用户 * 注册用户
* @param $data * @param $data
* @param $identity
* @return bool * @return bool
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function register($data) public function register($data,$identity = 3)
{ {
$User = new User();
$user = $this->where('phone', $data['phone'])->find(); $user = $User->where('phone', $data['phone'])->find();
if ($user) return false; if ($user) return 0;
// 生成盐值 // 生成盐值
$salt = $this->generateSalt(); $salt = $this->generateSalt();
// 密码加盐值后哈希存储 // 密码加盐值后哈希存储
$password = $this->generateHashedPassword($data['password'], $salt); $password = $this->generateHashedPassword($data['password'], $salt);
$this->save([ $User->save([
// 随机头像 // 随机头像
'avatar' => rand_avatar(), 'avatar' => rand_avatar(),
'password' => $password, 'password' => $password,
'salt' => $salt, 'salt' => $salt,
'phone' => $data['phone'], 'phone' => $data['phone'],
'aid' => $data['aid'], 'aid' => $data['aid'],
'identity' => $identity,
'register_time' => date("Y-m-d H:i:s",time()) 'register_time' => date("Y-m-d H:i:s",time())
]); ]);
return true; return $User->id;
} }
/** /**
@ -56,10 +60,11 @@ class User extends Model
public function login($data) public function login($data)
{ {
// 根据用户名查询用户信息 // 根据用户名查询用户信息
$user = $this->where('phone', $data['phone'])->field('id,avatar,phone,password,salt,status')->find(); $user = $this->where('phone', $data['phone'])->field('id,avatar,phone,password,salt,status,identity')->find();
if ($user) { if ($user) {
if ($user['status'] != 1) return ['status' => false, 'msg' => '用户已被停用']; $msg = $this->identityArr[$user['identity']];
if ($user['status'] != 1) return ['status' => false, 'msg' => $msg.'已被停用'];
// 使用相同的盐值对输入密码进行哈希验证 // 使用相同的盐值对输入密码进行哈希验证
$hashedPassword = $this->generateHashedPassword($data['password'], $user->salt); $hashedPassword = $this->generateHashedPassword($data['password'], $user->salt);
@ -167,14 +172,15 @@ class User extends Model
*/ */
public function verifyInviteCode($invite_code) public function verifyInviteCode($invite_code)
{ {
$AgentUser = new AgentUser(); if (empty($invite_code)) return 0;
$AgentUser = new AgentInfo();
$codeRes = $AgentUser->where('invite_code',$invite_code)->find(); $codeRes = $AgentUser->where('invite_code',$invite_code)->find();
if (empty($codeRes)) { if (empty($codeRes)) {
return 0; return 0;
} }
return $codeRes->id; return $codeRes->aid;
} }
/** /**
@ -250,7 +256,7 @@ class User extends Model
} }
/** /**
* 检查用户余额是否足够 * 检查用户|代理可提余额是否足够
* @param $user_id * @param $user_id
* @param $balance * @param $balance
* @return array|int[] * @return array|int[]
@ -263,7 +269,7 @@ class User extends Model
$userModel = new User(); $userModel = new User();
$user = $userModel->find($user_id); $user = $userModel->find($user_id);
if ($user->withdrawal_balance < $balance) { if ($user->withdrawal_balance < $balance) {
return ['status' => 0, 'msg' => '用户可提余额不足']; return ['status' => 0, 'msg' => $userModel->identityArr[$user->identity].'可提余额不足'];
} }
return ['status' => 1]; return ['status' => 1];
} }
@ -289,6 +295,41 @@ class User extends Model
return ['status' => 1]; return ['status' => 1];
} }
/**
* 检测代理|用户余额
* @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)
{
$userModel = new User();
$user = $userModel->find($aid);
if ($user->balance < $balance) {
return ['status' => 0, 'msg' => $userModel->identityArr[$user->identity].'余额不足'];
}
return ['status' => 1];
}
/**
* 修改身份
* @param $user_id
* @param $identity
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function changeIdentity($user_id,$identity = 2)
{
$user = new User();
$userRes = $user->find($user_id);
$userRes->identity = $identity;
$userRes->save();
}
/** /**
* 生成盐值 * 生成盐值
* @return string * @return string

4
app/model/WithdrawalRecords.php

@ -17,9 +17,10 @@ class WithdrawalRecords extends Model
* @param $withdrawal_balance * @param $withdrawal_balance
* @param $status * @param $status
* @param $trade_type * @param $trade_type
* @param $trade_id
* @return mixed * @return mixed
*/ */
public static function createRecords($user_id,$withdrawal_amount,$withdrawal_balance,$status = 0,$trade_type = 1) public static function createRecords($user_id,$withdrawal_amount,$withdrawal_balance,$status = 0,$trade_type = 1,$trade_id = 0)
{ {
$Records = new WithdrawalRecords(); $Records = new WithdrawalRecords();
@ -27,6 +28,7 @@ class WithdrawalRecords extends Model
$Records->withdrawal_amount = $withdrawal_amount; $Records->withdrawal_amount = $withdrawal_amount;
$Records->withdrawal_balance = $withdrawal_balance; $Records->withdrawal_balance = $withdrawal_balance;
$Records->trade_type = $trade_type; $Records->trade_type = $trade_type;
$Records->trade_id = $trade_id;
$Records->status = $status; $Records->status = $status;
$Records->apply_time = date("Y-m-d H:i:s",time()); $Records->apply_time = date("Y-m-d H:i:s",time());
if ($status == 1) { if ($status == 1) {

5
app/validate/Agent.php

@ -20,6 +20,7 @@ class Agent extends Validate
'repassword|确认密码' => 'require|confirm:password', 'repassword|确认密码' => 'require|confirm:password',
'aid|代理id' => 'require|number', 'aid|代理id' => 'require|number',
'phone|手机号' => 'require|mobile', 'phone|手机号' => 'require|mobile',
'rebate_ratio|返点占比' => 'float|egt:1'
]; ];
/** /**
@ -32,9 +33,9 @@ class Agent extends Validate
protected $scene = [ protected $scene = [
'scores' => ['user_id','quota'], 'scores' => ['user_id','quota'],
'edit' => ['aid','password'], 'edit' => ['aid','password','rebate_ratio'],
'del' => ['aid'], 'del' => ['aid'],
'register' => ['phone','password'], 'register' => ['phone','password','rebate_ratio'],
'modifyPassword' => ['password','repassword'], 'modifyPassword' => ['password','repassword'],
]; ];
} }

Loading…
Cancel
Save