LiveController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace addons\WechatMini\api\modules\v1\controllers\live;
  3. use addons\WechatMini\common\enums\live\LiveStatusEnum;
  4. use addons\WechatMini\common\models\live\Live;
  5. use api\controllers\OnAuthController;
  6. use common\enums\StatusEnum;
  7. use Yii;
  8. use yii\data\ActiveDataProvider;
  9. /**
  10. * Class LiveController
  11. * @package addons\WechatMini\api\modules\v1\controllers\live
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class LiveController extends OnAuthController
  15. {
  16. /**
  17. * @var Live
  18. */
  19. public $modelClass = Live::class;
  20. /**
  21. * 不用进行登录验证的方法
  22. *
  23. * 例如: ['index', 'update', 'create', 'view', 'delete']
  24. * 默认全部需要验证
  25. *
  26. * @var array
  27. */
  28. protected $authOptional = ['index'];
  29. /**
  30. * @return ActiveDataProvider
  31. */
  32. public function actionIndex()
  33. {
  34. $is_recommend = Yii::$app->request->get('is_recommend');
  35. $live_status = Yii::$app->request->get('live_status');
  36. $where = [];
  37. switch ($live_status) {
  38. // 进行中
  39. case LiveStatusEnum::UNDERWAY :
  40. $where = [
  41. 'and',
  42. ['<', 'start_time', time()],
  43. ['>', 'end_time', time()]
  44. ];
  45. break;
  46. // 未开始
  47. case LiveStatusEnum::NOT_STARTED :
  48. $where = ['>', 'start_time', time()];
  49. break;
  50. // 已结束
  51. case LiveStatusEnum::END :
  52. $where = ['<', 'end_time', time()];
  53. break;
  54. }
  55. return new ActiveDataProvider([
  56. 'query' => $this->modelClass::find()
  57. ->where(['status' => StatusEnum::ENABLED])
  58. ->andWhere(['in', 'live_status' , [LiveStatusEnum::UNDERWAY, LiveStatusEnum::NOT_STARTED, LiveStatusEnum::END]])
  59. ->andFilterWhere($where)
  60. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  61. ->andFilterWhere(['is_recommend' => $is_recommend])
  62. ->cache(60)
  63. ->orderBy('is_stick asc, id desc')
  64. ->asArray(),
  65. 'pagination' => [
  66. 'pageSize' => $this->pageSize,
  67. 'validatePage' => false,// 超出分页不返回data
  68. ],
  69. ]);
  70. }
  71. }
粤ICP备19079148号