Files
cunkebao_v3/Server/application/chukebao/controller/WechatFriendController.php

297 lines
11 KiB
PHP
Raw Normal View History

2025-09-13 17:20:34 +08:00
<?php
namespace app\chukebao\controller;
2025-09-26 15:37:15 +08:00
use app\chukebao\model\FriendSettings;
2025-09-13 17:20:34 +08:00
use library\ResponseHelper;
use think\Db;
class WechatFriendController extends BaseController
{
2025-10-16 16:04:53 +08:00
public function getList()
{
2025-09-13 17:20:34 +08:00
$page = $this->request->param('page', 1);
2025-10-16 16:04:53 +08:00
$limit = $this->request->param('limit', 10);
2025-12-05 10:44:43 +08:00
$keyword = $this->request->param('keyword', '');
$groupIds = $this->request->param('groupIds', '');
2025-09-13 17:20:34 +08:00
$accountId = $this->getUserInfo('s2_accountId');
2025-10-16 16:04:53 +08:00
if (empty($accountId)) {
2025-09-13 17:20:34 +08:00
return ResponseHelper::error('请先登录');
}
2025-09-16 09:57:06 +08:00
$query = Db::table('s2_wechat_friend')
2025-12-05 10:44:43 +08:00
->where(['accountId' => $accountId, 'isDeleted' => 0]);
// 关键字搜索:昵称、备注、微信号
if ($keyword !== '' && $keyword !== null) {
$query->where(function ($q) use ($keyword) {
$like = '%' . $keyword . '%';
$q->whereLike('nickname', $like)
->whereOr('conRemark', 'like', $like)
->whereOr('alias', 'like', $like)
->whereOr('wechatId', 'like', $like);
});
}
// 分组筛选groupIds单个分组ID
if ($groupIds !== '' && $groupIds !== null) {
$query->where('groupIds', $groupIds);
}
$query->order('id desc');
2025-09-16 09:57:06 +08:00
$total = $query->count();
2025-09-25 17:52:56 +08:00
$list = $query->page($page, $limit)->select();
// 提取所有好友ID
2025-09-17 16:51:11 +08:00
$friendIds = array_column($list, 'id');
2025-10-16 16:04:53 +08:00
2025-09-19 16:48:42 +08:00
$aiTypeData = [];
if (!empty($friendIds)) {
2025-09-26 15:37:15 +08:00
$aiTypeData = FriendSettings::where('friendId', 'in', $friendIds)->column('friendId,type');
2025-09-19 16:48:42 +08:00
}
2025-09-17 16:51:11 +08:00
// 处理每个好友的数据
foreach ($list as $k => &$v) {
2025-09-25 17:52:56 +08:00
$v['labels'] = json_decode($v['labels'], true);
$v['siteLabels'] = json_decode($v['siteLabels'], true);
2025-09-17 16:51:11 +08:00
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s', $v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s', $v['updateTime']) : '';
$v['passTime'] = !empty($v['passTime']) ? date('Y-m-d H:i:s', $v['passTime']) : '';
2025-09-19 16:48:42 +08:00
$v['aiType'] = isset($aiTypeData[$v['id']]) ? $aiTypeData[$v['id']] : 0;
2025-09-16 09:57:06 +08:00
}
unset($v);
2025-10-16 16:04:53 +08:00
return ResponseHelper::success(['list' => $list, 'total' => $total]);
2025-09-13 17:20:34 +08:00
}
2025-10-28 09:25:39 +08:00
/**
* 获取单个好友详情
* @return \think\response\Json
*/
public function getDetail()
{
$friendId = $this->request->param('id');
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)) {
return ResponseHelper::error('请先登录');
}
if (empty($friendId)) {
return ResponseHelper::error('好友ID不能为空');
}
// 查询好友详情
$friend = Db::table('s2_wechat_friend')
->where(['id' => $friendId, 'isDeleted' => 0])
->find();
2025-10-28 09:25:39 +08:00
if (empty($friend)) {
return ResponseHelper::error('好友不存在');
}
// 处理好友数据
$friend['labels'] = json_decode($friend['labels'], true);
$friend['siteLabels'] = json_decode($friend['siteLabels'], true);
$friend['createTime'] = !empty($friend['createTime']) ? date('Y-m-d H:i:s', $friend['createTime']) : '';
$friend['updateTime'] = !empty($friend['updateTime']) ? date('Y-m-d H:i:s', $friend['updateTime']) : '';
$friend['passTime'] = !empty($friend['passTime']) ? date('Y-m-d H:i:s', $friend['passTime']) : '';
2025-10-28 09:25:39 +08:00
// 获取AI类型设置
$aiTypeSetting = FriendSettings::where('friendId', $friendId)->find();
$friend['aiType'] = $aiTypeSetting ? $aiTypeSetting['type'] : 0;
2025-10-28 09:25:39 +08:00
return ResponseHelper::success(['detail' => $friend]);
}
2025-11-13 16:08:12 +08:00
/**
* 更新好友资料(公司、姓名、手机号等字段可单独更新)
* @return \think\response\Json
*/
public function updateFriendInfo()
{
$friendId = $this->request->param('id');
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)) {
return ResponseHelper::error('请先登录');
}
if (empty($friendId)) {
return ResponseHelper::error('好友ID不能为空');
}
$friend = Db::table('s2_wechat_friend')
->where(['id' => $friendId, 'accountId' => $accountId, 'isDeleted' => 0])
->find();
if (empty($friend)) {
return ResponseHelper::error('好友不存在或无权限操作');
}
$requestData = $this->request->param();
$updatableColumns = [
'phone',
2025-12-04 09:43:25 +08:00
'conRemark',
2025-11-13 16:08:12 +08:00
];
$columnUpdates = [];
foreach ($updatableColumns as $field) {
if (array_key_exists($field, $requestData)) {
$columnUpdates[$field] = $requestData[$field];
}
}
$extendFieldsData = [];
if (!empty($friend['extendFields'])) {
$decodedExtend = json_decode($friend['extendFields'], true);
$extendFieldsData = is_array($decodedExtend) ? $decodedExtend : [];
}
$extendFieldKeys = [
'company',
'name',
'position',
'email',
'address',
'wechat',
'qq',
'remark'
];
$extendFieldsUpdated = false;
foreach ($extendFieldKeys as $key) {
if (array_key_exists($key, $requestData)) {
$extendFieldsData[$key] = $requestData[$key];
$extendFieldsUpdated = true;
}
}
if ($extendFieldsUpdated) {
$columnUpdates['extendFields'] = json_encode($extendFieldsData, JSON_UNESCAPED_UNICODE);
}
if (empty($columnUpdates)) {
return ResponseHelper::error('没有可更新的字段');
}
$columnUpdates['updateTime'] = time();
try {
Db::table('s2_wechat_friend')->where('id', $friendId)->update($columnUpdates);
} catch (\Exception $e) {
return ResponseHelper::error('更新失败:' . $e->getMessage());
}
return ResponseHelper::success(['id' => $friendId]);
}
2025-11-25 14:52:46 +08:00
/**
* 获取添加好友任务记录列表(全新功能)
* 返回当前账号的所有添加好友任务记录,无论是否通过都展示
* 包含:添加者头像、昵称、微信号、添加状态、添加时间、通过时间等信息
* @return \think\response\Json
*/
public function getAddTaskList()
{
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$status = $this->request->param('status', ''); // 可选:筛选状态 0执行中1执行成功2执行失败
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)) {
return ResponseHelper::error('请先登录');
}
2025-11-25 14:52:46 +08:00
// 直接使用operatorAccountId查询添加好友任务记录
$query = Db::table('s2_friend_task')
->where('operatorAccountId', $accountId)
->order('createTime desc');
2025-11-25 14:52:46 +08:00
// 如果指定了状态筛选
if ($status !== '' && $status !== null) {
$query->where('status', $status);
}
2025-11-25 14:52:46 +08:00
$total = $query->count();
$tasks = $query->page($page, $limit)->select();
2025-11-25 14:52:46 +08:00
// 处理任务数据
$list = [];
foreach ($tasks as $task) {
// 提取所有任务的phone、wechatId用于查询好友信息获取通过时间
$friendInfo = Db::table('s2_wechat_friend')
->where(['isDeleted' => 0, 'ownerWechatId' => $task['wechatId']])
->where(function ($query) use ($task) {
$query->whereLike('phone', '%'.$task['phone'].'%')->whereOr('alias', $task['phone'])->whereOr('wechatId', $task['phone']);
})->field('phone,wechatId,alias,passTime,nickname')->find();
2025-11-25 14:52:46 +08:00
$item = [
'taskId' => $task['id'] ?? 0,
'phone' => $task['phone'] ?? '',
'wechatId' => $task['wechatId'] ?? '',
'alias' => $task['alias'] ?? '',
2025-11-25 14:52:46 +08:00
// 添加者信息
'adder' => [
'avatar' => $task['wechatAvatar'] ?? '', // 添加者头像
'nickname' => $task['wechatNickname'] ?? '', // 添加者昵称
'username' => $task['accountUsername'] ?? '', // 添加者微信号
'accountNickname' => $task['accountNickname'] ?? '', // 账号昵称
'accountRealName' => $task['accountRealName'] ?? '', // 账号真实姓名
],
// 添加状态
'status' => [
'code' => $task['status'] ?? 0, // 状态码0执行中1执行成功2执行失败
'text' => $this->getTaskStatusText($task['status'] ?? 0), // 状态文本
'extra' => ''
2025-11-25 14:52:46 +08:00
],
// 时间信息
'time' => [
'addTime' => !empty($task['createTime']) ? date('Y-m-d H:i:s', $task['createTime']) : '', // 添加时间
'addTimeStamp' => $task['createTime'] ?? 0, // 添加时间戳
'updateTime' => !empty($task['updateTime']) ? date('Y-m-d H:i:s', $task['updateTime']) : '', // 更新时间
'updateTimeStamp' => $task['updateTime'] ?? 0, // 更新时间戳
'passTime' => !empty($friendInfo['passTime']) ? date('Y-m-d H:i:s', $friendInfo['passTime']) : '', // 通过时间
'passTimeStamp' => $friendInfo['passTime'] ?? 0, // 通过时间戳
],
// 好友信息(如果已通过)
'friend' => [
'nickname' => $friendInfo['nickname'] ?? '', // 好友昵称
'isPassed' => !empty($friendInfo['passTime']), // 是否已通过
],
// 其他信息
'other' => [
'msgContent' => $task['msgContent'] ?? '', // 验证消息
'remark' => $task['remark'] ?? '', // 备注
'from' => $task['from'] ?? '', // 来源
'labels' => !empty($task['labels']) ? explode(',', $task['labels']) : [], // 标签
]
];
2025-11-25 14:52:46 +08:00
$list[] = $item;
}
2025-11-25 14:52:46 +08:00
return ResponseHelper::success(['list' => $list, 'total' => $total]);
}
/**
* 获取任务状态文本
* @param int $status 状态码
* @return string 状态文本
*/
private function getTaskStatusText($status)
{
$statusMap = [
0 => '执行中',
1 => '执行成功',
2 => '执行失败',
];
2025-11-25 14:52:46 +08:00
return isset($statusMap[$status]) ? $statusMap[$status] : '未知状态';
}
2025-09-13 17:20:34 +08:00
}