Files
cunkebao_v3/Server/application/api/controller/FriendTaskController.php

163 lines
5.7 KiB
PHP
Raw Normal View History

2025-03-18 14:56:14 +08:00
<?php
namespace app\api\controller;
2025-03-24 16:42:36 +08:00
use app\api\model\FriendTaskModel;
2025-03-18 14:56:14 +08:00
use think\facade\Request;
class FriendTaskController extends BaseController
{
2025-04-12 15:08:21 +08:00
/************************ 好友任务管理相关接口 ************************/
2025-03-18 14:56:14 +08:00
/**
* 获取添加好友记录列表
2025-04-12 15:08:21 +08:00
* @param int $pageIndex 页码
* @param int $pageSize 每页数量
2025-04-30 11:15:19 +08:00
* @param bool $isInner 是否为定时任务调用
2025-03-18 14:56:14 +08:00
* @return \think\response\Json
*/
2025-04-30 11:15:19 +08:00
public function getlist($pageIndex, $pageSize, $isInner = false)
2025-03-18 14:56:14 +08:00
{
// 获取授权token
2025-04-09 14:45:27 +08:00
$authorization = trim($this->request->header('authorization', $this->authorization));
2025-03-18 14:56:14 +08:00
if (empty($authorization)) {
2025-04-30 11:15:19 +08:00
if($isInner){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
}
2025-03-18 14:56:14 +08:00
}
try {
// 构建请求参数
$params = [
'keyword' => $this->request->param('keyword', ''),
'status' => $this->request->param('status', ''),
'pageIndex' => !empty($pageIndex) ? $pageIndex : $this->request->param('pageIndex', 0),
'pageSize' => !empty($pageSize) ? $pageSize : $this->request->param('pageSize', 20),
2025-03-18 14:56:14 +08:00
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求获取添加好友记录列表
$result = requestCurl($this->baseUrl . 'api/AddFriendByPhoneTask/list', $params, 'GET', $header,'json');
$response = handleApiResponse($result);
// 保存数据到数据库
if (!empty($response['results'])) {
foreach ($response['results'] as $item) {
$this->saveFriendTask($item);
}
}
2025-04-30 11:15:19 +08:00
if($isInner){
return json_encode(['code'=>200,'msg'=>'获取添加好友记录列表成功','data'=>$response]);
}else{
return successJson($response);
}
2025-03-18 14:56:14 +08:00
} catch (\Exception $e) {
2025-04-30 11:15:19 +08:00
if($isInner){
return json_encode(['code'=>500,'msg'=>'获取添加好友记录列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取添加好友记录列表失败:' . $e->getMessage());
}
2025-03-18 14:56:14 +08:00
}
}
2025-03-24 14:59:19 +08:00
/**
* 添加好友任务
* @return \think\response\Json
*/
public function addFriendTask()
{
// 获取授权token
2025-04-09 14:45:27 +08:00
$authorization = trim($this->request->header('authorization', $this->authorization));
2025-03-24 14:59:19 +08:00
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取请求参数
$phone = $this->request->param('phone', '');
$message = $this->request->param('message', '');
$remark = $this->request->param('remark', '');
$labels = $this->request->param('labels', []);
$wechatAccountId = $this->request->param('wechatAccountId', 0);
// 参数验证
if (empty($phone)) {
return errorJson('手机号不能为空');
}
if (empty($wechatAccountId)) {
return errorJson('微信号不能为空');
}
// 构建请求参数
$params = [
'phone' => $phone,
'message' => $message,
'remark' => $remark,
'labels' => is_array($labels) ? $labels : [$labels],
'wechatAccountId' => (int)$wechatAccountId
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求添加好友任务
$result = requestCurl($this->baseUrl . 'api/AddFriendByPhoneTask/add', $params, 'POST', $header, 'json');
// 处理响应
return successJson([], '添加好友任务创建成功');
} catch (\Exception $e) {
return errorJson('添加好友任务失败:' . $e->getMessage());
}
}
2025-04-12 15:08:21 +08:00
/************************ 私有辅助方法 ************************/
2025-03-18 14:56:14 +08:00
/**
* 保存添加好友记录到数据库
* @param array $item 添加好友记录数据
*/
private function saveFriendTask($item)
{
// 将日期时间字符串转换为时间戳
$createTime = isset($item['createTime']) ? strtotime($item['createTime']) : null;
$data = [
2025-03-29 17:01:56 +08:00
'id' => $item['id'],
2025-03-18 14:56:14 +08:00
'tenantId' => $item['tenantId'],
'operatorAccountId' => $item['operatorAccountId'],
'status' => $item['status'],
'phone' => $item['phone'],
'msgContent' => $item['msgContent'],
'wechatAccountId' => $item['wechatAccountId'],
'createTime' => $createTime,
'remark' => $item['remark'],
'extra' => $item['extra'],
'labels' => $item['labels'],
'from' => $item['from'],
'alias' => $item['alias'],
'wechatId' => $item['wechatId'],
'wechatAvatar' => $item['wechatAvatar'],
'wechatNickname' => $item['wechatNickname'],
'accountNickname' => $item['accountNickname'],
'accountRealName' => $item['accountRealName'],
'accountUsername' => $item['accountUsername']
];
// 使用taskId作为唯一性判断
2025-03-29 17:01:56 +08:00
$task = FriendTaskModel::where('id', $item['id'])->find();
2025-03-18 14:56:14 +08:00
if ($task) {
$task->save($data);
} else {
FriendTaskModel::create($data);
}
}
}