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.

152 lines
3.8 KiB

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