本次更新增强了CURD生成器的字段类型自定义功能,允许开发者通过Web界面设置特定字段的类型,例如忽略字段、下拉字段、单选字段、多选字段、图片字段、多选图片字段、日期字段、日期时间字段和编辑器字段。这些设置会直接影响生成的控制器、模型和视图文件,从而提供更高的灵活性和定制化能力。此外,还对代码模板进行了调整,引入了`$notes`变量来存储字段定义,简化了视图中的脚本处理,并优化了控制器和模型中的代码结构。这些改动旨在改善代码的可读性和可维护性,同时使CURD生成器的使用更加直观和便捷。 通过这次更新,我们希望进一步提升CURD生成器的实用性和用户体验,减少开发者在日常 CRUD操作中重复编写代码的工作量。相关的代码改动包括对`BuildCurd.php`文件的多处调整,以实现新的字段类型设置功能;对`CommonTool.php`的修改,以支持新的数组字符串处理逻辑;对`controller.code`、`curd_generate.js`、`curd_generate.php`、`index.code`和`index.html`等文件的修改,以确保生成的代码与新的设置逻辑兼容,并改善前端交互体验。
105 lines
3.3 KiB
PHP
105 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\admin\service\tool;
|
|
|
|
class CommonTool
|
|
{
|
|
|
|
/**
|
|
* 下划线转驼峰
|
|
* @param $str
|
|
* @return null|string|string[]
|
|
*/
|
|
public static function lineToHump($str)
|
|
{
|
|
$str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
|
|
return strtoupper($matches[2]);
|
|
}, $str);
|
|
return $str;
|
|
}
|
|
|
|
/**
|
|
* 驼峰转下划线
|
|
* @param $str
|
|
* @return null|string|string[]
|
|
*/
|
|
public static function humpToLine($str)
|
|
{
|
|
$str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
|
|
return '_' . strtolower($matches[0]);
|
|
}, $str);
|
|
return $str;
|
|
}
|
|
|
|
/**
|
|
* 获取真实IP
|
|
* @return mixed
|
|
*/
|
|
public static function getRealIp()
|
|
{
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
|
|
foreach ($matches[0] as $xip) {
|
|
if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
|
|
$ip = $xip;
|
|
break;
|
|
}
|
|
}
|
|
}elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
}elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
|
|
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
|
|
}elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
|
|
$ip = $_SERVER['HTTP_X_REAL_IP'];
|
|
}
|
|
return $ip;
|
|
}
|
|
|
|
/**
|
|
* 读取文件夹下的所有文件
|
|
* @param $path
|
|
* @param $basePath
|
|
* @return array|mixed
|
|
*/
|
|
public static function readDirAllFiles($path, $basePath = '')
|
|
{
|
|
list($list, $temp_list) = [[], scandir($path)];
|
|
empty($basePath) && $basePath = $path;
|
|
foreach ($temp_list as $file) {
|
|
if ($file != ".." && $file != ".") {
|
|
if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
|
|
$childFiles = self::readDirAllFiles($path . DIRECTORY_SEPARATOR . $file, $basePath);
|
|
$list = array_merge($childFiles, $list);
|
|
}else {
|
|
$filePath = $path . DIRECTORY_SEPARATOR . $file;
|
|
$fileName = str_replace($basePath . DIRECTORY_SEPARATOR, '', $filePath);
|
|
$list[$fileName] = $filePath;
|
|
}
|
|
}
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 模板值替换
|
|
* @param $string
|
|
* @param $array
|
|
* @return mixed
|
|
*/
|
|
public static function replaceTemplate($string, $array)
|
|
{
|
|
foreach ($array as $key => $val) {
|
|
if (is_null($val)) $val = '';
|
|
$string = str_replace("{{" . $key . "}}", $val, $string);
|
|
}
|
|
return $string;
|
|
}
|
|
|
|
|
|
public static function replaceArrayString(?string $arrayString): string
|
|
{
|
|
$arrayString = str_replace('array (', '[', $arrayString);
|
|
$arrayString = str_replace(')', ']', $arrayString);
|
|
return $arrayString;
|
|
}
|
|
} |