From 71c56152c583af1303532f421081d5f022020d8e Mon Sep 17 00:00:00 2001 From: ASUS <515617283@qq.com> Date: Tue, 18 Nov 2025 15:32:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=A4=E6=93=8D=E4=BD=9C=E5=AE=8C=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chat/controller/GroupController.php | 62 ++++++++++++++++++++----- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/app/chat/controller/GroupController.php b/app/chat/controller/GroupController.php index 45c18c6..5d761e1 100644 --- a/app/chat/controller/GroupController.php +++ b/app/chat/controller/GroupController.php @@ -6,6 +6,7 @@ use app\chat\model\ChatGroupMember; use app\chat\model\ChatGroupMute; use app\chat\model\ChatDoNotDisturb; use app\common\controller\ApiController; +use app\exception\MyBusinessException; use app\utils\Session; use support\Request; use support\Response; @@ -124,17 +125,56 @@ class GroupController extends ApiController $insert_arr['group_id'] = $decryptedText_arr['group_id']; $group_id = $decryptedText_arr['group_id']; } - - // 加入群 - if($is_insert) { - $res = Db::name('chat_group_member')->insert($insert_arr); - }else { - $res = Db::name('chat_group_member')->where(['user_id' => $this->uid, 'group_id' => $group_id])->update($insert_arr); - } - if($res) { - return $this->success(); - }else { - return $this->error(); + try { + Db::startTrans(); + // 插入用户信息 + if($this->user_type == 1) { + // 用户 + $user = Db::name('tb_user_info')->where(['id' => $this->uid])->find(); + if($user) { + $user_arr = [ + 'user_id' => $this->uid, + 'nick_name' => $user['nick_name'], + 'avatar' => $user['head_img'], + 'type' => 1, + 'created_time' => d(), + ]; + }else { + throw new MyBusinessException('用户信息不存在'); + } + }elseif ($this->user_type == 2) { + // 商家 + $user = Db::name('sys_user')->where(['id' => $this->uid])->find(); + if($user) { + $user_arr = [ + 'user_id' => $this->uid, + 'nick_name' => $user['nick_name'], + 'avatar' => $user['avatar'], + 'type' => 2, + 'created_time' => d(), + ]; + }else { + throw new MyBusinessException('用户信息不存在'); + } + } + if(!Db::name('chat_user')->where(['user_id' => $this->uid])->find()) { + Db::name('chat_user')->insert($user_arr); + } + // 加入群 + if($is_insert) { + $res = Db::name('chat_group_member')->insert($insert_arr); + }else { + $res = Db::name('chat_group_member')->where(['user_id' => $this->uid, 'group_id' => $group_id])->update($insert_arr); + } + Db::commit(); + if($res) { + return $this->success(); + }else { + return $this->error(); + } + }catch (\Throwable $exception) { + Db::rollback(); + return $this->error($exception->getMessage()); } }