zory преди 3 седмици
родител
ревизия
d717bec79b

+ 116 - 0
app/controller/admin/Category.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace app\controller\admin;
+
+use app\extra\basic\Base;
+use app\extra\tools\DataExtend;
+use app\middleware\AuthMiddleware;
+use app\model\saas\SaasCategory;
+use app\service\saas\CategoryService;
+use DI\Attribute\Inject;
+use LinFly\Annotation\Attributes\Route\Controller;
+use LinFly\Annotation\Attributes\Route\GetMapping;
+use LinFly\Annotation\Attributes\Route\Middleware;
+use LinFly\Annotation\Attributes\Route\PostMapping;
+use support\Request;
+use support\Response;
+use Webman\RedisQueue\Redis;
+
+
+#[Controller("/api/category"),Middleware(AuthMiddleware::class)]
+class Category extends Base
+{
+
+    #[Inject]
+    protected CategoryService $service;
+
+    #[Inject]
+    protected SaasCategory $model;
+
+    /**
+     * 数据列表
+     * @param Request $request
+     * @return Response
+     */
+    #[GetMapping('list')]
+    public function getStoreCategory(Request $request): Response
+    {
+        try {
+            $data = $this->service->setModel()->getList($request->all(),null,true);
+            $tree = DataExtend::arr2tree($data,"category_id","parent_id");
+            return successTrans(100010,$tree,200);
+        } catch (\Throwable $th) {
+            return error($th->getMessage());
+        }
+    }
+
+
+    /**
+     * 同步门店分类
+     * @param Request $request
+     * @return Response
+     */
+    #[GetMapping('sync')]
+    public function syncStore(Request $request): Response
+    {
+        try {
+            Redis::send("sync-category",[
+                "appid"     => sConf("wechat.appid"),
+                "secret"    => sConf("wechat.secret"),
+                "account"   => sConf("wechat.shop_id"),
+            ]);
+            return success("发起成功");
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+    /**
+     * 编辑代理信息
+     * @param Request $request
+     * @return Response
+     */
+    #[PostMapping("edit")]
+    public function setListData(Request $request): Response
+    {
+        try {
+            $param = $request->post();
+            $factory = $this->model->where("id",$param['id'])->findOrEmpty();
+            if ($factory->isEmpty()) return errorTrans("error.data");
+            $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
+     */
+    #[PostMapping("batch")]
+    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());
+        }
+    }
+
+
+}

+ 14 - 0
app/controller/admin/Goods.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace app\controller\admin;
+
+use app\extra\basic\Base;
+use app\middleware\AuthMiddleware;
+use LinFly\Annotation\Attributes\Route\Controller;
+use LinFly\Annotation\Attributes\Route\Middleware;
+
+#[Controller("/api/goods"),Middleware(AuthMiddleware::class)]
+class Goods extends Base
+{
+
+}

+ 4 - 0
app/controller/api/Home.php

@@ -3,10 +3,13 @@
 namespace app\controller\api;
 
 use app\extra\basic\Base;
+use app\extra\dyLife\data\BaseData;
 use app\middleware\AuthMiddleware;
 use LinFly\Annotation\Attributes\Route\Controller;
+use LinFly\Annotation\Attributes\Route\GetMapping;
 use LinFly\Annotation\Attributes\Route\Middleware;
 use support\Request;
+use Webman\RedisQueue\Redis;
 
 
 #[Controller("/dy/home"),Middleware(AuthMiddleware::class)]
@@ -18,6 +21,7 @@ class Home extends Base
      */
     protected array $noNeedLogin = ["getHomeData"];
 
+    #[GetMapping("data")]
     public function getHomeData(Request $request)
     {
         try {

+ 10 - 5
app/extra/basic/Service.php

@@ -104,7 +104,7 @@ class Service
     /**
      *
      */
-    public function getList(array $param = [],$with = null)
+    public function getList(array $param = [],$with = null,bool $all = false)
     {
         $model = $this->searchVal($param,$this->searchFilter($param));
         // 支持多种类型的 $with 参数
@@ -120,10 +120,15 @@ class Service
                 $model->with($with);
             }
         }
-        return $model->paginate([
-            "list_rows" => $param['pageSize'],
-            "page"      => $param['page']
-        ]);
+        if ($all) {
+            $resp = $model->select()->toArray();
+        } else {
+            $resp = $model->paginate([
+                "list_rows" => $param['pageSize'],
+                "page"      => $param['page']
+            ]);
+        }
+        return $resp;
     }
 
 }

+ 25 - 0
app/functions.php

@@ -492,4 +492,29 @@ if (!function_exists("strToUniqueNumberV4"))
         }
         return str_pad($number, 12, '0', STR_PAD_LEFT);
     }
+}
+
+if (!function_exists("extractNodesClean"))
+{
+    /**
+     * 多维数组转N个一维
+     * @param $data
+     * @param array $result
+     * @return array
+     */
+    function extractNodesClean($data, array &$result = []): array
+    {
+        foreach ($data as $node) {
+            // 复制节点并删除 sub_tree_infos
+            $cleanNode = $node;
+            unset($cleanNode['sub_tree_infos']);
+            $result[] = $cleanNode;
+
+            // 递归处理子节点
+            if (isset($node['sub_tree_infos']) && is_array($node['sub_tree_infos'])) {
+                extractNodesClean($node['sub_tree_infos'], $result);
+            }
+        }
+        return $result;
+    }
 }

+ 49 - 0
app/model/saas/SaasCategory.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace app\model\saas;
+
+use app\extra\basic\Model;
+
+
+/**
+ * @property integer $id (主键)
+ * @property integer $category_id 
+ * @property integer $level 
+ * @property string $name 
+ * @property mixed $parent_id 
+ * @property mixed $is_publish_block 是否屏蔽发品,true为屏蔽发品
+ * @property mixed $is_leaf 是否是叶子结点
+ * @property mixed $create_at
+ */
+class SaasCategory 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_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;
+
+
+}

+ 41 - 0
app/queue/redis/SyncCategory.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace app\queue\redis;
+
+use app\extra\dyLife\data\BaseData;
+use app\model\saas\SaasCategory;
+use Webman\RedisQueue\Consumer;
+
+class SyncCategory implements Consumer
+{
+    /**
+     * 消费队列名
+     * @var string
+     */
+    public string $queue = "sync-category";
+
+    /**
+     * 连接配置
+     * @var string
+     */
+    public string $connection = "default";
+
+    public function consume($data): bool
+    {
+        echo getDateFull()."==执行同步分类\n";
+        $result = (new BaseData)->config(['appid' => $data['appid'],'secret' => $data['secret']])->token()->getStoreCategoryData($data['account']);
+        if (empty($result['category_tree_infos'])) return true;
+        $model = (new SaasCategory);
+        $categoryData = extractNodesClean($result['category_tree_infos']);
+        foreach ($categoryData as $key=>$val) {
+            $category = $model->where("category_id",$val["category_id"])->findOrEmpty();
+            if ($category->isEmpty()) {
+                echo "分类入库=={$val['level']}=={$val['name']}\n";
+                $state = $category->strict(false)->insertGetId($val);
+                echo "入库状态==={$state}\n";
+            }
+        }
+        return true;
+    }
+
+}

+ 36 - 0
app/service/saas/CategoryService.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace app\service\saas;
+
+use app\extra\basic\Service;
+use app\model\saas\SaasCategory;
+
+class CategoryService extends Service
+{
+
+
+    /**
+     *
+     * @return $this
+     */
+    public function setModel()
+    {
+        $this->mode = (new SaasCategory);
+        return $this;
+    }
+
+
+    /**
+     *
+     * @param array $param
+     * @return array
+     */
+    public function searchFilter(array $param = []): array
+    {
+        $filter = [];
+        !empty($param['status']) && $filter[] = ["enable", '=', $param['status']];
+        !empty($param['name']) && $filter[] = ["name", 'like', "%{$param['name']}%"];
+        return $filter;
+    }
+
+}