新增模块上传

This commit is contained in:
JaguarJack 2020-07-13 17:06:21 +08:00
parent a5416bd0ad
commit 81e7f78419
3 changed files with 170 additions and 14 deletions

View File

@ -23,8 +23,6 @@ class CatchAuth
public function __construct() public function __construct()
{ {
$this->auth = config('catch.auth'); $this->auth = config('catch.auth');
$this->guard = $this->auth['default']['guard'];
} }
/** /**
@ -34,7 +32,7 @@ class CatchAuth
* @param $guard * @param $guard
* @return $this * @return $this
*/ */
public function guard($guard): self public function guard($guard)
{ {
$this->guard = $guard; $this->guard = $guard;

View File

@ -85,8 +85,8 @@ class CatchRequest extends Request
// 设置默认参数 // 设置默认参数
if ($this->needCreatorId) { if ($this->needCreatorId) {
$this->param['creator_id'] = $this->user()->id; $this->param['creator_id'] = $this->user()->id;
$this->post['creator'] = $this->user()->id; $this->post['creator'] = $this->user()->id;
} }
return true; return true;

View File

@ -11,7 +11,6 @@
namespace catcher\command\install; namespace catcher\command\install;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use catcher\exceptions\FailedException;
use catcher\facade\Http; use catcher\facade\Http;
use catcher\library\Compress; use catcher\library\Compress;
use think\console\Command; use think\console\Command;
@ -19,13 +18,12 @@ use think\console\Input;
use think\console\input\Argument; use think\console\input\Argument;
use think\console\input\Option; use think\console\input\Option;
use think\console\Output; use think\console\Output;
use think\facade\Console;
class UploadCatchModuleCommand extends Command class UploadCatchModuleCommand extends Command
{ {
protected $module; protected $module;
protected $moduleZipPath; protected $path;
/** /**
* @var Compress * @var Compress
@ -36,28 +34,188 @@ class UploadCatchModuleCommand extends Command
{ {
$this->setName('upload:module') $this->setName('upload:module')
->addArgument('module', Argument::REQUIRED, 'module name') ->addArgument('module', Argument::REQUIRED, 'module name')
->addOption('path', '-p',Option::VALUE_OPTIONAL, 'path that you need')
->setDescription('install catch module'); ->setDescription('install catch module');
} }
protected function execute(Input $input, Output $output) protected function execute(Input $input, Output $output)
{ {
// 打包项目 try {
// 认证用户
// 上传 $this->module = $this->input->getArgument('module');
$this->path = $this->getCompressPath($this->input->getOption('path'));
$moduleInfo = $this->checking();
// 打包项目
$moduleZip = $this->compressModule();
// 认证用户
$name = $this->output->ask($this->input, 'please input your name');
$password = $this->output->ask($this->input, 'please input your password');
$token = $this->authenticate($name, $password);
// 上传
$this->upload($token, $moduleZip);
$this->output->info('upload successfully!');
}catch (\Throwable $e) {
$this->error($e->getMessage(). ': Error happens at ' .$e->getFile() . ' '. $e->getLine() . '行');
}
} }
/**
* 检查模块文件
*
* @time 2020年07月13日
* @return mixed
*/
public function checking()
{
if (!file_exists($this->path . 'module.json')) {
$this->error('there is no module.json file');
}
if (!file_exists($this->path . 'route.php')) {
$this->error('there is no route.php file');
}
$module = \json_decode(file_get_contents($this->path . 'module.json'), true);
if (!isset($module['name']) && !$module['name']) {
$this->error('module.json not set name');
}
if (!isset($module['version']) && !$module['name']) {
$this->error('module.json not set version');
}
if (!isset($module['services']) && empty($module['services'])) {
$this->error('module.json not set services');
}
$services = $module['services'];
foreach ($services as $service) {
$s = explode('\\', $service);
$serviceName = array_pop($s);
if (!file_exists($this->path. $serviceName . '.php')) {
$this->error("[$serviceName] Service not found");
}
}
$this->output->info('checking has no problem');
return $module;
}
/**
* 认证用户
*
* @time 2020年07月13日
* @param $name
* @param $password
* @return mixed
*/
protected function authenticate($name, $password)
{
$response = Http::form([
'username' => $name,
'password' => $password,
])->post($this->authenticateAddress());
$data = $response->json();
if ($data['code'] == 10000) {
return $data['data'];
}
$this->error('login failed');
}
/**
* 上传地址
*
* @time 2020年07月13日
* @return mixed
*/
protected function uploadAddress() protected function uploadAddress()
{ {
return env('API_URL') . '/upload/module';
} }
protected function upload() protected function authenticateAddress()
{ {
return env('API_URL') . '/developer/authenticate';
} }
/**
* 上传
*
* @time 2020年07月13日
* @param $token
* @param $zip
* @return bool
*/
protected function upload($token, $zip)
{
return Http::token($token)
->attach('module', fopen($zip, 'r+'), pathinfo($zip, PATHINFO_FILENAME))
->post($this->uploadAddress())->ok();
}
/**
* 打包模块
*
* @time 2020年07月13日
* @return bool
*/
protected function compressModule() protected function compressModule()
{ {
$composerZip = $this->compressZipPath() . $this->module . '_' . time() . '.zip';
(new Compress())->dirToZip($this->path, $composerZip);
$this->output->info('compress module ' . $this->module . ' successfully');
return $composerZip;
} }
/**
* 获取打包的path
*
* @time 2020年07月13日
* @param $path
* @return string
*/
protected function getCompressPath($path)
{
if ($path) {
return root_path($path) . $this->module. DIRECTORY_SEPARATOR;
}
return CatchAdmin::moduleSeedsDirectory($this->module);
}
/**
* zip 打包路径
*
* @time 2020年07月13日
* @return string
*/
protected function compressZipPath()
{
return CatchAdmin::makeDirectory(runtime_path('catch'.DIRECTORY_SEPARATOR.'compress'));
}
/**
* 输出错误信息
*
* @time 2020年07月13日
* @param string $message
* @return void
*/
protected function error(string $message)
{
exit($this->output->error($message));
}
} }