|
|
@@ -0,0 +1,121 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\controller\admin;
|
|
|
+
|
|
|
+use app\extra\basic\Base;
|
|
|
+use app\extra\service\blue\ArticleService;
|
|
|
+use app\middleware\AuthMiddleware;
|
|
|
+use app\model\blue\BlueArticle;
|
|
|
+use DI\Attribute\Inject;
|
|
|
+use LinFly\Annotation\Route\Controller;
|
|
|
+use LinFly\Annotation\Route\Middleware;
|
|
|
+use LinFly\Annotation\Route\Route;
|
|
|
+use support\Request;
|
|
|
+use support\Response;
|
|
|
+
|
|
|
+
|
|
|
+#[Controller(prefix: "/api/article"),Middleware(AuthMiddleware::class)]
|
|
|
+class Article extends Base
|
|
|
+{
|
|
|
+
|
|
|
+ #[Inject]
|
|
|
+ protected BlueArticle $model;
|
|
|
+
|
|
|
+ #[Inject]
|
|
|
+ protected ArticleService $service;
|
|
|
+
|
|
|
+
|
|
|
+ #[Route(path: "list",methods: "get")]
|
|
|
+ public function getDataList(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $request->all();
|
|
|
+ $list = $this->service->getList($param);
|
|
|
+ return successTrans("success.data",pageFormat($list),200);
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑信息
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ #[Route(path: "save",methods: "post")]
|
|
|
+ public function setListData(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $request->post();
|
|
|
+ if (isset($param['id'])) {
|
|
|
+ $factory = $this->model->where("id",$param['id'])->findOrEmpty();
|
|
|
+ if ($factory->isEmpty()) return errorTrans("error.data");
|
|
|
+ }
|
|
|
+ if (isset($param['category_path'])) {
|
|
|
+ $pathNum = count($param['category_path']);
|
|
|
+ $param['category'] = $param['category_path'][($pathNum-1)];
|
|
|
+ $param['category_path'] = json_encode($param['category_path']);
|
|
|
+ }
|
|
|
+ $param['attribute'] = empty($param['attribute']) ? [] : json_encode($param['attribute']);
|
|
|
+ $state = $this->model->setAutoData($param);
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
+ return successTrans("success.data");
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量操作
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ #[Route(path: "batch",methods: "post")]
|
|
|
+ public function setGoodsBatch(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $this->_valid([
|
|
|
+ "id.require" => "参数错误",
|
|
|
+ "value.require" => "参数错误",
|
|
|
+ "field.require" => "参数错误",
|
|
|
+ "type.require" => "参数错误"
|
|
|
+ ],"post");
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
+ if ($param['type'] == "batch") {
|
|
|
+ $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
|
|
|
+ } else {
|
|
|
+ $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
|
|
|
+ }
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
+ return successTrans("success.data");
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ #[Route(path: "del",methods: "post")]
|
|
|
+ public function delData(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $param = $this->_valid([
|
|
|
+ "id.require" => "参数错误",
|
|
|
+ ],"post");
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
+ $data = $this->model->where("id",$param['id'])->findOrEmpty();
|
|
|
+ if ($data->isEmpty()) return errorTrans("empty.data");
|
|
|
+ $state = $data->delete();
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
+ return successTrans("success.data");
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
+ return error($throwable->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|