私域抄盘手 - 设备详情返工

This commit is contained in:
柳清爽
2025-04-16 16:10:37 +08:00
parent 2891a40dc4
commit bd3f17baed
12 changed files with 309 additions and 163 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace app\cunkebao\controller;
use think\Controller;
/**
* 设备管理控制器
*/
class BaseController extends Controller
{
/**
* 用户信息
* @var object
*/
protected $user;
/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
date_default_timezone_set('Asia/Shanghai');
$this->user = request()->userInfo;;
}
/**
* 获取用户信息
*
* @param string $column
* @return mixed
* @throws \Exception
*/
protected function getUserInfo(string $column = '')
{
if (!$this->user) {
throw new \Exception('未授权访问,缺少有效的身份凭证', 401);
}
return $column ? $this->user[$column] : $this->user;
}
}

View File

@@ -0,0 +1,176 @@
<?php
namespace app\cunkebao\controller\device;
use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceTaskconf as DeviceTaskconfModel;
use app\common\model\DeviceWechatLogin;
use app\common\model\WechatFriend;
use app\cunkebao\controller\BaseController;
use think\facade\Request;
/**
* 设备管理控制器
*/
class GetDeviceDetailV1Controller extends BaseController
{
/**
* 检查用户是否有权限操作指定设备
* @param int $userId 用户ID
* @return void
*/
protected function checkUserDevicePermission(int $deviceId): void
{
$where = [
'deviceId' => $deviceId,
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId')
];
$hasPermission = DeviceUserModel::where($where)->count() > 0;
if (!$hasPermission) {
throw new \Exception('您没有权限查看该设备', '403');
}
}
/**
* 解析设备额外信息
*
* @param string $extra
* @return int
*/
protected function parseExtraForBattery(string $extra): int
{
if (!empty($extra)) {
$extra = json_decode($extra, true);
if (is_array($extra) && isset($extra['battery'])) {
return intval($extra['battery']);
}
}
return 0;
}
/**
* 解析taskConfig字段获取功能开关
*
* @param int $deviceId
* @return int[]
* @throws \Exception
*/
protected function getTaskConfig(int $deviceId): array
{
$where = [
'deviceId' => $deviceId,
'companyId' => $this->getUserInfo('companyId'),
'deleteTime' => 0
];
$conf = DeviceTaskconfModel::where($where)->field('autoAddFriend,autoReply,contentSync,aiChat')->find();
return $conf ? $conf->toArray() : [
'autoAddFriend' => 0,
'autoReply' => 0,
'contentSync' => 0,
'aiChat' => 0
];
}
/**
* 统计设备登录微信的好友
*
* @param int $deviceId
* @return int
* @throws \Exception
*/
protected function getTotalFriend(int $deviceId): int
{
$where = [
'deviceId' => $deviceId,
'companyId' => $this->getUserInfo('companyId'),
];
$ownerWechatId = DeviceWechatLogin::where($where)->order('createTime desc')->value('wechatId');
if ($ownerWechatId) {
return WechatFriend::where(['ownerWechatId' => $ownerWechatId])->count();
}
return 0;
}
/**
* 获取设备绑定微信你的消息总数
*
* @return int
*/
protected function getThirtyDayMsgCount(): int
{
return 0;
}
/**
* 获取设备详情
* @param int $id 设备ID
* @return array|null 设备信息
*/
protected function getDeviceInfo(int $id)
{
// 查询设备基础信息与关联的微信账号信息
$device = DeviceModel::alias('d')
->field([
'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.updateTime as lastUpdateTime', 'd.extra'
])
->where('d.deleteTime', 0)
->find($id);
if (empty($device)) {
throw new \Exception('设备不存在', '404');
}
$device['battery'] = $this->parseExtraForBattery($device['extra']);
$device['features'] = $this->getTaskConfig($id);
$device['totalFriend'] = $this->getTotalFriend($id);
$device['thirtyDayMsgCount'] = $this->getThirtyDayMsgCount();
// 设备最后活跃时间为设备状态更新时间
$device['lastUpdateTime'] = date('Y-m-d H:i:s', $device['lastUpdateTime']);
// 删除冗余字段
unset($device['extra']);
return $device;
}
/**
* 获取设备详情
* @return \think\response\Json
*/
public function index()
{
try {
// 获取设备ID
$id = Request::param('id/d');
if ($this->getUserInfo('isAdmin') != 1) {
$this->checkUserDevicePermission($id);
}
$info = $this->getDeviceInfo($id);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $info
]);
} catch (\Exception $e) {
return json([
'code' => $e->getMessage(),
'msg' => $e->getMessage()
]);
}
}
}

View File

@@ -1,35 +1,17 @@
<?php
namespace app\cunkebao\controller\device;
use app\cunkebao\model\Device as DeviceModel;
use app\cunkebao\model\DeviceUser as DeviceUserModel;
use app\cunkebao\model\WechatFriend;
use think\Controller;
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\WechatFriend;
use app\cunkebao\controller\BaseController;
use think\facade\Request;
/**
* 设备管理控制器
*/
class GetDeviceListV1Controller extends Controller
class GetDeviceListV1Controller extends BaseController
{
/**
* 用户信息
* @var object
*/
protected $user;
/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
date_default_timezone_set('Asia/Shanghai');
$this->user = request()->userInfo;;
}
/**
* 构建查询条件
*
@@ -50,7 +32,7 @@ class GetDeviceListV1Controller extends Controller
$where['d.alive'] = $alive;
}
$where['d.companyId'] = $this->user['companyId'];
$where['d.companyId'] = $this->getUserInfo('companyId');
$where['d.deleteTime'] = 0;
return array_merge($params, $where);
@@ -64,8 +46,8 @@ class GetDeviceListV1Controller extends Controller
protected function makeDeviceIdsWhere(): array
{
$deviceIds = DeviceUserModel::where(
$this->user['id'],
$this->user['companyId']
$this->getUserInfo('id'),
$this->getUserInfo('companyId')
)
->column('deviceId');
@@ -85,11 +67,12 @@ class GetDeviceListV1Controller extends Controller
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
protected function getDeviceList($where, $page = 1, $limit = 10)
protected function getDeviceList(array $where, int $page = 1, int $limit = 10)
{
$query = DeviceModel::alias('d')
->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend'])
->leftJoin('device_wechat_login l', 'd.id = l.deviceId');
->leftJoin('device_wechat_login l', 'd.id = l.deviceId')
->order('d.alive desc');
foreach ($where as $key => $value) {
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
@@ -136,7 +119,7 @@ class GetDeviceListV1Controller extends Controller
$page = (int)Request::param('page', 1);
$limit = (int)Request::param('limit', 10);
if ($this->user['isAdmin'] == 1) {
if ($this->getUserInfo('isAdmin') == 1) {
$where = $this->makeWhere();
$result = $this->getDeviceList($where, $page, $limit);
} else {
@@ -148,14 +131,14 @@ class GetDeviceListV1Controller extends Controller
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $this->countFriend($result),
'list' => $this->countFriend($result),
'total' => $result->total(),
]
]);
} catch (\Exception $e) {
return json([
'code' => $e->getCode(),
'msg' => $e->getMessage()
'msg' => $e->getMessage()
]);
}
}