后端代码生成器
访问地址 域名 / admin/php?method=&class=level&form=user_level
<?php
namespace app\admin\controller;
use think\captcha\facade\Captcha;
use app\BaseController;
use think\Validate; // 验证器
use think\facade\Db; // 数据库的链接
use think\facade\Request;
class Php extends BaseController
{
// 代码生产器
// --- method 控制器
// --- class 类名字
// --- form 数据库表昵称
// --- ceshi.beijingzhiyulegs.cn?method=&class=index&form=表名字
public function php()
{
$post = input();
if(!empty(@$post['method'])){
$directory = app()->getAppPath()."controller/".@$post['method']."/";
}else{
$directory = app()->getAppPath()."controller/";
}
$class = ucfirst($post['class']);
$filename = $class.".php";
$fullpath = $directory . $filename;
// 确保目录存在
if (!file_exists($directory)) {
mkdir($directory, 0755, true);
}
// 创建文件
$file = fopen($fullpath, "w"); // "w" 表示写入模式,会覆盖已有文件
// 写入内容
$content = "<?php"."\n\n";
$nbsp = " ";
$content .= "// 当前代码由Api生成\n\n";
$content_route = "\n// ==========路由管理".$class."\n";
if(!empty(@$post['method'])){
// 控制器存在
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['method']."/".$post['class']."/getList', '".$post['method'].".".$class."/getList', 'get');\n";
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['method']."/".$post['class']."/doEdit', '".$post['method'].".".$class."/doEdit', 'post');\n";
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['method']."/".$post['class']."/doDelete', '".$post['method'].".".$class."/doDelete', 'post');\n";
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['method']."/".$post['class']."/doSwitch', '".$post['method'].".".$class."/doSwitch', 'post');\n";
$name = "namespace app\admin\controller".'\\'.$post['method'].";\n\n";
}else{
// 控制器不在
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['class']."/getList', '".$class."/getList', 'get');\n";
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['class']."/doEdit', '".$class."/doEdit', 'post');\n";
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['class']."/doDelete','".$class."/doDelete', 'post');\n";
$content_route .=$nbsp.$nbsp. "Route::rule('".$post['class']."/doSwitch','".$class."/doSwitch', 'post');\n";
$name = "namespace app\admin\controller;\n\n";
}
$filePath = app()->getAppPath().'route/route.php'; // 文件路径
$lineNumber = 20; // 要插入文本的行号
// 读取文件内容到数组
$lines = file($filePath, FILE_IGNORE_NEW_LINES);
// 插入文本到指定行
array_splice($lines, $lineNumber-1, 0, $content_route);
// 将修改后的数组内容写回文件
file_put_contents($filePath, implode(PHP_EOL, $lines));
$content .= "// ==========路由管理\n";
$content .= $name;
$content .= "use app\BaseController;\n";
$content .= "use think\Validate;\n";
$content .= "use think\\facade\Db;\n";
$content .= "class ".$post['class']." extends BaseController\n{\n";
$content .= $nbsp."// 获取数据列表\n";
$content .= $nbsp."public function getlist()"."\n".$nbsp."{\n";
$content .= $nbsp.$nbsp."$"."post = input();\n";
$content .= $nbsp.$nbsp."$"."page = isset("."$"."post['pageNo'])? "."$" ."post['pageNo'] : 1;\n";
$content .= $nbsp.$nbsp."$"."limit = isset("."$"."post['pageSize'])? "."$"."post['pageSize'] : 10;\n";
$content .= $nbsp.$nbsp."$"."where = [];\n\n";
$content .= $nbsp.$nbsp."if (isset("."$"."post['title']) && "."$"."post['title']!= '') {\n";
$content .= $nbsp.$nbsp.$nbsp."$"."where[] = ['name', 'like', '%'."."$"."post['title']. '%'];\n";
$content .= $nbsp.$nbsp."}\n\n";
$content .= $nbsp.$nbsp."// 获取数据\n";
$content .= $nbsp.$nbsp."$"."data = Db::name('".$post['form']."')->where("."$"."where)->paginate(['list_rows' => "."$"."limit,'page' => "."$"."page])->toArray();\n\n";
$content .= $nbsp.$nbsp."// 返回数据\n";
$content .= $nbsp.$nbsp."$"."data['list'] = $"."data['data'];\n";
$content .= $nbsp.$nbsp."unset("."$"."data['data']);\n";
$content .= $nbsp.$nbsp."return array('code' => 200,'data' => "."$"."data);\n";
$content .= $nbsp."}\n\n\n";
// 第一个方法完成
$content .= $nbsp."// 修改和新增数据\n";
$content .= $nbsp."public function doEdit()"."\n".$nbsp."{\n";
$content .= $nbsp.$nbsp."$"."post = input();\n";
$content .= $nbsp.$nbsp."$"."id = isset("."$"."post['id'])? "."$"."post['id'] : 0;\n";
$content .= $nbsp.$nbsp."$"."validate = new Validate;\n\n";
$content .= $nbsp.$nbsp."// 验证字段 \n";
$content .= $nbsp.$nbsp."$"."validate->rule([\n";
$content .= $nbsp.$nbsp.$nbsp."//"."'name|角色名称' =>'require|max:20',\n";
$content .= $nbsp.$nbsp."]);\n";
$content .= $nbsp.$nbsp."$"."validate->message([\n";
$content .= $nbsp.$nbsp.$nbsp."// 'name.require' => '角色名称不能为空',\n";
$content .= $nbsp.$nbsp.$nbsp."// 'name.max' => '角色名称不能超过20个字符',\n";
$content .= $nbsp.$nbsp."]);\n";
$content .= $nbsp.$nbsp."if (!"."$"."validate->check("."$"."post)) {\n";
$content .= $nbsp.$nbsp.$nbsp."return array('code' => 500, 'msg' => "."$"."validate->getError());\n";
$content .= $nbsp.$nbsp."}\n";
$content .= $nbsp.$nbsp."$"."data = [\n";
$content .= $nbsp.$nbsp.$nbsp."'字段1' => "."$"."post['值1'],\n";
$content .= $nbsp.$nbsp."];\n\n";
$content .= $nbsp.$nbsp."if ("."$"."id) {\n";
$content .= $nbsp.$nbsp.$nbsp."$"."res = Db::name('".$post['form']."')->where('id', "."$"."id)->update("."$"."data);\n";
$content .= $nbsp.$nbsp."} else {\n";
$content .= $nbsp.$nbsp.$nbsp."$"."res = Db::name('".$post['form']."')->insert("."$"."data);\n";
$content .= $nbsp.$nbsp."}\n";
$content .= $nbsp.$nbsp."if ("."$"."res) {\n";
$content .= $nbsp.$nbsp.$nbsp."return array('code' => 200,'msg' => '操作成功');\n";
$content .= $nbsp.$nbsp."} else {\n";
$content .= $nbsp.$nbsp.$nbsp."return array('code' => 500,'msg' => '操作失败');\n";
$content .= $nbsp.$nbsp."}\n";
$content .= $nbsp."}\n\n\n";
$content .= $nbsp."// 删除数据\n";
$content .= $nbsp."public function doDelete()"."\n".$nbsp."{\n";
$content .= $nbsp.$nbsp."$"."post = input();\n";
$content .= $nbsp.$nbsp."$"."id = isset("."$"."post['ids'])? "."$"."post['ids'] : 0;\n";
$content .= $nbsp.$nbsp."if ("."$"."id) {\n";
$content .= $nbsp.$nbsp.$nbsp."$"."res = Db::name('".$post['form']."')->where('id','in', "."$"."id)->delete();\n";
$content .= $nbsp.$nbsp.$nbsp."if ("."$"."res) {\n";
$content .= $nbsp.$nbsp.$nbsp.$nbsp."return array('code' => 200,'msg' => '删除成功');\n";
$content .= $nbsp.$nbsp.$nbsp."} else {\n";
$content .= $nbsp.$nbsp.$nbsp.$nbsp."return array('code' => 500,'msg' => '删除失败');\n";
$content .= $nbsp.$nbsp.$nbsp."}\n";
$content .= $nbsp.$nbsp."} else {\n";
$content .= $nbsp.$nbsp.$nbsp."return array('code' => 500,'msg' => '删除失败');\n";
$content .= $nbsp.$nbsp."}\n";
$content .= $nbsp."}\n\n\n";
$content .= $nbsp."// 开关按钮\n";
$content .= $nbsp."public function doSwitch()"."\n".$nbsp."{\n";
$content .= $nbsp.$nbsp."$"."post = input();\n";
$content .= $nbsp.$nbsp."$"."sql[] = ['id', '=', "."$"."post['id']];\n";
$content .= $nbsp.$nbsp."$"."res = Db::name('".$post['form']."')->where("."$"."sql)->update(["."$"."post['type'] => "."$"."post['value']]);\n";
$content .= $nbsp.$nbsp."if ("."$"."res) {\n";
$content .= $nbsp.$nbsp.$nbsp."return array('code' => 200,'msg' => '操作成功');\n";
$content .= $nbsp.$nbsp."} else {\n";
$content .= $nbsp.$nbsp.$nbsp."return array('code' => 500,'msg' => '操作失败');\n";
$content .= $nbsp.$nbsp."}\n";
$content .= $nbsp."}\n";
$content .= "}";
fwrite($file, $content);
// 关闭文件资源
fclose($file);
echo "文件已创建并保存到: " . $fullpath;
}
}
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果