ActivityController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wniu
  5. * Date: 2022/3/16
  6. * Time: 10:23 上午
  7. */
  8. namespace addons\Course\api\modules\v1\controllers;
  9. use addons\Course\common\enums\CourseEnum;
  10. use addons\Course\common\models\AddonsCourse;
  11. use addons\Course\common\models\AddonsCourseActivity;
  12. use addons\Course\common\models\AddonsCourseBought;
  13. use api\controllers\OnAuthController;
  14. use common\enums\StatusEnum;
  15. use addons\Course\common\services\CourseActivityService;
  16. class ActivityController extends OnAuthController
  17. {
  18. public $modelClass = '';
  19. /**
  20. * 不用进行登录验证的方法
  21. *
  22. * 例如: ['index', 'update', 'create', 'view', 'delete']
  23. * 默认全部需要验证
  24. *
  25. * @var array
  26. */
  27. protected $authOptional = [];
  28. public function actionIndex()
  29. {
  30. $get = \Yii::$app->request->get();
  31. $where[] = ['mall_id' => $this->mall_id];
  32. $where[] = ['status' => StatusEnum::ENABLED];
  33. if (isset($get['activity_id']) && $get['activity_id'] > 0) {
  34. $where[] = ['id' => $get['activity_id']];
  35. }
  36. try {
  37. $activity = AddonsCourseActivity::getOne($where, true, [], 'created_at desc');
  38. if (empty($activity)) {
  39. throw new \Exception("活动不存在");
  40. }
  41. if (empty($activity['goods_id'])) {
  42. throw new \Exception("活动未设置商品");
  43. }
  44. $banners = [];
  45. if (!empty($activity['banners'])) {
  46. $banners = \Qiniu\json_decode($activity['banners']);
  47. } else {
  48. throw new \Exception("活动未设置广告图");
  49. }
  50. $courseList = [];
  51. if (!empty($activity['course_id'])) {
  52. $idList = explode(",", trim($activity['course_id']));
  53. $courseAll = AddonsCourse::getList($this->mall_id, $idList, 'id,name as course_name,logo,introduce');
  54. $boughtIdsList = [];
  55. $courseIds = array_column($courseAll, 'id');
  56. if (!empty($courseIds)) {
  57. $boughtList = AddonsCourseBought::find()->where(["mall_id" => $this->mall_id, "user_id" => $this->user_id, "status" => StatusEnum::ENABLED, 'is_pay' => CourseEnum::IS_PAY_YES])->andWhere(['IN', 'course_id', $courseIds])->asArray()->all();
  58. $boughtIdsList = array_column($boughtList, 'course_id');
  59. }
  60. foreach ($courseAll as $item) {
  61. $courseList[$item['id']] = $item;
  62. $courseList[$item['id']]['is_buy'] = 0;
  63. if (in_array($item['id'], $boughtIdsList)) {
  64. $courseList[$item['id']]['is_buy'] = 1;
  65. }
  66. }
  67. $courseList = array_values($courseList);
  68. } else {
  69. throw new \Exception("活动未设置课程");
  70. }
  71. $data['banners'] = $banners;
  72. $data['courses'] = $courseList;
  73. $data['activity'] = $activity;
  74. } catch (\Exception $exception) {
  75. return $this->error($exception->getMessage());
  76. }
  77. return $this->success('success', $data);
  78. }
  79. public function actionList()
  80. {
  81. try {
  82. $get = \Yii::$app->request->get();
  83. $activity = AddonsCourseActivity::listPage([
  84. 'select' => 'id,name,logo,goods_id',
  85. 'where' => [
  86. ['mall_id' => $this->mall_id],
  87. ['=', 'status', StatusEnum::ENABLED]
  88. ],
  89. 'order' => 'id desc',
  90. 'limit' => $get['page_size'] ?? 10,
  91. ]);
  92. if ($activity['list']) {
  93. $activity['list'] = CourseActivityService::getList($activity['list'], $this->mall_id);
  94. }
  95. return $this->success('success', $activity);
  96. } catch (\Exception $e) {
  97. return $this->error($e->getMessage());
  98. }
  99. }
  100. }