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.

148 lines
4.1 KiB

  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Ad;
  5. use App\Http\Requests\AdRequest;
  6. use Validator;
  7. class AdLogic extends BaseLogic
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getModel()
  14. {
  15. return new Ad();
  16. }
  17. public function getValidate($data, $scene_name)
  18. {
  19. //数据验证
  20. $validate = new AdRequest();
  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['count'] > 0)
  28. {
  29. foreach($res['list'] as $k=>$v)
  30. {
  31. $res['list'][$k] = $this->getDataView($v);
  32. }
  33. }
  34. return $res;
  35. }
  36. //分页html
  37. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  38. {
  39. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  40. if($res->count() > 0)
  41. {
  42. foreach($res as $k=>$v)
  43. {
  44. $res[$k] = $this->getDataView($v);
  45. }
  46. }
  47. return $res;
  48. }
  49. //全部列表
  50. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  51. {
  52. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  53. if($res)
  54. {
  55. foreach($res as $k=>$v)
  56. {
  57. $res[$k] = $this->getDataView($v);
  58. }
  59. }
  60. return $res;
  61. }
  62. //详情
  63. public function getOne($where = array(), $field = '*')
  64. {
  65. $res = $this->getModel()->getOne($where, $field);
  66. if(!$res){return false;}
  67. $res = $this->getDataView($res);
  68. return $res;
  69. }
  70. //添加
  71. public function add($data = array(), $type=0)
  72. {
  73. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  74. //添加时间
  75. $time = time();
  76. if (!(isset($data['add_time']) && !empty($data['add_time']))) {
  77. $data['add_time'] = $time;
  78. }
  79. $validator = $this->getValidate($data, 'add');
  80. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  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. //更新时间
  90. $time = time();
  91. if (!(isset($data['add_time']) && !empty($data['add_time']))) {
  92. $data['add_time'] = $time;
  93. }
  94. $validator = $this->getValidate($data, 'edit');
  95. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  96. $res = $this->getModel()->edit($data,$where);
  97. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  98. return ReturnData::create(ReturnData::FAIL);
  99. }
  100. //删除
  101. public function del($where)
  102. {
  103. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  104. $validator = $this->getValidate($where,'del');
  105. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  106. $res = $this->getModel()->del($where);
  107. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  108. return ReturnData::create(ReturnData::FAIL);
  109. }
  110. /**
  111. * 数据获取器
  112. * @param array $data 要转化的数据
  113. * @return array
  114. */
  115. private function getDataView($data = array())
  116. {
  117. return getDataAttr($this->getModel(),$data);
  118. }
  119. }