19
This commit is contained in:
+132
-6
@@ -10,6 +10,9 @@ use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use support\think\Db;
|
||||
use app\model\User as UserModel;
|
||||
use \think\db\PDOConnection;
|
||||
use think\db\exception\PDOException;
|
||||
use think\db\exception\InvalidArgumentException;
|
||||
|
||||
|
||||
class Database extends Command
|
||||
@@ -22,10 +25,12 @@ class Database extends Command
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->addOption('action','a', InputArgument::OPTIONAL, '要做什么操作');
|
||||
$this->addOption('table','t', InputArgument::OPTIONAL, '表名');
|
||||
$this->addOption('domain','ym', InputArgument::OPTIONAL, 'domain');
|
||||
$this->addOption('robot_id','rid', InputArgument::OPTIONAL, 'robot_id');
|
||||
$this->addOption('action','a', InputOption::VALUE_OPTIONAL, '要做什么操作');
|
||||
$this->addOption('table','t', InputOption::VALUE_OPTIONAL, '表名');
|
||||
$this->addOption('domain','ym', InputOption::VALUE_OPTIONAL, 'domain');
|
||||
$this->addOption('connection','c', InputOption::VALUE_OPTIONAL, '数据库链接名,默认mysql','mysql');
|
||||
$this->addOption('dir','d', InputOption::VALUE_OPTIONAL, '缓存目录');
|
||||
$this->addOption('robot_id','rid', InputOption::VALUE_OPTIONAL, 'robot_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,12 +41,32 @@ class Database extends Command
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$action = $input->getOption('action');
|
||||
if($action == 'prototype'){
|
||||
return $this->prototype($input, $output);
|
||||
if(method_exists($this, $action)){
|
||||
return $this->$action($input, $output);
|
||||
}
|
||||
cp('操作不存在:'.$action);
|
||||
return 0;
|
||||
}
|
||||
function optimize_schema(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$table = $input->getOption('table');
|
||||
try {
|
||||
if ($table) {
|
||||
$this->cacheTable($table, $input->getOption('connection'));
|
||||
} else {
|
||||
$dirs = ((array) $input->getOption('dir')) ?: $this->getDefaultDirs();
|
||||
foreach ($dirs as $dir) {
|
||||
$this->cacheModel($dir);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$output->write($e->getMessage());
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$output->write('Succeed!');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
function prototype(InputInterface $input, OutputInterface $output){
|
||||
$table = $input->getOption('table');
|
||||
// 获取表前缀并构建完整表名
|
||||
@@ -132,4 +157,105 @@ class Database extends Command
|
||||
// 默认返回混合类型
|
||||
return 'mixed';
|
||||
}
|
||||
|
||||
|
||||
protected function buildModelSchema(string $class): void
|
||||
{
|
||||
$reflect = new \ReflectionClass($class);
|
||||
if ($reflect->isAbstract() || ! $reflect->isSubclassOf('\think\Model')) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
/** @var \think\Model $model */
|
||||
$model = new $class;
|
||||
$connection = $model->db()->getConnection();
|
||||
if ($connection instanceof PDOConnection) {
|
||||
$table = $model->getTable();
|
||||
//预读字段信息
|
||||
$connection->getSchemaInfo($table, true);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildDataBaseSchema(PDOConnection $connection, array $tables, string $dbName): void
|
||||
{
|
||||
foreach ($tables as $table) {
|
||||
//预读字段信息
|
||||
$connection->getSchemaInfo("{$dbName}.{$table}", true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存表
|
||||
*/
|
||||
private function cacheTable(string $table, ?string $connectionName = null): void
|
||||
{
|
||||
$connection = Db::connect($connectionName);
|
||||
if (! $connection instanceof PDOConnection) {
|
||||
throw new Exception('only PDO connection support schema cache!');
|
||||
}
|
||||
|
||||
if (str_contains($table, '.')) {
|
||||
[$dbName, $table] = explode('.', $table);
|
||||
} else {
|
||||
$dbName = $connection->getConfig('database');
|
||||
}
|
||||
|
||||
if ($table == '*') {
|
||||
$table = $connection->getTables($dbName);
|
||||
}
|
||||
|
||||
$this->buildDataBaseSchema($connection, (array) $table, $dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存模型
|
||||
*/
|
||||
private function cacheModel(?string $dir = null): void
|
||||
{
|
||||
if ($dir) {
|
||||
$modelDir = app_path('model') . $dir . DIRECTORY_SEPARATOR;
|
||||
$namespace = 'app\\' . $dir;
|
||||
} else {
|
||||
$modelDir = app_path('model').DIRECTORY_SEPARATOR;
|
||||
$namespace = 'app';
|
||||
}
|
||||
if (! is_dir($modelDir)) {
|
||||
throw new InvalidArgumentException("{$modelDir} directory does not exist");
|
||||
}
|
||||
|
||||
/** @var \SplFileInfo[] $iterator */
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($modelDir, \RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
|
||||
foreach ($iterator as $fileInfo) {
|
||||
$relativePath = substr($fileInfo->getRealPath(), strlen($modelDir));
|
||||
if (! str_ends_with($relativePath, '.php')) {
|
||||
continue;
|
||||
}
|
||||
// 去除 .php
|
||||
$relativePath = substr($relativePath, 0, -4);
|
||||
|
||||
$class = '\\' . $namespace . '\\model\\' . str_replace('/', '\\', $relativePath);
|
||||
if (! class_exists($class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->buildModelSchema($class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认目录名
|
||||
* @return array<int, ?string>
|
||||
*/
|
||||
private function getDefaultDirs(): array
|
||||
{
|
||||
// 包含默认的模型目录
|
||||
$dirs = [null];
|
||||
return $dirs;
|
||||
}
|
||||
}
|
||||
|
||||
+68
-18
@@ -32,26 +32,76 @@ class User extends Command
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$action = $input->getOption('action','login');
|
||||
if($action == 'login'){
|
||||
$IM = new \support\OpenImSdk\Client([
|
||||
'host' => 'http://127.0.0.1:10002', // OpenIM API地址
|
||||
'secret' => 'n1e5a6s6m7', // OpenIM密钥
|
||||
]);
|
||||
$user_id = $input->getOption('user_id');
|
||||
if(!$user_id){
|
||||
return false;
|
||||
}
|
||||
$user = \support\Jwt::direct($user_id);
|
||||
$imToken = $IM->auth->getUserToken($user['userID'],2);
|
||||
cp('userID:' . $user['id']);
|
||||
cp('nickname:' . $user['nickname']);
|
||||
cp('token:' . $user['token']);
|
||||
cp('imToken:' . $imToken['token']);
|
||||
return 0;
|
||||
$action = $input->getOption('action');
|
||||
if(method_exists($this, $action)){
|
||||
return $this->$action($input, $output);
|
||||
}
|
||||
cp('action not exist');
|
||||
cp('操作不存在:'.$action);
|
||||
return 0;
|
||||
|
||||
}
|
||||
function login(InputInterface $input, OutputInterface $output){
|
||||
// $IM = new \support\OpenImSdk\Client([
|
||||
// 'host' => 'http://127.0.0.1:10002', // OpenIM API地址
|
||||
// 'secret' => 'n1e5a6s6m7', // OpenIM密钥
|
||||
// ]);
|
||||
$user_id = $input->getOption('user_id');
|
||||
if(!$user_id){
|
||||
return false;
|
||||
}
|
||||
$user = \support\Jwt::direct($user_id);
|
||||
//$imToken = $IM->auth->getUserToken($user['userID'],2);
|
||||
cp('userID:' . $user['id']);
|
||||
cp('nickname:' . $user['nickname']);
|
||||
cp('token:' . $user['token']);
|
||||
//cp('imToken:' . $imToken['token']);
|
||||
return 0;
|
||||
}
|
||||
function build_team(InputInterface $input, OutputInterface $output){
|
||||
$list = Db::name('user')->field('id')->order('id','asc')->select();
|
||||
foreach($list as $k=>$user){
|
||||
//team_total
|
||||
$team_user_ids = Db::name('user_team')->where('descendant_id',$user['id'])
|
||||
->where('depth','>',0)
|
||||
->order('depth','ASC')
|
||||
->column('ancestor_id');
|
||||
Db::name('user_extend')->where('user_id',$user['id'])->data([
|
||||
'team_total'=> count($team_user_ids)
|
||||
])->save();
|
||||
cache('team_user_count_'.$user['id'],count($team_user_ids));
|
||||
|
||||
$direct_use_count = Db::name('user')->where('parent_id',$user['id'])->count('id');
|
||||
$vip_user_count = Db::name('user')->whereIn('id',$team_user_ids)->where('role_id','>',0)->count('id');
|
||||
|
||||
Db::name('user_extend')->where('user_id',$user['id'])->data([
|
||||
'direct_total'=> $direct_use_count,
|
||||
'vip_total'=> $vip_user_count
|
||||
])->save();
|
||||
cache('team_direct_total_'.$user['id'],$direct_use_count);
|
||||
cache('team_vip_total_'.$user['id'],$vip_user_count);
|
||||
$this->level_up($user['id'],$vip_user_count);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
protected function level_up($user_id,$count=0){
|
||||
$levels = [
|
||||
0,
|
||||
500,
|
||||
1000,
|
||||
5000,
|
||||
10000,
|
||||
20000,
|
||||
];
|
||||
$level = 0;
|
||||
foreach($levels as $k=>$v){
|
||||
if($count>=$v){
|
||||
$level= $k;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Db::name('user')->where('id',$user_id)->data(['level'=>$level])->save();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user