123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\service\ProductClassService;
- use app\service\ZdXzqService;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\annotation\Route;
- use think\annotation\route\Middleware;
- use hg\apidoc\annotation as Apidoc;
- use think\facade\Cache;
- use think\response\Json;
- /**
- * @Apidoc\Title("产品字典")
- * @Apidoc\Group("产品")
- * @Apidoc\Sort (7)
- */
- class ProductClass extends BaseController
- {
- /**
- * @Apidoc\Title("通过分类id 无限级分类")
- * @Apidoc\Tag("产品字典")
- * @Apidoc\Method ("GET")
- * @Apidoc\Author ("ihavoc")
- * @Route("getProductClassById",method="GET")
- * @Middleware({"jwt"})
- * @throws InvalidArgumentException
- */
- public function getProductClassById(): Json
- {
- $id = $this->request->get();
- $product_dict = Cache::store('redis')->get('product_class_'.$id["id"]);
- if ($product_dict){
- return $this->JsonSucess($product_dict);
- }
- try {
- $productDict = ProductClassService::getProductClassById($id["id"]);
- } catch (DataNotFoundException|DbException $e) {
- return $this->JsonError($e->getData());
- }
- $dict =$this->getArray("0".$id["id"],$productDict->toArray());
- Cache::store('redis')->set('product_class_'.$id["id"],$dict);
- return $this->JsonSucess($dict);
- }
- function getArray($parentCode,$data): array
- {
-
- $arr = array();
- foreach($data as $k => $v){
-
- if($v["parent_id"]==$parentCode){
- unset($data[$k]);
- $c=$this->getArray($v["product_code"],$data);
- if($c){
- $v["children"]=$c;
- }
- $arr[]=$v;
-
- }
-
- }
-
- return $arr;
- }
- /**
- * @Apidoc\Title("通过分类id查找产品分类")
- * @Apidoc\Tag("产品字典")
- * @Apidoc\Method ("GET")
- * @Apidoc\Author ("yang")
- * @Route("getProductClass",method="GET")
- * @Middleware({"jwt"})
- */
- public function getProductClass(){
- return $this->JsonSucess(ProductClassService::getProductClass());
- }
- }
|