常见群聊
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加群
|
||||
*/
|
||||
|
||||
@@ -149,8 +149,8 @@ class MessageController extends ApiController
|
||||
return $this->error('请传入有效会话ID');
|
||||
}
|
||||
$msgIds = explode(',', $msgIds);
|
||||
$query = Db::name('chat_message')->whereIn('session_id', $msgIds)->where('to_id', $this->uid)
|
||||
->where('is_read', 0);
|
||||
// $query = Db::name('chat_message')->whereIn('session_id', $msgIds)->where('to_id', $this->uid)
|
||||
// ->where('is_read', 0);
|
||||
try {
|
||||
Db::startTrans();
|
||||
// 更新未读计数
|
||||
@@ -158,7 +158,7 @@ class MessageController extends ApiController
|
||||
Db::name('chat_unread_count')->where(['user_id' => $this->uid, 'session_id' => $sessionId])->update(['count' => 0, 'updated_time' => d()]);
|
||||
}
|
||||
// 批量更新
|
||||
$query->update(['is_read' => 1]);
|
||||
// $query->update(['is_read' => 1]);
|
||||
Db::commit();
|
||||
return $this->success(true);
|
||||
}catch (\Throwable $exception) {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user