ProductClass.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\service\ProductClassService;
  5. use app\service\ZdXzqService;
  6. use Psr\SimpleCache\InvalidArgumentException;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\annotation\Route;
  11. use think\annotation\route\Middleware;
  12. use hg\apidoc\annotation as Apidoc;
  13. use think\facade\Cache;
  14. use think\response\Json;
  15. /**
  16. * @Apidoc\Title("产品字典")
  17. * @Apidoc\Group("产品")
  18. * @Apidoc\Sort (7)
  19. */
  20. class ProductClass extends BaseController
  21. {
  22. /**
  23. * @Apidoc\Title("通过分类id 无限级分类")
  24. * @Apidoc\Tag("产品字典")
  25. * @Apidoc\Method ("GET")
  26. * @Apidoc\Author ("ihavoc")
  27. * @Route("getProductClassById",method="GET")
  28. * @Middleware({"jwt"})
  29. * @throws InvalidArgumentException
  30. */
  31. public function getProductClassById(): Json
  32. {
  33. $id = $this->request->get();
  34. $product_dict = Cache::store('redis')->get('product_class_'.$id["id"]);
  35. if ($product_dict){
  36. return $this->JsonSucess($product_dict);
  37. }
  38. try {
  39. $productDict = ProductClassService::getProductClassById($id["id"]);
  40. } catch (DataNotFoundException|DbException $e) {
  41. return $this->JsonError($e->getData());
  42. }
  43. $dict =$this->getArray("0".$id["id"],$productDict->toArray());
  44. Cache::store('redis')->set('product_class_'.$id["id"],$dict);
  45. return $this->JsonSucess($dict);
  46. }
  47. function getArray($parentCode,$data): array
  48. {
  49. $arr = array();
  50. foreach($data as $k => $v){
  51. if($v["parent_id"]==$parentCode){
  52. unset($data[$k]);
  53. $c=$this->getArray($v["product_code"],$data);
  54. if($c){
  55. $v["children"]=$c;
  56. }
  57. $arr[]=$v;
  58. }
  59. }
  60. return $arr;
  61. }
  62. /**
  63. * @Apidoc\Title("通过分类id查找产品分类")
  64. * @Apidoc\Tag("产品字典")
  65. * @Apidoc\Method ("GET")
  66. * @Apidoc\Author ("yang")
  67. * @Route("getProductClass",method="GET")
  68. * @Middleware({"jwt"})
  69. */
  70. public function getProductClass(){
  71. return $this->JsonSucess(ProductClassService::getProductClass());
  72. }
  73. }