AnnounceController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Repositories\AnnounceRepository;
  5. use App\Repositories\Criterias\AnnouncePagesCriteria;
  6. use App\Repositories\Criterias\OrderLimitCriteria;
  7. use Illuminate\Support\Facades\Validator;
  8. use Modules\ManageApi\Responses\AnnouncePagesResponse;
  9. use Modules\ManageApi\Responses\CommonSuccessUidResponse;
  10. use Modules\ManageApi\Responses\NotIncrementIdDetailResponse;
  11. class AnnounceController extends BaseController
  12. {
  13. /**
  14. * 公告列表
  15. * @return \Illuminate\Http\JsonResponse|AnnouncePagesResponse
  16. */
  17. public function lists(Validator $validatorFactory, AnnounceRepository $repo)
  18. {
  19. $inputData = $this->getJsonData();
  20. // 针对layui table自动分页请求参数,优先GET值.
  21. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  22. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  23. if (!is_null($queryPage)) {
  24. $inputData['page'] = $queryPage;
  25. }
  26. if (!is_null($queryPageSize)) {
  27. $inputData['page_size'] = $queryPageSize;
  28. }
  29. $rules = [
  30. 'page' => 'nullable|integer',
  31. 'page_size' => 'nullable|integer',
  32. 'type' => 'nullable|integer',
  33. 'title' => 'nullable',
  34. 'uid' => 'nullable',
  35. ];
  36. $validator = Validator::make($inputData,$rules);
  37. if ($validator->fails()) {
  38. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  39. return json_fail($validator->errors()->first());
  40. }
  41. $orders = [
  42. ['created_at', 'DESC'],
  43. ];
  44. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  45. if ($pageSize > 500) {
  46. $pageSize = CommonConstant::PAGE_SIZE;
  47. }
  48. $page = format_page($inputData['page'] ?? 0);
  49. $items = $repo->pushCriteria(new AnnouncePagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  50. return new AnnouncePagesResponse($items);
  51. }
  52. /**
  53. * 应用详情
  54. * @return \Illuminate\Http\JsonResponse|NotIncrementIdDetailResponse
  55. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  56. */
  57. public function detail(Validator $validatorFactory, AnnounceRepository $repo)
  58. {
  59. $inputData = $this->getJsonData();
  60. $rules = [
  61. 'uid' => 'required|string',
  62. ];
  63. $validator = Validator::make($inputData,$rules);
  64. if ($validator->fails()) {
  65. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  66. return json_fail($validator->errors()->first());
  67. }
  68. $video = $repo->findBy('uid', $inputData['uid']);
  69. return new NotIncrementIdDetailResponse($video);
  70. }
  71. /**
  72. * 添加应用
  73. * @return \Illuminate\Http\JsonResponse|mixed
  74. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  75. */
  76. public function create(Validator $validatorFactory, AnnounceRepository $repo)
  77. {
  78. $inputData = $this->getJsonData();
  79. $rules = [
  80. 'title' => 'required|max:255',
  81. 'type' => 'required|integer|max:6',
  82. 'content' => 'required|string',
  83. 'status' => 'nullable|integer',
  84. ];
  85. $validator = Validator::make($inputData,$rules);
  86. if ($validator->fails()) {
  87. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  88. return json_fail($validator->errors()->first());
  89. }
  90. $setarr = [
  91. 'title' => $inputData['title'],
  92. 'content' => $inputData['content'],
  93. 'type' => $inputData['type'],
  94. 'status' => $inputData['status'] ?? 1,
  95. ];
  96. $result = $repo->create($setarr);
  97. if (!$result) {
  98. return json_fail('创建失败');
  99. } else {
  100. $logSubject = '添加一个公告, 名称:' . $result->title;
  101. $logSvalue = $result->uid;
  102. $logDbData = [
  103. 'after_items' => [
  104. $result->toArray(),
  105. ],
  106. 'before_items' => [
  107. ],
  108. ];
  109. $this->log($logSubject, $logSvalue, $logDbData);
  110. return new CommonSuccessUidResponse($result->uid);
  111. }
  112. }
  113. public function editStatus(Validator $validatorFactory, AnnounceRepository $repo)
  114. {
  115. $inputData = $this->getJsonData();
  116. $rules = [
  117. 'uid' => 'required|string'
  118. ];
  119. $validator = Validator::make($inputData,$rules);
  120. if ($validator->fails()) {
  121. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  122. return json_fail($validator->errors()->first());
  123. }
  124. $app = $repo->findBy('uid', $inputData['uid']);
  125. if (empty($app)) {
  126. return json_fail(trans('common.err_resource_not_found'));
  127. }
  128. $oldApp = clone $app;
  129. $app->status = $app->status === 1 ? 2 : 1;
  130. $result = $app->save();
  131. if (!$result) {
  132. return json_fail('修改失败');
  133. } else {
  134. $logSubject = '编辑一个公告, 名称:' . $app->title;
  135. $logSvalue = $app->uid;
  136. $logDbData = [
  137. 'after_items' => [
  138. $app->toArray(),
  139. ],
  140. 'before_items' => [
  141. $oldApp->toArray(),
  142. ],
  143. ];
  144. $this->log($logSubject, $logSvalue, $logDbData);
  145. return new CommonSuccessUidResponse($app->uid);
  146. }
  147. }
  148. /**
  149. * 编辑应用
  150. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  151. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  152. */
  153. public function modify(Validator $validatorFactory, AnnounceRepository $repo)
  154. {
  155. $inputData = $this->getJsonData();
  156. $rules = [
  157. 'title' => 'required|max:255',
  158. 'type' => 'required|integer|max:6',
  159. 'content' => 'required|string',
  160. 'status' => 'nullable|integer',
  161. ];
  162. $validator = Validator::make($inputData,$rules);
  163. if ($validator->fails()) {
  164. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  165. return json_fail($validator->errors()->first());
  166. }
  167. $item = $repo->findBy('uid', $inputData['uid']);
  168. if (empty($item)) {
  169. return json_fail(trans('common.err_resource_not_found'));
  170. }
  171. $oldItem = clone $item;
  172. if (isset($inputData['title'])) {
  173. $item->title = trim($inputData['title']);
  174. }
  175. if (isset($inputData['content'])) {
  176. $item->content = rtrim($inputData['content']);
  177. }
  178. if (isset($inputData['type'])) {
  179. $item->type = intval($inputData['type']);
  180. }
  181. $item->status = $inputData['status'];
  182. $result = $item->save();
  183. if (!$result) {
  184. return json_fail('修改失败');
  185. } else {
  186. $logSubject = '编辑一个公告, 名称:' . $item->title;
  187. $logSvalue = $item->uid;
  188. $logDbData = [
  189. 'after_items' => [
  190. $item->toArray(),
  191. ],
  192. 'before_items' => [
  193. $oldItem->toArray(),
  194. ],
  195. ];
  196. $this->log($logSubject, $logSvalue, $logDbData);
  197. return new CommonSuccessUidResponse($item->uid);
  198. }
  199. }
  200. /**
  201. * 应用删除
  202. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  203. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  204. */
  205. public function delete(Validator $validatorFactory, AnnounceRepository $repo)
  206. {
  207. $inputData = $this->getJsonData();
  208. $rules = [
  209. 'uid' => 'required',
  210. ];
  211. $validator = Validator::make($inputData,$rules);
  212. if ($validator->fails()) {
  213. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  214. return json_fail($validator->errors()->first());
  215. }
  216. $item = $repo->findBy('uid', $inputData['uid']);
  217. if (empty($item)) {
  218. return json_fail(trans('common.err_resource_not_found'));
  219. }
  220. $oldItem = clone $item;
  221. $result = $item->delete();
  222. if (!$result) {
  223. return json_fail('删除失败');
  224. } else {
  225. $logSubject = '删除一个应用,名称:' . $item->title;
  226. $logSvalue = $item->uid;
  227. $logDbData = [
  228. 'after_items' => [
  229. ],
  230. 'before_items' => [
  231. $oldItem->toArray(),
  232. ],
  233. ];
  234. $this->log($logSubject, $logSvalue, $logDbData);
  235. return new CommonSuccessUidResponse($item->uid);
  236. }
  237. }
  238. /**
  239. * 批量操作应用
  240. * @return \Illuminate\Http\JsonResponse|mixed
  241. */
  242. public function batch(Validator $validatorFactory, AnnounceRepository $repo)
  243. {
  244. $inputData = $this->getJsonData();
  245. $operateName = $inputData['operate_name'] ?? '';
  246. switch ($operateName) {
  247. case 'delete':
  248. $itemPks = $inputData['item_pks'] ?? [];
  249. if (!empty($itemPks)) {
  250. $resultItems = $repo->makeModel()->query()->whereIn('uid', $itemPks)->delete();
  251. if ($resultItems) {
  252. $strPks = implode(',', $itemPks);
  253. $logSubject = '批量删除多个公告';
  254. $logSvalue = $strPks;
  255. $logDbData = [
  256. 'after_items' => [
  257. ],
  258. 'before_items' => [
  259. ],
  260. ];
  261. $this->log($logSubject, $logSvalue, $logDbData);
  262. }
  263. }
  264. $data = [
  265. 'result' => 'success',
  266. 'pks' => $itemPks,
  267. ];
  268. return json_success($data);
  269. break;
  270. default:
  271. break;
  272. }
  273. return json_fail('未知操作');
  274. }
  275. }