常见群聊
This commit is contained in:
@@ -94,72 +94,6 @@ class GroupController extends ApiController
|
|||||||
return $this->success($data);
|
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');
|
return $this->error('请传入有效会话ID');
|
||||||
}
|
}
|
||||||
$msgIds = explode(',', $msgIds);
|
$msgIds = explode(',', $msgIds);
|
||||||
$query = Db::name('chat_message')->whereIn('session_id', $msgIds)->where('to_id', $this->uid)
|
// $query = Db::name('chat_message')->whereIn('session_id', $msgIds)->where('to_id', $this->uid)
|
||||||
->where('is_read', 0);
|
// ->where('is_read', 0);
|
||||||
try {
|
try {
|
||||||
Db::startTrans();
|
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()]);
|
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();
|
Db::commit();
|
||||||
return $this->success(true);
|
return $this->success(true);
|
||||||
}catch (\Throwable $exception) {
|
}catch (\Throwable $exception) {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
namespace app\chat\model;
|
namespace app\chat\model;
|
||||||
|
|
||||||
|
|
||||||
|
use ba\Random;
|
||||||
|
use support\Log;
|
||||||
use support\think\Db;
|
use support\think\Db;
|
||||||
|
|
||||||
class ChatGroup extends BaseModel
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,6 @@ Route::group('/api/chat', function () {
|
|||||||
Route::group('/group', function () {
|
Route::group('/group', function () {
|
||||||
Route::post('/shopinfo', app\chat\controller\GroupController::class . '@shopInfo'); // 店铺群信息
|
Route::post('/shopinfo', app\chat\controller\GroupController::class . '@shopInfo'); // 店铺群信息
|
||||||
Route::post('/info', app\chat\controller\GroupController::class . '@info'); // 群信息
|
Route::post('/info', app\chat\controller\GroupController::class . '@info'); // 群信息
|
||||||
Route::post('/create', app\chat\controller\GroupController::class . '@create'); // 创建群(商家)
|
|
||||||
Route::post('/join', app\chat\controller\GroupController::class . '@join'); // 加群
|
Route::post('/join', app\chat\controller\GroupController::class . '@join'); // 加群
|
||||||
Route::post('/getgrepurl', app\chat\controller\GroupController::class . '@getgrepurl'); // 群邀请参数
|
Route::post('/getgrepurl', app\chat\controller\GroupController::class . '@getgrepurl'); // 群邀请参数
|
||||||
Route::post('/quit', app\chat\controller\GroupController::class . '@quit'); // 退群
|
Route::post('/quit', app\chat\controller\GroupController::class . '@quit'); // 退群
|
||||||
|
|||||||
@@ -40,35 +40,39 @@ use support\Redis;
|
|||||||
Log::info('MQ收到消息[商品状态更新]--->' . $data . '--->' . $date_time . "\n");
|
Log::info('MQ收到消息[商品状态更新]--->' . $data . '--->' . $date_time . "\n");
|
||||||
$datInfo= json_decode($data, true);
|
$datInfo= json_decode($data, true);
|
||||||
if(is_array($datInfo)) {
|
if(is_array($datInfo)) {
|
||||||
// 如果是数字。则打印订单
|
if($datInfo['type'] == 'add_shop') {
|
||||||
$is_log = false;
|
// 创建群聊
|
||||||
$curl_error = '';
|
\app\chat\model\ChatGroup::addShop($datInfo);
|
||||||
$rand = 'product_update' . Random::build();
|
}else {
|
||||||
$send_id = Base::get_shopping_save_uid($datInfo['shopId'], 'all');
|
$is_log = false;
|
||||||
$snd_data =[
|
$curl_error = '';
|
||||||
'msg' => '购物车刷新',
|
$rand = 'product_update' . Random::build();
|
||||||
'operate_type' => 'reload',
|
$send_id = Base::get_shopping_save_uid($datInfo['shopId'], 'all');
|
||||||
'msg_id' => $rand,
|
$snd_data =[
|
||||||
'type' => $datInfo['type'],
|
'msg' => '购物车刷新',
|
||||||
'data_type' => 'cart',
|
'operate_type' => 'reload',
|
||||||
'status' => 1
|
'msg_id' => $rand,
|
||||||
];
|
'type' => $datInfo['type'],
|
||||||
Log::info('推送组'.$send_id.'内容'.json_encode($snd_data));
|
'data_type' => 'cart',
|
||||||
$snd_data_json = json_encode($snd_data);
|
'status' => 1
|
||||||
Gateway::$registerAddress = '127.0.0.1:1238';
|
];
|
||||||
$res = Gateway::sendToGroup($send_id, $snd_data_json);
|
Log::info('推送组'.$send_id.'内容'.json_encode($snd_data));
|
||||||
Log::info('商品状态更新推送结果-->' . $res);
|
$snd_data_json = json_encode($snd_data);
|
||||||
|
Gateway::$registerAddress = '127.0.0.1:1238';
|
||||||
|
$res = Gateway::sendToGroup($send_id, $snd_data_json);
|
||||||
|
Log::info('商品状态更新推送结果-->' . $res);
|
||||||
|
|
||||||
if($is_log) {
|
if($is_log) {
|
||||||
Db::table('tb_mq_log')->insert([
|
Db::table('tb_mq_log')->insert([
|
||||||
'queue' => $queue,
|
'queue' => $queue,
|
||||||
'msg' => $data,
|
'msg' => $data,
|
||||||
'type' => 'product_update',
|
'type' => 'product_update',
|
||||||
'plat' => 'product',
|
'plat' => 'product',
|
||||||
'create_time' => date('Y-m-d H:i:s'),
|
'create_time' => date('Y-m-d H:i:s'),
|
||||||
'fail_time' => date('Y-m-d H:i:s'),
|
'fail_time' => date('Y-m-d H:i:s'),
|
||||||
'err_info' => $curl_error,
|
'err_info' => $curl_error,
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
|
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
|
||||||
|
|||||||
Reference in New Issue
Block a user