ShopController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\controller\medical;
  3. use app\admin\validate\medical\ShopValidate;
  4. use app\controller\Curd;
  5. use app\model\MedicalShop;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class ShopController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new MedicalShop();
  14. $this->validate = true;
  15. $this->validateClass = new ShopValidate();
  16. }
  17. /**
  18. * @Desc 列表
  19. * @Author Gorden
  20. * @Date 2024/3/1 9:15
  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. $order = $request->get('order', 'desc');
  30. $field = $field ?? 'shop_sort';
  31. $where['shop_is_del'] = 0;
  32. $query = $this->doSelect($where, $field, $order);
  33. return $this->doFormat($query, $format, $limit);
  34. }
  35. /**
  36. * @Desc 插入数据处理
  37. * @Author Gorden
  38. * @Date 2024/3/19 13:37
  39. *
  40. * @param Request $request
  41. * @return array
  42. * @throws \support\exception\BusinessException
  43. */
  44. protected function insertInput(Request $request): array
  45. {
  46. $data = $this->inputFilter($request->post());
  47. $data['shop_logo'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['shop_logo']);
  48. if ($data['shop_business_hours']){
  49. $shopBusinessHoursStart = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[0]));
  50. $shopBusinessHoursEnd = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[1]));
  51. $data['shop_business_hours'] = $shopBusinessHoursStart . '~' . $shopBusinessHoursEnd;
  52. }
  53. return $data;
  54. }
  55. protected function updateInput(Request $request): array
  56. {
  57. $primary_key = $this->model->getKeyName();
  58. $id = $request->post($primary_key);
  59. $data = $this->inputFilter($request->post());
  60. $model = $this->model->find($id);
  61. if (!$model) {
  62. throw new BusinessException('记录不存在', 2);
  63. }
  64. unset($data[$primary_key]);
  65. // 头像
  66. $data['shop_logo'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['shop_logo']);
  67. if ($data['shop_business_hours']){
  68. $shopBusinessHoursStart = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[0]));
  69. $shopBusinessHoursEnd = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[1]));
  70. $data['shop_business_hours'] = $shopBusinessHoursStart . '~' . $shopBusinessHoursEnd;
  71. }
  72. return [$id, $data];
  73. }
  74. public function afterQuery($items)
  75. {
  76. foreach ($items as &$item) {
  77. $item->shop_logo = getenv('STORAGE_DOMAIN').$item->shop_logo;
  78. $shopBusinessHoursArr = explode('~', $item->shop_business_hours);
  79. foreach ($shopBusinessHoursArr as $key => $v){
  80. $shopBusinessHoursArr[$key] = date("Y-m-d\TH:i:s\Z",strtotime(date('Y-m-d ').$v)-8*3600);
  81. }
  82. $item->shop_business_hours = $shopBusinessHoursArr;
  83. $item->shop_label = !empty($item->shop_label) ? explode(',',$item->shop_label) : [];
  84. }
  85. return $items;
  86. }
  87. /**
  88. * @Desc 删除
  89. * @Author Gorden
  90. * @Date 2024/3/1 9:14
  91. *
  92. * @param Request $request
  93. * @return Response
  94. * @throws \support\exception\BusinessException
  95. */
  96. public function delete(Request $request): Response
  97. {
  98. $ids = $this->deleteInput($request);
  99. $this->doSoftDelete($ids, ['shop_is_del' => 1]);
  100. return json_success('success');
  101. }
  102. }