Category.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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,keywords')
  50. ->order('weigh ASC')
  51. ->cache(20)
  52. ->select();
  53. $this->success('',$list);
  54. }
  55. public function inlist_old(){
  56. $list = Db::name('unishop_category')
  57. ->where('status','normal')
  58. ->field('id,name,nickname,pid')
  59. ->order('weigh ASC')
  60. ->cache(20)
  61. ->select();
  62. //按级拆分
  63. $list_1 = [];
  64. $list_2 = [];
  65. foreach($list as $key => $value){
  66. if($value['pid'] == 0){
  67. $list_1[] = $value;
  68. }else{
  69. $list_2[] = $value;
  70. }
  71. }
  72. //市级并到省级
  73. foreach($list_1 as $k1 => $v1){
  74. foreach($list_2 as $k2 => $v2){
  75. if($v1['id'] == $v2['pid']){
  76. $list_1[$k1]['child'][] = $v2;
  77. }
  78. }
  79. }
  80. $this->success('',$list_1);
  81. }
  82. /**
  83. * @ApiTitle (首页广告下面的分类)
  84. * @ApiSummary (首页广告下面的分类)
  85. * @ApiMethod (GET)
  86. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  87. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  88. * @ApiReturn ({"code":1,"msg":"","data":{}})
  89. *
  90. * @ApiReturnParams (name="id", type="integer", description="分类id")
  91. * @ApiReturnParams (name="name", type="string", description="分类名称")
  92. * @ApiReturnParams (name="pid", type="integer", description="上级id")
  93. * @ApiReturnParams (name="image", type="string", description="图片")
  94. * @ApiReturnParams (name="type", type="string", description="类型")
  95. * @ApiReturnParams (name="flag", type="integer", description="标签/位置")
  96. * @ApiReturnParams (name="weigh", type="integer", description="排序")
  97. */
  98. public function menu()
  99. {
  100. $list = $this->model
  101. ->where('flag','index')
  102. ->where('status','normal')
  103. ->cache(20)
  104. ->select();
  105. if ($list) {
  106. $list = collection($list)->toArray();
  107. }
  108. $this->success('菜单',$list);
  109. }
  110. }