工作台 - 通讯录导入功能
This commit is contained in:
@@ -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控制器路由
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/************************ 设备分组相关接口 ************************/
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -62,6 +62,11 @@ class Workbench extends Model
|
||||
return $this->hasOne('WorkbenchTrafficConfig', 'workbenchId', 'id');
|
||||
}
|
||||
|
||||
public function importContact()
|
||||
{
|
||||
return $this->hasOne('WorkbenchTrafficConfig', 'workbenchId', 'id');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 用户关联
|
||||
|
||||
25
Server/application/cunkebao/model/WorkbenchImportContact.php
Normal file
25
Server/application/cunkebao/model/WorkbenchImportContact.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 自动点赞工作台模型
|
||||
*/
|
||||
class WorkbenchImportContact extends Model
|
||||
{
|
||||
protected $pk = 'id';
|
||||
protected $name = 'workbench_import_contact';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $createTime = 'createTime';
|
||||
protected $updateTime = 'updateTime';
|
||||
|
||||
// 定义关联的工作台
|
||||
public function workbench()
|
||||
{
|
||||
return $this->belongsTo('Workbench', 'workbenchId', 'id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user