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.

138 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\UserMessage;
  5. use App\Http\Requests\UserMessageRequest;
  6. use Validator;
  7. class UserMessageLogic extends BaseLogic
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getModel()
  14. {
  15. return new UserMessage();
  16. }
  17. public function getValidate($data, $scene_name)
  18. {
  19. //数据验证
  20. $validate = new UserMessageRequest();
  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. $validator = $this->getValidate($data, 'add');
  75. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  76. $data['add_time'] = time();
  77. $res = $this->getModel()->add($data,$type);
  78. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  79. return ReturnData::create(ReturnData::FAIL);
  80. }
  81. //修改
  82. public function edit($data, $where = array())
  83. {
  84. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  85. $validator = $this->getValidate($data, 'edit');
  86. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  87. $res = $this->getModel()->edit($data,$where);
  88. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  89. return ReturnData::create(ReturnData::FAIL);
  90. }
  91. //删除
  92. public function del($where)
  93. {
  94. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  95. $validator = $this->getValidate($where,'del');
  96. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  97. $res = $this->getModel()->del($where);
  98. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  99. return ReturnData::create(ReturnData::FAIL);
  100. }
  101. /**
  102. * 数据获取器
  103. * @param array $data 要转化的数据
  104. * @return array
  105. */
  106. private function getDataView($data = array())
  107. {
  108. return getDataAttr($this->getModel(),$data);
  109. }
  110. }