Zory 3 dagar sedan
förälder
incheckning
94011cc65c
3 ändrade filer med 171 tillägg och 0 borttagningar
  1. 67 0
      app/controller/admin/StoreCategory.php
  2. 58 0
      app/functions.php
  3. 46 0
      app/model/saas/SaasStoreCategory.php

+ 67 - 0
app/controller/admin/StoreCategory.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace app\controller\admin;
+
+use app\extra\basic\Base;
+use app\extra\tools\DataExtend;
+use app\middleware\AuthMiddleware;
+use app\model\saas\SaasStoreCategory;
+use app\service\saas\CategoryService;
+use app\service\saas\GoodsService;
+use DI\Attribute\Inject;
+use LinFly\Annotation\Attributes\Route\Controller;
+use LinFly\Annotation\Attributes\Route\GetMapping;
+use LinFly\Annotation\Attributes\Route\Middleware;
+use support\Request;
+use support\Response;
+
+
+#[Controller("/api/store/category"),Middleware(AuthMiddleware::class)]
+class StoreCategory extends Base
+{
+
+    protected array $noNeedLogin = ["getCategoryList"];
+
+    #[Inject]
+    protected CategoryService $service;
+
+    #[Inject]
+    protected GoodsService $goodsService;
+
+    #[Inject]
+    protected SaasStoreCategory $model;
+
+    #[GetMapping('list')]
+    public function getCategoryList(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "poi.require"   => trans("empty.require")
+            ]);
+            if (!is_array($param)) return error($param);
+            $data = $this->service->setModel()->getList([],null,false);
+            $tree = DataExtend::arr2tree($data,"category_id","parent_id");
+            $storeCate = $this->model->where("poi_id",$param['poi'])->column('product_type',"category_id");
+            if (empty($storeCate)) return successTrans(100010,[],200);
+            $storeIds = [];
+            $storeTypes = [];
+            foreach ($storeCate as $k=>$v){
+                $storeIds[] = $k;
+                $productType = !empty($v) ? json_decode($v,true) : [];
+                $typeData = [];
+                foreach ($this->goodsService->productType() as $key => $value) {
+                    if (in_array($value['key'],$productType)) {
+                        $typeData[] = $value;
+                    }
+                }
+                $storeTypes[$k] = $typeData;
+            }
+            $resp = filterTreeByTargets($tree,$storeIds);
+            addTypesRecursive($resp,$storeTypes);
+            return successTrans(100010,$resp,200);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+}

+ 58 - 0
app/functions.php

@@ -593,4 +593,62 @@ if(! function_exists ('timeDiff') ) {
         $secs = $remain % 60;
         return ["day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs];
     }
+}
+
+if(! function_exists('filterTreeByTargets'))
+{
+
+    /**
+     * 过滤树,仅保留包含任何目标 category_id 的节点及其祖先
+     *
+     * @param array $tree       原始树
+     * @param array $targetIds  目标 category_id 列表
+     * @return array 过滤后的树(保持原结构)
+     */
+    function filterTreeByTargets($tree, $targetIds) {
+        $filtered = [];
+        foreach ($tree as $node) {
+            // 检查当前节点或子树中是否包含任一目标 ID
+            $contains = false;
+            if (in_array($node['category_id'], $targetIds)) {
+                $contains = true;
+            } else {
+                // 递归检查子节点
+                $children = [];
+                if (!empty($node['children']) && is_array($node['children'])) {
+                    $children = filterTreeByTargets($node['children'], $targetIds);
+                    if (!empty($children)) {
+                        $contains = true;
+                    }
+                }
+            }
+
+            if ($contains) {
+                // 保留当前节点,并合并过滤后的子节点
+                $newNode = $node;
+                if (!empty($node['children']) && is_array($node['children'])) {
+                    $newNode['children'] = filterTreeByTargets($node['children'], $targetIds);
+                } else {
+                    $newNode['children'] = [];
+                }
+                $filtered[] = $newNode;
+            }
+        }
+        return $filtered;
+    }
+}
+
+if(! function_exists('addTypesRecursive'))
+{
+    /**
+     * 为树中每个节点添加 types 字段
+     */
+    function addTypesRecursive(&$nodes, $typeMap) {
+        foreach ($nodes as &$node) {
+            $node['types'] = $typeMap[$node['category_id']] ?? '';
+            if (isset($node['children']) && is_array($node['children'])) {
+                addTypesRecursive($node['children'], $typeMap);
+            }
+        }
+    }
 }

+ 46 - 0
app/model/saas/SaasStoreCategory.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace app\model\saas;
+
+use app\extra\basic\Model;
+
+
+/**
+ * @property integer $id (主键)
+ * @property mixed $product_type 
+ * @property integer $category_id 
+ * @property integer $poi_id 
+ * @property mixed $create_at
+ */
+class SaasStoreCategory extends Model
+{
+    /**
+     * The connection name for the model.
+     *
+     * @var string|null
+     */
+    protected $connection = 'mysql';
+    
+    /**
+     * The table associated with the model.
+     *
+     * @var string
+     */
+    protected string $table = "saas_store_category";
+    
+    /**
+     * The primary key associated with the table.
+     *
+     * @var string
+     */
+    protected string $primaryKey = "id";
+    
+    /**
+     * Indicates if the model should be timestamped.
+     *
+     * @var bool
+     */
+    public bool $timestamps = false;
+
+
+}