Category.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace addons\unishop\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 分类
  7. */
  8. class Category extends Api
  9. {
  10. protected $noNeedLogin = ['all','menu','inlist'];
  11. protected $noNeedRight = ['*'];
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->model = new \addons\unishop\model\Category();
  16. }
  17. /**
  18. * @ApiTitle (全部分类数据)
  19. * @ApiSummary (全部分类数据)
  20. * @ApiMethod (GET)
  21. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  22. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  23. * @ApiReturn ({"code":1,"msg":"","data":[]})
  24. *
  25. * @ApiReturnParams (name="id", type="integer", description="分类id")
  26. * @ApiReturnParams (name="name", type="string", description="分类名称")
  27. * @ApiReturnParams (name="pid", type="integer", description="上级id")
  28. * @ApiReturnParams (name="image", type="string", description="图片")
  29. * @ApiReturnParams (name="type", type="string", description="类型")
  30. * @ApiReturnParams (name="flag", type="integer", description="标签/位置")
  31. * @ApiReturnParams (name="weigh", type="integer", description="排序")
  32. */
  33. public function all(){
  34. $all = $this->model
  35. ->where('type','product')
  36. ->where('status','normal')
  37. ->field('id,name,nickname,pid,image,type,flag,weigh')
  38. ->order('weigh ASC')
  39. ->cache(20)
  40. ->select();
  41. if ($all) {
  42. $all = collection($all)->toArray();
  43. }
  44. $this->success('',$all);
  45. }
  46. public function inlist(){
  47. $list = Db::name('unishop_category')
  48. ->where('status','normal')
  49. ->field('id,name,nickname,pid')
  50. ->order('weigh ASC')
  51. ->cache(20)
  52. ->select();
  53. //按级拆分
  54. $list_1 = [];
  55. $list_2 = [];
  56. foreach($list as $key => $value){
  57. if($value['pid'] == 0){
  58. $list_1[] = $value;
  59. }else{
  60. $list_2[] = $value;
  61. }
  62. }
  63. //市级并到省级
  64. foreach($list_1 as $k1 => $v1){
  65. foreach($list_2 as $k2 => $v2){
  66. if($v1['id'] == $v2['pid']){
  67. $list_1[$k1]['child'][] = $v2;
  68. }
  69. }
  70. }
  71. $this->success('',$list_1);
  72. }
  73. /**
  74. * @ApiTitle (首页广告下面的分类)
  75. * @ApiSummary (首页广告下面的分类)
  76. * @ApiMethod (GET)
  77. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  78. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  79. * @ApiReturn ({"code":1,"msg":"","data":{}})
  80. *
  81. * @ApiReturnParams (name="id", type="integer", description="分类id")
  82. * @ApiReturnParams (name="name", type="string", description="分类名称")
  83. * @ApiReturnParams (name="pid", type="integer", description="上级id")
  84. * @ApiReturnParams (name="image", type="string", description="图片")
  85. * @ApiReturnParams (name="type", type="string", description="类型")
  86. * @ApiReturnParams (name="flag", type="integer", description="标签/位置")
  87. * @ApiReturnParams (name="weigh", type="integer", description="排序")
  88. */
  89. public function menu()
  90. {
  91. $list = $this->model
  92. ->where('flag','index')
  93. ->where('status','normal')
  94. ->cache(20)
  95. ->select();
  96. if ($list) {
  97. $list = collection($list)->toArray();
  98. }
  99. $this->success('菜单',$list);
  100. }
  101. }