常见群聊

This commit is contained in:
2025-12-05 13:55:24 +08:00
parent 5fec786ff0
commit 46abae8148
5 changed files with 87 additions and 98 deletions

View File

@@ -94,72 +94,6 @@ class GroupController extends ApiController
return $this->success($data);
}
/**
* 创建群(仅商家可创建)
*/
public function create(Request $request): Response
{
if ($this->user_type == 1) { // 2=商家
return $this->error('仅商家可创建群聊');
}
$shop_id = $request->post('shop_id', 0);
if(empty($shop_id)) {
return $this->error('参数不完整');
}
$shop = Db::name('tb_shop_info')->where(['id' => $shop_id])->find();
if(empty($shop)) {
return $this->error('店铺不存在');
}
// 查找群数量
$count = Db::name('chat_group')->where(['shop_id' => $shop_id])->count();
$name = $request->post('name')?:$shop['shop_name'] . '粉丝福利' . $count + 1 . '群';
$avatar = $request->post('avatar')?:$shop['logo'];
$announcement = $request->post('announcement', '');
$isPublic = $request->post('is_public', 0);
$now = d();
try {
Db::startTrans();
// 创建群
$group_id = Db::name('chat_group')->insertGetId([
'id' => Random::build('alnum', 32),
'name' => $name,
'shop_id' => $shop_id,
'avatar' => $avatar,
'owner_id' => $this->uid,
'announcement' => $announcement,
'is_public' => $isPublic,
'created_time' => $now,
]);
// 群主加入群
Db::name('chat_group_member')->insert([
'group_id' => $group_id,
'user_id' => $this->uid,
'role' => 1, // 1=群主
'join_time' => $now,
]);
$user = Db::name('chat_user')->where(['user_id' => $this->uid])->find();
$data = [
'group_info' => [
'id' => $group_id,
'name' => $name,
'avatar' => $avatar,
],
'member_info' => [
'user_id' => $this->uid,
'avatar' => $user['avatar'],
'nick_name' => $user['nick_name'],
]
];
Db::commit();
return $this->success($data);
}catch (\Throwable $exception) {
Db::rollback();
return $this->error($exception->getMessage());
}
}
/**
* 加群
*/