12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\admin\controller\sys_manage;
- use app\admin\validate\sys_manage\AdvValidate;
- use app\controller\Curd;
- use app\model\Adv;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- class AdvController extends Curd
- {
- public function __construct()
- {
- $this->model = new Adv();
- $this->validate = true;
- $this->validateClass = new AdvValidate();
- }
- /** 列表
- * @Desc
- * @Author Gorden
- * @Date 2024/3/5 10:00
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- // $where['adv_category'] = 'adv';
- $order = $request->get('order', 'desc');
- $field = $field ?? 'adv_addtimes';
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- if (!empty($item->adv_media_json)) {
- $mediaJson = json_decode($item->adv_media_json, true);
- if (isset($mediaJson['type'])) {
- $item->adv_media_json = $mediaJson['type'];
- }
- if (empty($mediaJson)){
- $item->adv_media_json = "";
- }
- }
- }
- return $items;
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/27 10:24
- *
- * @param Request $request
- * @return array
- * @throws \support\exception\BusinessException
- */
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- if (!empty($data['adv_media'])) {
- $data['adv_media_json'] = json_encode(['type' => $data['adv_media_json']]);
- } else {
- $data['adv_media_json'] = '[]';
- }
-
- return $data;
- }
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- if (!empty($data['adv_media_json'])) {
- $data['adv_media_json'] = json_encode(['type' => $data['adv_media_json']]);
- } else {
- $data['adv_media_json'] = '[]';
- }
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- }
|