群创建 代码优化
This commit is contained in:
@@ -200,14 +200,18 @@ class WebSocketController extends BaseController
|
||||
* @param array $data 消息数据
|
||||
* @return array
|
||||
*/
|
||||
protected function sendMessage($data)
|
||||
protected function sendMessage($data,$receive = true)
|
||||
{
|
||||
$this->checkConnection();
|
||||
|
||||
try {
|
||||
$this->client->send(json_encode($data));
|
||||
$response = $this->client->receive();
|
||||
return json_decode($response, true);
|
||||
if ($receive){
|
||||
$response = $this->client->receive();
|
||||
return json_decode($response, true);
|
||||
}else{
|
||||
return ['code' => 200, 'msg' => '成功'];
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error("发送消息失败:" . $e->getMessage());
|
||||
$this->reconnect();
|
||||
@@ -787,57 +791,6 @@ class WebSocketController extends BaseController
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 邀请好友入群
|
||||
* @param array $data 请求参数
|
||||
* @return string JSON响应
|
||||
*/
|
||||
public function CmdChatroomInvite($data = [])
|
||||
{
|
||||
try {
|
||||
// 参数验证
|
||||
if (empty($data)) {
|
||||
return json_encode(['code' => 400, 'msg' => '参数缺失']);
|
||||
}
|
||||
|
||||
// 验证必要参数
|
||||
if (empty($data['wechatChatroomId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '群ID不能为空']);
|
||||
}
|
||||
if (empty($data['wechatFriendId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '好友ID不能为空']);
|
||||
}
|
||||
|
||||
if (!is_array($data['wechatFriendId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '好友数据格式必须为数组']);
|
||||
}
|
||||
|
||||
if (empty($data['wechatAccountId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '微信账号ID不能为空']);
|
||||
}
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
"cmdType" => "CmdChatroomInvite",
|
||||
"seq" => time(),
|
||||
"wechatChatroomId" => $data['wechatChatroomId'],
|
||||
"wechatFriendId" => $data['wechatFriendId'],
|
||||
"wechatAccountId" => $data['wechatAccountId']
|
||||
];
|
||||
|
||||
// 记录请求日志
|
||||
Log::info('邀请好友入群请求:' . json_encode($params, 256));
|
||||
|
||||
$message = $this->sendMessage($params);
|
||||
return json_encode(['code' => 200, 'msg' => '邀请成功', 'data' => $message]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误日志
|
||||
Log::error('邀请好友入群异常:' . $e->getMessage());
|
||||
// 返回错误响应
|
||||
return json_encode(['code' => 500, 'msg' => '邀请好友入群异常:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加群好友
|
||||
* @param $data
|
||||
@@ -887,4 +840,162 @@ class WebSocketController extends BaseController
|
||||
return json_encode(['code' => 500, 'msg' => '添加群好友异常:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建群聊
|
||||
* @param array $data 请求参数
|
||||
* @return string JSON响应
|
||||
*/
|
||||
public function CmdChatroomCreate($data = [])
|
||||
{
|
||||
try {
|
||||
// 参数验证
|
||||
if (empty($data)) {
|
||||
return json_encode(['code' => 400, 'msg' => '参数缺失']);
|
||||
}
|
||||
|
||||
// 验证必要参数
|
||||
if (empty($data['chatroomName'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '群名称不能为空']);
|
||||
}
|
||||
|
||||
if (empty($data['wechatFriendIds']) || !is_array($data['wechatFriendIds'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '好友ID列表不能为空且必须为数组']);
|
||||
}
|
||||
|
||||
if (count($data['wechatFriendIds']) < 2) {
|
||||
return json_encode(['code' => 400, 'msg' => '创建群聊至少需要2个好友']);
|
||||
}
|
||||
|
||||
if (empty($data['wechatAccountId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '微信账号ID不能为空']);
|
||||
}
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
"cmdType" => "CmdChatroomCreate",
|
||||
"seq" => time(),
|
||||
"wechatAccountId" => $data['wechatAccountId'],
|
||||
"chatroomName" => $data['chatroomName'],
|
||||
"wechatFriendIds" => $data['wechatFriendIds']
|
||||
];
|
||||
// 记录请求日志
|
||||
Log::info('创建群聊请求:' . json_encode($params, 256));
|
||||
|
||||
$message = $this->sendMessage($params,false);
|
||||
return json_encode(['code' => 200, 'msg' => '群聊创建成功', 'data' => $message]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误日志
|
||||
Log::error('创建群聊异常:' . $e->getMessage());
|
||||
// 返回错误响应
|
||||
return json_encode(['code' => 500, 'msg' => '创建群聊异常:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 邀请好友入群
|
||||
* @param array $data 请求参数
|
||||
* @return string JSON响应
|
||||
*/
|
||||
public function CmdChatroomInvite($data = [])
|
||||
{
|
||||
try {
|
||||
// 参数验证
|
||||
if (empty($data)) {
|
||||
return json_encode(['code' => 400, 'msg' => '参数缺失']);
|
||||
}
|
||||
|
||||
// 验证必要参数
|
||||
if (empty($data['wechatChatroomId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '群ID不能为空']);
|
||||
}
|
||||
if (empty($data['wechatFriendIds'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '好友ID不能为空']);
|
||||
}
|
||||
|
||||
if (!is_array($data['wechatFriendIds'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '好友数据格式必须为数组']);
|
||||
}
|
||||
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
"cmdType" => "CmdChatroomInvite",
|
||||
"seq" => time(),
|
||||
"wechatChatroomId" => $data['wechatChatroomId'],
|
||||
"wechatFriendIds" => $data['wechatFriendIds']
|
||||
];
|
||||
|
||||
// 记录请求日志
|
||||
Log::info('邀请好友入群请求:' . json_encode($params, 256));
|
||||
$message = $this->sendMessage($params,false);
|
||||
return json_encode(['code' => 200, 'msg' => '邀请成功', 'data' => $message]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误日志
|
||||
Log::error('邀请好友入群异常:' . $e->getMessage());
|
||||
// 返回错误响应
|
||||
return json_encode(['code' => 500, 'msg' => '邀请好友入群异常:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 修改群信息(群昵称和群公告)
|
||||
* @param array $data 请求参数
|
||||
* @return string JSON响应
|
||||
*/
|
||||
public function CmdChatroomModifyInfo($data = [])
|
||||
{
|
||||
try {
|
||||
// 参数验证
|
||||
if (empty($data)) {
|
||||
return json_encode(['code' => 400, 'msg' => '参数缺失']);
|
||||
}
|
||||
|
||||
// 验证必要参数
|
||||
if (empty($data['wechatChatroomId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '群ID不能为空']);
|
||||
}
|
||||
|
||||
if (empty($data['wechatAccountId'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '微信账号ID不能为空']);
|
||||
}
|
||||
|
||||
// 检查是否至少提供了一个修改项
|
||||
if (empty($data['chatroomName']) && !isset($data['announce'])) {
|
||||
return json_encode(['code' => 400, 'msg' => '请至少提供群昵称或群公告中的一个参数']);
|
||||
}
|
||||
|
||||
|
||||
if (!empty($data['chatroomName'])) {
|
||||
$extra = [
|
||||
"chatroomName" => $data['chatroomName']
|
||||
];
|
||||
} else {
|
||||
$extra = [
|
||||
"announce" => $data['announce']
|
||||
];
|
||||
}
|
||||
|
||||
$params = [
|
||||
"chatroomOperateType" => !empty($data['chatroomName']) ? 6 : 5,
|
||||
"cmdType" => "CmdChatroomOperate",
|
||||
"extra" => json_encode($extra,256),
|
||||
"seq" => time(),
|
||||
"wechatAccountId" => $data['wechatAccountId'],
|
||||
"wechatChatroomId" => $data['wechatChatroomId']
|
||||
];
|
||||
// 记录请求日志
|
||||
Log::info('创建群聊请求:' . json_encode($params, 256));
|
||||
$message = $this->sendMessage($params,false);
|
||||
return json_encode(['code' => 200, 'msg' => '群聊创建成功', 'data' => $message]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('修改群信息异常: ' . $e->getMessage());
|
||||
return json_encode(['code' => 500, 'msg' => '修改群信息失败: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -13,16 +13,12 @@ class WechatChatroomController extends BaseController
|
||||
* 获取微信群聊列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getlist($pageIndex = '',$pageSize = '',$isInner = false, $isDel = '')
|
||||
public function getlist($data = [],$isInner = false, $isDel = '')
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', $this->authorization));
|
||||
$authorization = $this->authorization;
|
||||
if (empty($authorization)) {
|
||||
if($isInner){
|
||||
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
|
||||
}else{
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -36,15 +32,15 @@ class WechatChatroomController extends BaseController
|
||||
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'keyword' => $this->request->param('keyword', ''),
|
||||
'wechatAccountKeyword' => $this->request->param('wechatAccountKeyword', ''),
|
||||
'isDeleted' => $this->request->param('isDeleted', $isDeleted),
|
||||
'allotAccountId' => $this->request->param('allotAccountId', ''),
|
||||
'groupId' => $this->request->param('groupId', ''),
|
||||
'wechatChatroomId' => $this->request->param('wechatChatroomId', 0),
|
||||
'memberKeyword' => $this->request->param('memberKeyword', ''),
|
||||
'pageIndex' => !empty($pageIndex) ? $pageIndex : input('pageIndex', 0),
|
||||
'pageSize' => !empty($pageSize) ? $pageSize : input('pageSize', 20)
|
||||
'keyword' => $data['keyword'] ?? '',
|
||||
'wechatAccountKeyword' => $data['wechatAccountKeyword'] ?? '',
|
||||
'isDeleted' => $data['isDeleted'] ?? $isDeleted ,
|
||||
'allotAccountId' => $data['allotAccountId'] ?? '',
|
||||
'groupId' => $data['groupId'] ?? '',
|
||||
'wechatChatroomId' => $data['wechatChatroomId'] ?? '',
|
||||
'memberKeyword' => $data['memberKeyword'] ?? '',
|
||||
'pageIndex' => $data['pageIndex'] ?? 1,
|
||||
'pageSize' => $data['pageSize'] ?? 20
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
|
||||
Reference in New Issue
Block a user