diff --git a/Server/application/chukebao/config/route.php b/Server/application/chukebao/config/route.php index 353f4877..f9d04ddf 100644 --- a/Server/application/chukebao/config/route.php +++ b/Server/application/chukebao/config/route.php @@ -13,6 +13,7 @@ Route::group('v1/', function () { Route::group('wechatFriend/', function () { Route::get('list', 'app\chukebao\controller\WechatFriendController@getList'); // 获取好友列表 Route::get('detail', 'app\chukebao\controller\WechatFriendController@getDetail'); // 获取好友详情 + Route::post('updateInfo', 'app\chukebao\controller\WechatFriendController@updateFriendInfo'); // 更新好友资料 }); //群相关 Route::group('wechatChatroom/', function () { diff --git a/Server/application/chukebao/controller/WechatFriendController.php b/Server/application/chukebao/controller/WechatFriendController.php index 5851e0f8..74710c1e 100644 --- a/Server/application/chukebao/controller/WechatFriendController.php +++ b/Server/application/chukebao/controller/WechatFriendController.php @@ -23,53 +23,9 @@ class WechatFriendController extends BaseController $total = $query->count(); $list = $query->page($page, $limit)->select(); - // 提取所有好友ID $friendIds = array_column($list, 'id'); - /* // 一次性查询所有好友的未读消息数量 - $unreadCounts = []; - if (!empty($friendIds)) { - $unreadResults = Db::table('s2_wechat_message') - ->field('wechatFriendId, COUNT(*) as count') - ->where('wechatFriendId', 'in', $friendIds) - ->where('isRead', 0) - ->group('wechatFriendId') - ->select(); - if (!empty($unreadResults)) { - foreach ($unreadResults as $result) { - $unreadCounts[$result['wechatFriendId']] = $result['count']; - } - } - } - - // 一次性查询所有好友的最新消息 - $latestMessages = []; - if (!empty($friendIds)) { - // 使用子查询获取每个好友的最新消息ID - $subQuery = Db::table('s2_wechat_message') - ->field('MAX(id) as max_id, wechatFriendId') - ->where('wechatFriendId', 'in', $friendIds) - ->group('wechatFriendId') - ->buildSql(); - - if (!empty($subQuery)) { - // 查询最新消息的详细信息 - $messageResults = Db::table('s2_wechat_message') - ->alias('m') - ->join([$subQuery => 'sub'], 'm.id = sub.max_id') - ->field('m.*, sub.wechatFriendId') - ->select(); - - if (!empty($messageResults)) { - foreach ($messageResults as $message) { - $latestMessages[$message['wechatFriendId']] = $message; - } - } - } - }*/ - - $aiTypeData = []; if (!empty($friendIds)) { $aiTypeData = FriendSettings::where('friendId', 'in', $friendIds)->column('friendId,type'); @@ -80,21 +36,10 @@ class WechatFriendController extends BaseController foreach ($list as $k => &$v) { $v['labels'] = json_decode($v['labels'], true); $v['siteLabels'] = json_decode($v['siteLabels'], true); + $v['extendFields'] = json_decode($v['extendFields'], true); $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']) : ''; - - - /* $config = [ - 'unreadCount' => isset($unreadCounts[$v['id']]) ? $unreadCounts[$v['id']] : 0, - 'chat' => isset($latestMessages[$v['id']]), - 'msgTime' => isset($latestMessages[$v['id']]) ? $latestMessages[$v['id']]['wechatTime'] : 0 - ]; - - // 将消息配置添加到好友数据中 - $v['config'] = $config;*/ - - $v['aiType'] = isset($aiTypeData[$v['id']]) ? $aiTypeData[$v['id']] : 0; } unset($v); @@ -141,4 +86,85 @@ class WechatFriendController extends BaseController return ResponseHelper::success(['detail' => $friend]); } + + /** + * 更新好友资料(公司、姓名、手机号等字段可单独更新) + * @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', + ]; + $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]); + } } \ No newline at end of file