From 2f4386f196fd85ee32b3d71786a6afb57424b423 Mon Sep 17 00:00:00 2001 From: tensent Date: Fri, 3 Apr 2020 17:14:49 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E5=A4=8D=E5=87=A0=E5=A4=84b?= =?UTF-8?q?ug=EF=BC=9B=202=E3=80=81=E5=AE=8C=E5=96=84=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E4=B8=AD=E7=9A=84=E7=BB=86=E8=8A=82=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/admin/User.php | 3 +-- app/controller/user/Base.php | 19 +++++++++++++++++++ app/http/validate/Category.php | 6 ++++-- app/model/Attribute.php | 27 ++++++++++++++++++++++----- app/model/Category.php | 20 ++++---------------- app/model/Model.php | 13 +++++++++++-- extend/com/Datatable.php | 2 +- public/uploads/files/.gitignore | 2 -- public/uploads/images/.gitignore | 2 -- route/app.php | 16 ++++++++-------- view/admin/category/edit.html | 10 ---------- view/admin/public/base.html | 4 ++-- 12 files changed, 72 insertions(+), 52 deletions(-) delete mode 100644 public/uploads/files/.gitignore delete mode 100644 public/uploads/images/.gitignore diff --git a/app/controller/admin/User.php b/app/controller/admin/User.php index d60c1576..019737b2 100644 --- a/app/controller/admin/User.php +++ b/app/controller/admin/User.php @@ -150,9 +150,8 @@ class User extends Base { * @author colin */ private function getUserinfo($uid = null, $pass = null, $errormsg = null) { - $uid = $uid ? $uid : input('id'); //如果无UID则修改当前用户 - $uid = $uid ? $uid : session('user_auth.uid'); + $uid = $uid ? $uid : $this->request->param('uid', session('userInfo.uid')); $map['uid'] = $uid; if ($pass != null) { unset($map); diff --git a/app/controller/user/Base.php b/app/controller/user/Base.php index 3570c258..bfe22661 100644 --- a/app/controller/user/Base.php +++ b/app/controller/user/Base.php @@ -14,6 +14,16 @@ use \app\controller\Base as BaseC; class Base extends BaseC { + protected function initialize() { + $url = str_replace(".", "/", strtolower($this->request->controller())) . '/' . $this->request->action(); + if (!is_login() and !in_array($url, array('admin/index/login', 'admin/index/logout', 'admin/index/verify'))) { + $this->redirect('/admin/index/login'); + } + + if (!in_array($url, array('admin/index/login', 'admin/index/logout', 'admin/index/verify'))) { + } + } + protected function fetch($template = '') { $config = Cache::get('system_config_data'); $this->tpl_config['view_depr'] = '_'; @@ -29,6 +39,15 @@ class Base extends BaseC { if ($template == '') { $template = str_replace(".", "@", strtolower($this->request->controller())) . "/" . $this->request->action(); } + $template_path = str_replace("public", "", $this->tpl_config['view_dir_name']); + $this->tpl_config['tpl_replace_string'] = [ + '__static__' => '/static', + '__img__' => $template_path . 'static/images', + '__css__' => $template_path . 'static/css', + '__js__' => $template_path . 'static/js', + '__plugins__' => '/static/plugins', + '__public__' => $template_path . 'static', + ]; View::config($this->tpl_config); View::assign($this->data); diff --git a/app/http/validate/Category.php b/app/http/validate/Category.php index e4a9ac1f..0cf1206c 100644 --- a/app/http/validate/Category.php +++ b/app/http/validate/Category.php @@ -16,14 +16,16 @@ use think\Validate; class Category extends Validate{ protected $rule = [ 'title' => 'require', + 'model_id' => 'require' ]; protected $message = [ 'title.require' => '栏目名称必须', + 'model_id.require' => '绑定模型必须', ]; protected $scene = [ - 'adminadd' => ['title'], - 'adminedit' => ['title'], + 'adminadd' => ['title', 'model_id'], + 'adminedit' => ['title', 'model_id'], ]; } \ No newline at end of file diff --git a/app/model/Attribute.php b/app/model/Attribute.php index 433df2fd..428f4371 100644 --- a/app/model/Attribute.php +++ b/app/model/Attribute.php @@ -22,26 +22,32 @@ class Attribute extends \think\Model { 'id' => 'integer', ); + /** + * @title 新增后事件 + */ protected static function onAfterInsert($data){ $data = $data->toArray(); if ($data['model_id']) { $db = new \com\Datatable(); $name = Models::where('id', $data['model_id'])->value('name'); - $data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('id desc')->value('name'); + $data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('sort asc, id desc')->value('name'); return $db->columField(strtolower($name), $data)->query(); } } + /** + * @title 更新后事件 + */ protected static function onAfterUpdate($data){ $data = $data->toArray(); - if ($data['model_id']) { + if (isset($data['model_id']) && isset($data['name'])) { $tablename = Models::where('id', $data['model_id'])->value('name'); - //删除模型表中字段 $db = new \com\Datatable(); if ($db->CheckField($tablename, $data['name'])) { $data['action'] = 'CHANGE'; } + $data['after'] = self::where('name', '<>', $data['name'])->where('model_id', $data['model_id'])->order('sort asc, id asc')->value('name'); $result = $db->columField(strtolower($tablename), $data)->query(); return $result; }else{ @@ -49,6 +55,9 @@ class Attribute extends \think\Model { } } + /** + * @title 删除后事件 + */ protected static function onAfterDelete($data){ $data = $data->toArray(); if ($data['model_id']) { @@ -94,9 +103,14 @@ class Attribute extends \think\Model { }elseif($data['type'] == 'bool'){ $list = [['key'=>0,'label'=>'禁用'],['key'=>1,'label'=>'启用']]; }elseif($data['type'] == 'bind'){ + $map = []; + $db = new \com\Datatable(); if (strrpos($data['extra'], ":")) { $extra = explode(":", $data['extra']); - $row = Db::name($extra[0])->select()->toArray(); + if ($db->CheckField($extra[0], 'model_id')) { + $map[] = ['model_id', '=', $data['model_id']]; + } + $row = Db::name($extra[0])->where($map)->select()->toArray(); if ($extra[1] == 'tree') { $row = (new Tree())->toFormatTree($row); foreach ($row as $val) { @@ -108,7 +122,10 @@ class Attribute extends \think\Model { } } }else{ - $row = Db::name($data['extra'])->select()->toArray(); + if ($db->CheckField($data['extra'], 'model_id')) { + $map[] = ['model_id', '=', $data['model_id']]; + } + $row = Db::name($data['extra'])->select($map)->toArray(); foreach ($row as $val) { $list[] = ['key'=>$val['id'], 'label'=>$val['title']]; } diff --git a/app/model/Category.php b/app/model/Category.php index 40bd4e1f..2721e5a1 100644 --- a/app/model/Category.php +++ b/app/model/Category.php @@ -21,6 +21,10 @@ class Category extends \think\Model{ 'icon' => 'integer', ); + protected static function onAfterUpdate($model){ + $data = $model->toArray(); + } + public static function getCategoryTree($map = []){ $list = self::where($map)->select(); @@ -31,22 +35,6 @@ class Category extends \think\Model{ return $list; } - public function change() { - $data = input('post.'); - if ($data['id']) { - $result = $this->save($data, array('id' => $data['id'])); - } else { - unset($data['id']); - $result = $this->save($data); - } - if (false !== $result) { - return true; - } else { - $this->error = "失败!"; - return false; - } - } - public function info($id, $field = true) { return $this->db()->where(array('id' => $id))->field($field)->find(); } diff --git a/app/model/Model.php b/app/model/Model.php index fa495c76..2d55f948 100644 --- a/app/model/Model.php +++ b/app/model/Model.php @@ -69,19 +69,28 @@ class Model extends \think\Model{ } protected static function onAfterUpdate($data){ + $data = $data->toArray(); if (isset($data['attribute_sort']) && $data['attribute_sort']) { $attribute_sort = json_decode($data['attribute_sort'], true); $attr = []; if (!empty($attribute_sort)) { foreach ($attribute_sort as $key => $value) { + $attr[$key] = Attribute::where('id', 'IN', $value)->column('*', 'id'); foreach ($value as $k => $v) { - $attr[] = ['id' => $v, 'group_id' => $key, 'sort' => $k]; + $attr[$key][$v]['group'] = $key; + $attr[$key][$v]['sort'] = $k; } } } + $save = []; + foreach($attr as $value){ + if(!empty($value)){ + $save = array_merge($save, $value); + } + } if (!empty($attr)) { - (new Attribute())->saveAll($attr); + (new Attribute())->saveAll($save); } } return true; diff --git a/extend/com/Datatable.php b/extend/com/Datatable.php index 7a17c0cd..2c81dfb7 100644 --- a/extend/com/Datatable.php +++ b/extend/com/Datatable.php @@ -126,7 +126,7 @@ class Datatable { } elseif ($field_attr['action'] == 'CHANGE') { $field_attr['oldname'] = (isset($attr['oldname']) && $attr['oldname']) ? $attr['oldname'] : $attr['name']; - $this->sql = "ALTER TABLE `{$field_attr['table']}` CHANGE `{$field_attr['oldname']}` `{$field_attr['name']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'"; + $this->sql = "ALTER TABLE `{$field_attr['table']}` CHANGE `{$field_attr['oldname']}` `{$field_attr['name']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}' {$field_attr['after']}"; } return $this; } diff --git a/public/uploads/files/.gitignore b/public/uploads/files/.gitignore deleted file mode 100644 index c96a04f0..00000000 --- a/public/uploads/files/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file diff --git a/public/uploads/images/.gitignore b/public/uploads/images/.gitignore deleted file mode 100644 index c96a04f0..00000000 --- a/public/uploads/images/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file diff --git a/route/app.php b/route/app.php index ed1c70a8..4cbc1a10 100755 --- a/route/app.php +++ b/route/app.php @@ -14,9 +14,9 @@ use app\model\Model; $model = Model::where('status', '>', 0)->field(['id', 'name'])->select()->toArray(); foreach ($model as $value) { - Route::rule('/admin/' . $value['name'] . '/:function', 'admin.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]); - Route::rule('/front/' . $value['name'] . '/:function', 'front.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]); - Route::rule('/user/' . $value['name'] . '/:function', 'user.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]); + Route::rule('/admin/' . $value['name'] . '/:function', 'Admin.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]); + Route::rule('/front/' . $value['name'] . '/:function', 'Front.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]); + Route::rule('/user/' . $value['name'] . '/:function', 'User.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]); } Route::rule('/', 'Front.Index/index'); @@ -37,12 +37,12 @@ Route::group('admin', function () { }); Route::group('user', function () { - Route::rule('/', 'user.Index/index'); - Route::rule('login', 'user.Index/login'); - Route::rule('logout', 'user.Index/logout'); - Route::rule('register', 'user.Index/register'); + Route::rule('/', 'User.Index/index'); + Route::rule('login', 'User.Index/login'); + Route::rule('logout', 'User.Index/logout'); + Route::rule('register', 'User.Index/register'); Route::rule('upload/:function', 'Upload/:function'); - Route::rule(':controller/:function', 'user.:controller/:function'); + Route::rule(':controller/:function', 'User.:controller/:function'); }); Route::group('api', function () { diff --git a/view/admin/category/edit.html b/view/admin/category/edit.html index 1c47f5b7..8b2643a1 100644 --- a/view/admin/category/edit.html +++ b/view/admin/category/edit.html @@ -164,16 +164,6 @@