Category.php 4.1 KB

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