| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wniu
- * Date: 2022/3/16
- * Time: 10:23 上午
- */
- namespace addons\Course\api\modules\v1\controllers;
- use addons\Course\common\enums\CourseEnum;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\AddonsCourseActivity;
- use addons\Course\common\models\AddonsCourseBought;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use addons\Course\common\services\CourseActivityService;
- class ActivityController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- public function actionIndex()
- {
- $get = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- if (isset($get['activity_id']) && $get['activity_id'] > 0) {
- $where[] = ['id' => $get['activity_id']];
- }
- try {
- $activity = AddonsCourseActivity::getOne($where, true, [], 'created_at desc');
- if (empty($activity)) {
- throw new \Exception("活动不存在");
- }
- if (empty($activity['goods_id'])) {
- throw new \Exception("活动未设置商品");
- }
- $banners = [];
- if (!empty($activity['banners'])) {
- $banners = \Qiniu\json_decode($activity['banners']);
- } else {
- throw new \Exception("活动未设置广告图");
- }
- $courseList = [];
- if (!empty($activity['course_id'])) {
- $idList = explode(",", trim($activity['course_id']));
- $courseAll = AddonsCourse::getList($this->mall_id, $idList, 'id,name as course_name,logo,introduce');
- $boughtIdsList = [];
- $courseIds = array_column($courseAll, 'id');
- if (!empty($courseIds)) {
- $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();
- $boughtIdsList = array_column($boughtList, 'course_id');
- }
- foreach ($courseAll as $item) {
- $courseList[$item['id']] = $item;
- $courseList[$item['id']]['is_buy'] = 0;
- if (in_array($item['id'], $boughtIdsList)) {
- $courseList[$item['id']]['is_buy'] = 1;
- }
- }
- $courseList = array_values($courseList);
- } else {
- throw new \Exception("活动未设置课程");
- }
- $data['banners'] = $banners;
- $data['courses'] = $courseList;
- $data['activity'] = $activity;
- } catch (\Exception $exception) {
- return $this->error($exception->getMessage());
- }
- return $this->success('success', $data);
- }
- public function actionList()
- {
- try {
- $get = \Yii::$app->request->get();
- $activity = AddonsCourseActivity::listPage([
- 'select' => 'id,name,logo,goods_id',
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['=', 'status', StatusEnum::ENABLED]
- ],
- 'order' => 'id desc',
- 'limit' => $get['page_size'] ?? 10,
- ]);
- if ($activity['list']) {
- $activity['list'] = CourseActivityService::getList($activity['list'], $this->mall_id);
- }
- return $this->success('success', $activity);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|