getJsonData(); $rules = [ 'page' => 'required|int', 'page_size' => 'required|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $searchParams = data_get($inputData, 'search', []); $arrWhere = []; $model = AdModel::query(); if (!empty($searchParams['position'])) { $arrWhere['position'] = $searchParams['position']; } if (!empty($searchParams['status'])) { $arrWhere['status'] = $searchParams['status']; } if (!empty($searchParams['type'])) { $arrWhere['type'] = $searchParams['type']; } if (!empty($arrWhere)) { $model = $model->where($arrWhere); } if (!empty($searchParams['title'])) { $model = $model->where('title', 'LIKE', '%'. $searchParams['title'] .'%'); } $result = $model->orderBy('id', 'DESC')->paginate( (int)$inputData['page_size'], ['*'], 'page', (int)$inputData['page'] ); //处理图片/视频路径 // foreach ($result as $v) { // $v->cover = make(CommonService::class)->getImageUrl((string)$v->cover, true); // 图片 // $v->play = make(CommonService::class)->getVideoUrl((string)$v->play, true); // 视频 // } return json_success($result); } /** * 新增 / 修改 * @param ValidatorFactoryInterface $validatorFactory * @return mixed */ public function save(ValidatorFactoryInterface $validatorFactory) { $inputData = $this->getJsonData(); if (isset($inputData['ad_id'])) { $rules = [ 'ad_id' => 'int', 'position' => 'string', 'title' => 'string', 'cover' => 'string', // 'start_time' => 'string', // 'end_time' => 'string', 'sort' => 'int', 'url' => 'string', 'scheme' => 'string', 'type' => 'int', 'play' => 'string', 'status' => 'nullable|int', 'resolution' => 'required', ]; } else { $rules = [ 'ad_id' => 'int', 'position' => 'required|string', 'title' => 'required|string', 'cover' => 'required|string', // 'start_time' => 'string', // 'end_time' => 'string', 'sort' => 'int', 'url' => 'string', 'scheme' => 'required|string', 'type' => 'required|int', 'play' => 'string', 'status' => 'nullable|int', 'resolution' => 'required', ]; } $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } if (!isset($inputData['ad_id'])) { if ($inputData['type'] == 2 && !$inputData['play']) { return json_fail('添加视频广告请上传视频'); } } $resolution = strtolower(trim(data_get($inputData, 'resolution', ''))); $create = [ 'position' => $inputData['position'] ?? '', 'title' => $inputData['title'] ?? '', 'cover' => $inputData['cover'] ?? '', // 'start_time' => $inputData['start_time'] ?? '', // 'end_time' => $inputData['end_time'] ?? '', 'sort' => $inputData['sort'] ?? '', 'scheme' => data_get($inputData, 'scheme', ''), 'link' => $inputData['url'], 'type' => $inputData['type'] ?? '', 'play' => $inputData['play'] ?? '', 'resolution' => $resolution, ]; if (isset($inputData['status'])) { $create['status'] = intval($inputData['status']); } // $create = action_data($create); if (isset($inputData['ad_id'])) { $oldItem = Model::query() ->where('id', $inputData['ad_id']) ->first(); if (!$oldItem) { return json_fail('广告不存在'); } $result = Model::query() ->where('id', $inputData['ad_id']) ->update($create); $newitem = Model::query() ->where('id', $inputData['ad_id']) ->first(); // 创建管理员日志 $logSubject = "修改广告"; $logSvalue = $inputData['ad_id']; $logDbData = [ 'after_items' => [ $newitem->toArray(), ], 'before_items' => [ $oldItem->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); } else { $result = Model::query()->create($create); if (!$result) { return json_fail('创建失败'); } // 创建管理员日志 $logSubject = "创建广告"; $logSvalue = $result->id; $logDbData = [ 'after_items' => [ $result->toArray(), ], 'before_items' => [], ]; $this->log($logSubject, $logSvalue, $logDbData); } $this->clearCache(); if (isset($inputData['ad_id'])) { $this->clearCacheVideo($inputData['ad_id']); } return json_success($result); } /** * 编辑(标示上架/下架) * @author x.b * @param ValidatorFactoryInterface $validatorFactory * @return mixed * @example param_json_data string: {"handshake":"","data":{"ad_id":7,"status":1}} */ public function update(ValidatorFactoryInterface $validatorFactory) { $inputData = $this->getJsonData(); $rules = [ 'ad_id' => 'required|int', 'status' => 'required|in:1,2,3,4', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $data = [ 'id' => $inputData['ad_id'], 'status' => $inputData['status'] ?? '', ]; $data = action_data($data); $oldItem = Model::query() ->where('id', $inputData['ad_id']) ->first(); if (!$oldItem) { return json_fail('广告不存在'); } /*if ($inputData['status'] == 1 && strpos($oldItem->play, '.mp4')) { //状态改为转码中 $data['status'] = 2; //转码 make(QueueService::class)->addShiftVideoJob( [ 'path' => $oldItem->play, 'table' => 'ad', 'id' => $oldItem->id, 'update' => 'play', ] ); }*/ $result = Model::query() ->where('id', $inputData['ad_id']) ->update($data); $newitem = Model::query() ->where('id', $inputData['ad_id']) ->first(); // 创建管理员日志 $logSubject = "修改广告"; $logSvalue = $inputData['ad_id']; $logDbData = [ 'after_items' => [ $newitem->toArray(), ], 'before_items' => [ $oldItem->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); $this->clearCache(); $this->clearCacheVideo($inputData['ad_id']); return json_success($result); } /** * 详细 * @param ValidatorFactoryInterface $validatorFactory * @return mixed * @example param_json_body: {"handshake":"","data":{"ad_id":1}} */ public function detail(ValidatorFactoryInterface $validatorFactory) { $inputData = $this->getJsonData(); $rules = [ 'ad_id' => 'required|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $result = Model::query()->find($inputData['ad_id']); return json_success($result); } /** * 删除 * @param ValidatorFactoryInterface $validatorFactory * @return mixed * @example param_json_body: {"handshake":"","data":{"ad_id":5}} */ public function delete(ValidatorFactoryInterface $validatorFactory) { $inputData = $this->getJsonData(); $rules = [ 'ad_id' => 'required|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $info = Model::query() ->where('id', $inputData['ad_id']) ->first(); if (!$info) { return json_fail('广告不存在'); } $result = Model::query() ->where('id', $inputData['ad_id']) ->delete(); $this->clearCache(); $this->clearCacheVideo($inputData['ad_id']); // 创建管理员日志 $oldItem = clone $info; $logSubject = "删除广告"; $logSvalue = $inputData['ad_id']; $logDbData = [ 'after_items' => [], 'before_items' => [ $oldItem->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); return json_success($result); } public function type() { $ad['position'] = AdConstrant::$position; $ad['type'] = AdConstrant::$type; return json_success($ad); } public function clearCache() { $cacheRepo = make(CacheInterface::class); $arrKeys = []; foreach (AdConstrant::$position as $postionName => $v) { $arrKeys[] = 'ad:' .$postionName; } return $cacheRepo->deleteMultiple($arrKeys); } /** * @param $id * @return bool * @throws \Psr\SimpleCache\InvalidArgumentException */ protected function clearCacheVideo($id) { $cacheKey = 'ad:raw:'. $id . ':empty'; $cacheRepo = make(CacheInterface::class); return $cacheRepo->delete($cacheKey); } }