Article.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\controller\admin;
  3. use app\extra\basic\Base;
  4. use app\extra\service\blue\ArticleService;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\blue\BlueArticle;
  7. use DI\Attribute\Inject;
  8. use LinFly\Annotation\Route\Controller;
  9. use LinFly\Annotation\Route\Middleware;
  10. use LinFly\Annotation\Route\Route;
  11. use support\Request;
  12. use support\Response;
  13. #[Controller(prefix: "/api/article"),Middleware(AuthMiddleware::class)]
  14. class Article extends Base
  15. {
  16. #[Inject]
  17. protected BlueArticle $model;
  18. #[Inject]
  19. protected ArticleService $service;
  20. #[Route(path: "list",methods: "get")]
  21. public function getDataList(Request $request): Response
  22. {
  23. try {
  24. $param = $request->all();
  25. $list = $this->service->getList($param);
  26. return successTrans("success.data",pageFormat($list),200);
  27. } catch (\Throwable $throwable) {
  28. return error($throwable->getMessage());
  29. }
  30. }
  31. /**
  32. * 编辑信息
  33. * @param Request $request
  34. * @return Response
  35. */
  36. #[Route(path: "save",methods: "post")]
  37. public function setListData(Request $request): Response
  38. {
  39. try {
  40. $param = $request->post();
  41. if (isset($param['id'])) {
  42. $factory = $this->model->where("id",$param['id'])->findOrEmpty();
  43. if ($factory->isEmpty()) return errorTrans("error.data");
  44. }
  45. if (isset($param['category_path'])) {
  46. $pathNum = count($param['category_path']);
  47. $param['category'] = $param['category_path'][($pathNum-1)];
  48. $param['category_path'] = json_encode($param['category_path']);
  49. }
  50. $param['attribute'] = empty($param['attribute']) ? [] : json_encode($param['attribute']);
  51. $state = $this->model->setAutoData($param);
  52. if (!$state) return errorTrans("error.data");
  53. return successTrans("success.data");
  54. } catch (\Throwable $throwable) {
  55. return error($throwable->getMessage());
  56. }
  57. }
  58. /**
  59. * 批量操作
  60. * @param Request $request
  61. * @return Response
  62. */
  63. #[Route(path: "batch",methods: "post")]
  64. public function setGoodsBatch(Request $request): Response
  65. {
  66. try {
  67. $param = $this->_valid([
  68. "id.require" => "参数错误",
  69. "value.require" => "参数错误",
  70. "field.require" => "参数错误",
  71. "type.require" => "参数错误"
  72. ],"post");
  73. if (!is_array($param)) return error($param);
  74. if ($param['type'] == "batch") {
  75. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  76. } else {
  77. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  78. }
  79. if (!$state) return errorTrans("error.data");
  80. return successTrans("success.data");
  81. } catch (\Throwable $throwable) {
  82. return error($throwable->getMessage());
  83. }
  84. }
  85. /**
  86. * 删除
  87. * @param Request $request
  88. * @return Response
  89. */
  90. #[Route(path: "del",methods: "post")]
  91. public function delData(Request $request): Response
  92. {
  93. try {
  94. $param = $this->_valid([
  95. "id.require" => "参数错误",
  96. ],"post");
  97. if (!is_array($param)) return error($param);
  98. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  99. if ($data->isEmpty()) return errorTrans("empty.data");
  100. $state = $data->delete();
  101. if (!$state) return errorTrans("error.data");
  102. return successTrans("success.data");
  103. } catch (\Throwable $throwable) {
  104. return error($throwable->getMessage());
  105. }
  106. }
  107. }