From 4385995742cc364c07b1280ca3b6b501f7d6fb8b Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 10 Sep 2025 11:43:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E5=8F=B0=20-=20=E9=80=9A?= =?UTF-8?q?=E8=AE=AF=E5=BD=95=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/api/config/route.php | 2 + .../api/controller/DeviceController.php | 113 +++++++++++++++++- Server/application/common.php | 3 +- .../controller/WorkbenchController.php | 74 ++++++++++-- .../application/cunkebao/model/Workbench.php | 5 + .../cunkebao/model/WorkbenchImportContact.php | 25 ++++ 6 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 Server/application/cunkebao/model/WorkbenchImportContact.php diff --git a/Server/application/api/config/route.php b/Server/application/api/config/route.php index a71303e1..8bee16da 100644 --- a/Server/application/api/config/route.php +++ b/Server/application/api/config/route.php @@ -27,6 +27,8 @@ Route::group('v1', function () { Route::post('createGroup', 'app\api\controller\DeviceController@createGroup'); // 创建设备分组 √ Route::get('groupList', 'app\api\controller\DeviceController@getGroupList'); // 获取设备分组列表 √ Route::post('updateDeviceToGroup', 'app\api\controller\DeviceController@updateDeviceToGroup'); // 更新设备的分组 √ + + Route::post('importContact', 'app\api\controller\DeviceController@importContact'); // 更新设备联系人 √ }); // FriendTask控制器路由 diff --git a/Server/application/api/controller/DeviceController.php b/Server/application/api/controller/DeviceController.php index df072c2c..509a5ee7 100644 --- a/Server/application/api/controller/DeviceController.php +++ b/Server/application/api/controller/DeviceController.php @@ -321,6 +321,117 @@ class DeviceController extends BaseController } } + /** + * 更新设备联系人 + * @param int $id 设备ID + * @param int $groupId 分组ID + * @return \think\response\Json + */ + public function importContact($data = [],$isInner = false) + { + // 获取授权token + $authorization = $this->authorization; + if (empty($authorization)) { + if($isInner){ + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + }else{ + return errorJson('缺少授权信息'); + } + } + + try { + // 获取参数 + $deviceId = !empty($data['deviceId']) ? $data['deviceId'] : $this->request->param('deviceId', ''); + $rawContactJson = !empty($data['contactJson']) ? $data['contactJson'] : $this->request->param('contactJson', ''); + $clearContact = !empty($data['clearContact']) ? $data['clearContact'] : $this->request->param('clearContact', false); + + + if (empty($deviceId)) { + if($isInner){ + return json_encode(['code'=>500,'msg'=>'设备ID不能为空']); + }else{ + return errorJson('设备ID不能为空'); + } + } + + $contacts = []; + if (!empty($rawContactJson)) { + if (is_string($rawContactJson)) { + $decodedContacts = json_decode($rawContactJson, true); + if (json_last_error() === JSON_ERROR_NONE) { + // It's a valid JSON string + $contacts = $decodedContacts; + } else { + // It's not a JSON string, treat as multi-line text + $lines = explode("\n", str_replace("\r\n", "\n", $rawContactJson)); + foreach ($lines as $line) { + $line = trim($line); + if (empty($line)) continue; + $parts = explode(',', $line); + if (count($parts) == 2) { + $contacts[] = ['name' => trim($parts[0]), 'phone' => trim($parts[1])]; + } + } + } + } elseif (is_array($rawContactJson)) { + // It's already an array + $contacts = $rawContactJson; + } + } + + + if (empty($contacts)){ + if($isInner){ + return json_encode(['code'=>500,'msg'=>'更新设备联系人失败:通讯录不能为空' ]); + }else{ + return errorJson('更新设备联系人失败:通讯录不能为空' ); + } + } + + + // Trim whitespace from name and phone in all cases + if (!empty($contacts)) { + foreach ($contacts as &$contact) { + if (isset($contact['name'])) { + $contact['name'] = trim($contact['name']); + } + if (isset($contact['phone'])) { + $contact['phone'] = trim($contact['phone']); + } + } + unset($contact); // Unset reference to the last element + } + + $contactJsonForApi = json_encode($contacts); + + // 构建请求参数 + $params = [ + 'deviceId' => $deviceId, + 'contactJson' => $contactJsonForApi, + 'clearContact' => $clearContact + ]; + // 设置请求头 + $headerData = ['client:system']; + $header = setHeader($headerData, $authorization, 'json'); + + // 发送请求 + $result = requestCurl($this->baseUrl . 'api/device/importContact', $params, 'POST', $header,'json'); + $response = handleApiResponse($result); + + if(empty($response)){ + return successJson([], '操作成功'); + }else{ + return errorJson([],$response); + } + } catch (\Exception $e) { + if($isInner){ + return json_encode(['code'=>500,'msg'=>'更新设备联系人失败:' . $e->getMessage()]); + }else{ + return errorJson('更新设备联系人失败:' . $e->getMessage()); + } + } + } + /************************ 设备分组相关接口 ************************/ @@ -642,4 +753,4 @@ class DeviceController extends BaseController return $base64; } -} \ No newline at end of file +} \ No newline at end of file diff --git a/Server/application/common.php b/Server/application/common.php index 5b6987ae..3f0239ca 100644 --- a/Server/application/common.php +++ b/Server/application/common.php @@ -375,6 +375,7 @@ if (!function_exists('handleApiResponse')) { return $decoded; } + // 不是JSON格式,直接返回原始数据 if($response == '无效路径或登录状态失效'){ Cache::rm('system_refresh_token'); @@ -382,7 +383,7 @@ if (!function_exists('handleApiResponse')) { //AuthService::getSystemAuthorization(); } - return $decoded; + return $response; } } diff --git a/Server/application/cunkebao/controller/WorkbenchController.php b/Server/application/cunkebao/controller/WorkbenchController.php index 450e155b..e4220016 100644 --- a/Server/application/cunkebao/controller/WorkbenchController.php +++ b/Server/application/cunkebao/controller/WorkbenchController.php @@ -7,6 +7,7 @@ use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel; use app\common\model\WechatCustomer as WechatCustomerModel; use app\cunkebao\model\Workbench; use app\cunkebao\model\WorkbenchAutoLike; +use app\cunkebao\model\WorkbenchImportContact; use app\cunkebao\model\WorkbenchMomentsSync; use app\cunkebao\model\WorkbenchGroupPush; use app\cunkebao\model\WorkbenchGroupCreate; @@ -30,6 +31,7 @@ class WorkbenchController extends Controller const TYPE_GROUP_PUSH = 3; // 群消息推送 const TYPE_GROUP_CREATE = 4; // 自动建群 const TYPE_TRAFFIC_DISTRIBUTION = 5; // 流量分发 + const TYPE_IMPORT_CONTACT = 6; // 联系人导入 /** * 创建工作台 @@ -153,6 +155,19 @@ class WorkbenchController extends Controller $config->updateTime = time(); $config->save(); break; + case self::TYPE_IMPORT_CONTACT: //联系人导入 + $config = new WorkbenchImportContact; + $config->workbenchId = $workbench->id; + $config->devices = json_encode($param['deveiceGroups'], JSON_UNESCAPED_UNICODE); + $config->pools = json_encode($param['pools'], JSON_UNESCAPED_UNICODE); + $config->num = $param['num']; + $config->clearContact = $param['clearContact']; + $config->remark = $param['remark']; + $config->startTime = $param['startTime']; + $config->endTime = $param['endTime']; + $config->createTime = time(); + $config->save(); + break; } Db::commit(); @@ -206,6 +221,9 @@ class WorkbenchController extends Controller 'groupCreate' => function($query) { $query->field('workbenchId,devices,startTime,endTime,groupSizeMin,groupSizeMax,maxGroupsPerDay,groupNameTemplate,groupDescription,poolGroups,wechatGroups'); }, + 'importContact' => function($query) { + $query->field('workbenchId,devices,pools,num,remarkType,remark,clearContact,startTime,endTime'); + }, 'user' => function ($query) { $query->field('id,username'); } @@ -321,21 +339,15 @@ class WorkbenchController extends Controller } $totalUsers = $totalUsers->count(); - - $totalAccounts = count($item->config->account); - $dailyAverage = Db::name('workbench_traffic_config_item') ->where('workbenchId', $item->id) ->count(); $day = (time() - strtotime($item->createTime)) / 86400; $day = intval($day); - - if ($dailyAverage > 0 && $totalAccounts > 0 && $day > 0) { $dailyAverage = $dailyAverage / $totalAccounts / $day; } - $item->config->total = [ 'dailyAverage' => intval($dailyAverage), 'totalAccounts' => $totalAccounts, @@ -346,6 +358,15 @@ class WorkbenchController extends Controller } unset($item->trafficConfig, $item->traffic_config); break; + + case self::TYPE_IMPORT_CONTACT: + if (!empty($item->importContact)) { + $item->config = $item->importContact; + $item->config->devices = json_decode($item->config->devices, true); + $item->config->pools = json_decode($item->config->pools, true); + } + unset($item->importContact, $item->import_contact); + break; } // 添加创建人名称 $item['creatorName'] = $item->user ? $item->user->username : ''; @@ -396,7 +417,10 @@ class WorkbenchController extends Controller }, 'groupCreate' => function($query) { $query->field('workbenchId,devices,startTime,endTime,groupSizeMin,groupSizeMax,maxGroupsPerDay,groupNameTemplate,groupDescription,poolGroups,wechatGroups'); - } + }, + 'importContact' => function($query) { + $query->field('workbenchId,devices,pools,num,remarkType,remark,clearContact,startTime,endTime'); + }, ]; $workbench = Workbench::where([ ['id', '=', $id], @@ -531,6 +555,14 @@ class WorkbenchController extends Controller unset($workbench->trafficConfig, $workbench->traffic_config); } break; + case self::TYPE_IMPORT_CONTACT: + if (!empty($item->importContact)) { + $item->config = $item->importContact; + $item->config->devices = json_decode($item->config->devices, true); + $item->config->pools = json_decode($item->config->pools, true); + } + unset($item->importContact, $item->import_contact); + break; } unset( $workbench->autoLike, @@ -763,6 +795,19 @@ class WorkbenchController extends Controller $config->save(); } break; + case self::TYPE_IMPORT_CONTACT: //联系人导入 + $config = WorkbenchImportContact::where('workbenchId', $param['id'])->find();; + if ($config) { + $config->devices = json_encode($param['deveiceGroups']); + $config->pools = json_encode($param['pools']); + $config->num = $param['num']; + $config->clearContact = $param['clearContact']; + $config->remark = $param['remark']; + $config->startTime = $param['startTime']; + $config->endTime = $param['endTime']; + $config->save(); + } + break; } Db::commit(); @@ -945,6 +990,21 @@ class WorkbenchController extends Controller $newConfig->save(); } break; + case self::TYPE_IMPORT_CONTACT: //联系人导入 + $config = WorkbenchImportContact::where('workbenchId',$id)->find();; + if ($config) { + $newConfig = new WorkbenchImportContact; + $newConfig->devices = json_encode($config->deveiceGroups); + $newConfig->pools = json_encode($config->pools); + $newConfig->num = $config->num; + $newConfig->clearContact = $config->clearContact; + $newConfig->remark = $config->remark; + $newConfig->startTime = $config->startTime; + $newConfig->endTime = $config->endTime; + $newConfig->createTime = time(); + $newConfig->save(); + } + break; } Db::commit(); diff --git a/Server/application/cunkebao/model/Workbench.php b/Server/application/cunkebao/model/Workbench.php index fcd4bce6..85b967a5 100644 --- a/Server/application/cunkebao/model/Workbench.php +++ b/Server/application/cunkebao/model/Workbench.php @@ -62,6 +62,11 @@ class Workbench extends Model return $this->hasOne('WorkbenchTrafficConfig', 'workbenchId', 'id'); } + public function importContact() + { + return $this->hasOne('WorkbenchTrafficConfig', 'workbenchId', 'id'); + } + /** * 用户关联 diff --git a/Server/application/cunkebao/model/WorkbenchImportContact.php b/Server/application/cunkebao/model/WorkbenchImportContact.php new file mode 100644 index 00000000..58b73b63 --- /dev/null +++ b/Server/application/cunkebao/model/WorkbenchImportContact.php @@ -0,0 +1,25 @@ +belongsTo('Workbench', 'workbenchId', 'id'); + } +} \ No newline at end of file