Files
im/app/controller/HookController.php
T

382 lines
14 KiB
PHP
Raw Normal View History

2025-12-24 16:59:05 +08:00
<?php
namespace app\controller;
use support\Request;
use support\Response;
use think\facade\Db;
use app\model\User as UserModel;
class HookController{
2026-02-27 13:53:53 +08:00
public $sdk = null;
2025-12-24 16:59:05 +08:00
function index(){
return 'ok';
}
function __call($method, $args):Response
{
//log_alert(Input());
2026-02-27 13:53:53 +08:00
return $this->success();
2026-01-08 05:42:44 +08:00
}
2026-02-24 21:02:17 +08:00
//用户注册完成后
2026-01-08 05:42:44 +08:00
function callbackAfterUserRegisterCommand(Request $request): Response
{
$userID= Input('userID');
$nickname= Input('nickname');
2026-02-15 19:41:56 +08:00
$users = Input('users');
2026-01-08 05:42:44 +08:00
$im = new \support\OpenImSdk\Client([
'host' => 'http://127.0.0.1:10002', // OpenIM API地址
'secret' => 'n1e5a6s6m7', // OpenIM密钥
]);
2026-02-15 19:41:56 +08:00
foreach($users as $k=>$v){
// 'userID' => '0EO9K107ON',
// 'nickname' => '坏蛋',
// 'faceURL' => '/static/img/avatar.png',
// 'ex' => '',
// 'createTime' => 0,
// 'appMangerLevel' => 0,
// 'globalRecvMsgOpt' => 0,
2026-02-27 13:53:53 +08:00
$im->message->sendBusinessNotification('official_team',$v['userID'],[
2026-02-15 19:41:56 +08:00
'contentType' => 101,
'textElem' => [
'content' => '欢迎使用'.Config('site.name')
]
]);
}
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//在发送单聊消息前的回调
2025-12-24 16:59:05 +08:00
public function callbackBeforeSendSingleMsgCommand(Request $request): Response
{
//log_alert(Input());
2026-02-24 21:02:17 +08:00
// $user_id = Input('sendID');
// $recv_user_id = Input( 'recvID');
// $status = Input('status',1);
// $sessionType = Input('sessionType',null);
// if($status == 1 && $sessionType != 4){
// //$max = 10000000000;//限制消息数量
// $user_rights = get_user_rights($user_id);
2026-02-27 13:53:53 +08:00
// $max = $user_rights['right']['max_send_msg_count'];
2026-02-24 21:02:17 +08:00
// $sended_msg_count = cache('single_msg_count_'.$user_id)??0 + cache('group_msg_count_'.$user_id)??0;
// if($sended_msg_count > $max){
2026-02-27 13:53:53 +08:00
// return $this->error(1002,'超出消息数量限制,请先开通或升级会员');
2026-02-24 21:02:17 +08:00
// }
// }
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//发送单聊消息后的回调
2025-12-24 16:59:05 +08:00
public function callbackAfterSendSingleMsgCommand(Request $request): Response
{
$user_id = Input('sendID');
$recv_user_id = Input('recvID');
$status = Input('status',1);
$sessionType = Input('sessionType',null);
if($status == 1 && $sessionType != 4){
$key = '_msg_count_'.$user_id;
if($sessionType == 1){
$key = 'single'.$key;
}
if($sessionType == 2){
$key = 'group'.$key;
}
cache_add($key,1);
}
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//发送群聊消息前的回调
2025-12-24 16:59:05 +08:00
public function callbackBeforeSendGroupleMsgCommand(Request $request): Response
{
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//发送群聊消息后的回调
2025-12-24 16:59:05 +08:00
public function callbackAfterSendGroupleMsgCommand(Request $request): Response
{
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//发送好友申请之前的回调
2025-12-24 16:59:05 +08:00
public function callbackBeforeAddFriendCommand(Request $request): Response{
2026-02-27 13:53:53 +08:00
// $from_user_id = Input('fromUserID');
// $to_user_id = Input('toUserID');
// $handleResult = Input('handleResult');
// $key = 'friend_count_'.$from_user_id;
// $user_rights = get_user_rights($from_user_id);
// $max = isset($user_rights['right']['max_friend_count']) ? $user_rights['right']['max_friend_count'] : -1;
// if(cache($key) > $max){
// return $this->error(1001,'超出好友数量限制,请先开通或升级会员');
// }
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//发送好友申请之后的回调
2025-12-24 16:59:05 +08:00
public function callbackAfterAddFriendCommand(Request $request): Response
{
$from_user_id = Input('fromUserID');
$to_user_id = Input('toUserID');
cache_add('friend_count_'.$to_user_id,1);
cache_add('friend_count_'.$from_user_id,1);
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//在添加好友对方同意之前的回调
2025-12-24 16:59:05 +08:00
public function callbackBeforeAddFriendAgreeCommand(Request $request): Response
{
2026-02-27 13:53:53 +08:00
// $from_user_id = Input('fromUserID');
// $to_user_id = Input('toUserID');
// $handleResult = Input('handleResult');
// if($handleResult == 1){
// $key = 'friend_count_'.$to_user_id;
// $user_rights = get_user_rights($to_user_id);
// $max = isset($user_rights['right']['max_friend_count']) ? $user_rights['right']['max_friend_count'] : -1;
// if(cache($key) > $max){
// return $this->error(1001,'超出好友数量限制,请先开通或升级会员');
// }
// }
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//在添加好友对方同意之后的回调
2025-12-24 16:59:05 +08:00
public function callbackAfterAddFriendAgreeCommand(Request $request):Response
{
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//用户在线状态回调
2025-12-24 16:59:05 +08:00
public function callbackAfterUserOnlineCommand(Request $request): Response
{
$user_id = Input('userID');
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-27 13:53:53 +08:00
//用户离线状态回调
2025-12-24 16:59:05 +08:00
public function callbackAfterUserOfflineCommand(Request $request): Response{
$user_id = Input('userID');
2026-02-27 13:53:53 +08:00
return $this->success();
2025-12-24 16:59:05 +08:00
}
2026-02-24 21:02:17 +08:00
//用户删除好友之后得回调
2026-02-27 13:53:53 +08:00
public function callbackAfterDeleteFriendCommand(Request $request): Response {
2026-02-15 19:41:56 +08:00
$friendUserID = Input('friendUserID');
$ownerUserID = Input('ownerUserID');
$sdk = $this->getSdk();
$relation = $sdk->friend->isFriend($friendUserID,$ownerUserID);
if($relation){
if($relation['inUser1Friends']){
$this->getSdk()->friend->deleteFriend($friendUserID,$ownerUserID);
}
if($relation['inUser2Friends']){
$this->getSdk()->friend->deleteFriend($ownerUserID,$friendUserID);
}
}
2026-02-27 13:53:53 +08:00
return $this->success();
2026-02-15 19:41:56 +08:00
}
2026-02-24 21:02:17 +08:00
2026-02-27 13:53:53 +08:00
//在创建群组之前的回调
//执行顺序,callbackBeforeCreateGroupCommand -> callbackBeforeMembersJoinGroupCommand -> callbackAfterCreateGroupCommand
2026-02-24 21:02:17 +08:00
public function callbackbeforeCreateGroupCommand(Request $request): Response
{
2026-03-01 21:05:19 +08:00
return $this->success();
2026-02-27 13:53:53 +08:00
$groupID = Input('groupID');
$creatorUserID = Input('creatorUserID');
$key = 'user_'.$creatorUserID.'_create_group_count';
$user_rights = get_user_rights($creatorUserID);
$max_group_create_count = isset($user_rights['right']['max_group_create_count']) ? $user_rights['right']['max_group_create_count'] : -1;
if(cache($key) > $max_group_create_count){
return $this->error(2001,'超出创建群组数量限制,请先开通或升级会员');
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
$max_group_user = $user_rights['right']['max_group_user_count'];
if(count(Input('initMemberList')) > $max_group_user){
return $this->error(2002,'超出群组成员数量限制,请先开通或升级会员');
}
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//在创建群组之后的回调
2026-02-24 21:02:17 +08:00
public function callbackafterCreateGroupCommand(Request $request):Response
{
2026-02-27 13:53:53 +08:00
$groupID = Input('groupID');
$creatorUserID = Input('creatorUserID');
cache_add('user_'.$creatorUserID.'_create_group_count',1);
//增加群组用户数量缓存
cache_add('group_'.$groupID.'_user_count',count(Input('initMemberList')));
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//转让群主之后的回调
2026-02-24 21:02:17 +08:00
public function callbackAfterTransferGroupOwnerCommand(Request $request):Response
{
$oldOwnerUserID = Input('oldOwnerUserID');
$newOwnerUserID = Input('newOwnerUserID');
2026-02-27 13:53:53 +08:00
$groupID = Input('groupID');
2026-02-24 21:02:17 +08:00
cache_add('user_'.$oldOwnerUserID.'_create_group_count',-1);
cache_add('user_'.$newOwnerUserID.'_create_group_count',1);
2026-02-27 13:53:53 +08:00
cache_add('group_owner_'.$groupID,$newOwnerUserID);
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//解散群组后回调
2026-02-24 21:02:17 +08:00
public function callbackAfterDisMissGroupCommand(Request $request):Response
{
$groupID = Input('groupID');
$ownerID = Input('ownerID');
2026-02-27 13:53:53 +08:00
//减少群主创建群组数量缓存
cache_add('user_'.$ownerID.'_create_group_count',-1);
//删除群组用户数量缓存
cache('group_'.$groupID.'_user_count',null);
//删除群组群主缓存
cache('group_owner_'.$groupID,null);
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//用户退出群组的回调
2026-02-24 21:02:17 +08:00
public function callbackAfterQuitGroupCommand(Request $request):Response
{
$groupID = Input('groupID');
$userID = Input('userID');
2026-02-27 13:53:53 +08:00
// //减少用户加入群组数量缓存
// cache_add('user_'.$userID.'_join_group_count',-1);
//减少群组用户数量缓存
2026-02-24 21:02:17 +08:00
cache_add('group_'.$groupID.'_user_count',-1);
2026-02-27 13:53:53 +08:00
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//群成员进群之前的回调
2026-02-24 21:02:17 +08:00
public function callbackBeforeMembersJoinGroupCommand(Request $request):Response
{
2026-03-01 21:05:19 +08:00
return $this->success();
2026-02-24 21:02:17 +08:00
$groupID = Input('groupID');
$memberList = Input('memberList');
2026-02-27 13:53:53 +08:00
$ownerID = $this->getGroupOwner($groupID);
2026-02-28 16:18:52 +08:00
if(!$ownerID){
return $this->success();
}
2026-02-27 13:53:53 +08:00
//获取群组当前用户数量
$group_user_count = cache('group_'.$groupID.'_user_count');
if($group_user_count === null){
$group_user_count = 0;
}
//获取群组最大用户数量
$max_group_user = get_user_rights($ownerID)['right']['max_group_user_count'];
if((count($memberList) + $group_user_count) > $max_group_user){
return $this->error(2003,'超出群组成员数量限制,请先开通或升级会员');
}
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//踢除群组成员的回调
2026-02-24 21:02:17 +08:00
public function callbackAfterKickGroupCommand(Request $request):Response
{
$groupID = Input('groupID');
$kickedUserIDs = Input('kickedUserIDs');
2026-02-27 13:53:53 +08:00
//减少群组用户数量缓存
cache_add('group_'.$groupID.'_user_count',-count($kickedUserIDs));
// foreach($kickedUserIDs as $kickedUserID){
// //减少用户加入群组数量缓存
// cache_add('user_'.$kickedUserID.'_join_group_count',-1);
// }
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//新成员加入群组之后的回调
2026-02-24 21:02:17 +08:00
public function callbackAfterJoinGroupCommand(Request $request):Response
{
$groupID = Input('groupID');
$userID = Input('userID');
2026-02-27 13:53:53 +08:00
// //增加用户加入群组数量缓存
// cache_add('user_'.$userID.'_join_group_count',1);
//增加群组用户数量缓存
cache_add('group_'.$groupID.'_user_count',1);
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//邀请新成员加入群组之前的回调
//执行顺序,callbackBeforeInviteJoinGroupCommand -> callbackBeforeMembersJoinGroupCommand
2026-02-24 21:02:17 +08:00
public function callbackBeforeInviteJoinGroupCommand(Request $request):Response
{
2026-03-01 21:05:19 +08:00
return $this->success();
2026-02-24 21:02:17 +08:00
$groupID = Input('groupID');
$invitedUserIDs = Input('invitedUserIDs');
2026-02-27 13:53:53 +08:00
//获取群组当前用户数量
$group_user_count = cache('group_'.$groupID.'_user_count');
if($group_user_count === null){
$group_user_count = 0;
}
//获取群组最大用户数量
$max_group_user = get_user_rights($this->getGroupOwner($groupID))['right']['max_group_user_count'];
if((count($invitedUserIDs) + $group_user_count) > $max_group_user){
return $this->error(2003,'超出群组成员数量限制,请先开通或升级会员');
}
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
//申请加入群组之前的回调
2026-02-24 21:02:17 +08:00
public function callbackBeforeJoinGroupCommand(Request $request):Response
{
2026-03-01 21:05:19 +08:00
return $this->success();
2026-02-24 21:02:17 +08:00
$groupID = Input('groupID');
$applyID = Input('applyID');
2026-02-27 13:53:53 +08:00
//获取群组当前用户数量
$group_user_count = cache('group_'.$groupID.'_user_count')?:0;
//获取群组最大用户数量
$max_group_user = get_user_rights($this->getGroupOwner($groupID))['right']['max_group_user_count'];
if((1 + $group_user_count) > $max_group_user){
return $this->error(2003,'群组已经满员');
}
// //获取用户加入群组数量限制
// $max_group_join_count = get_user_rights($applyID)['right']['max_group_join_count'];
// //获取用户加入群组数量
// $user_join_group_count = cache('user_'.$applyID.'_join_group_count')?:0;
// if((1 + $user_join_group_count) > $max_group_join_count){
// return $this->error(2004,'超出加入群组数量限制,请先开通或升级会员');
// }
return $this->success();
2026-02-24 21:02:17 +08:00
}
2026-02-27 13:53:53 +08:00
function getGroupOwner($groupID=''){
$result = cache('group_owner_'.$groupID);
if($result){
return $result;
}
$groupsInfo = $this->getSdk()->group->getGroupsInfo([$groupID]);
foreach($groupsInfo['groupInfos'] as $groupInfo){
if($groupInfo['groupID'] == $groupID){
cache('group_owner_'.$groupID,$groupInfo['ownerUserID']);
return $groupInfo['ownerUserID'];
}
}
return '';
}
function result($code,$msg,$nextCode=0){
return json([
"actionCode" => 0,
"errCode" => $code,
"errMsg" => $msg,
2026-02-28 16:18:52 +08:00
"errDlt" => '',
2026-02-27 13:53:53 +08:00
"nextCode"=> $nextCode
]);
}
function success(){
return $this->result(0,"");
}
function error($errCode=0,$errMsg='',$nextCode=1){
return $this->result($errCode,$errMsg,$nextCode);
}
2026-02-15 19:41:56 +08:00
function getSdk(){
2026-02-27 13:53:53 +08:00
if($this->sdk){
return $this->sdk;
}
$this->sdk = new \support\OpenImSdk\Client([
2026-02-15 19:41:56 +08:00
'host' => 'http://127.0.0.1:10002', // OpenIM API地址
'secret' => 'n1e5a6s6m7', // OpenIM密钥
]);
2026-02-27 13:53:53 +08:00
return $this->sdk;
2026-02-15 19:41:56 +08:00
}
2025-12-24 16:59:05 +08:00
}