Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Commit

Permalink
优化代码自动生成,将模型名设置为驼峰式(CamelCase),更方便的进行模型关联
Browse files Browse the repository at this point in the history
  • Loading branch information
yuan1994 committed Dec 17, 2016
1 parent 92081b0 commit dcbacf7
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 29 deletions.
21 changes: 19 additions & 2 deletions application/admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ protected function getModel($controller = '', $type = false)
if (!$controller) {
$controller = $this->request->controller();
}
if (class_exists($modelName = Loader::parseClass($module, 'model', $controller))) {
$model = new $modelName();
if (
class_exists($modelClass = Loader::parseClass($module, 'model', $this->parseCamelCase($controller)))
|| class_exists($modelClass = Loader::parseClass($module, 'model', $controller))
) {
$model = new $modelClass();
$modelType = 'model';
} else {
$model = Db::name($this->parseTable($controller));
Expand Down Expand Up @@ -401,4 +404,18 @@ protected function datalist($model, $map, $field = null, $sortBy = '', $asc = fa
}
}
}

/**
* 将abc.def.Gh转为AbcDefGh
*
* @param $string
*
* @return mixed
*/
protected function parseCamelCase($string)
{
return preg_replace_callback('/(\.|^)([a-zA-Z])/', function ($match) {
return ucfirst($match[2]);
}, $string);
}
}
20 changes: 20 additions & 0 deletions application/admin/controller/one/two/three/Four.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace app\admin\controller\one\two\three;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;

class Four extends Controller
{
use \app\admin\traits\controller\Controller;
// 方法黑名单
protected static $blacklist = [];

protected function filter(&$map)
{
if ($this->request->param("field1")) {
$map['field1'] = ["like", "%" . $this->request->param("field1") . "%"];
}
}
}
2 changes: 1 addition & 1 deletion application/admin/extra/conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
3 => '值三',
4 => '值四',
]
];
];
10 changes: 8 additions & 2 deletions application/admin/traits/controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function add()
}

// 写入数据
if (class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $controller))) {
if (
class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $this->parseCamelCase($controller)))
|| class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $controller))
) {
//使用模型写入,可以在模型中定义更高级的操作
$model = new $modelClass();
$ret = $model->isUpdate(false)->save($data);
Expand Down Expand Up @@ -127,7 +130,10 @@ public function edit()
}

// 更新数据
if (class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $controller))) {
if (
class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $this->parseCamelCase($controller)))
|| class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $controller))
) {
// 使用模型更新,可以在模型中定义更高级的操作
$model = new $modelClass();
$ret = $model->isUpdate(true)->save($data, ['id' => $data['id']]);
Expand Down
161 changes: 161 additions & 0 deletions application/admin/view/one/two/three/four/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{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="{$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">
<input type="text" class="input-text" placeholder="字段一" name="field1" value="{$vo.field1 ?? ''}" datatype="*" nullmsg="请填写字段一">
</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">选填:</label>
<div class="formControls col-xs-6 col-sm-6">
<input type="text" class="input-text" placeholder="选填" name="option" value="{$vo.option ?? ''}" >
</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">下拉框:</label>
<div class="formControls col-xs-6 col-sm-6">
<div class="select-box">
<select name="select" class="select">
<option value="1">选项一</option>
<option value="2">选项二</option>
<option value="3">选项三</option>
</select>
</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">单选:</label>
<div class="formControls col-xs-6 col-sm-6 skin-minimal">
{foreach name="$Think.config.conf.sex" item='v' key='k'}
<div class="radio-box">
<input type="radio" name="radio" id="radio-{$k}" value="{$k}">
<label for="radio-{$k}">{$v}</label>
</div>
{/foreach}
</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">复选框:</label>
<div class="formControls col-xs-6 col-sm-6 skin-minimal">
<div class="radio-box">
<input type="checkbox" name="checkbox[]" id="checkbox-1" value="1">
<label for="checkbox-1">选项一</label>
</div>
<div class="radio-box">
<input type="checkbox" name="checkbox[]" id="checkbox-2" value="2">
<label for="checkbox-2">选项二</label>
</div>
<div class="radio-box">
<input type="checkbox" name="checkbox[]" id="checkbox-3" value="3">
<label for="checkbox-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">密码:</label>
<div class="formControls col-xs-6 col-sm-6">
<input type="password" class="input-text" placeholder="密码" name="password" value="{$vo.password ?? ''}" >
</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">文本域:</label>
<div class="formControls col-xs-6 col-sm-6">
<textarea class="textarea" placeholder="" name="textarea" onKeyUp="textarealength(this, 100)">{$vo.textarea ?? ''}</textarea>
<p class="textarea-numberbar"><em class="textarea-length">0</em>/100</p>
</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="date" value="{$vo.date ?? ''}" {literal} onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" {/literal} datatype="*" nullmsg="请选择日期">
</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" placeholder="手机号" name="mobile" value="{$vo.mobile ?? ''}" datatype="/^1[34578]\d{9}$/" nullmsg="请填写手机号" errormsg="手机号格式错误">
</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" placeholder="邮箱" name="email" value="{$vo.email ?? ''}" datatype="e" nullmsg="请填写邮箱" errormsg="邮箱格式错误">
</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="number" class="input-text" placeholder="排序" name="sort" value="{$vo.sort ?? '50'}" datatype="*" nullmsg="请填写排序">
</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="*" nullmsg="请选择状态">
<label for="status-1">启用</label>
</div>
<div class="radio-box">
<input type="radio" name="status" id="status-0" value="0" datatype="*" nullmsg="请选择状态">
<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='radio'][value='{$vo.radio ?? '1'}']").attr("checked", true);
var checks = ''.split(",");
if (checks.length > 0){
for (var i in checks){
$("[name='checkbox[]'][value='"+checks[i]+"']").attr("checked", true);
}
}
$("[name='status'][value='{$vo.status ?? '1'}']").attr("checked", true);
$("[name='select']").find("[value='{$vo.select ?? ''}']").attr("selected", 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}
11 changes: 11 additions & 0 deletions application/admin/view/one/two/three/four/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form class="mb-20" method="get" action="{:\\think\\Url::build($Request.action)}">
<input type="text" class="input-text" style="width:250px" placeholder="字段一" name="field1" value="{$Request.param.field1}" >
<div class="select-box" style="width:250px">
<select name="select" class="select">
<option value="1">选项一</option>
<option value="2">选项二</option>
<option value="3">选项三</option>
</select>
</div>
<button type="submit" class="btn btn-success"><i class="Hui-iconfont">&#xe665;</i> 搜索</button>
</form>
43 changes: 43 additions & 0 deletions application/admin/view/one/two/three/four/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{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,saveorder" /}
</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='select']").find("[value='{$Request.param.select}']").attr("selected", true);
})
</script>
{/block}

8 changes: 8 additions & 0 deletions application/admin/view/one/two/three/four/recyclebin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{extend name="template/recyclebin" /}
{block name="script"}
<script>
$(function () {
$("[name='select']").find("[value='{$Request.param.select}']").attr("selected", true);
})
</script>
{/block}
15 changes: 15 additions & 0 deletions application/admin/view/one/two/three/four/td.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<td><input type="checkbox" name="id[]" value="{$vo.id}"></td>
<td>{$vo.field1|high_light=$Request.param.field1}</td>
<td>{$vo.option}</td>
<td>{$vo.select}</td>
<td>{$vo.radio}</td>
<td>{$vo.checkbox}</td>
<td>{$vo.password}</td>
<td>{$vo.textarea}</td>
<td>{$vo.date}</td>
<td>{$vo.mobile}</td>
<td>{$vo.email}</td>
<td style="padding: 0">
<input type="number" name="sort[{$vo.id}]" value="{$vo.sort}" style="width: 60px;"
class="input-text text-c order-input" data-id="{$vo.id}"></td>
<td>{$vo.status|get_status}</td>
13 changes: 13 additions & 0 deletions application/admin/view/one/two/three/four/th.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<th width="25"><input type="checkbox"></th>
<th width="">字段一</th>
<th width="">选填</th>
<th width="">下拉框</th>
<th width="">单选</th>
<th width="">复选框</th>
<th width="">密码</th>
<th width="">文本域</th>
<th width="">日期</th>
<th width="">手机号</th>
<th width="">邮箱</th>
<th width="">{:sort_by('排序','sort')}</th>
<th width="">状态</th>
12 changes: 12 additions & 0 deletions application/common/model/OneTwoThreeFour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace app\common\model;

use think\Model;

class OneTwoThreeFour extends Model
{
// 指定表名,不含前缀
protected $name = 'one_two_three_four';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
}
16 changes: 16 additions & 0 deletions application/common/validate/one/two/three/Four.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace app\common\validate\one\two\three;

use think\Validate;

class Four extends Validate
{
protected $rule = [
"field1|字段一" => "require",
"date|日期" => "require",
"mobile|手机号" => "require",
"email|邮箱" => "require",
"sort|排序" => "require",
"status|状态" => "require",
];
}
Loading

0 comments on commit dcbacf7

Please sign in to comment.