From 3912a4c489603df4a977d207f2168d2cae35cc56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Thu, 17 Apr 2025 10:15:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=93=8D=E7=9B=98=E6=89=8B=E7=AB=AF=20-=20?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=85=B3=E8=81=94=E8=B4=A6=E5=8F=B7=E8=BF=94?= =?UTF-8?q?=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/cunkebao/config/route.php | 2 +- .../cunkebao/controller/BaseController.php | 8 +- .../device/GetDeviceListV1Controller.php | 9 +- .../device/GetRelatedAccountsV1Controller.php | 173 ++++++++++++++++++ .../cunkebao/model/DeviceWechatLogin.php | 28 +-- 5 files changed, 183 insertions(+), 37 deletions(-) create mode 100644 Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 59ef114d..7444d1fd 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -10,7 +10,7 @@ Route::group('v1/', function () { // 设备管理相关 Route::group('devices', function () { - Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\Device@getRelatedAccounts'); // 设备关联微信账号路由 + Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\device\\GetRelatedAccountsV1Controller@index'); // 设备关联微信账号路由 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'); // 获取设备总数 diff --git a/Server/application/cunkebao/controller/BaseController.php b/Server/application/cunkebao/controller/BaseController.php index 59aa8f58..1189cafa 100644 --- a/Server/application/cunkebao/controller/BaseController.php +++ b/Server/application/cunkebao/controller/BaseController.php @@ -23,8 +23,6 @@ class BaseController extends Controller parent::initialize(); date_default_timezone_set('Asia/Shanghai'); - - $this->user = request()->userInfo; } /** @@ -36,10 +34,12 @@ class BaseController extends Controller */ protected function getUserInfo(string $column = '') { - if (!$this->user) { + $user = $this->request->userInfo; + + if (!$user) { throw new \Exception('未授权访问,缺少有效的身份凭证', 401); } - return $column ? $this->user[$column] : $this->user; + return $column ? $user[$column] : $user; } } \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index dd87b040..b3d47db7 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -5,7 +5,6 @@ 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; /** * 设备管理控制器 @@ -23,12 +22,12 @@ class GetDeviceListV1Controller extends BaseController $where = []; // 关键词搜索(同时搜索IMEI和备注) - if (!empty($keyword = Request::param('keyword'))) { + if (!empty($keyword = $this->request->param('keyword'))) { $where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"]; } // 设备在线状态 - if (is_numeric($alive = Request::param('alive'))) { + if (is_numeric($alive = $this->request->param('alive'))) { $where['d.alive'] = $alive; } @@ -116,8 +115,8 @@ class GetDeviceListV1Controller extends BaseController public function index() { try { - $page = (int)Request::param('page', 1); - $limit = (int)Request::param('limit', 10); + $page = (int)$this->request->param('page', 1); + $limit = (int)$this->request->param('limit', 10); if ($this->getUserInfo('isAdmin') == 1) { $where = $this->makeWhere(); diff --git a/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php b/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php new file mode 100644 index 00000000..2891db7e --- /dev/null +++ b/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php @@ -0,0 +1,173 @@ + $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ]; + + $hasPermission = DeviceUserModel::where($where)->count() > 0; + + if (!$hasPermission) { + throw new \Exception('您没有权限查看该设备', '403'); + } + } + + /** + * 查询设备关联的微信ID列表 + * + * @param int $deviceId 设备ID + * @return array 微信ID列表 + */ + protected function getDeviceWechatIds(int $deviceId): array + { + $where = [ + 'deviceId' => $deviceId, + 'companyId' => $this->getUserInfo('companyId') + ]; + + return DeviceWechatLoginModel::where($where)->group('wechatId')->column('wechatId'); + } + + /** + * 通过设备关联的微信号列表获取微信账号 + * + * @param array $wechatIds + * @return array + */ + protected function getWechatAccountsByIds(array $wechatIds): array + { + return (array)WechatAccountModel::alias('a') + ->field([ + 'a.wechatId', 'a.nickname', 'a.avatar', 'a.gender', 'a.createTime' + ]) + ->whereIn('a.wechatId', $wechatIds) + ->select() + ->toArray(); + } + + /** + * 通过微信id获取微信最后活跃时间 + * + * @param int $wechatId + * @return string + */ + protected function getWechatLastActiveTime($wechatId): string + { + return date('Y-m-d H:i:s', $wechatId ?: time()); + } + + /** + * 加友状态 + * + * @param string $wechatId + * @return string + */ + protected function getWechatStatusText(string $wechatId): string + { + return 1 ? '可加友' : '已停用'; + } + + /** + * 账号状态 + * + * @param string $wechatId + * @return string + */ + protected function getWechatAliveText(string $wechatId): string + { + return 1 ? '正常' : '异常'; + } + + /** + * 统计微信好友 + * + * @param string $wechatId + * @return int + */ + protected function countFriend(string $wechatId): int + { + return WechatFriend::where(['ownerWechatId' => $wechatId])->count(); + } + + /** + * 获取设备关联的微信账号信息 + * + * @param int $deviceId 设备ID + * @return array 微信账号信息列表 + */ + protected function getDeviceRelatedAccounts(int $deviceId) + { + // 获取设备关联的微信ID列表 + $wechatIds = $this->getDeviceWechatIds($deviceId); + + if (!empty($wechatIds)) { + $results = $this->getWechatAccountsByIds($wechatIds); + + foreach ($results as &$account) { + $account['lastActive'] = $this->getWechatLastActiveTime($account['createTime']); + $account['statusText'] = $this->getWechatStatusText($account['wechatId']); + $account['totalFriend'] = $this->countFriend($account['wechatId']); + $account['wechatAliveText'] = $this->getWechatAliveText($account['wechatId']); + } + + return $results; + } + + return []; + } + + /** + * 获取设备关联的微信账号 + * @return \think\response\Json + */ + public function index() + { + try { + $deviceId = $this->request->param('id/d'); + + if ($this->getUserInfo('isAdmin') != 1) { + $this->checkUserDevicePermission($deviceId); + } + + // 获取设备关联的微信账号 + $wechatAccounts = $this->getDeviceRelatedAccounts($deviceId); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'deviceId' => $deviceId, + 'accounts' => $wechatAccounts, + 'total' => count($wechatAccounts) + ] + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/DeviceWechatLogin.php b/Server/application/cunkebao/model/DeviceWechatLogin.php index 602edb35..206cbbd4 100644 --- a/Server/application/cunkebao/model/DeviceWechatLogin.php +++ b/Server/application/cunkebao/model/DeviceWechatLogin.php @@ -11,33 +11,7 @@ class DeviceWechatLogin extends Model // 设置表名 protected $name = 'device_wechat_login'; - /** - * 查询设备关联的微信ID列表 - * @param int $deviceId 设备ID - * @param int $companyId 公司/租户ID - * @return array 微信ID列表 - */ - public static function getDeviceWechatIds($deviceId, $companyId = null) - { - $query = self::where('deviceId', $deviceId); - - // 如果提供了公司ID,则添加对应的条件 - if ($companyId !== null) { - $query->where('companyId', $companyId); - } - - // 提取微信ID - $records = $query->select(); - $wechatIds = []; - - foreach ($records as $record) { - if (!empty($record['wechatId'])) { - $wechatIds[] = $record['wechatId']; - } - } - - return $wechatIds; - } + /** * 根据微信ID查询关联的设备