<?php
declare (strict_types = 1);
namespace app\common\util;
use think\facade\Request;
use think\facade\Db;
class Crud
{
//验证
protected static $check = ['admin_admin','admin_admin_log','admin_admin_permission','admin_admin_role','admin_permission','admin_photo','admin_role','admin_role_permission'];
//参数
protected static $data;
// 获取所有表
public static function getTable()
{
foreach (Db::getTables() as $k =>$v) {
$list[] = ['name'=>$v];
}
return ['code'=>0,'data'=>$list];
}
// 添加
public static function goAdd()
{
if (Request::isPost()){
$data = Request::post();
//数据验证
if (!preg_match('/^[a-z]+_[a-z]+$/i',$data['name'])) return ['code' => 201, 'msg' => '表名格式不正确'];
try {
Db::execute('CREATE TABLE '.config('database.connections.mysql.prefix').$data['name'].'(
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT "id",
`create_time` timestamp NULL DEFAULT NULL COMMENT "创建时间",
`update_time` timestamp NULL DEFAULT NULL COMMENT "更新时间",
`delete_time` timestamp NULL DEFAULT NULL COMMENT "删除时间",
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 COMMENT="'.$data['desc'].'";
');
}catch (\Exception $e){
return ['code'=>201,'msg'=>$e->getMessage()];
}
}
}
// 获取Crud变量
public static function getCrud($name)
{
return [
'data' => Db::getFields($name),
'permissions' => get_tree((new \app\common\model\AdminPermission)->order('sort','asc')->select()->toArray()),
'desc' => Db::query('SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_NAME = "' . $name . '"')[0]['TABLE_COMMENT']
];
}
// 获取Crud变量
public static function goRemove($name)
{
if (Request::isPost()){
$type = Request::param('type');
//验证
if (in_array(substr($name,strlen(config('database.connections.mysql.prefix'))),self::$check)) return ['code'=>201,'msg'=>'默认字段禁止操作'];
Db::query('drop table '.$name);
if(Request::param('type')){
try {
$data['table'] = substr($name,strlen(config('database.connections.mysql.prefix')));
//表名转驼峰
$data['table_hump'] = underline_hump($data['table']);
//左
$data['left'] = strstr($data['table'], '_',true);
//右
$data['right'] = substr($data['table'],strlen($data['left'])+1);
//右转驼峰
$data['right_hump'] = underline_hump($data['right']);
$commom = root_path().'app'.DS.'common'.DS;
// 控制器
$controller = app_path().'controller'.DS.$data['left'].DS.$data['right_hump'].'.php';
if (file_exists($controller)) unlink($controller);
// 模型
$model = $commom.DS.'model'.DS.$data['table_hump'].'.php';
if (file_exists($model)) unlink($model);
// 服务
$model = $commom.DS.'service'.DS.$data['table_hump'].'.php';
if (file_exists($model)) unlink($model);
// 验证器
$model = $commom.DS.'validate'.DS.$data['table_hump'].'.php';
if (file_exists($model)) unlink($model);
//删除视图目录
$view = root_path().'view'.DS.'admin'.DS.$data['left'].DS.$data['right'];
if (file_exists($view)) delete_dir($view);
//删除菜单
(new \app\common\model\AdminPermission)->where('href', 'like', "%" . $data['left'].'.'.$data['right'] . "%")->delete();rm();
}catch (\Exception $e){
return ['msg'=>'删除失败','code'=>'201'];
}
}
}
}
// CRUD生成
public static function goCrud($name)
{
if (Request::isPost()){
//参数
self::$data = Request::post();
//去除前缀表名
self::$data['table'] = substr($name,strlen(config('database.connections.mysql.prefix')));
//验证字段
if (in_array(self::$data['table'],self::$check)) return ['code'=>201,'msg'=>'默认字段禁止操作'];
//表名转驼峰
self::$data['table_hump'] = underline_hump(self::$data['table']);
//左
self::$data['left'] = strstr(self::$data['table'], '_',true);
//右
self::$data['right'] = substr(self::$data['table'],strlen(self::$data['left'])+1);
//右转驼峰
self::$data['right_hump'] = underline_hump(self::$data['right']);
//构造选中参数补全
for ($i=0; $i < count(self::$data['name']); $i++) {
self::getNull($i);self::getList($i);self::getSearch($i);self::getForm($i);self::getNull($i);
if (self::$data['name'][$i] == 'delete_time'){
self::$data['model_del'] = ' protected $deleteTime = "delete_time";';
}else{
self::$data['model_del'] = ' protected $deleteTime = false;';
}
}
//路径
$tpl = root_path().'extend'.DS.'tpl'.DS;
$commom = root_path().'app'.DS.'common'.DS;
$view = root_path().'view'.DS.'admin'.DS.self::$data['left'].DS.self::$data['right'].DS;
$crud = [
app_path().'controller'.DS.self::$data['left'].DS.self::$data['right_hump'].'.php' => self::getController($tpl.'controller.tpl'),
$commom.'model'.DS.self::$data['table_hump'].'.php' => self::getModel($tpl.'model.tpl'),
$commom.'service'.DS.self::$data['table_hump'].'.php' => self::getService($tpl.'service.tpl'),
$commom.'validate'.DS.self::$data['table_hump'].'.php' => self::getValidate($tpl.'validate.tpl'),
$view.'index.html' => self::getIndex($tpl.'index.tpl'),
$view.'add.html' => self::getAdd($tpl.'add.tpl'),
$view.'edit.html' => self::getAdd($tpl.'edit.tpl'),
$view.'recycle.html' => self::getRecycle($tpl.'recycle.tpl'),
];
foreach ($crud as $k=>$v) {
@mkdir(dirname($k), 0755, true);
@file_put_contents($k, $v);
}
//添加菜单
if(self::$data['menu']!='') (new \app\common\service\AdminPermission)->goMenu(self::$data);
}
}
// 控制器
public static function getController($tpl)
{
return str_replace(['{{$left}}','{{$right_hump}}','{{$table_hump}}'],
[self::$data['left'],self::$data['right_hump'],self::$data['table_hump']],file_get_contents($tpl));
}
// 模型
public static function getModel($tpl)
{
return str_replace(['{{$table_hump}}','{{$model_search}}','{{$model_del}}'],
[self::$data['table_hump'],implode("",self::$data['model_search']??[]),self::$data['model_del']??''],file_get_contents($tpl));
}
// 服务
public static function getService($tpl)
{
return str_replace(['{{$table_hump}}','{{$model_search}}'],
[self::$data['table_hump'],implode("",self::$data['model_search']??[])],file_get_contents($tpl));
}
// 验证
public static function getValidate($tpl)
{
return str_replace(['{{$table_hump}}','{{$validate_rule}}','{{$validate_msg}}','{{$validate_scene}}'],
[self::$data['table_hump