Files
cunkebao_v3/Server/application/common/model/User.php
2025-03-27 15:28:38 +08:00

165 lines
4.0 KiB
PHP

<?php
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
class User extends Model
{
use SoftDelete;
/**
* 数据表名
* @var string
*/
protected $table = 'tk_users';
/**
* 主键
* @var string
*/
protected $pk = 'id';
/**
* 自动写入时间戳
* @var bool
*/
protected $autoWriteTimestamp = true;
/**
* 创建时间字段
* @var string
*/
protected $createTime = 'createTime';
/**
* 更新时间字段
* @var string
*/
protected $updateTime = 'updateTime';
/**
* 软删除字段
* @var string
*/
protected $deleteTime = 'deleteTime';
/**
* 隐藏属性
* @var array
*/
protected $hidden = ['passwordMd5', 'passwordLocal', 'deleteTime'];
/**
* 字段类型
* @var array
*/
protected $type = [
'id' => 'integer',
'isAdmin' => 'integer',
'companyId' => 'integer',
'typeId' => 'integer',
'lastLoginTime' => 'integer',
'status' => 'integer',
'createTime' => 'integer',
'updateTime' => 'integer',
'deleteTime' => 'integer'
];
/**
* 获取管理员用户信息
* @param string $account 账号(手机号)
* @param string $password 密码(可能是加密后的)
* @param int $typeId 身份信息
* @return array|null
*/
public static function getAdminUser($account, $password, $typeId)
{
// 查询用户
$user = self::where('account', $account)
->where('typeId', $typeId)
->where('status', 1)
->find();
if (!$user) {
// 记录日志
\think\facade\Log::info('用户不存在或已禁用', ['account' => $account]);
return null;
}
// 记录密码验证信息
\think\facade\Log::info('密码验证', [
'account' => $account,
'input_password' => $password,
'stored_hash' => $user->passwordMd5,
]);
// 验证密码
$isValid = password_verify($password, $user->passwordMd5);
\think\facade\Log::info('密码验证结果', [
'account' => $account,
'is_valid' => $isValid,
]);
if (!$isValid) {
return null;
}
// 更新登录信息
$user->lastLoginIp = request()->ip();
$user->lastLoginTime = time();
$user->save();
return [
'id' => $user->id,
'account' => $user->account,
'isAdmin' => $user->isAdmin,
'companyId' => $user->companyId,
'typeId' => $user->typeId,
'lastLoginIp' => $user->lastLoginIp,
'lastLoginTime' => $user->lastLoginTime
];
}
/**
* 通过手机号获取用户信息
* @param string $account 手机号
* @return array|null
*/
public static function getUserByMobile($account)
{
// 查询用户
$user = self::where('account', $account)
->where('status', 1)
->find();
if (!$user) {
return null;
}
return [
'id' => $user->id,
'account' => $user->account,
'isAdmin' => $user->isAdmin,
'companyId' => $user->companyId,
'typeId' => $user->typeId,
'role' => $user->isAdmin ? 'admin' : 'user',
'permissions' => $user->isAdmin ? ['*'] : ['user']
];
}
/**
* 验证用户密码
* @param string $password 密码
* @param bool $isEncrypted 是否已加密
* @return bool
*/
public function verifyPassword($password, $isEncrypted = false)
{
if ($isEncrypted) {
return hash_equals($this->passwordMd5, $password);
} else {
return $this->passwordMd5 === md5($password);
}
}
}