Modules_Manage_Controllers_BulletinController.proxy.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. use \Hyperf\Di\Aop\ProxyTrait;
  14. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  15. function __construct()
  16. {
  17. if (method_exists(parent::class, '__construct')) {
  18. parent::__construct(...func_get_args());
  19. }
  20. $this->__handlePropertyHandler(__CLASS__);
  21. }
  22. /**
  23. * 首页数据
  24. * @param ValidatorFactoryInterface $validatorFactory
  25. * @return mixed
  26. */
  27. public function lists(ValidatorFactoryInterface $validatorFactory)
  28. {
  29. $inputData = $this->getJsonData();
  30. $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'title' => 'nullable', 'status' => 'nullable|int|in:1,2', 'device' => 'nullable|int|in:1,2,3'];
  31. $validator = $validatorFactory->make($inputData, $rules);
  32. if ($validator->fails()) {
  33. return json_fail($validator->errors()->first());
  34. }
  35. $bulletinModel = BulletinModel::query()->whereNull('deleted_at')->where(function ($quire) use($inputData) {
  36. if ($inputData['title']) {
  37. $quire->where('title', 'like', '%' . $inputData['title'] . '%');
  38. }
  39. if ($inputData['status']) {
  40. $quire->where('status', $inputData['status']);
  41. }
  42. if ($inputData['device']) {
  43. $quire->where('device', $inputData['device']);
  44. }
  45. })->orderByDesc('sort')->paginate($inputData['page_size'], ['*'], 'page', $inputData['page']);
  46. return json_success($bulletinModel);
  47. }
  48. /**
  49. * 添加
  50. * @param ValidatorFactoryInterface $validatorFactory
  51. * @return mixed
  52. */
  53. public function create(ValidatorFactoryInterface $validatorFactory)
  54. {
  55. $inputData = $this->getJsonData();
  56. $rules = ['title' => 'required', 'status' => 'required|int|in:1,2', 'sort' => 'required|int', 'device' => 'required|int|in:1,2,3', 'start_time' => 'required|date', 'end_time' => 'required|date|after_or_equal:start_time', 'text' => 'required'];
  57. $validator = $validatorFactory->make($inputData, $rules);
  58. if ($validator->fails()) {
  59. return json_fail($validator->errors()->first());
  60. }
  61. $bulletinModel = BulletinModel::query()->create($inputData);
  62. if (!$bulletinModel) {
  63. return json_fail('添加失败请从试!');
  64. }
  65. $this->setCacheBulletin();
  66. return json_success('添加成功!');
  67. }
  68. /**
  69. * 修改
  70. * @param ValidatorFactoryInterface $validatorFactory
  71. * @return mixed
  72. */
  73. public function update(ValidatorFactoryInterface $validatorFactory)
  74. {
  75. $inputData = $this->getJsonData();
  76. $rules = ['id' => 'required|int', 'title' => 'required', 'status' => 'required|int|in:1,2', 'sort' => 'required|int', 'device' => 'required|int|in:1,2,3', 'start_time' => 'required|date', 'end_time' => 'required|date|after_or_equal:start_time', 'text' => 'required'];
  77. $validator = $validatorFactory->make($inputData, $rules);
  78. if ($validator->fails()) {
  79. return json_fail($validator->errors()->first());
  80. }
  81. $id = $inputData['id'];
  82. unset($inputData['id']);
  83. $bulletinModel = BulletinModel::query()->where('id', $id)->update($inputData);
  84. if (!$bulletinModel) {
  85. return json_fail('修改失败请从试!');
  86. }
  87. $this->setCacheBulletin();
  88. return json_success('修改成功!');
  89. }
  90. public function delete(ValidatorFactoryInterface $validatorFactory)
  91. {
  92. $inputData = $this->getJsonData();
  93. $rules = ['id' => 'required|int'];
  94. $validator = $validatorFactory->make($inputData, $rules);
  95. if ($validator->fails()) {
  96. return json_fail($validator->errors()->first());
  97. }
  98. $bulletinModel = BulletinModel::query()->where('id', $inputData['id'])->update(['deleted_at' => Carbon::now()->toDateTimeString()]);
  99. if (!$bulletinModel) {
  100. return json_fail('删除失败请从试!');
  101. }
  102. $this->setCacheBulletin();
  103. return json_success('删除成功!');
  104. }
  105. /**
  106. * 重置缓存
  107. */
  108. public function setCacheBulletin()
  109. {
  110. $cacheInterface = make(CacheInterface::class);
  111. // 安卓的
  112. $cacheInterface->delete('bulletin:1');
  113. // IOS的
  114. $cacheInterface->delete('bulletin:2');
  115. }
  116. }