You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.1 KiB
174 lines
5.1 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\model;
|
|
|
|
use think\facade\Session;
|
|
use think\Model;
|
|
|
|
/**
|
|
* @mixin \think\Model
|
|
*/
|
|
class AgentUser extends Model
|
|
{
|
|
//
|
|
|
|
/**
|
|
* 代理登陆
|
|
* @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);
|
|
|
|
return ['status' => 1, 'msg' => '登陆成功', 'data' => $login_user_data];
|
|
} catch (\Exception $e) {
|
|
return ['status' => 0, 'msg' => $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 扣减余额(给用户上分)
|
|
* @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 $password
|
|
* @param $salt
|
|
* @return string
|
|
*/
|
|
private function generateHashedPassword($password,$salt)
|
|
{
|
|
return md5(md5($password) . md5($salt));
|
|
}
|
|
}
|
|
|