10 changed files with 221 additions and 331 deletions
@ -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(); |
||||
|
} |
||||
|
} |
||||
@ -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; |
|
||||
} |
|
||||
} |
|
||||
@ -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)); |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue