| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Repositories\AnnounceRepository;
- use App\Repositories\Criterias\AnnouncePagesCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use Illuminate\Support\Facades\Validator;
- use Modules\ManageApi\Responses\AnnouncePagesResponse;
- use Modules\ManageApi\Responses\CommonSuccessUidResponse;
- use Modules\ManageApi\Responses\NotIncrementIdDetailResponse;
- class AnnounceController extends BaseController
- {
- /**
- * 公告列表
- * @return \Illuminate\Http\JsonResponse|AnnouncePagesResponse
- */
- public function lists(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- // 针对layui table自动分页请求参数,优先GET值.
- $queryPage = request()->has('page') ? request()->input('page', 1) : null;
- $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
- if (!is_null($queryPage)) {
- $inputData['page'] = $queryPage;
- }
- if (!is_null($queryPageSize)) {
- $inputData['page_size'] = $queryPageSize;
- }
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'type' => 'nullable|integer',
- 'title' => 'nullable',
- 'uid' => 'nullable',
- ];
- $validator = Validator::make($inputData,$rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $orders = [
- ['created_at', 'DESC'],
- ];
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 0);
- $items = $repo->pushCriteria(new AnnouncePagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- return new AnnouncePagesResponse($items);
- }
- /**
- * 应用详情
- * @return \Illuminate\Http\JsonResponse|NotIncrementIdDetailResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function detail(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string',
- ];
- $validator = Validator::make($inputData,$rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $video = $repo->findBy('uid', $inputData['uid']);
- return new NotIncrementIdDetailResponse($video);
- }
- /**
- * 添加应用
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function create(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'title' => 'required|max:255',
- 'type' => 'required|integer|max:6',
- 'content' => 'required|string',
- 'status' => 'nullable|integer',
- ];
- $validator = Validator::make($inputData,$rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $setarr = [
- 'title' => $inputData['title'],
- 'content' => $inputData['content'],
- 'type' => $inputData['type'],
- 'status' => $inputData['status'] ?? 1,
- ];
- $result = $repo->create($setarr);
- if (!$result) {
- return json_fail('创建失败');
- } else {
- $logSubject = '添加一个公告, 名称:' . $result->title;
- $logSvalue = $result->uid;
- $logDbData = [
- 'after_items' => [
- $result->toArray(),
- ],
- 'before_items' => [
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($result->uid);
- }
- }
- public function editStatus(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string'
- ];
- $validator = Validator::make($inputData,$rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $repo->findBy('uid', $inputData['uid']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- $app->status = $app->status === 1 ? 2 : 1;
- $result = $app->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '编辑一个公告, 名称:' . $app->title;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->uid);
- }
- }
- /**
- * 编辑应用
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function modify(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'title' => 'required|max:255',
- 'type' => 'required|integer|max:6',
- 'content' => 'required|string',
- 'status' => 'nullable|integer',
- ];
- $validator = Validator::make($inputData,$rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = $repo->findBy('uid', $inputData['uid']);
- if (empty($item)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldItem = clone $item;
- if (isset($inputData['title'])) {
- $item->title = trim($inputData['title']);
- }
- if (isset($inputData['content'])) {
- $item->content = rtrim($inputData['content']);
- }
- if (isset($inputData['type'])) {
- $item->type = intval($inputData['type']);
- }
- $item->status = $inputData['status'];
- $result = $item->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '编辑一个公告, 名称:' . $item->title;
- $logSvalue = $item->uid;
- $logDbData = [
- 'after_items' => [
- $item->toArray(),
- ],
- 'before_items' => [
- $oldItem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($item->uid);
- }
- }
- /**
- * 应用删除
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function delete(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required',
- ];
- $validator = Validator::make($inputData,$rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = $repo->findBy('uid', $inputData['uid']);
- if (empty($item)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldItem = clone $item;
- $result = $item->delete();
- if (!$result) {
- return json_fail('删除失败');
- } else {
- $logSubject = '删除一个应用,名称:' . $item->title;
- $logSvalue = $item->uid;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $oldItem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($item->uid);
- }
- }
- /**
- * 批量操作应用
- * @return \Illuminate\Http\JsonResponse|mixed
- */
- public function batch(Validator $validatorFactory, AnnounceRepository $repo)
- {
- $inputData = $this->getJsonData();
- $operateName = $inputData['operate_name'] ?? '';
- switch ($operateName) {
- case 'delete':
- $itemPks = $inputData['item_pks'] ?? [];
- if (!empty($itemPks)) {
- $resultItems = $repo->makeModel()->query()->whereIn('uid', $itemPks)->delete();
- if ($resultItems) {
- $strPks = implode(',', $itemPks);
- $logSubject = '批量删除多个公告';
- $logSvalue = $strPks;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- }
- }
- $data = [
- 'result' => 'success',
- 'pks' => $itemPks,
- ];
- return json_success($data);
- break;
- default:
- break;
- }
- return json_fail('未知操作');
- }
- }
|