FirstPageController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Modules\Operation\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Common\Errors\Errors;
  8. use Modules\Common\Exceptions\CommonBusinessException;
  9. use Modules\Common\Services\CommonConfigService;
  10. use Modules\User\Http\Controllers\UserTrait;
  11. class FirstPageController extends CatchController
  12. {
  13. use ValidatesRequests;
  14. use UserTrait;
  15. /**
  16. * 首页列表
  17. */
  18. public function list(Request $request) {
  19. $firstPageListTypeMap = CommonConfigService::getFirstPageListTypeMap();
  20. $miniprogramTypeMap = CommonConfigService::getMiniprogramTypeMap();
  21. $miniprogramType = $request->input('miniprogram_type');
  22. $firstPageListType = $request->input('type');
  23. $result = DB::table('first_pages')
  24. ->where('is_enabled', 1)
  25. ->when($miniprogramType, function ($query, $miniprogramType){
  26. return $query->where('miniprogram_type', $miniprogramType);
  27. })->when($firstPageListType, function ($query, $firstPageListType){
  28. return $query->where('type', $firstPageListType);
  29. })->orderBy('status', 'desc')
  30. ->orderBy('id', 'desc')
  31. ->paginate($request->input('limit', 15));
  32. foreach ($result as $item) {
  33. $item->type_str = $firstPageListTypeMap[$item->type]['label'] ?? '';
  34. $item->duanjus = collect(\json_decode($item->duanjus, true))->sortBy('sort')->values();
  35. $item->miniprogram_type_str = $miniprogramTypeMap[$item->miniprogram_type]['label'] ?? '';
  36. }
  37. return $result;
  38. }
  39. /**
  40. * 添加配置
  41. * @param Request $request
  42. * @return string
  43. * @throws \Illuminate\Validation\ValidationException
  44. */
  45. public function add(Request $request) {
  46. $this->validate($request, [
  47. 'type' => 'required|in:1,2',
  48. 'status' => 'required|in:0,1',
  49. 'miniprogram_type' => 'required|in:1,2'
  50. ]);
  51. $now = date('Y-m-d H:i:s');
  52. if(1 == $request->input('status')) {
  53. DB::table('first_pages')
  54. ->where([
  55. 'type' => $request->input('type'),
  56. 'miniprogram_type' => $request->input('miniprogram_type')
  57. ])
  58. ->update(['status' => 0, 'updated_at' => $now]);
  59. }
  60. DB::table('first_pages')
  61. ->insert([
  62. 'type' => $request->input('type'),
  63. 'status' => $request->input('status'),
  64. 'miniprogram_type' => $request->input('miniprogram_type'),
  65. 'created_at' => $now,
  66. 'updated_at' => $now,
  67. ]);
  68. return 'ok';
  69. }
  70. /**
  71. * 开启配置,一个列表类型中只允许一个配置开启
  72. * @param Request $request
  73. * @return string
  74. * @throws \Illuminate\Validation\ValidationException
  75. */
  76. public function enableStatus(Request $request) {
  77. $this->validate($request, ['id' => 'required']);
  78. $info = DB::table('first_pages')
  79. ->where('id', $request->input('id'))
  80. ->first();
  81. if(!$info) {
  82. CommonBusinessException::throwError(Errors::OPERATION_FIRST_PAGE_LIST_NOT_EXISTS);
  83. }
  84. $now = date('Y-m-d H:i:s');
  85. DB::table('first_pages')
  86. ->where([
  87. 'type' => $info->type,
  88. 'miniprogram_type' => $info->miniprogram_type,
  89. ])
  90. ->update(['status' => 0, 'updated_at' => $now]);
  91. DB::table('first_pages')
  92. ->where('id', $request->input('id'))
  93. ->update(['status' => 1, 'updated_at' => $now]);
  94. return 'ok';
  95. }
  96. /**
  97. * 配置剧集
  98. * @param Request $request
  99. * @return string
  100. * @throws \Illuminate\Validation\ValidationException
  101. */
  102. public function setConfig(Request $request) {
  103. $this->validate($request, [
  104. 'id' => 'required',
  105. 'duanjus' => 'nullable|array',
  106. ]);
  107. $now = date('Y-m-d H:i:s');
  108. DB::table('first_pages')
  109. ->where('id', $request->input('id'))
  110. ->update(['duanjus' => \json_encode($request->input('duanjus', [])),
  111. 'updated_at' => $now]);
  112. return 'ok';
  113. }
  114. /**
  115. * 删除
  116. * @param Request $request
  117. * @return string
  118. * @throws \Illuminate\Validation\ValidationException
  119. */
  120. public function delete(Request $request) {
  121. $this->validate($request, [
  122. 'id' => 'required',
  123. ]);
  124. $now = date('Y-m-d H:i:s');
  125. DB::table('first_pages')
  126. ->where('id', $request->input('id'))
  127. ->where('is_enabled', 1)
  128. ->update(['is_enabled' => 0,
  129. 'updated_at' => $now]);
  130. return 'ok';
  131. }
  132. }