From d81371b32cf1e2bb0ff48a9e11ef8fa6bb13d338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8E=8C?= Date: Mon, 29 Oct 2018 12:57:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E5=AD=A6=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/teach/controller/Xueqi.php | 254 +++++++++++++++---- application/teach/model/Xueqi.php | 24 +- application/teach/validate/Xueqi.php | 8 +- application/teach/view/index/index.html | 2 +- application/teach/view/xueqi/create.html | 85 ++----- application/teach/view/xueqi/index.html | 32 +-- database/migrations/20181028024920_xueqi.php | 6 +- database/seeds/Category.php | 4 + 8 files changed, 278 insertions(+), 137 deletions(-) diff --git a/application/teach/controller/Xueqi.php b/application/teach/controller/Xueqi.php index 1f3375e0..bf9ec04d 100644 --- a/application/teach/controller/Xueqi.php +++ b/application/teach/controller/Xueqi.php @@ -2,84 +2,234 @@ namespace app\teach\controller; -use think\Controller; -use think\Request; +// 引用控制器基类 +use app\common\controller\Base; +// 引用学期数据模型类 +use app\teach\model\Xueqi as XQ; -class Xueqi extends Controller +class Xueqi extends Base { - /** - * 显示资源列表 - * - * @return \think\Response - */ + // 显示学期列表 public function index() { - // + + // 设置数据总数 + $list['count'] = XQ::count(); + // 设置页面标题 + $list['title'] = '学期列表'; + + + // 模板赋值 + $this->assign('list', $list); + + // 渲染模板 + return $this->fetch(); } - /** - * 显示创建资源表单页. - * - * @return \think\Response - */ + + // 获取学期信息列表 + public function ajaxData() + { + + // 获取DT的传值 + $getdt = request()->param(); + + //得到排序的方式 + $order = $getdt['order'][0]['dir']; + //得到排序字段的下标 + $order_column = $getdt['order'][0]['column']; + //根据排序字段的下标得到排序字段 + $order_field = $getdt['columns'][$order_column]['data']; + //得到limit参数 + $limit_start = $getdt['start']; + $limit_length = $getdt['length']; + //得到搜索的关键词 + $search = $getdt['search']['value']; + + + // 获取记录集总数 + $cnt = XQ::count(); + //查询数据 + $data =XQ::field('id,title,xuenian,category,bfdate,enddate,status') + ->order([$order_field=>$order]) + ->limit($limit_start,$limit_length) + ->all(); + + + // 如果需要查询 + if($search){ + $data = XQ::field('id,title,xuenian,category,bfdate,enddate,status') + ->order([$order_field=>$order]) + ->limit($limit_start,$limit_length) + ->where('title|xuenian','like','%'.$search.'%') + ->where('category','in',function($query) use($search) + { + $query->table('catagory') + ->where('title','like','%'.$search.'%') + ->field('id'); + }) + ->all(); + } + + $datacnt = $data->count(); + + + + + $data = [ + 'draw'=> $getdt["draw"] , // ajax请求次数,作为标识符 + 'recordsTotal'=>$datacnt, // 获取到的结果数(每页显示数量) + 'recordsFiltered'=>$cnt, // 符合条件的总数据量 + 'data'=>$data, //获取到的数据结果 + ]; + + return json($data); + } + + + + // 创建学期 public function create() { - // + // 设置页面标题 + $list['title'] = '添加学期'; + + // 模板赋值 + $this->assign('list',$list); + + // 渲染 + return $this->fetch(); } - /** - * 保存新建的资源 - * - * @param \think\Request $request - * @return \think\Response - */ - public function save(Request $request) + + + // 保存信息 + public function save() { - // + // 实例化验证模型 + $validate = new \app\teach\validate\Teacher; + + + // 获取表单数据 + $list = request()->only(['xingming','sex','quanpin','shoupin','shengri','zhiwu','zhicheng','xueli','biye','worktime','zhuanye','danwei'],'post'); + + + // 验证表单数据 + $result = $validate->check($list); + $msg = $validate->getError(); + + + // 如果验证不通过则停止保存 + if(!$result){ + return json(['msg'=>$msg,'val'=>0]);; + } + + // 保存数据 + $data = XQ::create($list); + + // 根据更新结果设置返回提示信息 + $data ? $data=['msg'=>'添加成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0]; + + // 返回信息 + return json($data); } - /** - * 显示指定的资源 - * - * @param int $id - * @return \think\Response - */ + // public function read($id) { // } - /** - * 显示编辑资源表单页. - * - * @param int $id - * @return \think\Response - */ + + + + // 修改学期信息 public function edit($id) { - // + + // 获取学期信息 + $list = XQ::field('id,xingming,sex,quanpin,shoupin,shengri,zhiwu,zhicheng,xueli,biye,worktime,zhuanye,danwei') + ->get($id); + + + $this->assign('list',$list); + + return $this->fetch(); } - /** - * 保存更新的资源 - * - * @param \think\Request $request - * @param int $id - * @return \think\Response - */ - public function update(Request $request, $id) + + + + + // 更新学期信息 + public function update($id) { - // + $validate = new \app\teach\validate\Teacher; + + // 获取表单数据 + $list = request()->only(['xingming','sex','quanpin','shoupin','shengri','zhiwu','zhicheng','xueli','biye','worktime','zhuanye','danwei'],'put'); + + // 验证表单数据 + $result = $validate->check($list); + $msg = $validate->getError(); + + // 如果验证不通过则停止保存 + if(!$result){ + return json(['msg'=>$msg,'val'=>0]);; + } + + + // 更新数据 + $teacher = new Teacher(); + $data = $teacher->save($list,['id'=>$id]); + // $data = XQ::where('id',$id)->update($list); + + // 根据更新结果设置返回提示信息 + $data>=0 ? $data=['msg'=>'更新成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0]; + + // 返回信息 + return json($data); } - /** - * 删除指定资源 - * - * @param int $id - * @return \think\Response - */ + + + + + // 删除学期 public function delete($id) { - // + + if($id == 'm') + { + $id = request()->delete('ids/a');// 获取delete请求方式传送过来的数据并转换成数据 + } + + $data = XQ::destroy($id); + + // 根据更新结果设置返回提示信息 + $data ? $data=['msg'=>'删除成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0]; + + // 返回信息 + return json($data); + } + + + + // 设置学期状态 + public function setStatus() + { + + // 获取id变量 + $id = request()->post('id'); + $value = request()->post('value'); + + // 获取学期信息 + $data = XQ::where('id',$id)->update(['status'=>$value]); + + // 根据更新结果设置返回提示信息 + $data ? $data=['msg'=>'状态设置成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0]; + + // 返回信息 + return json($data); } } diff --git a/application/teach/model/Xueqi.php b/application/teach/model/Xueqi.php index 3012abd5..e7bbafec 100644 --- a/application/teach/model/Xueqi.php +++ b/application/teach/model/Xueqi.php @@ -7,5 +7,27 @@ use app\common\model\Base; class Xueqi extends Base { - // + // 开始时间修改器 + public function setBfdateAttr($value) + { + return strtotime($value); + } + + // 开始时间获取器 + public function getBfdateAttr($value) + { + return date('Y-m-d',$value); + } + + // 结束时间修改器 + public function setEnddateAttr($value) + { + return strtotime($value); + } + + // 结束时间获取器 + public function getEnddateAttr($value) + { + return date('Y-m-d',$value); + } } diff --git a/application/teach/validate/Xueqi.php b/application/teach/validate/Xueqi.php index f1ff9bae..99132f62 100644 --- a/application/teach/validate/Xueqi.php +++ b/application/teach/validate/Xueqi.php @@ -12,7 +12,13 @@ class Xueqi extends Validate * * @var array */ - protected $rule = []; + protected $rule = [ + 'title|标题' => 'required|length:2,25', + 'xuenian|学年' => 'required|length:1,15', + 'category|分类' => 'required|number', + 'bfdate|时间' => 'required|date', + 'enddate|时间' => 'required|date', + ]; /** * 定义错误信息 diff --git a/application/teach/view/index/index.html b/application/teach/view/index/index.html index 7f6f120c..c969c685 100644 --- a/application/teach/view/index/index.html +++ b/application/teach/view/index/index.html @@ -2,7 +2,7 @@ {block name="topnav"}