常见群聊

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

@@ -2,6 +2,8 @@
namespace app\chat\model;
use ba\Random;
use support\Log;
use support\think\Db;
class ChatGroup extends BaseModel
@@ -20,4 +22,54 @@ class ChatGroup extends BaseModel
}
// 创建群聊
public static function addShop($shop_id)
{
$shop = Db::name('tb_shop_info')->where(['id' => $shop_id])->find();
if(empty($shop)) {
Log::info('店铺不存在');
}
// 查找群数量
$count = Db::name('chat_group')->where(['shop_id' => $shop_id])->count();
$name = $shop['shop_name'] . '粉丝福利' . $count + 1 . '群';
$avatar = $shop['logo'];
$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' => $shop_id,
'created_time' => $now,
]);
// 群主加入群
Db::name('chat_group_member')->insert([
'group_id' => $group_id,
'user_id' => $shop_id,
'role' => 1, // 1=群主
'join_time' => $now,
]);
$user = Db::name('chat_user')->where(['user_id' => $shop_id])->find();
if(!$user) {
$res = Db::name('chat_user')->insert([
'user_id' => $shop_id,
'nick_name' => $shop['shop_name'],
'avatar' => $shop['logo'],
'type' => 2,
'created_time' => d(),
]);
Log::info('创建店铺结果 ' . $res . $shop_id);
}
Db::commit();
}catch (\Throwable $exception) {
Db::rollback();
Log::info('创建店铺错误 ' . $exception->getMessage());
}
}
}