存客宝 - 设备列表

This commit is contained in:
柳清爽
2025-04-14 18:42:38 +08:00
parent 1944833456
commit e782077fba
4 changed files with 189 additions and 17 deletions

View File

@@ -5,6 +5,7 @@ use app\cunkebao\model\DeviceHandleLog;
use app\cunkebao\model\DeviceWechatLogin;
use think\Controller;
use app\cunkebao\model\Device as DeviceModel;
use app\cunkebao\model\DeviceUser as DeviceUserModel;
use think\Db;
use think\facade\Request;
@@ -64,7 +65,7 @@ class Device extends Controller
$count = DeviceModel::getDeviceCount($where);
} else {
// 非管理员需要查询关联表
$deviceIds = \app\common\model\DeviceUser::getUserDeviceIds(
$deviceIds = DeviceUserModel::getUserDeviceIds(
$userInfo['id'],
$userInfo['companyId']
);
@@ -133,7 +134,7 @@ class Device extends Controller
$order = Request::param('order', 'desc');
// 添加公司ID过滤条件
$where['d.tenantId'] = $userInfo['companyId'];
$where['d.companyId'] = $userInfo['companyId'];
// 根据用户管理员状态调整查询条件
if ($userInfo['isAdmin'] == 1) {
@@ -141,7 +142,7 @@ class Device extends Controller
$list = DeviceModel::getDeviceList($where, "{$sort} {$order}", $page, $limit);
} else {
// 非管理员需要查询关联表
$deviceIds = \app\common\model\DeviceUser::getUserDeviceIds(
$deviceIds = DeviceUserModel::getUserDeviceIds(
$userInfo['id'],
$userInfo['companyId']
);

View File

@@ -10,7 +10,7 @@ use think\Db;
class Device extends Model
{
// 设置表名
protected $table = 's2_device';
protected $name = 'device';
/**
* 获取设备总数
@@ -20,8 +20,8 @@ class Device extends Model
public static function getDeviceCount($where = [])
{
// 默认只统计未删除的设备
if (!isset($where['isDeleted']) && !isset($where['d.isDeleted'])) {
$where['isDeleted'] = 0;
if (!isset($where['deleteTime']) && !isset($where['d.deleteTime'])) {
$where['deleteTime'] = 0;
}
// 确定是否使用了表别名
@@ -52,14 +52,14 @@ class Device extends Model
public static function getDeviceList($where = [], $order = 'd.id desc', $page = 1, $limit = 10)
{
// 默认只查询未删除的设备
if (!isset($where['isDeleted'])) {
$where['d.isDeleted'] = 0;
if (!isset($where['deleteTime'])) {
$where['d.deleteTime'] = 0;
}
// 构建查询对象
$query = self::alias('d')
->field(['d.id', 'd.imei', 'd.memo', 'w.wechatId', 'd.alive', 'w.totalFriend'])
->leftJoin('tk_wechat_account w', 'd.imei = w.imei COLLATE utf8mb4_unicode_ci');
->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend'])
->leftJoin('device_wechat_login l', 'd.id = l.deviceId');
// 处理查询条件
foreach ($where as $key => $value) {
@@ -92,7 +92,7 @@ class Device extends Model
'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.taskConfig', 'd.lastUpdateTime',
'w.id as wechatId', 'w.thirtyDayMsgCount', 'w.totalFriend', 'd.extra'
])
->leftJoin('tk_wechat_account w', 'd.imei = w.imei')
->leftJoin('ck_wechat_account w', 'd.imei = w.imei')
->where('d.id', $id)
->where('d.isDeleted', 0)
->find();

View File

@@ -0,0 +1,175 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 设备用户关联模型
* 用于管理设备与操盘手(用户)的关联关系
*/
class DeviceUser extends Model
{
/**
* 数据表名
* @var string
*/
protected $name = 'device_user';
/**
* 获取指定用户的所有设备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 removeDeviceUserRelation($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');
}
}

View File

@@ -4,6 +4,7 @@ namespace app\superadmin\controller;
use app\superadmin\model\Company as companyModel;
use app\superadmin\model\Users;
use GuzzleHttp\Client;
use think\Controller;
use think\Db;
use think\facade\Config;
use think\facade\Request;
@@ -12,7 +13,7 @@ use think\facade\Session;
/**
* 公司控制器
*/
class Company extends Base
class Company extends Controller
{
/**
* 创建新项目
@@ -20,11 +21,6 @@ class Company extends Base
*/
public function create()
{
// 验证登录状态
if (!$this->checkLogin()) {
return json(['code' => 401, 'msg' => '请先登录']);
}
// 获取参数
$params = Request::only(['nickname', 'account', 'password', 'realName', 'memo']);