diff --git a/Cunkebao/api/devices.ts b/Cunkebao/api/devices.ts index eb240e23..64da66da 100644 --- a/Cunkebao/api/devices.ts +++ b/Cunkebao/api/devices.ts @@ -35,7 +35,7 @@ export const fetchDeviceDetail = async (id: string | number): Promise> => { - return api.get>(`/v1/devices/${id}/related-accounts`); + return api.get>(`/v1/wechats/related-device/${id}`); }; // 获取设备操作记录 diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 52306a13..84bc40ec 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -9,19 +9,19 @@ use think\facade\Route; Route::group('v1/', function () { // 设备管理相关 Route::group('devices', function () { - Route::get('add-results', 'app\cunkebao\controller\device\GetAddResultedV1Controller@index'); - Route::get(':id/related-accounts', 'app\cunkebao\controller\device\GetRelatedAccountsV1Controller@index'); - Route::get(':id/handle-logs', 'app\cunkebao\controller\device\GetDeviceHandleLogsV1Controller@index'); - Route::get('', 'app\cunkebao\controller\device\GetDeviceListV1Controller@index'); - Route::get(':id', 'app\cunkebao\controller\device\GetDeviceDetailV1Controller@index'); - Route::post('', 'app\cunkebao\controller\device\PostAddDeviceV1Controller@index'); Route::put('refresh', 'app\cunkebao\controller\device\RefreshDeviceDetailV1Controller@index'); - Route::delete(':id', 'app\cunkebao\controller\device\DeleteDeviceV1Controller@index'); + Route::get('add-results', 'app\cunkebao\controller\device\GetAddResultedV1Controller@index'); Route::post('task-config', 'app\cunkebao\controller\device\UpdateDeviceTaskConfigV1Controller@index'); + Route::get(':id/handle-logs', 'app\cunkebao\controller\device\GetDeviceHandleLogsV1Controller@index'); + Route::get(':id', 'app\cunkebao\controller\device\GetDeviceDetailV1Controller@index'); + Route::delete(':id', 'app\cunkebao\controller\device\DeleteDeviceV1Controller@index'); + Route::get('', 'app\cunkebao\controller\device\GetDeviceListV1Controller@index'); + Route::post('', 'app\cunkebao\controller\device\PostAddDeviceV1Controller@index'); }); // 设备微信相关 Route::group('wechats', function () { + Route::get('related-device/:id', 'app\cunkebao\controller\wechat\GetWechatsRelatedDeviceV1Controller@index'); Route::get('', 'app\cunkebao\controller\wechat\GetWechatsOnDevicesV1Controller@index'); Route::get(':id/summary', 'app\cunkebao\controller\wechat\GetWechatOnDeviceSummarizeV1Controller@index'); Route::get(':id/friends', 'app\cunkebao\controller\wechat\GetWechatOnDeviceFriendsV1Controller@index'); diff --git a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php index aa6d62b9..b9c92600 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php @@ -26,13 +26,14 @@ class GetDeviceDetailV1Controller extends BaseController */ protected function checkUserDevicePermission(int $deviceId): void { - $where = [ - 'deviceId' => $deviceId, - 'userId' => $this->getUserInfo('id'), - 'companyId' => $this->getUserInfo('companyId') - ]; - - $hasPermission = DeviceUserModel::where($where)->count() > 0; + $hasPermission = DeviceUserModel::where( + [ + 'deviceId' => $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ] + ) + ->count() > 0; if (!$hasPermission) { throw new \Exception('您没有权限查看该设备', 403); @@ -67,21 +68,19 @@ class GetDeviceDetailV1Controller extends BaseController */ protected function getTaskConfig(int $deviceId): array { - $companyId = $this->getUserInfo('companyId'); - - $conf = DeviceTaskconfModel::alias('c') - ->field([ - 'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat' - ]) - ->where(compact('companyId', 'deviceId')) + $conf = DeviceTaskconfModel::alias('c')->field([ + 'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat' + ]) + ->where( + [ + 'companyId' => $this->getUserInfo('companyId'), + 'deviceId' => $deviceId + ] + ) ->find(); - if (!is_null($conf)) { - return $conf->toArray(); - } - // 未配置时赋予默认关闭的状态 - return ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0); + return !is_null($conf) ? $conf->toArray() : ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0); } /** diff --git a/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php b/Server/application/cunkebao/controller/wechat/GetWechatsRelatedDeviceV1Controller.php similarity index 55% rename from Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php rename to Server/application/cunkebao/controller/wechat/GetWechatsRelatedDeviceV1Controller.php index e1d30b09..92d20ce6 100644 --- a/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php +++ b/Server/application/cunkebao/controller/wechat/GetWechatsRelatedDeviceV1Controller.php @@ -1,19 +1,20 @@ $deviceId, - 'userId' => $this->getUserInfo('id'), - 'companyId' => $this->getUserInfo('companyId') - ]; - - $hasPermission = DeviceUserModel::where($where)->count() > 0; + $hasPermission = DeviceUserModel::where( + [ + 'deviceId' => $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ] + ) + ->count() > 0; if (!$hasPermission) { throw new \Exception('您没有权限查看该设备', 403); @@ -39,34 +41,35 @@ class GetRelatedAccountsV1Controller extends BaseController /** * 查询设备关联的微信ID列表 * - * @param int $deviceId 设备ID - * @return array 微信ID列表 + * @param int $deviceId + * @return array */ protected function getDeviceWechatIds(int $deviceId): array { - $where = [ - 'deviceId' => $deviceId, - 'companyId' => $this->getUserInfo('companyId') - ]; - - return DeviceWechatLoginModel::where($where)->group('wechatId')->column('wechatId'); + return DeviceWechatLoginModel::where( + [ + 'deviceId' => $deviceId, + 'companyId' => $this->getUserInfo('companyId') + ] + ) + ->group('wechatId')->column('wechatId'); } /** * 通过设备关联的微信号列表获取微信账号 * * @param array $wechatIds - * @return array + * @return ResultCollection */ - protected function getWechatAccountsByIds(array $wechatIds): array + protected function getWechatAccountsByIds(array $wechatIds): ResultCollection { - return (array)WechatAccountModel::alias('a') + return WechatAccountModel::alias('w') ->field([ - 'a.wechatId', 'a.nickname', 'a.avatar', 'a.gender', 'a.createTime' + 'w.wechatId', 'w.nickname', 'w.avatar', 'w.gender', 'w.createTime', + 'CASE WHEN w.alias IS NULL OR w.alias = "" THEN w.wechatId ELSE w.alias END AS wechatAccount', ]) - ->whereIn('a.wechatId', $wechatIds) - ->select() - ->toArray(); + ->whereIn('w.wechatId', $wechatIds) + ->select(); } /** @@ -75,9 +78,9 @@ class GetRelatedAccountsV1Controller extends BaseController * @param int $time * @return string */ - protected function getWechatLastActiveTime(int $time): string + protected function getWechatLastActiveTime(string $wechatId): string { - return date('Y-m-d H:i:s', $time ?: time()); + return date('Y-m-d H:i:s', time()); } /** @@ -105,36 +108,42 @@ class GetRelatedAccountsV1Controller extends BaseController /** * 统计微信好友 * - * @param string $wechatId + * @param string $ownerWechatId * @return int */ - protected function countFriend(string $wechatId): int + protected function getCountFriend(string $ownerWechatId): int { - return WechatFriendShip::where(['ownerWechatId' => $wechatId])->count(); + return WechatFriendShipModel::where( + [ + 'ownerWechatId' => $ownerWechatId, + 'companyId' => $this->getUserInfo('companyId') + ] + ) + ->count(); } /** * 获取设备关联的微信账号信息 * - * @param int $deviceId 设备ID - * @return array 微信账号信息列表 + * @param int $deviceId + * @return array */ - protected function getDeviceRelatedAccounts(int $deviceId) + protected function getDeviceRelatedAccounts(int $deviceId): array { // 获取设备关联的微信ID列表 $wechatIds = $this->getDeviceWechatIds($deviceId); if (!empty($wechatIds)) { - $results = $this->getWechatAccountsByIds($wechatIds); + $collection = $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']); + foreach ($collection as $account) { + $account->lastActive = $this->getWechatLastActiveTime($account->wechatId); + $account->statusText = $this->getWechatStatusText($account->wechatId); + $account->totalFriend = $this->getCountFriend($account->wechatId); + $account->wechatAliveText = $this->getWechatAliveText($account->wechatId); } - return $results; + return $collection->toArray(); } return []; @@ -142,6 +151,7 @@ class GetRelatedAccountsV1Controller extends BaseController /** * 获取设备关联的微信账号 + * * @return \think\response\Json */ public function index() diff --git a/Server/config/app.php b/Server/config/app.php index 722ca45c..78410a0d 100644 --- a/Server/config/app.php +++ b/Server/config/app.php @@ -101,7 +101,7 @@ return [ // 合并路由规则 'route_rule_merge' => false, // 路由是否完全匹配 - 'route_complete_match' => false, + 'route_complete_match' => true, // 使用注解路由 'route_annotation' => false, // 域名根,如thinkphp.cn diff --git a/Server/thinkphp/tpl/think_exception.tpl b/Server/thinkphp/tpl/think_exception.tpl index bd2e2cc2..ff7c2870 100644 --- a/Server/thinkphp/tpl/think_exception.tpl +++ b/Server/thinkphp/tpl/think_exception.tpl @@ -81,6 +81,7 @@ 系统发生错误 +