AdvController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\AdvValidate;
  4. use app\controller\Curd;
  5. use app\model\Adv;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class AdvController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new Adv();
  14. $this->validate = true;
  15. $this->validateClass = new AdvValidate();
  16. }
  17. /** 列表
  18. * @Desc
  19. * @Author Gorden
  20. * @Date 2024/3/5 10:00
  21. *
  22. * @param Request $request
  23. * @return Response
  24. * @throws \support\exception\BusinessException
  25. */
  26. public function select(Request $request): Response
  27. {
  28. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  29. // $where['adv_category'] = 'adv';
  30. $order = $request->get('order', 'desc');
  31. $field = $field ?? 'adv_addtimes';
  32. $query = $this->doSelect($where, $field, $order);
  33. return $this->doFormat($query, $format, $limit);
  34. }
  35. public function afterQuery($items)
  36. {
  37. foreach ($items as &$item) {
  38. if (!empty($item->adv_media_json)) {
  39. $mediaJson = json_decode($item->adv_media_json, true);
  40. if (isset($mediaJson['type'])) {
  41. $item->adv_media_json = $mediaJson['type'];
  42. }
  43. if (empty($mediaJson)){
  44. $item->adv_media_json = "";
  45. }
  46. }
  47. }
  48. return $items;
  49. }
  50. /**
  51. * @Desc
  52. * @Author Gorden
  53. * @Date 2024/3/27 10:24
  54. *
  55. * @param Request $request
  56. * @return array
  57. * @throws \support\exception\BusinessException
  58. */
  59. protected function insertInput(Request $request): array
  60. {
  61. $data = $this->inputFilter($request->post());
  62. if (!empty($data['adv_media'])) {
  63. $data['adv_media_json'] = json_encode(['type' => $data['adv_media_json']]);
  64. } else {
  65. $data['adv_media_json'] = '[]';
  66. }
  67. return $data;
  68. }
  69. protected function updateInput(Request $request): array
  70. {
  71. $primary_key = $this->model->getKeyName();
  72. $id = $request->post($primary_key);
  73. $data = $this->inputFilter($request->post());
  74. if (!empty($data['adv_media_json'])) {
  75. $data['adv_media_json'] = json_encode(['type' => $data['adv_media_json']]);
  76. } else {
  77. $data['adv_media_json'] = '[]';
  78. }
  79. $model = $this->model->find($id);
  80. if (!$model) {
  81. throw new BusinessException('记录不存在', 2);
  82. }
  83. unset($data[$primary_key]);
  84. return [$id, $data];
  85. }
  86. }