Files
im/app/command/OpenIm.php
T

119 lines
4.0 KiB
PHP
Raw Normal View History

2025-11-22 15:31:01 +08:00
<?php
namespace app\command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use support\think\Db;
class OpenIm extends Command
{
protected static $defaultName = 'openim';
protected static $defaultDescription = 'OpenIm';
2026-02-27 13:53:53 +08:00
public $sdk= null;
2025-11-22 15:31:01 +08:00
/**
* @return void
*/
protected function configure()
{
$this->addOption('action','a', InputArgument::OPTIONAL, '操作类型');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$action = $input->getOption('action');
if(!$action){
$output->writeln('空操作');
return self::FAILURE;
}
if(method_exists($this, $action)){
return $this->$action($input, $output);
}
2026-02-27 13:53:53 +08:00
$output->writeln($action.'不存在');
2025-11-22 15:31:01 +08:00
return self::FAILURE;
}
2025-12-24 16:59:05 +08:00
private function change_user(InputInterface $input, OutputInterface $output):int{
2026-02-27 13:53:53 +08:00
$im = $this->getSdk();
2026-03-01 21:05:19 +08:00
$data = $im->user->updateUserInfo(\support\Encrypt::userIDencode('100006'),['userInfo'=>['userId'=>'wx100001']]);
2025-12-24 16:59:05 +08:00
cp($data);
return self::SUCCESS;
}
2025-11-22 15:31:01 +08:00
private function sync_users(InputInterface $input, OutputInterface $output):int{
2026-02-27 13:53:53 +08:00
$im = $this->getSdk();
2025-11-22 15:31:01 +08:00
$data = $im->user->getAllUsersUid(1,1000);
2025-12-24 16:59:05 +08:00
cp($data);
return self::SUCCESS;
2025-11-22 15:31:01 +08:00
$exsit_user_ids = Db::name('user')->whereIn('id',$data['userIDs'])->column('id');
$not_exsit_user_ids =array_diff($data['userIDs'],$exsit_user_ids);
if(count($not_exsit_user_ids)> 0){
//同步用户
$res = $im->user->getUsersInfo($not_exsit_user_ids);
$save_data = [];
foreach($res['usersInfo'] as $k=>$_user){
array_push($save_data,[
'id' => $_user['userID'],
'nickname' => $_user['nickname'],
'password' => '123456',
'avatar' => $_user['faceURL']
]);
// "ex": "",
// "createTime": 1688454168302,
// "appMangerLevel": 18,
// "globalRecvMsgOpt": 0
}
if(!empty($save_data)){
Db::name('user')->insertAll($save_data);
}
}
return self::SUCCESS;
}
2026-02-27 13:53:53 +08:00
function sync_cache(){
$res = \app\model\Openim\Group::field('group_id,creator_user_id')->select();
$group_create = [];
foreach($res as $v){
if(!isset($group_create[$v['creator_user_id']])){
$group_create[$v['creator_user_id']] = 0;
}
if($v['status'] != 2){
$group_create[$v['creator_user_id']]+=1;
}
cache('group_owner_'.$v['group_id'],$v['creator_user_id']);
$group_user_count = \app\model\Openim\GroupMember::field('group_id,count(*) as count')
->where('group_id',$v['group_id'])
->count('user_id');
cp('群组数量',$v['group_id'],'成员数量:',$group_user_count);
cache('group_'.$v['group_id'].'_user_count',$group_user_count);
}
//cp($group_create);
foreach($group_create as $userId =>$count){
cp('用户:',$userId,'创建群组数量:',$count);
cache('user_'.$userId.'_create_group_count',$count);
}
return 0;
}
function getSdk(){
if($this->sdk){
return $this->sdk;
}
$this->sdk = new \support\OpenImSdk\Client([
'host' => 'http://127.0.0.1:10002', // OpenIM API地址
'secret' => 'n1e5a6s6m7', // OpenIM密钥
]);
return $this->sdk;
}
2025-11-22 15:31:01 +08:00
}