diff --git a/app/controller/api/Form.php b/app/controller/api/Form.php new file mode 100644 index 00000000..fcf3458d --- /dev/null +++ b/app/controller/api/Form.php @@ -0,0 +1,60 @@ + +// +---------------------------------------------------------------------- +namespace app\controller\api; + +use think\facade\Db; +use \app\model\Form as FormModel; +use \app\model\FormAttr; + +class Form extends Base { + + public function index(){ + $id = $this->request->param('id'); + $name = $this->request->param('name'); + + $map = []; + + $map[] = ['id', '=', $id]; + + $info = FormModel::where($map)->find(); + $attr = FormAttr::where('form_id', $info['id'])->select(); + + if ($info['relation'] && strpos($info['relation'], ":")) { + list($model, $relation) = explode(":", $info['relation']); + $relation = explode(",", $relation); + if (is_array($relation)) { + $rmap['id'] = ['IN', $relation]; + }elseif (is_string($relation)) { + $rmap['id'] = $relation; + } + $info['relation_list'] = Db::name(ucfirst($model))->where($rmap)->order('sort desc, id asc')->select(); + } + + $this->data['data'] = [ + 'info' => $info, + 'attr' => $attr + ]; + return $this->data; + } + + public function save(){ + $data = $this->request->post(); + $id = $this->request->param('id'); + + $info = FormModel::where('id', $id)->find(); + + $result = Db::name(ucfirst($info['name']))->save($data); + if (false !== $result) { + $this->data['code'] = 1; + }else{ + $this->data['code'] = 0; + } + return $this->data; + } +} \ No newline at end of file diff --git a/app/controller/front/Form.php b/app/controller/front/Form.php index 734080a2..beda7b1c 100644 --- a/app/controller/front/Form.php +++ b/app/controller/front/Form.php @@ -8,6 +8,7 @@ // +---------------------------------------------------------------------- namespace app\controller\front; +use think\facade\Db; use \app\model\Form as FormModel; use \app\model\FormAttr; @@ -19,7 +20,18 @@ class Form extends Base { */ public function index(){ if($this->request->isAjax()){ + $data = $this->request->post(); + $id = $this->request->param('id'); + $info = FormModel::where('id', $id)->find(); + + $result = Db::name(ucfirst($info['name']))->save($data); + if (false !== $result) { + $url = (isset($info['redirect_url']) && $info['redirect_url']) ? $info['redirect_url'] : ''; + return $this->success("提交成功!", $url); + }else{ + return $this->error("提交失败!"); + } }else{ $id = $this->request->param('id'); $name = $this->request->param('name'); @@ -28,10 +40,23 @@ class Form extends Base { $map[] = ['id', '=', $id]; - $info = Form::where($map)->find(); + $info = FormModel::where($map)->find(); + $attr = FormAttr::where('form_id', $info['id'])->select(); + + if ($info['relation'] && strpos($info['relation'], ":")) { + list($model, $relation) = explode(":", $info['relation']); + $relation = explode(",", $relation); + if (is_array($relation)) { + $rmap['id'] = ['IN', $relation]; + }elseif (is_string($relation)) { + $rmap['id'] = $relation; + } + $info['relation_list'] = Db::name(ucfirst($model))->where($rmap)->order('sort desc, id asc')->select(); + } $this->data = [ - 'info' => $info + 'info' => $info, + 'attr' => $attr ]; return $this->fetch(); } diff --git a/public/template/default/front/form_index.html b/public/template/default/front/form_index.html new file mode 100644 index 00000000..feb10157 --- /dev/null +++ b/public/template/default/front/form_index.html @@ -0,0 +1,35 @@ + + + + + + +SentCMS网站管理系统 + + + + + + +
+ 注册 + 登录 +
+
+
+
+
+ + diff --git a/route/app.php b/route/app.php index fb60af5c..8520f2e0 100755 --- a/route/app.php +++ b/route/app.php @@ -25,7 +25,7 @@ Route::rule('/', 'front.Index/index'); Route::rule('search', 'front.Content/search'); Route::rule('category', 'front.Content/category'); Route::rule('topic-:id', 'front.Content/topic'); -Route::rule('form/:id/:name', 'front.Form/index'); +Route::rule('form/:id/[:name]', 'front.Form/index'); Route::rule('front/:controller/:function', 'front.:controller/:function'); Route::group('admin', function () {