drag-create-api/app/admin/controller/Gen/GenVue/GenDto.php
2023-06-25 08:51:24 +08:00

224 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller\Gen\GenVue;
use app\admin\controller\Gen\Gen;
/**
* 前端生成类
*/
class GenDto
{
/**
* 项目根目录路径
* @var string
*/
protected $root;
/**
* 字段信息
* @var array
*/
protected $fields = [];
/**
* 生成信息
* @var array
*/
protected $table = [];
/**
* 实体类名
* @var string 例User
*/
protected $class_name;
/**
* 模块名
* @var string 例User
*/
protected $module_name;
/**
* 业务名 例user
* @var string
*/
protected $business_name;
/**
* 方法名
* @var string 例:用户
*/
protected $function_name;
/**
* vue基础生成路径
* @var string
*/
protected $genPath;
/**
* 模型层模板读取路径
* @var string
*/
protected $vue_index_temp_path;
/**
* 模型层模块生成路径
* @var string
*/
protected $model_module_path;
/**
* 模型层生成路径
* @var string
*/
protected $model_path;
/**
* 业务字段
* @var array [ 'user_name' => '用户名称',...]
*/
protected $business_fields = [];
/**
* 新增允许字段模板字符
* @var string
*/
protected $add_fields_temp = "";
/**
* 是否导入
* @var bool
*/
protected $is_import = false;
/**
* 是否导出
* @var bool
*/
protected $is_export = false;
/**
* 图片字段数组
* @var array
*/
protected $img_fields = [];
/**
* 构造器
*
* @param array $fields 字段信息
* @param array $table 生成信息
*/
public function __construct(array $fields, array $table)
{
$this->root = base_path();
$this->fields = $fields;
$this->table = $table;
$this->class_name = $this->table['className'];
$this->module_name = $this->table['moduleName'];
$this->business_name = $this->table['businessName'];
$this->function_name = $this->table['functionName'];
$this->genPath = $this->table['genPath'];
$this->vue_index_temp_path = self::initFilePath("{$this->root}resources/view/admin/model.tpl");
$this->model_module_path = self::initFilePath("{$this->root}common/model/{$this->module_name}");
$this->model_path = self::initFilePath("{$this->model_module_path}/{$this->class_name}.php");
$this->mkdirModelModule();
$this->buildBusinessFields();
$this->buildAddFieldsTemp();
$this->initImportExportStatus();
$this->initImgFields();
}
/**
* 初始化图片字段变量
*/
private function initImgFields(): void
{
foreach ($this->fields as $val) {
if ($val['htmlType'] == 'imageUpload') {
$this->img_fields[] = $val['columnName'];
}
};
}
/**
* 初始化导入导出状态
*/
private function initImportExportStatus(): void
{
//其他选项 4导出 6导入
$checked_btn_arr = $this->table['options']->CheckedBtn;
$this->is_import = in_array('6', $checked_btn_arr);
$this->is_export = in_array('4', $checked_btn_arr);
}
/**
* 构建新增允许字段模板字符
*/
private function buildAddFieldsTemp(): void
{
$this->add_fields_temp .= '[';
foreach ($this->business_fields as $key => $val) {
$this->add_fields_temp .= "'{$key}',";
};
$this->add_fields_temp .= ']';
}
/**
* 构建业务字段名
*/
private function buildBusinessFields()
{
foreach ($this->fields as $val) {
if (!$val['isInit']) {
$this->business_fields[$val['columnName']] = $val['columnComment'];
}
};
}
/**
* 生成模型层模块文件夹
*/
private function mkdirModelModule()
{
self::mkdir($this->model_module_path);
}
/**
* 初始化文件路径
*
* @param string $path 文件路径
* @return string 初始化后的文件路径
*/
protected static function initFilePath(string $path): string
{
//系统分隔符替换
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
/**
* 创建文件夹
*
* @param string $path 文件夹创建所属路径
*/
protected static function mkdir(string $path): void
{
if (!(new Gen(new \think\App()))->mkdir($path)) {
throwErrorMsg(__METHOD__ . "创建文件夹失败!");
};
}
/**
* 数组转换为模板字符串
* @param array $arr 数组
* @param int $type 模板字符类型 1:一维数组格式 2:验证器格式 3:列表条件查询 4:关联数组格式
*/
protected function toFormTempStr(array $arr, int $type = 1): string
{
return (new Gen(new \think\App()))->toFormTempStr($arr, $this->business_fields, $type);
}
}