调整登录逻辑
This commit is contained in:
200
Server/application/common/model/DeviceUser.php
Normal file
200
Server/application/common/model/DeviceUser.php
Normal file
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 设备用户关联模型
|
||||
* 用于管理设备与操盘手(用户)的关联关系
|
||||
*/
|
||||
class DeviceUser extends Model
|
||||
{
|
||||
/**
|
||||
* 数据表名
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'tk_device_user';
|
||||
|
||||
/**
|
||||
* 设置主键
|
||||
* 由于该表是多字段联合主键,这里不设置主键
|
||||
* @var string
|
||||
*/
|
||||
protected $pk = '';
|
||||
|
||||
/**
|
||||
* 自动时间戳
|
||||
* 该表不需要时间戳
|
||||
* @var bool
|
||||
*/
|
||||
protected $autoWriteTimestamp = false;
|
||||
|
||||
/**
|
||||
* 字段类型
|
||||
* @var array
|
||||
*/
|
||||
protected $type = [
|
||||
'companyId' => 'integer',
|
||||
'userId' => 'integer',
|
||||
'deviceId' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取指定用户的所有设备ID
|
||||
* @param int $userId 用户ID
|
||||
* @param int $companyId 公司ID
|
||||
* @return array 设备ID数组
|
||||
*/
|
||||
public static function getUserDeviceIds($userId, $companyId = null)
|
||||
{
|
||||
$where = ['userId' => $userId];
|
||||
|
||||
if (!is_null($companyId)) {
|
||||
$where['companyId'] = $companyId;
|
||||
}
|
||||
|
||||
return self::where($where)
|
||||
->column('deviceId');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定设备的所有用户ID
|
||||
* @param int $deviceId 设备ID
|
||||
* @param int $companyId 公司ID
|
||||
* @return array 用户ID数组
|
||||
*/
|
||||
public static function getDeviceUserIds($deviceId, $companyId = null)
|
||||
{
|
||||
$where = ['deviceId' => $deviceId];
|
||||
|
||||
if (!is_null($companyId)) {
|
||||
$where['companyId'] = $companyId;
|
||||
}
|
||||
|
||||
return self::where($where)
|
||||
->column('userId');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加设备与用户的关联
|
||||
* @param int $companyId 公司ID
|
||||
* @param int $userId 用户ID
|
||||
* @param int $deviceId 设备ID
|
||||
* @return bool 是否添加成功
|
||||
*/
|
||||
public static function addRelation($companyId, $userId, $deviceId)
|
||||
{
|
||||
// 检查关联是否已存在
|
||||
$exists = self::where([
|
||||
'companyId' => $companyId,
|
||||
'userId' => $userId,
|
||||
'deviceId' => $deviceId
|
||||
])->find();
|
||||
|
||||
if ($exists) {
|
||||
return true; // 已存在,视为添加成功
|
||||
}
|
||||
|
||||
// 添加新关联
|
||||
return self::create([
|
||||
'companyId' => $companyId,
|
||||
'userId' => $userId,
|
||||
'deviceId' => $deviceId
|
||||
]) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加设备与用户的关联
|
||||
* @param int $companyId 公司ID
|
||||
* @param int $userId 用户ID
|
||||
* @param array $deviceIds 设备ID数组
|
||||
* @return int 成功添加的记录数
|
||||
*/
|
||||
public static function batchAddRelations($companyId, $userId, array $deviceIds)
|
||||
{
|
||||
if (empty($deviceIds)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$data = [];
|
||||
foreach ($deviceIds as $deviceId) {
|
||||
$data[] = [
|
||||
'companyId' => $companyId,
|
||||
'userId' => $userId,
|
||||
'deviceId' => $deviceId
|
||||
];
|
||||
}
|
||||
|
||||
// 批量添加前先删除已存在的关联,避免主键冲突
|
||||
self::where('userId', $userId)
|
||||
->where('companyId', $companyId)
|
||||
->whereIn('deviceId', $deviceIds)
|
||||
->delete();
|
||||
|
||||
return self::insertAll($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除设备与用户的关联
|
||||
* @param int $companyId 公司ID
|
||||
* @param int $userId 用户ID
|
||||
* @param int $deviceId 设备ID
|
||||
* @return bool 是否删除成功
|
||||
*/
|
||||
public static function removeRelation($companyId, $userId, $deviceId)
|
||||
{
|
||||
return self::where([
|
||||
'companyId' => $companyId,
|
||||
'userId' => $userId,
|
||||
'deviceId' => $deviceId
|
||||
])->delete() !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否有权限操作指定设备
|
||||
* @param int $userId 用户ID
|
||||
* @param int $deviceId 设备ID
|
||||
* @param int $companyId 公司ID
|
||||
* @return bool 是否有权限
|
||||
*/
|
||||
public static function checkUserDevicePermission($userId, $deviceId, $companyId = null)
|
||||
{
|
||||
$where = [
|
||||
'userId' => $userId,
|
||||
'deviceId' => $deviceId
|
||||
];
|
||||
|
||||
if (!is_null($companyId)) {
|
||||
$where['companyId'] = $companyId;
|
||||
}
|
||||
|
||||
return self::where($where)->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联用户模型
|
||||
* @return \think\model\relation\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('User', 'userId', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联设备模型
|
||||
* @return \think\model\relation\BelongsTo
|
||||
*/
|
||||
public function device()
|
||||
{
|
||||
return $this->belongsTo('app\devices\model\Device', 'deviceId', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联公司模型
|
||||
* @return \think\model\relation\BelongsTo
|
||||
*/
|
||||
public function company()
|
||||
{
|
||||
return $this->belongsTo('Company', 'companyId', 'id');
|
||||
}
|
||||
}
|
||||
@@ -67,13 +67,13 @@ class User extends Model
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取管理员用户信息
|
||||
* 获取用户信息
|
||||
* @param string $account 账号(手机号)
|
||||
* @param string $password 密码(可能是加密后的)
|
||||
* @param int $typeId 身份信息
|
||||
* @return array|null
|
||||
*/
|
||||
public static function getAdminUser($account, $password, $typeId)
|
||||
public static function getUser($account, $password, $typeId)
|
||||
{
|
||||
// 查询用户
|
||||
$user = self::where('account', $account)
|
||||
|
||||
@@ -40,7 +40,7 @@ class AuthService
|
||||
public function login($account, $password, $typeId, $ip)
|
||||
{
|
||||
// 获取用户信息
|
||||
$user = User::getAdminUser($account, $password, $typeId);
|
||||
$user = User::getUser($account, $password, $typeId);
|
||||
|
||||
if (empty($user)) {
|
||||
// 记录登录失败
|
||||
|
||||
@@ -38,7 +38,9 @@ class JwtUtil
|
||||
// 附加过期时间
|
||||
$payload['exp'] = time() + $expire;
|
||||
$payload['iat'] = time(); // 签发时间
|
||||
|
||||
|
||||
unset($payload['passwordMd5']);
|
||||
|
||||
$payload = self::base64UrlEncode(json_encode($payload, JSON_UNESCAPED_UNICODE));
|
||||
$signature = self::signature($header . '.' . $payload, self::$secret);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user