From e816d8538b78f2b9b0da6ea0bd2b746ce8b103af Mon Sep 17 00:00:00 2001 From: Karson Date: Thu, 1 Jun 2017 22:13:03 +0800 Subject: [PATCH] =?UTF-8?q?CRUD=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=99=A8=E5=8A=9F=E8=83=BD=20=E5=A6=82=E6=9E=9C=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=98=AFenum,set,select=E7=AD=89=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=BC=9A=E8=87=AA=E5=8A=A8=E5=9C=A8Model?= =?UTF-8?q?=E7=94=9F=E6=88=90=E8=8E=B7=E5=8F=96=E5=99=A8=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eint=E6=97=A5=E6=9C=9F=E5=9E=8B=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E5=99=A8=E5=92=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=99=A8=20=E7=A7=BB=E9=99=A4Traits=E4=B8=ADadd/edit=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=87=AA=E5=8A=A8=E4=BF=AE=E6=94=B9=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=20=E4=BF=AE=E5=A4=8DForm.api.bindev?= =?UTF-8?q?ent=E5=8D=95=E6=AC=A1=E7=BB=91=E5=AE=9A=E5=A4=9A=E4=B8=AAform?= =?UTF-8?q?=E6=97=B6=E7=9A=84BUG=20=E4=BF=AE=E5=A4=8D=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E5=88=B7=E6=96=B0=E4=B8=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E8=BE=B9=E6=A0=8F=E7=9A=84BUG=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E6=83=85=E5=86=B5=E4=B8=8B=E4=BD=BF=E7=94=A8?= =?UTF-8?q?addtabsit=E8=BF=9B=E8=A1=8C=E6=8E=A7=E5=88=B6=E5=99=A8=E9=97=B4?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E6=97=B6=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/command/Crud.php | 95 ++++++++++++++++++- .../admin/command/Crud/stubs/model.stub | 9 ++ application/admin/library/traits/Backend.php | 49 ++++++++-- application/common/controller/Backend.php | 2 +- public/assets/js/backend.js | 4 +- public/assets/js/require-form.js | 6 +- 6 files changed, 151 insertions(+), 14 deletions(-) diff --git a/application/admin/command/Crud.php b/application/admin/command/Crud.php index 69459497..d5846b60 100644 --- a/application/admin/command/Crud.php +++ b/application/admin/command/Crud.php @@ -217,6 +217,9 @@ class Crud extends Command try { Form::setEscapeHtml(false); + $setAttrArr = []; + $getAttrArr = []; + $appendAttrList = []; //循环所有字段,开始构造视图的HTML和JS信息 foreach ($columnList as $k => $v) @@ -257,6 +260,7 @@ class Crud extends Command //如果状态类型不是enum或set $itemArr = !$itemArr ? ['normal', 'hidden'] : $itemArr; $inputType = 'radio'; + $this->getAttr($getAttrArr, $field); } if ($inputType == 'select') { @@ -270,6 +274,10 @@ class Crud extends Command $attrStr = $this->getArrayString($attrArr); $itemArr = $this->getLangArray($itemArr, FALSE); $itemString = $this->getArrayString($itemArr); + + //添加一个获取器 + $this->getAttr($getAttrArr, $field, $itemArr, $v['DATA_TYPE'] == 'set' ? 'multiple' : 'select'); + $this->appendAttr($appendAttrList, $field); $formAddElement = "{:build_select('{$fieldName}', [{$itemString}], '{$defaultValue}', [{$attrStr}])}"; $formEditElement = "{:build_select('{$fieldName}', [{$itemString}], \$row.{$field}, [{$attrStr}])}"; } @@ -302,6 +310,9 @@ class Crud extends Command break; default: $fieldFunc = 'datetime'; + $this->getAttr($getAttrArr, $field, '', $inputType); + $this->setAttr($setAttrArr, $field, '', $inputType); + $this->appendAttr($appendAttrList, $field); break; } $defaultDateTime = "{:date('{$phpFormat}')}"; @@ -316,6 +327,9 @@ class Crud extends Command $fieldName .= "[]"; $itemArr = $this->getLangArray($itemArr, FALSE); $itemString = $this->getArrayString($itemArr); + //添加一个获取器 + $this->getAttr($getAttrArr, $field, $itemArr, $inputType); + $this->appendAttr($appendAttrList, $field); $formAddElement = "{:build_checkboxs('{$fieldName}', [{$itemString}], '{$defaultValue}')}"; $formEditElement = "{:build_checkboxs('{$fieldName}', [{$itemString}], \$row.{$field})}"; } @@ -324,6 +338,9 @@ class Crud extends Command $itemArr = $this->getLangArray($itemArr, FALSE); $itemString = $this->getArrayString($itemArr); $defaultValue = $defaultValue ? $defaultValue : key($itemArr); + //添加一个获取器 + $this->getAttr($getAttrArr, $field, $itemArr, $inputType); + $this->appendAttr($appendAttrList, $field); $formAddElement = "{:build_radios('{$fieldName}', [{$itemString}], '{$defaultValue}')}"; $formEditElement = "{:build_radios('{$fieldName}', [{$itemString}], \$row.{$field})}"; } @@ -477,6 +494,9 @@ class Crud extends Command 'relationPrimaryKey' => '', 'relationSearch' => $relation ? 'true' : 'false', 'controllerIndex' => '', + 'appendAttrList' => implode(",\n", $appendAttrList), + 'getAttrList' => implode("\n\n", $getAttrArr), + 'setAttrList' => implode("\n\n", $setAttrArr), 'modelMethod' => '', ]; @@ -526,6 +546,79 @@ class Crud extends Command $output->writeln("Build Successed"); } + protected function getAttr(&$getAttr, $field, $itemArr = '', $inputType = '') + { + if (!in_array($inputType, ['datetime', 'select', 'multiple', 'checkbox', 'radio'])) + return; + $attrField = ucfirst($field); + if ($inputType == 'datetime') + { + $return = <<getArrayString($itemArr); + $return = << \$v) + { + if (isset(\$arr[\$v])) + { + \$resultArr[] = \$arr[\$v]; + } + } + return implode(',', \$resultArr); +EOD; + } + else + { + $itemString = $this->getArrayString($itemArr); + $return = <<order($sort, $order) ->limit($offset, $limit) ->select(); + $result = array("total" => $total, "rows" => $list); return json($result); @@ -44,10 +45,27 @@ trait Backend foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; - $v = substr($k, -4) == 'time' && !is_numeric($v) ? strtotime($v) : $v; } - $this->model->create($params); - $this->code = 1; + try + { + $result = $this->model->create($params); + if ($result !== false) + { + $this->code = 1; + } + else + { + $this->msg = $this->model->getError(); + } + } + catch (think\Exception $e) + { + $this->msg = $e->getMessage(); + } + } + else + { + $this->msg = __('Parameter %s can not be empty', ''); } return; @@ -72,10 +90,27 @@ trait Backend foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; - $v = substr($k, -4) == 'time' && !is_numeric($v) ? strtotime($v) : $v; } - $row->save($params); - $this->code = 1; + try + { + $result = $row->save($params); + if ($result !== false) + { + $this->code = 1; + } + else + { + $this->msg = $row->getError(); + } + } + catch (think\Exception $e) + { + $this->msg = $e->getMessage(); + } + } + else + { + $this->msg = __('Parameter %s can not be empty', ''); } return; @@ -126,7 +161,7 @@ trait Backend } else { - $this->code = 1; + $this->msg = __('Parameter %s can not be empty', ''); } } diff --git a/application/common/controller/Backend.php b/application/common/controller/Backend.php index 198e247e..c243449b 100644 --- a/application/common/controller/Backend.php +++ b/application/common/controller/Backend.php @@ -92,7 +92,7 @@ class Backend extends Controller !defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax()); // 非选项卡时重定向 - if (!IS_AJAX && !IS_ADDTABS && $controllername != 'index' && $actionname == 'index') + if (!IS_AJAX && !IS_ADDTABS && !IS_DIALOG && $path !== "/{$modulename}/index/index" && $controllername != 'ajax') { $url = $this->request->url(); $this->redirect('index/index', [], 302, ['referer' => $url]); diff --git a/public/assets/js/backend.js b/public/assets/js/backend.js index 42fc3109..f934c6c9 100755 --- a/public/assets/js/backend.js +++ b/public/assets/js/backend.js @@ -246,7 +246,7 @@ define(['jquery', 'bootstrap', 'toastr', 'layer', 'lang', 'moment'], function ($ }); }, addtabs: function (url, title, icon) { - var dom = ".sidebar-menu li a[url='{url}']" + var dom = "a[url='{url}']" var leftlink = top.window.$(dom.replace(/\{url\}/, url)); if (leftlink.size() > 0) { leftlink.trigger("click"); @@ -273,7 +273,7 @@ define(['jquery', 'bootstrap', 'toastr', 'layer', 'lang', 'moment'], function ($ var id = Math.floor(new Date().valueOf() * Math.random()); icon = typeof icon != 'undefined' ? icon : 'fa fa-circle-o'; title = typeof title != 'undefined' ? title : ''; - top.window.$("").append(' ' + title + '').prop("href", url).attr({url: url, addtabs: id}).appendTo(top.window.document.body).trigger("click"); + top.window.$("").append(' ' + title + '').prop("href", url).attr({url: url, addtabs: id}).addClass("hide").appendTo(top.window.document.body).trigger("click"); } } } diff --git a/public/assets/js/require-form.js b/public/assets/js/require-form.js index 807ecba1..147ddd4c 100755 --- a/public/assets/js/require-form.js +++ b/public/assets/js/require-form.js @@ -76,9 +76,9 @@ define(['jquery', 'bootstrap', 'backend', 'toastr', 'upload', 'validator'], func stopOnError: true, valid: function (ret) { //验证通过提交表单 - Form.api.submit(form, onBeforeSubmit, function (data) { + Form.api.submit($(ret), onBeforeSubmit, function (data) { if (typeof onAfterSubmit == 'function') { - if (!onAfterSubmit.call(form, data)) { + if (!onAfterSubmit.call($(ret), data)) { return false; } } @@ -239,7 +239,7 @@ define(['jquery', 'bootstrap', 'backend', 'toastr', 'upload', 'validator'], func $(document).on('click', ".fachoose", function () { var multiple = $(this).data("multiple") ? $(this).data("multiple") : false; var mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : ''; - Backend.api.open("general/attachment/select?callback=refreshchoose&element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype="+mimetype, __('Choose')); + Backend.api.open("general/attachment/select?callback=refreshchoose&element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype, __('Choose')); return false; });