AdController.php 12 KB

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