From 025bbab5124b7dc96a33021846ed49b9e584c247 Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Fri, 13 Apr 2018 17:39:46 +0800 Subject: [PATCH] arctype --- app/Common/function.php | 3 +- .../Controllers/Admin/ArticleController.php | 55 +++----- .../Controllers/Api/ArticleController.php | 26 ++-- app/Http/Logic/ArticleLogic.php | 13 +- app/Http/Model/Article.php | 18 +-- app/Http/Model/BaseModel.php | 43 +++++-- app/Http/Requests/ArctypeRequest.php | 120 +++++++++++++++--- app/Http/Requests/ArticleRequest.php | 7 +- app/Http/Requests/BaseRequest.php | 9 ++ app/Http/Requests/GoodsRequest.php | 4 +- 10 files changed, 199 insertions(+), 99 deletions(-) create mode 100644 app/Http/Requests/BaseRequest.php diff --git a/app/Common/function.php b/app/Common/function.php index 7f8b233..d334e39 100644 --- a/app/Common/function.php +++ b/app/Common/function.php @@ -1178,7 +1178,8 @@ function getDataAttr($dataModel,$data = []) if(method_exists($dataModel, $_method)) { - $data[$k.'_text'] = $dataModel::$_method($data); + $tmp = $k.'_text'; + $data->$tmp = $dataModel::$_method($data); } } diff --git a/app/Http/Controllers/Admin/ArticleController.php b/app/Http/Controllers/Admin/ArticleController.php index 45f5a22..484c3fd 100644 --- a/app/Http/Controllers/Admin/ArticleController.php +++ b/app/Http/Controllers/Admin/ArticleController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Admin\CommonController; use DB; +use App\Common\ReturnData; use Illuminate\Http\Request; use App\Http\Logic\ArticleLogic; @@ -43,7 +44,7 @@ class ArticleController extends CommonController } }; - $posts = $this->getLogic()->getPaginate($where); + $posts = $this->getLogic()->getPaginate($where, array('id', 'desc')); $data['posts'] = $posts; @@ -87,17 +88,6 @@ class ArticleController extends CommonController public function doadd() { - //数据验证 - $validate = new ArticleRequest(); - $validator = Validator::make($_REQUEST, $validate->getSceneRules('add'), $validate->getSceneRulesMessages()); - - if ($validator->fails()) - { - dd($validator->errors()->first()); - //$validator->errors()->all(); - error_jump('参数错误'); - } - $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图 if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description $content="";if(!empty($_POST["body"])){$content = $_POST["body"];} @@ -144,18 +134,15 @@ class ArticleController extends CommonController $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0]; } } - - unset($_POST["dellink"]); - unset($_POST["autolitpic"]); - unset($_POST["_token"]); - if(isset($_POST['editorValue'])){unset($_POST['editorValue']);} - if(DB::table('article')->insert($_POST)) - { - success_jump("添加成功!"); - } + + $res = $this->getLogic()->add($_POST); + if($res['code']==ReturnData::SUCCESS) + { + success_jump($res['msg'], route('admin_article')); + } else { - error_jump("添加失败!请修改后重新添加"); + error_jump($res['msg']); } } @@ -164,7 +151,7 @@ class ArticleController extends CommonController if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;} $data['id'] = $id; - $data['post'] = object_to_array(DB::table('article')->where('id', $id)->first(), 1); + $data['post'] = object_to_array($this->getLogic()->getOne(['id'=>$id]), 1); return view('admin.article.edit', $data); } @@ -176,8 +163,7 @@ class ArticleController extends CommonController if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description $content="";if(!empty($_POST["body"])){$content = $_POST["body"];} $_POST['pubdate'] = time();//更新时间 - $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 修改者id - + if(!empty($_POST["keywords"])) { $_POST['keywords']=str_replace(",",",",$_POST["keywords"]); @@ -216,18 +202,15 @@ class ArticleController extends CommonController $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0]; } } - - unset($_POST["dellink"]); - unset($_POST["autolitpic"]); - unset($_POST["_token"]); - if(isset($_POST['editorValue'])){unset($_POST['editorValue']);} - if(DB::table('article')->where('id', $id)->update($_POST)) - { - success_jump("修改成功!", route('admin_article')); - } + + $res = $this->getLogic()->edit($_POST,array('id'=>$id)); + if($res['code']==ReturnData::SUCCESS) + { + success_jump($res['msg'], route('admin_article')); + } else { - error_jump("修改失败!"); + error_jump($res['msg']); } } @@ -290,4 +273,4 @@ class ArticleController extends CommonController return DB::table("article")->where($where)->count(); } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/ArticleController.php b/app/Http/Controllers/Api/ArticleController.php index bdb4288..ba1840e 100644 --- a/app/Http/Controllers/Api/ArticleController.php +++ b/app/Http/Controllers/Api/ArticleController.php @@ -4,9 +4,10 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Api\CommonController; use Illuminate\Http\Request; use Log; +use DB; use App\Common\ReturnData; - use App\Http\Model\Article; +use App\Http\Logic\ArticleLogic; class ArticleController extends CommonController { @@ -14,16 +15,21 @@ class ArticleController extends CommonController { parent::__construct(); } - + + public function getLogic() + { + return new ArticleLogic(); + } + public function articleList(Request $request) { //参数 - $data['limit'] = $request->input('limit', 10); - $data['offset'] = $request->input('offset', 0); - if($request->input('typeid', '') != ''){$data['typeid'] = $request->input('typeid');} - $data['ischeck'] = Article::IS_CHECK; + $limit = $request->input('limit', 10); + $offset = $request->input('offset', 0); + if($request->input('typeid', null) != null){$where['typeid'] = $request->input('typeid');} + $where['ischeck'] = Article::IS_CHECK; - $res = Article::getList($data); + $res = $this->getLogic()->getList($where, array('id', 'desc'), '*', $offset, $limit); if($res === false) { return ReturnData::create(ReturnData::SYSTEM_FAIL); @@ -44,7 +50,7 @@ class ArticleController extends CommonController $data['ischeck'] = Article::IS_CHECK; if($data['id']==''){return ReturnData::create(ReturnData::PARAMS_ERROR);} - $res = Article::getOne($data); + $res = $this->getLogic()->getOne($data); if($res === false) { return ReturnData::create(ReturnData::SYSTEM_FAIL); @@ -52,9 +58,7 @@ class ArticleController extends CommonController //$res->pubdate = date('Y-m-d H:i',$res->pubdate); //$res->addtime = date('Y-m-d H:i',$res->addtime); - - \DB::table('article')->where(array('id'=>$data['id']))->increment('click', 1); - + return ReturnData::create(ReturnData::SUCCESS,$res); } } \ No newline at end of file diff --git a/app/Http/Logic/ArticleLogic.php b/app/Http/Logic/ArticleLogic.php index 6920297..8ee1539 100644 --- a/app/Http/Logic/ArticleLogic.php +++ b/app/Http/Logic/ArticleLogic.php @@ -34,7 +34,7 @@ class ArticleLogic extends BaseLogic foreach($res['list'] as $k=>$v) { $res['list'][$k] = $this->getDataView($v); - $res['list'][$k]['typename'] = Article::getTypenameAttr(array('typeid'=>$v->typeid)); + $res['list'][$k]->typename = Article::getTypenameAttr(array('typeid' => $v->typeid)); } } @@ -76,9 +76,9 @@ class ArticleLogic extends BaseLogic if(!$res){return false;} $res = $this->getDataView($res); - $res['typename'] = Article::getTypenameAttr(array('typeid'=>$res->typeid)); + $res->typename = Article::getTypenameAttr(array('typeid'=>$res->typeid)); - Article::getDb()->increment('click', 1); + Article::getDb()->where($where)->increment('click', 1); return $res; } @@ -88,7 +88,7 @@ class ArticleLogic extends BaseLogic { if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);} - $validator = $this->getValidate($_REQUEST,'add'); + $validator = $this->getValidate($_REQUEST, 'add'); if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} $res = Article::add($data,$type); @@ -101,7 +101,10 @@ class ArticleLogic extends BaseLogic public function edit($data, $where = array()) { if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);} - + + $validator = $this->getValidate($_REQUEST, 'edit'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + $res = Article::edit($data,$where); if($res === false){return ReturnData::create(ReturnData::SYSTEM_FAIL);} diff --git a/app/Http/Model/Article.php b/app/Http/Model/Article.php index b3041cf..7774fac 100644 --- a/app/Http/Model/Article.php +++ b/app/Http/Model/Article.php @@ -64,7 +64,7 @@ class Article extends BaseModel public static function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10) { $model = self::getDb(); - if($where){$res = $res->where($where);} + if($where){$model = $model->where($where);} $res['count'] = $model->count(); $res['list'] = array(); @@ -72,7 +72,7 @@ class Article extends BaseModel if($res['count'] > 0) { if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}} - if($order){$model = $model->orderBy($order);} + if($order){$model = parent::getOrderByData($model, $order);} if($offset){}else{$offset = 0;} if($limit){}else{$limit = 10;} @@ -142,7 +142,7 @@ class Article extends BaseModel if($where){$res = $res->where($where);} if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} - if($order){$res = $res->orderBy($order);} + if($order){$res = parent::getOrderByData($res, $order);} if($limit){}else{$limit = 10;} return $res->paginate($limit); @@ -162,7 +162,7 @@ class Article extends BaseModel if($where){$res = $res->where($where);} if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} - if($order){$res = $res->orderBy($order);} + if($order){$res = parent::getOrderByData($res, $order);} if($offset){}else{$offset = 0;} if($limit){}else{$limit = 10;} @@ -199,7 +199,7 @@ class Article extends BaseModel if($type==0) { // 新增单条数据并返回主键值 - return self::insertGetId(parent::filterTableColumn($data,$this->table)); + return self::insertGetId(parent::filterTableColumn($data,'article')); } elseif($type==1) { @@ -225,7 +225,7 @@ class Article extends BaseModel */ public static function edit($data, $where = array()) { - if (self::where($where)->update(parent::filterTableColumn($data,$this->table)) !== false) + if (self::where($where)->update(parent::filterTableColumn($data,'article')) !== false) { return true; } @@ -258,14 +258,14 @@ class Article extends BaseModel { return self::where($where)->delete(); } - + //是否审核 public static function getIscheckAttr($data) { $arr = array(0 => '已审核', 1 => '未审核'); - return $arr[$data['ischeck']]; + return $arr[$data->ischeck]; } - + //是否栏目名称 public static function getTypenameAttr($data) { diff --git a/app/Http/Model/BaseModel.php b/app/Http/Model/BaseModel.php index cc67ae4..94d41b8 100644 --- a/app/Http/Model/BaseModel.php +++ b/app/Http/Model/BaseModel.php @@ -5,30 +5,53 @@ use Illuminate\Support\Facades\Schema; class BaseModel extends Model { + // 打印SQL DB::table('article')->orderBy(DB::raw('rand()'))->toSql(); //获取某一表的所有字段 public static function getColumnListing($table) { return Schema::getColumnListing($table); } - + //过滤不是某一表的字段 public static function filterTableColumn($data, $table) { $table_column = Schema::getColumnListing($table); - - if(!$table_column) - { + + if (!$table_column) { return $data; } - - foreach($data as $k=>$v) - { - if (!in_array($k,$table_column)) - { + + foreach ($data as $k => $v) { + if (!in_array($k, $table_column)) { unset($data[$k]); } } - + return $data; } + + //获取排序排序 + public static function getOrderByData($model, $orderby) + { + if ($orderby == 'rand()') + { + $model = $model->orderBy(\DB::raw('rand()')); + } + else + { + if (count($orderby) == count($orderby, 1)) + { + $model = $model->orderBy($orderby[0], $orderby[1]); + } + else + { + foreach ($orderby as $row) + { + $model = $model->orderBy($row[0], $row[1]); + } + } + } + + return $model; + } } \ No newline at end of file diff --git a/app/Http/Requests/ArctypeRequest.php b/app/Http/Requests/ArctypeRequest.php index ec2dc93..e94c576 100644 --- a/app/Http/Requests/ArctypeRequest.php +++ b/app/Http/Requests/ArctypeRequest.php @@ -1,29 +1,109 @@ 'required|integer', + 'pid' => 'integer', + 'addtime' => 'required|integer', + 'name' => 'required|max:30', + 'seotitle' => 'max:150', + 'keywords' => 'max:60', + 'description' => 'max:250', + 'listorder' => 'integer', + 'typedir' => 'required|max:30', + 'templist' => 'max:50', + 'temparticle' => 'max:50', + 'litpic' => 'max:100', + 'seokeyword' => 'max:50', + ]; + + //总的自定义错误信息 + protected $messages = [ + 'id.required' => 'ID必填', + 'id.integer' => 'ID必须为数字', + 'pid.integer' => '父级id必须是数字', + 'addtime.required' => '添加时间必填', + 'addtime.integer' => '添加时间必须是数字', + 'name.required' => '栏目名称必填', + 'name.max' => '栏目名称不能超过30个字符', + 'seotitle.max' => 'seo标题不能超过150个字符', + 'keywords.max' => '关键词不能超过60个字符', + 'description.max' => '描述不能超过250个字符', + 'listorder.integer' => '排序必须是数字', + 'typedir.required' => '栏目别名必填', + 'typedir.max' => '栏目别名不能超过30个字符', + 'templist.max' => '列表页模板不能超过50个字符', + 'temparticle.max' => '文章页模板不能超过50个字符', + 'litpic.max' => '封面或缩略图不能超过100个字符', + 'seokeyword.max' => 'seokeyword不能超过50个字符', ]; + //场景验证规则 protected $scene = [ - 'add' => ['typename', 'typedir', 'reid', 'addtime', 'seotitle', 'keywords', 'description', 'listorder', 'templist', 'temparticle', 'litpic', 'seokeyword'], + 'add' => ['typename', 'typedir', 'pid', 'addtime', 'seotitle', 'keywords', 'description', 'listorder', 'templist', 'temparticle', 'litpic', 'seokeyword'], 'del' => ['id'], ]; + + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return true; //修改为true + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return $this->rules; + } + + /** + * 获取被定义验证规则的错误消息. + * + * @return array + */ + public function messages() + { + return $this->messages; + } + + //获取场景验证规则 + public function getSceneRules($name, $fields = null) + { + $res = array(); + + if(!isset($this->scene[$name])) + { + return false; + } + + $scene = $this->scene[$name]; + if($fields != null && is_array($fields)) + { + $scene = $fields; + } + + foreach($scene as $k=>$v) + { + if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];} + } + + return $res; + } + + //获取场景验证规则自定义错误信息 + public function getSceneRulesMessages() + { + return $this->messages; + } } \ No newline at end of file diff --git a/app/Http/Requests/ArticleRequest.php b/app/Http/Requests/ArticleRequest.php index 8d6ae40..2d886f8 100644 --- a/app/Http/Requests/ArticleRequest.php +++ b/app/Http/Requests/ArticleRequest.php @@ -1,9 +1,7 @@ '栏目ID必填', 'typeid.integer' => '栏目ID必须为数字', 'tuijian.integer' => '推荐等级必须是数字', - 'click.integer' => '点击必须为数字', + 'click.required' => '点击量必填', + 'click.integer' => '点击量必须为数字', 'title.max' => '标题不能大于150个字', 'title.required' => '必须填写标题', 'writer.max' => '作者不能超过20个字符', diff --git a/app/Http/Requests/BaseRequest.php b/app/Http/Requests/BaseRequest.php new file mode 100644 index 0000000..24f89d7 --- /dev/null +++ b/app/Http/Requests/BaseRequest.php @@ -0,0 +1,9 @@ +