Category.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service;
  10. use mall\utils\Data;
  11. use mall\utils\Tool;
  12. use think\facade\Cache;
  13. use app\common\models\Category as CategoryModel;
  14. class Category extends Service {
  15. /**
  16. * 获取分类数据
  17. * @return array|mixed
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public static function getList(){
  23. $array = Cache::get("api_category");
  24. if(empty($array)){
  25. $category = Data::familyProcess(CategoryModel::where('module','goods')->where("is_menu",0)->select()->toArray());
  26. $data = [];
  27. foreach($category as $key=>$value){
  28. $data[$value['id']]["title"] = $value["title"];
  29. foreach($value["children"] as $val){
  30. $data[$value['id']]["children"][] = [
  31. "id"=>$val["id"],
  32. "name"=>$val["title"],
  33. "thumb_img"=>Tool::thumb($val["photo"],"medium",true)
  34. ];
  35. foreach($val["children"] as $v){
  36. $data[$value['id']]["children"][] = [
  37. "id"=>$v["id"],
  38. "name"=>$v["title"],
  39. "thumb_img"=>Tool::thumb($v["photo"],"medium",true)
  40. ];
  41. }
  42. }
  43. }
  44. $i = 0;
  45. $array = [];
  46. foreach($data as $value){
  47. $array[$i] = $value;
  48. $i++;
  49. }
  50. Cache::set("api_category",$array,strtotime("+10 day"));
  51. }
  52. return $array;
  53. }
  54. }