* ./artisan
#!/usr/bin/env php
<?php
// 应用目录
define('APP_PATH', __DIR__.'/application/');
// 定义缓存目录
define('RUNTIME_PATH', __DIR__.'/runtime/');
// 加载框架引导文件
require dirname(__FILE__).'/thinkphp/console.php';
* application/oa/command/Users.php
<?php
namespace app\oa\command;
use think\console\Command;
use think\Config;
use think\console\Input;
use think\console\Output;
class Users extends Command
{
protected function configure() {
parent::configure();
$this->setName('users')->setDescription("代理用户名");
}
/**
* @param Input $input An InputInterface instance
* @param Output $output An OutputInterface instance
*/
protected function initialize(Input $input, Output $output) {
// 读取数据库配置文件
$filename = ROOT_PATH . 'config/database.php';
// echo $filename.PHP_EOL; die;
Config::load($filename, 'database');
}
private static function getLine() {
$c = <<<EOF
zhuopeijie
gezhiming
guchunmei
huyuesheng
lixuefeng
czwangfang
luweidangcz
yaozhengrong
lilie
yuanyoufeng
EOF;
for ($i = 0, $j = 0; isset($c[$j]); $j++) {
if ($c[$j] === "\n") {
if (isset($c[$j-1]) && $c[$j-1]==="\r") {
yield substr($c, $i, $j-$i-1);
} else {
yield substr($c, $i, $j-$i);
}
$i = $j+1;
}
}
}
protected function execute(Input $input, Output $output) {
// $lines = explode("\n", $content);
foreach (self::getLine() as $line) {
printf("[%s]\n", $line);
}
}
}
配置命令行脚本文件路径
* application/command.php
<?php
return [
'app\oa\command\Sample',
'app\oa\command\ApprovalCommand',
'app\oa\command\Channels',
'app\oa\command\Proxy',
'app\oa\command\District',
'app\oa\command\Users',
'app\oa\command\Reports',
];
在项目根目录
php artisan users
带命令行参数的情况:
<?php
/**
* Created by PhpStorm.
* User: mzh
* Date: 3/12/2020
* Time: 10:20
*/
namespace app\admin\command;
use app\admin\model\SystemUser;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
/**
* php think sys_user
* Class SysUserCommand
* @package app\admin\command
*/
class SysUserCommand extends \think\console\Command
{
protected function configure() {
parent::configure();
$this->setName('sys_user')->setDescription("Add system user: php think sys_user [username] [password]");
$this->addArgument("username", Argument::REQUIRED, "username")
->addArgument("password", Argument::REQUIRED, "password")
->addArgument("wxapp_id", Argument::OPTIONAL, "wxapp_id")
->setHelp("php think sys_user [username] [password] [wxapp_id]");
}
/**
* @param Input $input An InputInterface instance
* @param Output $output An OutputInterface instance
*/
protected function initialize(Input $input, Output $output)
{
// 读取数据库配置文件
$filename = ROOT_PATH . 'config/database.php';
// echo $filename.PHP_EOL; die;
\think\Config::load($filename, 'database');
}
protected function execute(Input $input, Output $output) {
$username = $input->getArgument("username");
$password = $input->getArgument("password");
$wxappId = $input->getArgument("wxapp_id") ?: "10002";
$user = new SystemUser();
$user->setUsername($username);
$user->setPassword($password);
$user->setWxAppId($wxappId);
$user->save();
$output->writeln(json_encode($user));
}
}
php think sys_user admin 123456