| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- declare(strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Model\CommentModel;
- use App\Repositories\CommentRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- /**
- * 评论管理
- */
- class CommentController extends BaseController
- {
- /**
- * 首页数据
- * @param ValidatorFactoryInterface $validatorFactory
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'stime' => 'nullable|date',
- 'otime' => 'nullable|date',
- 'module' => 'nullable|string',
- 'like_content' => 'nullable|string',
- 'digest' => 'nullable|numeric',
- 'top' => 'nullable|numeric',
- 'user_uid' => 'nullable',
- 'page' => 'nullable|numeric',
- 'page_size' => 'nullable|numeric'
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $commentModel = make(CommentRepository::class);
- $commentGroup = clone $commentModel;
- $module = $commentGroup->getModule();
- $commentList = make(CommentRepository::class)->getLists($inputData);
- $data = [
- 'dataList'=>$commentList,
- 'module'=>$module
- ];
- return json_success($data);
- }
- /**
- * 修改状态
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function editType(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'comment_id' => 'required|numeric',
- 'type' => 'required|string|in:is_digest,is_top',
- 'val' => 'required|numeric',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $res = CommentModel::query()
- ->where('id', $inputData['comment_id'])
- ->update([$inputData['type'] => $inputData['val']]);
- if (!$res) {
- return json_fail('修改失败');
- }
- return json_success('修改成功');
- }
- /**
- * 删除评论
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function delete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'comment_id' => 'required|numeric'
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $res = CommentModel::query()
- ->where('id', $inputData['comment_id'])
- ->delete();
- if (!$res) {
- return json_fail('修改失败');
- }
- return json_success('修改成功');
- }
- /**
- * 批量删除评论
- *
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function deleteLists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'ids' => 'required'
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $ids = $inputData['ids'];
- $aIds = explode(',', $ids);
- $res = CommentModel::query()->whereIn('id', $aIds)->delete();
- if ($res) {
- return json_success($res);
- } else {
- return json_fail('删除失败');
- }
- }
- }
|