Modules_Manage_Controllers_AdController.proxy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * 广告
  4. */
  5. declare (strict_types=1);
  6. namespace Modules\Manage\Controllers;
  7. use App\Constants\AdConstrant;
  8. use App\Model\AdModel;
  9. use App\Model\AdModel as Model;
  10. use App\Repositories\ConfigRepoistory;
  11. use App\Repositories\Service\CommonService;
  12. use App\Repositories\Service\QueueService;
  13. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  14. use Psr\SimpleCache\CacheInterface;
  15. class AdController extends BaseController
  16. {
  17. use \Hyperf\Di\Aop\ProxyTrait;
  18. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  19. function __construct()
  20. {
  21. if (method_exists(parent::class, '__construct')) {
  22. parent::__construct(...func_get_args());
  23. }
  24. $this->__handlePropertyHandler(__CLASS__);
  25. }
  26. /**
  27. * 列表
  28. * @param ValidatorFactoryInterface $validatorFactory
  29. * @return mixed
  30. * @example param_json_body: {"handshake":"","data":{"page":1,"page_size":20}}
  31. */
  32. public function lists(ValidatorFactoryInterface $validatorFactory)
  33. {
  34. $inputData = $this->getJsonData();
  35. $rules = ['page' => 'required|int', 'page_size' => 'required|int'];
  36. $validator = $validatorFactory->make($inputData, $rules);
  37. if ($validator->fails()) {
  38. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  39. return json_fail($validator->errors()->first());
  40. }
  41. $searchParams = data_get($inputData, 'search', []);
  42. $arrWhere = [];
  43. $model = AdModel::query();
  44. if (!empty($searchParams['position'])) {
  45. $arrWhere['position'] = $searchParams['position'];
  46. }
  47. if (!empty($searchParams['status'])) {
  48. $arrWhere['status'] = $searchParams['status'];
  49. }
  50. if (!empty($searchParams['type'])) {
  51. $arrWhere['type'] = $searchParams['type'];
  52. }
  53. if (!empty($arrWhere)) {
  54. $model = $model->where($arrWhere);
  55. }
  56. if (!empty($searchParams['title'])) {
  57. $model = $model->where('title', 'LIKE', '%' . $searchParams['title'] . '%');
  58. }
  59. $result = $model->orderBy('id', 'DESC')->paginate((int) $inputData['page_size'], ['*'], 'page', (int) $inputData['page']);
  60. //处理图片/视频路径
  61. // foreach ($result as $v) {
  62. // $v->cover = make(CommonService::class)->getImageUrl((string)$v->cover, true); // 图片
  63. // $v->play = make(CommonService::class)->getVideoUrl((string)$v->play, true); // 视频
  64. // }
  65. return json_success($result);
  66. }
  67. /**
  68. * 新增 / 修改
  69. * @param ValidatorFactoryInterface $validatorFactory
  70. * @return mixed
  71. */
  72. public function save(ValidatorFactoryInterface $validatorFactory)
  73. {
  74. $inputData = $this->getJsonData();
  75. if (isset($inputData['ad_id'])) {
  76. $rules = [
  77. 'ad_id' => 'int',
  78. 'position' => 'string',
  79. 'title' => 'string',
  80. 'cover' => 'string',
  81. // 'start_time' => 'string',
  82. // 'end_time' => 'string',
  83. 'sort' => 'int',
  84. 'url' => 'string',
  85. 'scheme' => 'string',
  86. 'type' => 'int',
  87. 'play' => 'string',
  88. 'status' => 'nullable|int',
  89. 'resolution' => 'required',
  90. ];
  91. } else {
  92. $rules = [
  93. 'ad_id' => 'int',
  94. 'position' => 'required|string',
  95. 'title' => 'required|string',
  96. 'cover' => 'required|string',
  97. // 'start_time' => 'string',
  98. // 'end_time' => 'string',
  99. 'sort' => 'int',
  100. 'url' => 'string',
  101. 'scheme' => 'required|string',
  102. 'type' => 'required|int',
  103. 'play' => 'string',
  104. 'status' => 'nullable|int',
  105. 'resolution' => 'required',
  106. ];
  107. }
  108. $validator = $validatorFactory->make($inputData, $rules);
  109. if ($validator->fails()) {
  110. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  111. return json_fail($validator->errors()->first());
  112. }
  113. if (!isset($inputData['ad_id'])) {
  114. if ($inputData['type'] == 2 && !$inputData['play']) {
  115. return json_fail('添加视频广告请上传视频');
  116. }
  117. }
  118. $resolution = strtolower(trim(data_get($inputData, 'resolution', '')));
  119. $create = [
  120. 'position' => $inputData['position'] ?? '',
  121. 'title' => $inputData['title'] ?? '',
  122. 'cover' => $inputData['cover'] ?? '',
  123. // 'start_time' => $inputData['start_time'] ?? '',
  124. // 'end_time' => $inputData['end_time'] ?? '',
  125. 'sort' => $inputData['sort'] ?? '',
  126. 'scheme' => data_get($inputData, 'scheme', ''),
  127. 'link' => $inputData['url'],
  128. 'type' => $inputData['type'] ?? '',
  129. 'play' => $inputData['play'] ?? '',
  130. 'resolution' => $resolution,
  131. ];
  132. if (isset($inputData['status'])) {
  133. $create['status'] = intval($inputData['status']);
  134. }
  135. // $create = action_data($create);
  136. if (isset($inputData['ad_id'])) {
  137. $oldItem = Model::query()->where('id', $inputData['ad_id'])->first();
  138. if (!$oldItem) {
  139. return json_fail('广告不存在');
  140. }
  141. $result = Model::query()->where('id', $inputData['ad_id'])->update($create);
  142. $newitem = Model::query()->where('id', $inputData['ad_id'])->first();
  143. // 创建管理员日志
  144. $logSubject = "修改广告";
  145. $logSvalue = $inputData['ad_id'];
  146. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$oldItem->toArray()]];
  147. $this->log($logSubject, $logSvalue, $logDbData);
  148. } else {
  149. $result = Model::query()->create($create);
  150. if (!$result) {
  151. return json_fail('创建失败');
  152. }
  153. // 创建管理员日志
  154. $logSubject = "创建广告";
  155. $logSvalue = $result->id;
  156. $logDbData = ['after_items' => [$result->toArray()], 'before_items' => []];
  157. $this->log($logSubject, $logSvalue, $logDbData);
  158. }
  159. $this->clearCache();
  160. if (isset($inputData['ad_id'])) {
  161. $this->clearCacheVideo($inputData['ad_id']);
  162. }
  163. return json_success($result);
  164. }
  165. /**
  166. * 编辑(标示上架/下架)
  167. * @author x.b
  168. * @param ValidatorFactoryInterface $validatorFactory
  169. * @return mixed
  170. * @example param_json_data string: {"handshake":"","data":{"ad_id":7,"status":1}}
  171. */
  172. public function update(ValidatorFactoryInterface $validatorFactory)
  173. {
  174. $inputData = $this->getJsonData();
  175. $rules = ['ad_id' => 'required|int', 'status' => 'required|in:1,2,3,4'];
  176. $validator = $validatorFactory->make($inputData, $rules);
  177. if ($validator->fails()) {
  178. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  179. return json_fail($validator->errors()->first());
  180. }
  181. $data = ['id' => $inputData['ad_id'], 'status' => $inputData['status'] ?? ''];
  182. $data = action_data($data);
  183. $oldItem = Model::query()->where('id', $inputData['ad_id'])->first();
  184. if (!$oldItem) {
  185. return json_fail('广告不存在');
  186. }
  187. /*if ($inputData['status'] == 1 && strpos($oldItem->play, '.mp4')) {
  188. //状态改为转码中
  189. $data['status'] = 2;
  190. //转码
  191. make(QueueService::class)->addShiftVideoJob(
  192. [
  193. 'path' => $oldItem->play,
  194. 'table' => 'ad',
  195. 'id' => $oldItem->id,
  196. 'update' => 'play',
  197. ]
  198. );
  199. }*/
  200. $result = Model::query()->where('id', $inputData['ad_id'])->update($data);
  201. $newitem = Model::query()->where('id', $inputData['ad_id'])->first();
  202. // 创建管理员日志
  203. $logSubject = "修改广告";
  204. $logSvalue = $inputData['ad_id'];
  205. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$oldItem->toArray()]];
  206. $this->log($logSubject, $logSvalue, $logDbData);
  207. $this->clearCache();
  208. $this->clearCacheVideo($inputData['ad_id']);
  209. return json_success($result);
  210. }
  211. /**
  212. * 详细
  213. * @param ValidatorFactoryInterface $validatorFactory
  214. * @return mixed
  215. * @example param_json_body: {"handshake":"","data":{"ad_id":1}}
  216. */
  217. public function detail(ValidatorFactoryInterface $validatorFactory)
  218. {
  219. $inputData = $this->getJsonData();
  220. $rules = ['ad_id' => 'required|int'];
  221. $validator = $validatorFactory->make($inputData, $rules);
  222. if ($validator->fails()) {
  223. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  224. return json_fail($validator->errors()->first());
  225. }
  226. $result = Model::query()->find($inputData['ad_id']);
  227. return json_success($result);
  228. }
  229. /**
  230. * 删除
  231. * @param ValidatorFactoryInterface $validatorFactory
  232. * @return mixed
  233. * @example param_json_body: {"handshake":"","data":{"ad_id":5}}
  234. */
  235. public function delete(ValidatorFactoryInterface $validatorFactory)
  236. {
  237. $inputData = $this->getJsonData();
  238. $rules = ['ad_id' => 'required|int'];
  239. $validator = $validatorFactory->make($inputData, $rules);
  240. if ($validator->fails()) {
  241. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  242. return json_fail($validator->errors()->first());
  243. }
  244. $info = Model::query()->where('id', $inputData['ad_id'])->first();
  245. if (!$info) {
  246. return json_fail('广告不存在');
  247. }
  248. $result = Model::query()->where('id', $inputData['ad_id'])->delete();
  249. $this->clearCache();
  250. $this->clearCacheVideo($inputData['ad_id']);
  251. // 创建管理员日志
  252. $oldItem = clone $info;
  253. $logSubject = "删除广告";
  254. $logSvalue = $inputData['ad_id'];
  255. $logDbData = ['after_items' => [], 'before_items' => [$oldItem->toArray()]];
  256. $this->log($logSubject, $logSvalue, $logDbData);
  257. return json_success($result);
  258. }
  259. public function type()
  260. {
  261. $ad['position'] = AdConstrant::$position;
  262. $ad['type'] = AdConstrant::$type;
  263. return json_success($ad);
  264. }
  265. public function clearCache()
  266. {
  267. $cacheRepo = make(CacheInterface::class);
  268. $arrKeys = [];
  269. foreach (AdConstrant::$position as $postionName => $v) {
  270. $arrKeys[] = 'ad:' . $postionName;
  271. }
  272. return $cacheRepo->deleteMultiple($arrKeys);
  273. }
  274. /**
  275. * @param $id
  276. * @return bool
  277. * @throws \Psr\SimpleCache\InvalidArgumentException
  278. */
  279. protected function clearCacheVideo($id)
  280. {
  281. $cacheKey = 'ad:raw:' . $id . ':empty';
  282. $cacheRepo = make(CacheInterface::class);
  283. return $cacheRepo->delete($cacheKey);
  284. }
  285. }