BulletinController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * 公告管理
  4. */
  5. declare(strict_types=1);
  6. namespace Modules\Manage\Controllers;
  7. use App\Model\BulletinModel;
  8. use Carbon\Carbon;
  9. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  10. use Psr\SimpleCache\CacheInterface;
  11. class BulletinController extends BaseController
  12. {
  13. /**
  14. * 首页数据
  15. * @param ValidatorFactoryInterface $validatorFactory
  16. * @return mixed
  17. */
  18. public function lists(ValidatorFactoryInterface $validatorFactory)
  19. {
  20. $inputData = $this->getJsonData();
  21. $rules = [
  22. 'page' => 'required|int',
  23. 'page_size' => 'required|int',
  24. 'title' => 'nullable',
  25. 'status' => 'nullable|int|in:1,2',
  26. 'device' => 'nullable|int|in:1,2,3',
  27. ];
  28. $validator = $validatorFactory->make($inputData, $rules);
  29. if ($validator->fails()) {
  30. return json_fail($validator->errors()->first());
  31. }
  32. $bulletinModel = BulletinModel::query()
  33. ->whereNull('deleted_at')
  34. ->where(function ($quire) use ($inputData) {
  35. if ($inputData['title']) {
  36. $quire->where('title', 'like', '%' . $inputData['title'] . '%');
  37. }
  38. if ($inputData['status']) {
  39. $quire->where('status', $inputData['status']);
  40. }
  41. if ($inputData['device']) {
  42. $quire->where('device', $inputData['device']);
  43. }
  44. })
  45. ->orderByDesc('sort')
  46. ->paginate($inputData['page_size'], ['*'], 'page', $inputData['page']);
  47. return json_success($bulletinModel);
  48. }
  49. /**
  50. * 添加
  51. * @param ValidatorFactoryInterface $validatorFactory
  52. * @return mixed
  53. */
  54. public function create(ValidatorFactoryInterface $validatorFactory)
  55. {
  56. $inputData = $this->getJsonData();
  57. $rules = [
  58. 'title' => 'required',
  59. 'status' => 'required|int|in:1,2',
  60. 'sort' => 'required|int',
  61. 'device' => 'required|int|in:1,2,3',
  62. 'start_time' => 'required|date',
  63. 'end_time' => 'required|date|after_or_equal:start_time',
  64. 'text' => 'required',
  65. ];
  66. $validator = $validatorFactory->make($inputData, $rules);
  67. if ($validator->fails()) {
  68. return json_fail($validator->errors()->first());
  69. }
  70. $bulletinModel = BulletinModel::query()->create($inputData);
  71. if (!$bulletinModel) {
  72. return json_fail('添加失败请从试!');
  73. }
  74. $this->setCacheBulletin();
  75. return json_success('添加成功!');
  76. }
  77. /**
  78. * 修改
  79. * @param ValidatorFactoryInterface $validatorFactory
  80. * @return mixed
  81. */
  82. public function update(ValidatorFactoryInterface $validatorFactory)
  83. {
  84. $inputData = $this->getJsonData();
  85. $rules = [
  86. 'id' => 'required|int',
  87. 'title' => 'required',
  88. 'status' => 'required|int|in:1,2',
  89. 'sort' => 'required|int',
  90. 'device' => 'required|int|in:1,2,3',
  91. 'start_time' => 'required|date',
  92. 'end_time' => 'required|date|after_or_equal:start_time',
  93. 'text' => 'required',
  94. ];
  95. $validator = $validatorFactory->make($inputData, $rules);
  96. if ($validator->fails()) {
  97. return json_fail($validator->errors()->first());
  98. }
  99. $id = $inputData['id'];
  100. unset($inputData['id']);
  101. $bulletinModel = BulletinModel::query()->where('id', $id)->update($inputData);
  102. if (!$bulletinModel) {
  103. return json_fail('修改失败请从试!');
  104. }
  105. $this->setCacheBulletin();
  106. return json_success('修改成功!');
  107. }
  108. public function delete(ValidatorFactoryInterface $validatorFactory)
  109. {
  110. $inputData = $this->getJsonData();
  111. $rules = [
  112. 'id' => 'required|int',
  113. ];
  114. $validator = $validatorFactory->make($inputData, $rules);
  115. if ($validator->fails()) {
  116. return json_fail($validator->errors()->first());
  117. }
  118. $bulletinModel = BulletinModel::query()->where('id', $inputData['id'])
  119. ->update([
  120. 'deleted_at' => Carbon::now()->toDateTimeString()
  121. ]);
  122. if (!$bulletinModel) {
  123. return json_fail('删除失败请从试!');
  124. }
  125. $this->setCacheBulletin();
  126. return json_success('删除成功!');
  127. }
  128. /**
  129. * 重置缓存
  130. */
  131. public function setCacheBulletin()
  132. {
  133. $cacheInterface = make(CacheInterface::class);
  134. // 安卓的
  135. $cacheInterface->delete('bulletin:1');
  136. // IOS的
  137. $cacheInterface->delete('bulletin:2');
  138. }
  139. }