| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- /**
- * 广告
- */
- declare (strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Constants\AdConstrant;
- use App\Model\AdModel;
- use App\Model\AdModel as Model;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\Service\CommonService;
- use App\Repositories\Service\QueueService;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Psr\SimpleCache\CacheInterface;
- class AdController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_body: {"handshake":"","data":{"page":1,"page_size":20}}
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->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);
- }
- }
|