💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
>[info] 终端命令直接继承于 ThinkPHP5 官方,所以该工具不仅仅支持 tpadmin 提供的独有功能,还有 ThinkPHP5 自带的命令行功能,其他 ThinkPHP5 的命令行功能请参考 [ThinkPHP5 - 命令行](http://www.kancloud.cn/manual/thinkphp5/122951) ## 开始使用 1. 在默认配置文件里(项目根目录下的 `generate.php` 文件里)填写需要的配置信息,格式请参考示例文件 ![](https://box.kancloud.cn/124ae33528e657e77fb3695e451c539b_844x1003.png) ![](https://box.kancloud.cn/7eb544c683c01e37ba996f0f7985ed52_1072x1011.png) 2. 打开终端,切换目录到项目根目录 3. 执行 `php tpadmin -l` ,查看所有命令,找到代码生成器的方法为 `generate` ![](https://box.kancloud.cn/1e0ca234e3f11aa3b3e001dc6e3faed6_1120x568.png) 4. 执行 `php tpadmin generate -h` ,查看代码自动生成器的使用方法(全英文,可以复制说明翻译) ![](https://box.kancloud.cn/a97f122907aff6bb379aef96db7b2594_1409x682.png) 5. 执行 `php tpadmin generate` ,会使用默认配置(即配置文件默认为 `generate.php`,生成文件为 `all`)生成文件 ![](https://box.kancloud.cn/6d2bf84d0793863ca22a4efbaa0b374d_479x101.png) 根目录下的 `generate.php` ``` <?php return [ 'module' => 'admin', 'controller' => 'Test', 'title' => '测试', 'form' => [ [ 'title' => '字段一', 'name' => 'field1', 'type' => 'radio', 'option' => '1:值一#2:值二#3:值三', 'default' => '默认值', 'sort' => false, 'search' => true, 'search_type' => 'select', 'require' => true, 'validate' => [ 'datatype' => '*', 'nullmsg' => '为空信息', 'errormsg' => '错误信息', ], ], [ 'title' => '字段一', 'name' => 'field2', 'type' => 'date', 'option' => '1:值一#2:值二#3:值三', 'default' => '2', 'sort' => true, 'search' => true, 'search_type' => 'text', 'require' => true, 'validate' => [ 'datatype' => 'n', 'nullmsg' => '为空信息', 'errormsg' => '错误信息', ], ], [ 'title' => '状态', 'name' => 'status', 'type' => 'radio', 'option' => '1:启用#0:禁用', 'default' => '0', 'sort' => false, 'search' => false, 'search_type' => 'select', 'require' => true, 'validate' => [ 'datatype' => 'n', 'nullmsg' => '为空信息', 'errormsg' => '错误信息', ], ], ], 'create_table' => true, 'create_table_force' => false, 'table_name' => '', 'table_engine' => 'InnoDB', 'field' => [ [ 'name' => 'field1', 'type' => 'varchar(25)', 'default' => 123, 'not_null' => true, 'key' => true, 'comment' => '', 'extra' => '', // 扩展属性,例如AUTO_INCREMENT ], [ 'name' => 'field2', 'type' => 'varchar(255)', 'default' => 123, 'allow_null' => true, 'key' => true, 'comment' => '', 'extra' => '', // 扩展属性,例如AUTO_INCREMENT ], ], 'menu' => ['add', 'forbid', 'resume', 'delete', 'recyclebin'], 'auto_timestamp' => true, 'model' => false, 'validate' => false, ]; ``` >[info] 以下代码、日志全部是自动生成,没有做任何修改,包括缩进,严格的缩进非常方便二次编辑和查阅 ## 生成文件 ### controller/Test.php ``` <?php namespace app\admin\controller; \think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT); use app\admin\Controller; class Test extends Controller { use \app\admin\traits\controller\Controller; // 方法黑名单 protected static $blacklist = []; protected function filter(&$map) { if ($this->request->param("field2")) { $map['field2'] = ["like", "%" . $this->request->param("field2") . "%"]; } } } ``` ### view/test/index.html ``` {extend name="template/base" /} {block name="content"} <div class="page-container"> {include file="form" /} <div class="cl pd-5 bg-1 bk-gray"> <span class="l"> {tp:menu menu="add,forbid,resume,delete,recyclebin" /} </span> <span class="r pt-5 pr-5"> 共有数据 :<strong>{$count ?? '0'}</strong> 条 </span> </div> <table class="table table-border table-bordered table-hover table-bg mt-20"> <thead> <tr class="text-c"> {include file="th" /} <th width="70">操作</th> </tr> </thead> <tbody> {volist name="list" id="vo"} <tr class="text-c"> {include file="td" /} <td class="f-14"> {$vo.status|show_status=$vo.id} {tp:menu menu='sedit' /} {tp:menu menu='sdelete' /} </td> </tr> {/volist} </tbody> </table> <div class="page-bootstrap">{$page ?? ''}</div> </div> {/block} {block name="script"} <script> $(function () { $("[name='field1']").find("[value='{$Request.param.field1}']").attr("selected", true); }) </script> {/block} ``` ### view/test/recyclebin.html ``` {extend name="template/recyclebin" /} {block name="script"} <script> $(function () { $("[name='field1']").find("[value='{$Request.param.field1}']").attr("selected", true); }) </script> {/block} ``` ### view/test/th.html ``` <th width="25"><input type="checkbox"></th> <th width="">字段一</th> <th width="">{:sort_by('字段一','field2')}</th> <th width="">状态</th> ``` ### view/test/td.html ``` <td><input type="checkbox" name="id[]" value="{$vo.id}"></td> <td>{$vo.field1}</td> <td>{$vo.field2|high_light=$Request.param.field2}</td> <td>{$vo.status|get_status}</td> ``` ### view/test/form.html ``` <form class="mb-20" method="get" action="{:\\think\\Url::build($Request.action)}"> <div class="select-box" style="width:250px"> <select name="field1" class="select"> <option value="1">值一</option> <option value="2">值二</option> <option value="3">值三</option> </select> </div> <input type="text" class="input-text" style="width:250px" placeholder="字段一" name="field2" value="{$Request.param.field2}" > <button type="submit" class="btn btn-success"><i class="Hui-iconfont">&#xe665;</i> 搜索</button> </form> ``` ### view/test/edit.html ``` {extend name="template/base" /} {block name="content"} <div class="page-container"> <form class="form form-horizontal" id="form" method="post" action="{:\\think\\Request::instance()->baseUrl()}"> <input type="hidden" name="id" value="{:isset($vo.id)?$vo.id:''}"> <div class="row cl"> <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>字段一:</label> <div class="formControls col-xs-6 col-sm-6 skin-minimal"> <div class="radio-box"> <input type="radio" name="field1" id="field1-1" value="1" datatype="*" nullmsg="为空信息" errormsg="错误信息"> <label for="field1-1">值一</label> </div> <div class="radio-box"> <input type="radio" name="field1" id="field1-2" value="2" datatype="*" nullmsg="为空信息" errormsg="错误信息"> <label for="field1-2">值二</label> </div> <div class="radio-box"> <input type="radio" name="field1" id="field1-3" value="3" datatype="*" nullmsg="为空信息" errormsg="错误信息"> <label for="field1-3">值三</label> </div> </div> <div class="col-xs-3 col-sm-3"></div> </div> <div class="row cl"> <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>字段一:</label> <div class="formControls col-xs-6 col-sm-6"> <input type="text" class="input-text Wdate" placeholder="字段一" name="field2" value="{$vo.field2 ?? '2'}" {literal} onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" {/literal} > </div> <div class="col-xs-3 col-sm-3"></div> </div> <div class="row cl"> <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>状态:</label> <div class="formControls col-xs-6 col-sm-6 skin-minimal"> <div class="radio-box"> <input type="radio" name="status" id="status-1" value="1" datatype="n" nullmsg="为空信息" errormsg="错误信息"> <label for="status-1">启用</label> </div> <div class="radio-box"> <input type="radio" name="status" id="status-0" value="0" datatype="n" nullmsg="为空信息" errormsg="错误信息"> <label for="status-0">禁用</label> </div> </div> <div class="col-xs-3 col-sm-3"></div> </div> <div class="row cl"> <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3"> <button type="submit" class="btn btn-primary radius">&nbsp;&nbsp;提交&nbsp;&nbsp;</button> <button type="button" class="btn btn-default radius ml-20" onClick="layer_close();">&nbsp;&nbsp;取消&nbsp;&nbsp;</button> </div> </div> </form> </div> {/block} {block name="script"} <script type="text/javascript" src="__LIB__/Validform/5.3.2/Validform.min.js"></script> <script type="text/javascript" src="__LIB__/My97DatePicker/WdatePicker.js"></script> <script> $(function () { $("[name='field1'][value='{$vo.field1 ?? '默认值'}']").attr("checked", true); $("[name='status'][value='{$vo.status ?? '0'}']").attr("checked", true); $('.skin-minimal input').iCheck({ checkboxClass: 'icheckbox-blue', radioClass: 'iradio-blue', increaseArea: '20%' }); $("#form").Validform({ tiptype: 2, ajaxPost: true, showAllError: true, callback: function (ret){ ajax_progress(ret); } }); }) </script> {/block} ``` ### view/test/config.php ``` <?php return array ( 'module' => 'admin', 'menu' => array ( 0 => 'add', 1 => 'forbid', 2 => 'resume', 3 => 'delete', 4 => 'recyclebin', ), 'create_config' => true, 'controller' => 'Test', 'title' => '测试', 'form' => array ( 0 => array ( 'title' => '字段一', 'name' => 'field1', 'type' => 'radio', 'option' => '1:值一#2:值二#3:值三', 'default' => '默认值', 'sort' => false, 'search' => true, 'search_type' => 'select', 'require' => true, 'validate' => array ( 'datatype' => '*', 'nullmsg' => '为空信息', 'errormsg' => '错误信息', ), ), 1 => array ( 'title' => '字段一', 'name' => 'field2', 'type' => 'date', 'option' => '1:值一#2:值二#3:值三', 'default' => '2', 'sort' => true, 'search' => true, 'search_type' => 'text', 'require' => true, 'validate' => array ( 'datatype' => 'n', 'nullmsg' => '为空信息', 'errormsg' => '错误信息', ), ), 2 => array ( 'title' => '状态', 'name' => 'status', 'type' => 'radio', 'option' => '1:启用#0:禁用', 'default' => '0', 'sort' => false, 'search' => false, 'search_type' => 'select', 'require' => true, 'validate' => array ( 'datatype' => 'n', 'nullmsg' => '为空信息', 'errormsg' => '错误信息', ), ), ), 'create_table' => true, 'create_table_force' => false, 'table_name' => '', 'table_engine' => 'InnoDB', 'field' => array ( 0 => array ( 'name' => 'field1', 'type' => 'varchar(25)', 'default' => 123, 'not_null' => true, 'key' => true, 'comment' => '', 'extra' => '', ), 1 => array ( 'name' => 'field2', 'type' => 'varchar(255)', 'default' => 123, 'allow_null' => true, 'key' => true, 'comment' => '', 'extra' => '', ), ), 'auto_timestamp' => true, 'model' => false, 'validate' => false, ); ``` ## 生成数据表 下面是 Linux 版 Navicat 截图 ![](https://box.kancloud.cn/06eb95e0c9e7954fb66cf6c453c63f4c_590x297.png) 下面是从 Linux 版 Navicat 中拷贝出的建表语句 ``` CREATE TABLE `tp_test` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '测试主键', `field1` varchar(25) NOT NULL DEFAULT '123', `field2` varchar(255) DEFAULT '123', `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态,1-正常 | 0-禁用', `isdelete` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '删除状态,1-删除 | 0-正常', `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', `update_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间', PRIMARY KEY (`id`), KEY `field1` (`field1`), KEY `field2` (`field2`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试'; ``` ## 生成日志 `runtime/log` 目录下的日志原文: >[info] 在当天的日志中搜索 build_sql 即可搜索到,日志原文中的标记是 BUILD_SQL ``` [ 2016-11-10T21:29:10+08:00 ] 0.0.0.0 0.0.0.0 CLI [ log ] cmd:tpadmin generate [运行时间:0.044912s][吞吐率:22.27req/s] [内存消耗:3,114.72kb] [文件加载:60] [ info ] [ DB ] INIT mysql [ info ] [ LOG ] INIT File [ sql ] [ DB ] CONNECT:[ UseTime:0.001210s ] mysql:dbname=tpadmin;host=127.0.0.1;charset=utf8 [ sql ] [ SQL ] SHOW TABLES LIKE 'tp_test' [ RunTime:0.000311s ] [ sql ] [ SQL ] DROP TABLE IF EXISTS `tp_test` [ RunTime:0.000150s ] [ sql ] [ SQL ] CREATE TABLE `tp_test` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '测试主键', `field1` varchar(25) NOT NULL DEFAULT '123', `field2` varchar(255) DEFAULT '123', `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态,1-正常 | 0-禁用', `isdelete` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '删除状态,1-删除 | 0-正常', `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', `update_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间', PRIMARY KEY (`id`), KEY `field1` (`field1`), KEY `field2` (`field2`) )ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '测试' [ RunTime:0.023072s ] [ sql ] [ SQL ] DROP TABLE IF EXISTS `tp_test_build_bak` [ RunTime:0.000250s ] --------------------------------------------------------------- ```