| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * 公告管理
- */
- declare (strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Model\BulletinModel;
- use Carbon\Carbon;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Psr\SimpleCache\CacheInterface;
- class BulletinController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 首页数据
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'title' => 'nullable', 'status' => 'nullable|int|in:1,2', 'device' => 'nullable|int|in:1,2,3'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $bulletinModel = BulletinModel::query()->whereNull('deleted_at')->where(function ($quire) use($inputData) {
- if ($inputData['title']) {
- $quire->where('title', 'like', '%' . $inputData['title'] . '%');
- }
- if ($inputData['status']) {
- $quire->where('status', $inputData['status']);
- }
- if ($inputData['device']) {
- $quire->where('device', $inputData['device']);
- }
- })->orderByDesc('sort')->paginate($inputData['page_size'], ['*'], 'page', $inputData['page']);
- return json_success($bulletinModel);
- }
- /**
- * 添加
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function create(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $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'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $bulletinModel = BulletinModel::query()->create($inputData);
- if (!$bulletinModel) {
- return json_fail('添加失败请从试!');
- }
- $this->setCacheBulletin();
- return json_success('添加成功!');
- }
- /**
- * 修改
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function update(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $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'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $id = $inputData['id'];
- unset($inputData['id']);
- $bulletinModel = BulletinModel::query()->where('id', $id)->update($inputData);
- if (!$bulletinModel) {
- return json_fail('修改失败请从试!');
- }
- $this->setCacheBulletin();
- return json_success('修改成功!');
- }
- public function delete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $bulletinModel = BulletinModel::query()->where('id', $inputData['id'])->update(['deleted_at' => Carbon::now()->toDateTimeString()]);
- if (!$bulletinModel) {
- return json_fail('删除失败请从试!');
- }
- $this->setCacheBulletin();
- return json_success('删除成功!');
- }
- /**
- * 重置缓存
- */
- public function setCacheBulletin()
- {
- $cacheInterface = make(CacheInterface::class);
- // 安卓的
- $cacheInterface->delete('bulletin:1');
- // IOS的
- $cacheInterface->delete('bulletin:2');
- }
- }
|