ShopController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_addTime';
  31. $where['shop_is_del'] = 0;
  32. $query = $this->doSelect($where, $field, $order);
  33. return $this->doFormat($query, $format, $limit);
  34. }
  35. public function selectList()
  36. {
  37. $class = get_class($this->model);
  38. $data = $class::where('shop_status', 1)
  39. ->select('shop_id', 'shop_name')
  40. ->get()
  41. ->toArray();
  42. return json_success('', $data);
  43. }
  44. /**
  45. * @Desc 插入数据处理
  46. * @Author Gorden
  47. * @Date 2024/3/19 13:37
  48. *
  49. * @param Request $request
  50. * @return array
  51. * @throws \support\exception\BusinessException
  52. */
  53. protected function insertInput(Request $request): array
  54. {
  55. $data = $this->inputFilter($request->post());
  56. $data['shop_logo'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['shop_logo']);
  57. if ($data['shop_business_hours']){
  58. $shopBusinessHoursStart = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[0]));
  59. $shopBusinessHoursEnd = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[1]));
  60. $data['shop_business_hours'] = $shopBusinessHoursStart . '~' . $shopBusinessHoursEnd;
  61. }
  62. return $data;
  63. }
  64. protected function updateInput(Request $request): array
  65. {
  66. $primary_key = $this->model->getKeyName();
  67. $id = $request->post($primary_key);
  68. $data = $this->inputFilter($request->post());
  69. $model = $this->model->find($id);
  70. if (!$model) {
  71. throw new BusinessException('记录不存在', 2);
  72. }
  73. unset($data[$primary_key]);
  74. // 头像
  75. $data['shop_logo'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['shop_logo']);
  76. if ($data['shop_business_hours']){
  77. $shopBusinessHoursStart = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[0]));
  78. $shopBusinessHoursEnd = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[1]));
  79. $data['shop_business_hours'] = $shopBusinessHoursStart . '~' . $shopBusinessHoursEnd;
  80. }
  81. return [$id, $data];
  82. }
  83. public function afterQuery($items)
  84. {
  85. foreach ($items as &$item) {
  86. $item->shop_logo = getenv('STORAGE_DOMAIN').$item->shop_logo;
  87. $item->timeFormat = '';
  88. if (!empty($item->shop_business_hours)){
  89. $shopBusinessHoursArr = explode('~', $item->shop_business_hours);
  90. foreach ($shopBusinessHoursArr as $key => $v){
  91. $item->timeFormat .= $v.'~';
  92. $shopBusinessHoursArr[$key] = date("Y-m-d\TH:i:s\Z",strtotime(date('Y-m-d ').$v)-8*3600);
  93. }
  94. $item->timeFormat = rtrim($item->timeFormat,'~');
  95. $item->shop_business_hours = $shopBusinessHoursArr;
  96. }
  97. $item->shop_label = !empty($item->shop_label) ? explode(',',$item->shop_label) : [];
  98. }
  99. return $items;
  100. }
  101. /**
  102. * @Desc 删除
  103. * @Author Gorden
  104. * @Date 2024/3/1 9:14
  105. *
  106. * @param Request $request
  107. * @return Response
  108. * @throws \support\exception\BusinessException
  109. */
  110. public function delete(Request $request): Response
  111. {
  112. $ids = $this->deleteInput($request);
  113. $this->doSoftDelete($ids, ['shop_is_del' => 1]);
  114. return json_success('success');
  115. }
  116. }