diff --git a/Server/application/api/controller/DeviceController.php b/Server/application/api/controller/DeviceController.php index a10a9a1d..fa1d52d5 100644 --- a/Server/application/api/controller/DeviceController.php +++ b/Server/application/api/controller/DeviceController.php @@ -14,12 +14,16 @@ class DeviceController extends BaseController * 获取设备列表 * @return \think\response\Json */ - public function getlist($pageIndex = '',$pageSize = '',$authorization = '') + public function getlist($pageIndex = '',$pageSize = '',$authorization = '',$isJob = false) { // 获取授权token $authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', '')); if (empty($authorization)) { - return errorJson('缺少授权信息'); + if($isJob){ + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + }else{ + return errorJson('缺少授权信息'); + } } try { @@ -61,9 +65,17 @@ class DeviceController extends BaseController } } - return successJson($response); + if($isJob){ + return json_encode(['code'=>200,'msg'=>'success','data'=>$response]); + }else{ + return successJson($response); + } } catch (\Exception $e) { - return errorJson('获取设备列表失败:' . $e->getMessage()); + if($isJob){ + return json_encode(['code'=>500,'msg'=>'获取设备列表失败:' . $e->getMessage()]); + }else{ + return errorJson('获取设备列表失败:' . $e->getMessage()); + } } } diff --git a/Server/application/api/controller/WechatChatroomController.php b/Server/application/api/controller/WechatChatroomController.php index e78569a6..0e9059e3 100644 --- a/Server/application/api/controller/WechatChatroomController.php +++ b/Server/application/api/controller/WechatChatroomController.php @@ -13,13 +13,17 @@ class WechatChatroomController extends BaseController * 获取微信群聊列表 * @return \think\response\Json */ - public function getlist() + public function getlist($pageIndex = '',$pageSize = '',$authorization = '',$isJob = false) { // 获取授权token - $authorization = trim($this->request->header('authorization', '')); - if (empty($authorization)) { + $authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', '')); + if (empty($authorization)) { + if($isJob){ + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + }else{ return errorJson('缺少授权信息'); } + } try { // 构建请求参数 @@ -31,16 +35,16 @@ class WechatChatroomController extends BaseController 'groupId' => $this->request->param('groupId', ''), 'wechatChatroomId' => $this->request->param('wechatChatroomId', 0), 'memberKeyword' => $this->request->param('memberKeyword', ''), - 'pageIndex' => $this->request->param('pageIndex', 0), - 'pageSize' => $this->request->param('pageSize', 20) + 'pageIndex' => !empty($pageIndex) ? $pageIndex : input('pageIndex', 0), + 'pageSize' => !empty($pageSize) ? $pageSize : input('pageSize', 20) ]; // 设置请求头 $headerData = ['client:system']; - $header = setHeader($headerData, $authorization, 'plain'); + $header = setHeader($headerData, $authorization, 'json'); // 发送请求获取群聊列表 - $result = requestCurl($this->baseUrl . 'api/WechatChatroom/pagelist', $params, 'GET', $header); + $result = requestCurl($this->baseUrl . 'api/WechatChatroom/pagelist', $params, 'GET', $header,'json'); $response = handleApiResponse($result); // 保存数据到数据库 @@ -50,7 +54,11 @@ class WechatChatroomController extends BaseController } } - return successJson($response); + if($isJob){ + return json_encode(['code'=>200,'msg'=>'success','data'=>$response]); + }else{ + return successJson($response); + } } catch (\Exception $e) { return errorJson('获取微信群聊列表失败:' . $e->getMessage()); } @@ -63,6 +71,7 @@ class WechatChatroomController extends BaseController private function saveChatroom($item) { $data = [ + 'id' => $item['id'], 'wechatAccountId' => $item['wechatAccountId'], 'wechatAccountAlias' => $item['wechatAccountAlias'], 'wechatAccountWechatId' => $item['wechatAccountWechatId'], @@ -70,29 +79,27 @@ class WechatChatroomController extends BaseController 'wechatAccountNickname' => $item['wechatAccountNickname'], 'chatroomId' => $item['chatroomId'], 'hasMe' => $item['hasMe'], - 'chatroomOwnerNickname' => $item['chatroomOwnerNickname'], - 'chatroomOwnerAvatar' => $item['chatroomOwnerAvatar'], + 'chatroomOwnerNickname' => isset($item['chatroomOwnerNickname']) ? $item['chatroomOwnerNickname'] : '', + 'chatroomOwnerAvatar' => isset($item['chatroomOwnerAvatar']) ? $item['chatroomOwnerAvatar'] : '', 'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '', - 'nickname' => $item['nickname'], - 'pyInitial' => $item['pyInitial'], - 'quanPin' => $item['quanPin'], - 'chatroomAvatar' => $item['chatroomAvatar'], + 'nickname' => isset($item['nickname']) ? $item['nickname'] : '', + 'pyInitial' => isset($item['pyInitial']) ? $item['pyInitial'] : '', + 'quanPin' => isset($item['quanPin']) ? $item['quanPin'] : '', + 'chatroomAvatar' => isset($item['chatroomAvatar']) ? $item['chatroomAvatar'] : '', 'members' => is_array($item['members']) ? json_encode($item['members']) : json_encode([]), - 'isDeleted' => $item['isDeleted'], - 'deleteTime' => $item['deleteTime'], - 'createTime' => $item['createTime'], - 'accountId' => $item['accountId'], - 'accountUserName' => $item['accountUserName'], - 'accountRealName' => $item['accountRealName'], - 'accountNickname' => $item['accountNickname'], - 'groupId' => $item['groupId'] + 'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : 0, + 'deleteTime' => isset($item['deleteTime']) ? $item['deleteTime'] : 0, + 'createTime' => isset($item['createTime']) ? $item['createTime'] : time(), + 'accountId' => isset($item['accountId']) ? $item['accountId'] : 0, + 'accountUserName' => isset($item['accountUserName']) ? $item['accountUserName'] : '', + 'accountRealName' => isset($item['accountRealName']) ? $item['accountRealName'] : '', + 'accountNickname' => isset($item['accountNickname']) ? $item['accountNickname'] : '', + 'groupId' => isset($item['groupId']) ? $item['groupId'] : 0, + 'updateTime' => time() ]; // 使用chatroomId和wechatAccountId的组合作为唯一性判断 - $chatroom = WechatChatroomModel::where([ - ['chatroomId', '=', $item['chatroomId']], - ['wechatAccountId', '=', $item['wechatAccountId']] - ])->find(); + $chatroom = WechatChatroomModel::where('id',$item['id'])->find(); if ($chatroom) { $chatroom->save($data); @@ -161,12 +168,13 @@ class WechatChatroomController extends BaseController { $data = [ 'chatroomId' => $wechatChatroomId, - 'wechatId' => $item['wechatId'], - 'nickname' => $item['nickname'], - 'avatar' => $item['avatar'], + 'wechatId' => isset($item['wechatId']) ? $item['wechatId'] : '', + 'nickname' => isset($item['nickname']) ? $item['nickname'] : '', + 'avatar' => isset($item['avatar']) ? $item['avatar'] : '', 'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '', 'alias' => isset($item['alias']) ? $item['alias'] : '', - 'friendType' => isset($item['friendType']) ? $item['friendType'] : false + 'friendType' => isset($item['friendType']) ? $item['friendType'] : false, + 'updateTime' => time() ]; // 使用chatroomId和wechatId的组合作为唯一性判断 @@ -178,6 +186,7 @@ class WechatChatroomController extends BaseController if ($member) { $member->save($data); } else { + $data['createTime'] = time(); WechatChatroomMemberModel::create($data); } } diff --git a/Server/application/api/controller/WechatFriendController.php b/Server/application/api/controller/WechatFriendController.php index 4e53df3f..cbdcd360 100644 --- a/Server/application/api/controller/WechatFriendController.php +++ b/Server/application/api/controller/WechatFriendController.php @@ -4,6 +4,7 @@ namespace app\api\controller; use app\api\model\WechatFriendModel; use think\facade\Request; +use think\facade\Log; class WechatFriendController extends BaseController { @@ -11,13 +12,17 @@ class WechatFriendController extends BaseController * 获取微信好友列表数据 * @return \think\response\Json */ - public function getlist() + public function getlist($pageIndex = '',$pageSize = '',$preFriendId = '',$authorization = '',$isJob = false) { - // 获取授权token - $authorization = trim($this->request->header('authorization', '')); - if (empty($authorization)) { + // 获取授权token + $authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', '')); + if (empty($authorization)) { + if($isJob){ + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + }else{ return errorJson('缺少授权信息'); } + } try { // 构建请求参数 @@ -34,12 +39,11 @@ class WechatFriendController extends BaseController 'isPass' => null, 'keyword' => input('keyword', ''), 'labels' => '[]', - 'pageIndex' => input('pageIndex', 0), - 'pageSize' => input('pageSize', 20), - 'preFriendId' => input('preFriendId', ''), + 'pageIndex' => !empty($pageIndex) ? $pageIndex : input('pageIndex', 0), + 'pageSize' => !empty($pageSize) ? $pageSize : input('pageSize', 20), + 'preFriendId' => !empty($preFriendId) ? $preFriendId : input('preFriendId', ''), 'wechatAccountKeyword' => input('wechatAccountKeyword', '') ]; - // 设置请求头 $headerData = ['client:system']; $header = setHeader($headerData, $authorization); @@ -55,9 +59,19 @@ class WechatFriendController extends BaseController } } - return successJson($response); + if($isJob){ + return json_encode(['code'=>200,'msg'=>'success','data'=>$response]); + }else{ + return successJson($response); + } + + } catch (\Exception $e) { - return errorJson('获取微信好友列表失败:' . $e->getMessage()); + if($isJob){ + return json_encode(['code'=>500,'msg'=>'获取微信好友列表失败:' . $e->getMessage()]); + }else{ + return errorJson('获取微信好友列表失败:' . $e->getMessage()); + } } } @@ -104,6 +118,7 @@ class WechatFriendController extends BaseController 'province' => isset($item['province']) ? $item['province'] : '', 'city' => isset($item['city']) ? $item['city'] : '', 'createTime' =>isset($item['createTime']) ? $item['createTime'] : '', + 'updateTime' => time() ]; // 使用三个字段的组合作为唯一性判断 diff --git a/Server/application/command/WechatFriendCommand.php b/Server/application/command/WechatFriendCommand.php index f3381056..c4b8cbdd 100644 --- a/Server/application/command/WechatFriendCommand.php +++ b/Server/application/command/WechatFriendCommand.php @@ -24,7 +24,7 @@ class WechatFriendCommand extends Command try { // 初始页码 $pageIndex = 0; - $pageSize = 100; // 每页获取100条记录 + $pageSize = 1000; // 每页获取1000条记录 // 将第一页任务添加到队列 $this->addToQueue($pageIndex, $pageSize); diff --git a/Server/application/job/DeviceListJob.php b/Server/application/job/DeviceListJob.php index 9e3ec969..9e383b8a 100644 --- a/Server/application/job/DeviceListJob.php +++ b/Server/application/job/DeviceListJob.php @@ -81,7 +81,7 @@ class DeviceListJob } // 调用设备列表获取方法 - $result = $deviceController->getlist($pageIndex,$pageSize,$authorization); + $result = $deviceController->getlist($pageIndex,$pageSize,$authorization,true); $response = json_decode($result,true); diff --git a/Server/application/job/WechatChatroomJob.php b/Server/application/job/WechatChatroomJob.php index 99563f05..ec20bf8a 100644 --- a/Server/application/job/WechatChatroomJob.php +++ b/Server/application/job/WechatChatroomJob.php @@ -81,9 +81,9 @@ class WechatChatroomJob } // 调用设备列表获取方法 - $result = $wechatChatroomController->getlist($pageIndex,$pageSize,$authorization); + $result = $wechatChatroomController->getlist($pageIndex,$pageSize,$authorization,true); + $response = json_decode($result,true); - // 判断是否成功 if ($response['code'] == 200) { diff --git a/Server/application/job/WechatFriendJob.php b/Server/application/job/WechatFriendJob.php index 769c7a06..a427f862 100644 --- a/Server/application/job/WechatFriendJob.php +++ b/Server/application/job/WechatFriendJob.php @@ -56,7 +56,8 @@ class WechatFriendJob { // 获取参数 $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0; - $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100; + $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 1000; + $preFriendId = isset($data['preFriendId']) ? $data['preFriendId'] : ''; Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize); @@ -66,7 +67,8 @@ class WechatFriendJob // 构建请求参数 $params = [ 'pageIndex' => $pageIndex, - 'pageSize' => $pageSize + 'pageSize' => $pageSize, + 'preFriendId' => $preFriendId ]; // 设置请求信息 @@ -81,7 +83,7 @@ class WechatFriendJob } // 调用设备列表获取方法 - $result = $wechatFriendController->getlist($pageIndex,$pageSize,$authorization); + $result = $wechatFriendController->getlist($pageIndex,$pageSize,$preFriendId,$authorization,true); $response = json_decode($result,true); @@ -90,10 +92,10 @@ class WechatFriendJob $data = $response['data']; // 判断是否有下一页 - if (!empty($data) && count($data['results']) > 0) { + if (!empty($data) && count($data) > 0) { // 有下一页,将下一页任务添加到队列 $nextPageIndex = $pageIndex + 1; - $this->addNextPageToQueue($nextPageIndex, $pageSize); + $this->addNextPageToQueue($nextPageIndex, $pageSize,$data[count($data)-1]['id']); Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); } @@ -110,11 +112,12 @@ class WechatFriendJob * @param int $pageIndex 页码 * @param int $pageSize 每页大小 */ - protected function addNextPageToQueue($pageIndex, $pageSize) + protected function addNextPageToQueue($pageIndex, $pageSize,$preFriendId) { $data = [ 'pageIndex' => $pageIndex, - 'pageSize' => $pageSize + 'pageSize' => $pageSize, + 'preFriendId' => $preFriendId ]; // 添加到队列,设置任务名为 wechat_friends diff --git a/Server/application/store/config/route.php b/Server/application/store/config/route.php index 57212848..4e0b1b94 100644 --- a/Server/application/store/config/route.php +++ b/Server/application/store/config/route.php @@ -23,4 +23,11 @@ Route::group('v1/store', function () { Route::group('customers', function () { Route::get('list', 'app\\store\\controller\\CustomerController@getList'); // 获取客户列表 }); + + + // 系统配置相关路由 + Route::group('system-config', function () { + Route::get('switch-status', 'app\\store\\controller\\SystemConfigController@getSwitchStatus'); // 获取系统开关状态 + Route::post('update-switch-status', 'app\\store\\controller\\SystemConfigController@updateSwitchStatus'); // 更新系统开关状态 + }); })->middleware(['jwt']); \ No newline at end of file diff --git a/Server/application/store/controller/BaseController.php b/Server/application/store/controller/BaseController.php new file mode 100644 index 00000000..b3e77a7e --- /dev/null +++ b/Server/application/store/controller/BaseController.php @@ -0,0 +1,66 @@ +userInfo = request()->userInfo; + + // 生成缓存key + $cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId']; + + // 尝试从缓存获取设备信息 + $device = Cache::get($cacheKey); + $device = ''; + // 如果缓存不存在,则从数据库获取 + if (!$device) { + $device = Db::name('device_user') + ->alias('du') + ->join('device d', 'd.id = du.deviceId','left') + ->where([ + 'du.userId' => $this->userInfo['id'], + 'du.companyId' => $this->userInfo['companyId'] + ]) + ->field('d.*') + ->find(); + + // 将设备信息存入缓存 + if ($device) { + Cache::set($cacheKey, $device, $this->cacheExpire); + } + } + + $this->device = $device; + } + + /** + * 清除设备信息缓存 + */ + protected function clearDeviceCache() + { + $cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId']; + Cache::delete($cacheKey); + } +} \ No newline at end of file diff --git a/Server/application/store/controller/SystemConfigController.php b/Server/application/store/controller/SystemConfigController.php new file mode 100644 index 00000000..cb56b582 --- /dev/null +++ b/Server/application/store/controller/SystemConfigController.php @@ -0,0 +1,107 @@ +device['id'] ?? 0; + if (!$deviceId) { + return $this->error('设备不存在'); + } + + // 获取已解析的配置 + $config = $this->device['taskConfig'] ?? []; + + // 返回开关状态 + return $this->success('获取成功', [ + 'autoLike' => $config['autoLike'] ?? false, + 'momentsSync' => $config['momentsSync'] ?? false, + 'autoCustomerDev' => $config['autoCustomerDev'] ?? false, + 'groupMessageDeliver' => $config['groupMessageDeliver'] ?? false, + 'autoGroup' => $config['autoGroup'] ?? false + ]); + + } catch (\Exception $e) { + Log::error('获取开关状态异常:' . $e->getMessage()); + return $this->error('获取开关状态失败'); + } + } + + /** + * 更新系统开关状态 + * + * @return \think\Response + */ + public function updateSwitchStatus() + { + try { + // 获取参数 + if (empty($this->device)) { + return errorJson('设备不存在'); + } + + $switchName = $this->request->param('switchName'); + $deviceId = $this->device['id']; + + if (empty($switchName)) { + return errorJson('开关名称不能为空'); + } + + // 验证开关名称是否有效 + $validSwitches = ['autoLike', 'momentsSync', 'autoCustomerDev', 'groupMessageDeliver', 'autoGroup']; + if (!in_array($switchName, $validSwitches)) { + return errorJson('无效的开关名称'); + } + + // 获取当前配置并确保是数组 + $taskConfig = json_decode($this->device['taskConfig'], true); + + // 更新指定开关状态 + $taskConfig[$switchName] = !$taskConfig[$switchName]; + $taskConfig = json_encode($taskConfig); + + + // 更新数据库 + $result = Db::name('device') + ->where('id', $deviceId) + ->update([ + 'taskConfig' => $taskConfig, + 'updateTime' => time() + ]); + + + if ($result === false) { + Log::error("更新设备{$switchName}开关状态失败,设备ID:{$deviceId}"); + return errorJson('更新失败'); + } + + // 清除缓存 + // $this->clearDeviceCache(); + + return successJson([], '更新成功'); + + } catch (\Exception $e) { + return errorJson('系统错误'. $e->getMessage()); + } + } +} \ No newline at end of file