CategoryController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 莫名疯
  5. * DateTime: 2017/3/8 10:09
  6. * Description:
  7. */
  8. namespace api\modules\v1\controllers;
  9. use api\common\controllers\Controller;
  10. use api\modules\v1\models\Category;
  11. use common\helpers\Tree;
  12. use yii\helpers\ArrayHelper;
  13. use api\common\behaviors\QueryParamAuth;
  14. class CategoryController extends Controller {
  15. public function behaviors()
  16. {
  17. return ArrayHelper::merge(parent::behaviors(), [
  18. [
  19. 'class' => QueryParamAuth::className(),
  20. 'tokenParam' => 'token',
  21. 'optional' => ['index']
  22. ]
  23. ]);
  24. }
  25. /**
  26. * @api {get} /v1/categorys 列表
  27. * @apiVersion 1.0.0
  28. * @apiName index
  29. * @apiGroup Category
  30. *
  31. *
  32. */
  33. public function actionIndex()
  34. {
  35. $lists = Category::find()->active()->orderBy(['sort' => SORT_ASC])->all();
  36. $lists = ArrayHelper::toArray($lists);
  37. $lists = Tree::build($lists);
  38. //添加热门推荐
  39. // array_unshift($lists, [
  40. // 'title' => '热门推荐',
  41. // 'children' => Cat::find()->hot()->all()
  42. // ]);
  43. return ['data' => $lists];
  44. }
  45. /**
  46. * @api {get} /v1/category/id:\d+ 分类详情
  47. * @apiVersion 1.0.0
  48. * @apiName view
  49. * @apiGroup Category
  50. *
  51. */
  52. public function actionView($id)
  53. {
  54. $model = Category::find()->where(['id' => $id])->one();
  55. return ['data' => $model];
  56. }
  57. }