shareUrlsController.php 10 KB

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