工作台 - 通讯录导入功能
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user