| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Repositories\AppRepository;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\Criterias\ShareUrlsPagesCriteria;
- use App\Repositories\ShareUrlsRepository;
- use Illuminate\Support\Facades\Validator;
- use Modules\ManageApi\Responses\CommonSuccessUidResponse;
- use Modules\ManageApi\Responses\NotIncrementIdDetailResponse;
- use Modules\ManageApi\Responses\shareUrlsPagesResponse;
- class shareUrlsController extends BaseController
- {
- public function lists(Validator $validatorFactory, ShareUrlsRepository $shareUrlsRepository)
- {
- $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',
- 'title' => 'nullable',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 0);
- $orders = [
- ['sort', 'DESC'],
- ];
- $select = ['id', 'title', 'url', 'mom_app_uid', 'cover', 'sort', 'status', 'note', 'updated_at'];
- $items = $shareUrlsRepository->pushCriteria(new ShareUrlsPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))
- ->with(['appName'])->paginate($pageSize, $select, $page);
- return new shareUrlsPagesResponse($items);
- }
- public function editStatus(Validator $validatorFactory, ShareUrlsRepository $shareUrlsRepository)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $shareUrlsRepository->findBy('id', $inputData['id']);
- 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 = '编辑一个代理分享url地址, 名称:' . $app->title;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->id);
- }
- }
- /**
- * 添加地址
- * @param Validator $validatorFactory
- * @param ShareUrlsRepository $shareUrlsRepository
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- */
- public function create(Validator $validatorFactory, ShareUrlsRepository $shareUrlsRepository)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'title' => 'required|string|max:120',
- 'url' => 'required|string|max:700',
- 'cover' => 'nullable|string|max:700',
- 'mom_app_uid' => 'required|max:16',
- 'note' => 'nullable|max:700',
- 'sort' => 'required|integer',
- 'status' => 'nullable|integer'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $setarr = [
- 'title' => $inputData['title'],
- 'url' => $inputData['url'],
- 'cover' => $inputData['cover'],
- 'mom_app_uid' => $inputData['mom_app_uid'],
- 'note' => $inputData['note'],
- 'sort' => $inputData['sort'],
- 'status' => $inputData['status'] ?? 1,
- ];
- $result = $shareUrlsRepository->create($setarr);
- if (!$result) {
- return json_fail('创建失败');
- } else {
- $logSubject = '添加一个代理分享url地址, 标题为:' . $result->title;
- $logSvalue = $result->uid;
- $logDbData = [
- 'after_items' => [
- $result->toArray(),
- ],
- 'before_items' => [
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($result->id);
- }
- }
- /**
- * 地址详情
- * @return \Illuminate\Http\JsonResponse|NotIncrementIdDetailResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function detail(Validator $validatorFactory, ShareUrlsRepository $shareUrlsRepository)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $shareUrlsRepository->findBy('id', $inputData['id']);
- return new NotIncrementIdDetailResponse($app);
- }
- /**
- * 编辑
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function modify(Validator $validatorFactory, ShareUrlsRepository $shareUrlsRepository)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- 'title' => 'required|string|max:120',
- 'url' => 'required|string|max:700',
- 'cover' => 'nullable|string|max:700',
- 'mom_app_uid' => 'required|max:16',
- 'note' => 'nullable|max:700',
- 'sort' => 'required|integer',
- 'status' => 'nullable|integer'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $shareUrlsRepository->findBy('id', $inputData['id']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- if (isset($inputData['title'])) {
- $app->title = trim($inputData['title']);
- }
- if (isset($inputData['url'])) {
- $app->url = trim($inputData['url']);
- }
- if (isset($inputData['cover'])) {
- $app->cover = trim($inputData['cover']);
- }
- if (isset($inputData['mom_app_uid'])) {
- $app->mom_app_uid = trim($inputData['mom_app_uid']);
- }
- if (isset($inputData['note'])) {
- $app->note = trim($inputData['note']);
- }
- if (isset($inputData['status'])) {
- $app->status = trim($inputData['status']);
- }
- if (isset($inputData['sort'])) {
- $app->sort = trim($inputData['sort']);
- }
- $result = $app->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '编辑一个代理分享url地址,标题:' . $app->title;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->id);
- }
- }
- /**
- * 删除
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function delete(Validator $validatorFactory, ShareUrlsRepository $shareUrlsRepository)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $shareUrlsRepository->findBy('id', $inputData['id']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- $result = $app->delete();
- if (!$result) {
- return json_fail('删除失败');
- } else {
- $logSubject = '删除一个代理分享url地址,标题:' . $app->title;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->id);
- }
- }
- }
|