私域抄盘手 - 设备详情返工
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
@@ -7,49 +8,4 @@ class Attachment extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'attachments';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = 'datetime';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'create_at';
|
||||
protected $updateTime = 'update_at';
|
||||
protected $deleteTime = 'delete_at';
|
||||
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'dl_count' => 'integer',
|
||||
'size' => 'integer',
|
||||
'scene' => 'integer',
|
||||
'create_at' => 'datetime',
|
||||
'update_at' => 'datetime',
|
||||
'delete_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* 添加附件记录
|
||||
* @param array $data 附件数据
|
||||
* @return int|bool
|
||||
*/
|
||||
public static function addAttachment($data)
|
||||
{
|
||||
$attachment = new self();
|
||||
return $attachment->allowField(true)->save($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据hash_key获取附件
|
||||
* @param string $hashKey
|
||||
* @return array|null
|
||||
*/
|
||||
public static function getByHashKey($hashKey)
|
||||
{
|
||||
return self::where('hash_key', $hashKey)
|
||||
->where('delete_at', null)
|
||||
->find();
|
||||
}
|
||||
}
|
||||
14
Server/application/common/model/Device.php
Normal file
14
Server/application/common/model/Device.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 设备模型类
|
||||
*/
|
||||
class Device extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'device';
|
||||
}
|
||||
14
Server/application/common/model/DeviceTaskconf.php
Normal file
14
Server/application/common/model/DeviceTaskconf.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 设备任务配置模型类
|
||||
*/
|
||||
class DeviceTaskconf extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'device_taskconf';
|
||||
}
|
||||
@@ -13,7 +13,7 @@ class DeviceUser extends Model
|
||||
* 数据表名
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'tk_device_user';
|
||||
protected $name = 'device_user';
|
||||
|
||||
/**
|
||||
* 设置主键
|
||||
@@ -150,26 +150,7 @@ class DeviceUser extends Model
|
||||
])->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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 关联用户模型
|
||||
|
||||
14
Server/application/common/model/DeviceWechatLogin.php
Normal file
14
Server/application/common/model/DeviceWechatLogin.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 微信好友模型类
|
||||
*/
|
||||
class DeviceWechatLogin extends Model
|
||||
{
|
||||
// 登录日志最新登录 alive = 1,旧数据全部设置0
|
||||
protected $name = 'device_wechat_login';
|
||||
}
|
||||
14
Server/application/common/model/WechatAccount.php
Normal file
14
Server/application/common/model/WechatAccount.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 微信账号模型类
|
||||
*/
|
||||
class WechatAccount extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'wechat_account';
|
||||
}
|
||||
14
Server/application/common/model/WechatFriend.php
Normal file
14
Server/application/common/model/WechatFriend.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 微信好友模型类
|
||||
*/
|
||||
class WechatFriend extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'wechat_friend';
|
||||
}
|
||||
@@ -14,7 +14,7 @@ Route::group('v1/', function () {
|
||||
Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录
|
||||
Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表
|
||||
Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数
|
||||
Route::get(':id', 'app\\cunkebao\\controller\\Device@read'); // 获取设备详情
|
||||
Route::get(':id', 'app\\cunkebao\\controller\\device\\GetDeviceDetailV1Controller@index'); // 获取设备详情
|
||||
Route::post('', 'app\\cunkebao\\controller\\Device@save'); // 添加设备
|
||||
Route::put('refresh', 'app\\cunkebao\\controller\\Device@refresh'); // 刷新设备状态
|
||||
Route::delete(':id', 'app\\cunkebao\\controller\\Device@delete'); // 删除设备
|
||||
|
||||
45
Server/application/cunkebao/controller/BaseController.php
Normal file
45
Server/application/cunkebao/controller/BaseController.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,72 +40,7 @@ class Device extends Model
|
||||
return self::where($where)->count();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取设备详情
|
||||
* @param int $id 设备ID
|
||||
* @return array|null 设备信息
|
||||
*/
|
||||
public static function getDeviceInfo($id)
|
||||
{
|
||||
// 查询设备基础信息与关联的微信账号信息
|
||||
$device = self::alias('d')
|
||||
->field([
|
||||
'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.taskConfig', 'd.lastUpdateTime',
|
||||
'w.id as wechatId', 'w.thirtyDayMsgCount', 'w.totalFriend', 'd.extra'
|
||||
])
|
||||
->leftJoin('ck_wechat_account w', 'd.imei = w.imei')
|
||||
->where('d.id', $id)
|
||||
->where('d.isDeleted', 0)
|
||||
->find();
|
||||
|
||||
// 如果设备存在,处理额外信息
|
||||
if ($device) {
|
||||
// 解析电量信息
|
||||
$battery = 0;
|
||||
if (!empty($device['extra'])) {
|
||||
$extra = json_decode($device['extra'], true);
|
||||
if (is_array($extra) && isset($extra['battery'])) {
|
||||
$battery = intval($extra['battery']);
|
||||
}
|
||||
}
|
||||
$device['battery'] = $battery;
|
||||
|
||||
// 解析taskConfig字段获取功能开关
|
||||
$features = [
|
||||
'autoAddFriend' => false,
|
||||
'autoReply' => false,
|
||||
'contentSync' => false,
|
||||
'aiChat' => false
|
||||
];
|
||||
|
||||
if (!empty($device['taskConfig'])) {
|
||||
$taskConfig = json_decode($device['taskConfig'], true);
|
||||
if (is_array($taskConfig)) {
|
||||
// 映射taskConfig中的字段到前端需要的features
|
||||
$features['autoAddFriend'] = isset($taskConfig['autoAddFriend']) ? (bool)$taskConfig['autoAddFriend'] : false;
|
||||
$features['autoReply'] = isset($taskConfig['autoReply']) ? (bool)$taskConfig['autoReply'] : false;
|
||||
$features['contentSync'] = isset($taskConfig['momentsSync']) ? (bool)$taskConfig['momentsSync'] : false;
|
||||
$features['aiChat'] = isset($taskConfig['aiChat']) ? (bool)$taskConfig['aiChat'] : false;
|
||||
}
|
||||
}
|
||||
|
||||
$device['features'] = $features;
|
||||
unset($device['extra']);
|
||||
unset($device['taskConfig']);
|
||||
|
||||
// 格式化最后活跃时间
|
||||
$device['lastUpdateTime'] = !empty($device['lastUpdateTime']) ? date('Y-m-d H:i:s', strtotime($device['lastUpdateTime'])) : date('Y-m-d H:i:s');
|
||||
|
||||
// 确保totalFriend和thirtyDayMsgCount有值,防止NULL
|
||||
$device['totalFriend'] = intval($device['totalFriend'] ?? 0);
|
||||
$device['thirtyDayMsgCount'] = intval($device['thirtyDayMsgCount'] ?? 0);
|
||||
}
|
||||
|
||||
return $device;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加设备
|
||||
* @param array $data 设备数据
|
||||
|
||||
Reference in New Issue
Block a user