Files
mini_tp/app/api/model/Member.php
T
2026-04-02 03:34:12 +00:00

146 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace app\api\model;
use think\Model;
/**
* 用户模型 (对应原系统 member 表)
* 数据库连接: dbmember (member库)
*/
class Member extends Model
{
// 设置连接名 (对应 config/database.php 中的 connections)
protected $connection = 'dbmember';
// 表名
protected $name = 'member';
// 主键
protected $pk = 'userid';
// 自动时间戳
protected $autoWriteTimestamp = false;
// 隐藏字段
protected $hidden = ['password'];
/**
* 根据用户名查找用户
* @param string $username
* @return Member|null
*/
public static function findByUsername(string $username): ?Member
{
return self::where('username', $username)->find();
}
/**
* 根据用户ID查找用户
* @param int $userid
* @return Member|null
*/
public static function findByUserid(int $userid): ?Member
{
return self::where('userid', $userid)->find();
}
/**
* 验证密码
* 当前项目统一使用双重 MD5。
*
* @param string $password 明文密码
* @return bool
*/
public function verifyPassword(string $password): bool
{
return self::makePassword($password) === $this->password;
}
/**
* 生成系统使用的密码摘要
* @param string $password 明文密码
* @return string
*/
public static function makePassword(string $password): string
{
return md5(md5($password));
}
/**
* 检查用户是否被禁用
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled == 1;
}
/**
* 检查账号是否过期
* @return bool
*/
public function isExpired(): bool
{
if (empty($this->endtime)) {
return false;
}
return $this->endtime < time();
}
/**
* 获取用户套餐信息
* @return array|null
*/
public function getProductInfo(): ?array
{
// 从主库获取套餐信息
$product = \think\facade\Db::connect('dbbiz')
->name('product_list')
->where('v_type', $this->v_type)
->find();
return $product;
}
/**
* 获取代理商信息
* @return array|null
*/
public function getAgentInfo(): ?array
{
if (empty($this->formtypeid)) {
return null;
}
return self::where('userid', $this->formtypeid)->find();
}
/**
* 记录登录日志
* @param bool $success
* @param string $loginType
* @return void
*/
public function logLogin(bool $success, string $loginType = 'password'): void
{
try {
\think\facade\Db::connect('dbmember')
->name('member_login_log')
->insert([
'userid' => $this->userid,
'ip' => request()->ip(),
'time' => time(),
'succeed' => $success ? 1 : 0,
'diqu' => '',
'login_type' => $loginType,
'adminid' => 0,
'v_type' => $this->v_type ?? 0,
]);
} catch (\Exception $e) {
// 日志记录失败不影响登录
}
}
}