diff --git a/Server/application/cunkebao/controller/Device.php b/Server/application/cunkebao/controller/Device.php index 98a3992e..5d7231fe 100644 --- a/Server/application/cunkebao/controller/Device.php +++ b/Server/application/cunkebao/controller/Device.php @@ -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'] ); diff --git a/Server/application/cunkebao/model/Device.php b/Server/application/cunkebao/model/Device.php index 71d0e987..a1e3402f 100644 --- a/Server/application/cunkebao/model/Device.php +++ b/Server/application/cunkebao/model/Device.php @@ -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(); diff --git a/Server/application/cunkebao/model/DeviceUser.php b/Server/application/cunkebao/model/DeviceUser.php new file mode 100644 index 00000000..411f9759 --- /dev/null +++ b/Server/application/cunkebao/model/DeviceUser.php @@ -0,0 +1,175 @@ + $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'); + } +} \ No newline at end of file diff --git a/Server/application/superadmin/controller/Company.php b/Server/application/superadmin/controller/Company.php index 7144645b..d28130d4 100644 --- a/Server/application/superadmin/controller/Company.php +++ b/Server/application/superadmin/controller/Company.php @@ -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']);