有大哥分享个artisan的酷炫代码吗?

打算用命令行生成初始数据,
逻辑:
你确认要初始化数据吗?
yes | no
输入yes会出现个加载条
no则展示个退出问候语

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
chowjiawei
最佳答案

<?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('您取消了命令,将自动为您退出!');
    }
3年前 评论
王大牛 (楼主) 3年前
chowjiawei (作者) 3年前
chowjiawei (作者) 3年前
chowjiawei (作者) 3年前
讨论数量: 10
chowjiawei

我做出来你会成为我的粉丝吗

3年前 评论
王大牛 (楼主) 3年前
chowjiawei

<?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('您取消了命令,将自动为您退出!');
    }
3年前 评论
王大牛 (楼主) 3年前
chowjiawei (作者) 3年前
chowjiawei (作者) 3年前
chowjiawei (作者) 3年前
chowjiawei

@王大牛

$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;
}
3年前 评论
王大牛 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!