You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Article;
  5. use App\Http\Requests\ArticleRequest;
  6. use Validator;
  7. class ArticleLogic extends BaseLogic
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getModel()
  14. {
  15. return model('Article');
  16. }
  17. public function getValidate($data, $scene_name)
  18. {
  19. //数据验证
  20. $validate = new ArticleRequest();
  21. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  22. }
  23. //列表
  24. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  25. {
  26. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  27. if($res['list'])
  28. {
  29. foreach($res['list'] as $k=>$v)
  30. {
  31. $res['list'][$k] = $this->getDataView($v);
  32. $res['list'][$k]->typename = $this->getModel()->getTypenameAttr(array('typeid' => $v->typeid));
  33. }
  34. }
  35. return $res;
  36. }
  37. //分页html
  38. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  39. {
  40. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  41. if($res->count() > 0)
  42. {
  43. foreach($res as $k=>$v)
  44. {
  45. $res[$k] = $this->getDataView($v);
  46. $res[$k]->typename = $this->getModel()->getTypenameAttr(array('typeid'=>$v->typeid));
  47. }
  48. }
  49. return $res;
  50. }
  51. //全部列表
  52. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  53. {
  54. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  55. if($res)
  56. {
  57. foreach($res as $k=>$v)
  58. {
  59. $res[$k] = $this->getDataView($v);
  60. }
  61. }
  62. return $res;
  63. }
  64. //详情
  65. public function getOne($where = array(), $field = '*')
  66. {
  67. $res = $this->getModel()->getOne($where, $field);
  68. if(!$res){return false;}
  69. $res = $this->getDataView($res);
  70. $res->typename = $this->getModel()->getTypenameAttr(array('typeid'=>$res->typeid));
  71. $this->getModel()->getDb()->where($where)->increment('click', 1); //点击量+1
  72. return $res;
  73. }
  74. //添加
  75. public function add($data = array(), $type=0)
  76. {
  77. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  78. $validator = $this->getValidate($data, 'add');
  79. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  80. $data['addtime'] = $data['pubdate'] = time();//添加、更新时间
  81. $res = $this->getModel()->add($data,$type);
  82. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  83. return ReturnData::create(ReturnData::FAIL);
  84. }
  85. //修改
  86. public function edit($data, $where = array())
  87. {
  88. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  89. $validator = $this->getValidate($data, 'edit');
  90. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  91. $data['pubdate'] = time();//更新时间
  92. $res = $this->getModel()->edit($data,$where);
  93. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  94. return ReturnData::create(ReturnData::FAIL);
  95. }
  96. //删除
  97. public function del($where)
  98. {
  99. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  100. $validator = $this->getValidate($where,'del');
  101. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  102. $res = $this->getModel()->del($where);
  103. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  104. return ReturnData::create(ReturnData::FAIL);
  105. }
  106. /**
  107. * 数据获取器
  108. * @param array $data 要转化的数据
  109. * @return array
  110. */
  111. private function getDataView($data = array())
  112. {
  113. return getDataAttr($this->getModel(),$data);
  114. }
  115. }