76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
namespace app\chat\model;
|
|
|
|
|
|
use ba\Random;
|
|
use support\Log;
|
|
use support\think\Db;
|
|
|
|
class ChatGroup extends BaseModel
|
|
{
|
|
public $tabla_name = 'chat_group';
|
|
|
|
|
|
public static function isTh($group_id, $uid)
|
|
{
|
|
$group_user = Db::name('chat_group_member')->where(['group_id' => $group_id, 'user_id' => $uid, 'quit_time' => null, 'is_kicked' => 0])->find();
|
|
if($group_user) {
|
|
return 1;
|
|
}else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
// 创建群聊
|
|
public static function addShop($shop_id)
|
|
{
|
|
$shop = Db::name('tb_shop_info')->where(['id' => $shop_id])->find();
|
|
if(empty($shop)) {
|
|
Log::info('店铺不存在');
|
|
return false;
|
|
}
|
|
// 查找群数量
|
|
$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() . '---' . $exception->getTraceAsString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} |