讨论数量:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class TestCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test';
/**
* The console command description.
*
* @var string
*/
protected $description = '测试命令';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if ($this->confirm('智能机器人将为您初始化系统数据,您确认要进行这个操作吗?')) {
$this->output->progressStart(1);
//初始化的逻辑
//初始化的逻辑
$this->output->progressFinish();
$this->output->title('');
$this->output->success('系统数据为您初始化成功,将自动为您退出!');
return ;
}
$this->output->success('您取消了命令,将自动为您退出!');
}
}
由于网络等不稳定因素造成的,想要内部重试代码 可以这样子写
public function handle()
{
if ($this->confirm('智能机器人将为您初始化系统数据,您确认要进行这个操作吗?')) {
$this->output->progressStart(1);
$errorTimes = 0;
$tryMaxNum = 3;
while ($errorTimes < $tryMaxNum) {
try {
//初始化的逻辑start
break;
} catch (\Exception $e) {
$errorTimes++;
$this->output->error('命令内部错误,我们将检测内部错误,自动为您再尝试第'.$errorTimes.'次');
}
}
if ($errorTimes === $tryMaxNum) {
$this->output->error('命令内部错误,且系统无法为您修复,请您允许系统健康检测命令,修复系统错误后再尝试');
return ;
}
$this->output->progressFinish();
$this->output->title('');
$this->output->success('系统数据为您初始化成功,将自动为您退出!');
return ;
}
$this->output->success('您取消了命令,将自动为您退出!');
}
@王大牛
$this->output->progressStart(10);
然后
$this->output->progressAdvance(3);
sleep(随机值);
$this->output->progressAdvance(2);
sleep(随机值);
$this->output->progressAdvance(1);
sleep(随机值);
$this->output->progressAdvance(2);
sleep(随机值);
$this->output->progressAdvance(2);
$this->output->progressFinish();
其中 progressAdvance
后面的值你可以使用循环,弄成每次随机一点点
大概是下面这种代码
$times=10;
$this->output->progressStart($times);
for($i=0;$i<5;$i++){
$subTime=$times-rand(0,2);
$this->output->progressAdvance($subTime);
sleep(rand(0,1));
$times-=$subTime;
}
由于网络等不稳定因素造成的,想要内部重试代码 可以这样子写