初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller\course;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\admin\model\course\Course as CourseModel;
|
||||
use app\common\model\user\Subscription as SubscriptionModel;
|
||||
/**
|
||||
* 订阅课程
|
||||
*/
|
||||
class Subscription extends Api
|
||||
{
|
||||
// 无需登录的接口,*表示全部
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedRight = '*';
|
||||
protected $courseModel;
|
||||
protected $SubscriptionModel;
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->courseModel = new CourseModel();
|
||||
$this->SubscriptionModel = new SubscriptionModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码兑换
|
||||
* @return void
|
||||
*/
|
||||
public function passwordPay(){
|
||||
$password = $this->request->post('password','');
|
||||
$courseId = $this->request->post('course_id','');
|
||||
|
||||
$userId = $this->auth->id;
|
||||
|
||||
if(empty($password) || empty($courseId)){
|
||||
$this->error('请完善参数后再提交');
|
||||
}
|
||||
|
||||
//获取课程详情
|
||||
$courseInfo = $this->courseModel->where([
|
||||
'id'=>$courseId,
|
||||
'status'=>1,
|
||||
'pay_type'=>'password'
|
||||
])->find();
|
||||
if (!$courseInfo) {
|
||||
$this->error(__('未找到相关课程信息'));
|
||||
}
|
||||
$courseInfo = $courseInfo->toArray();
|
||||
|
||||
\app\common\model\course\Course::checkSalesType($courseInfo);
|
||||
|
||||
if($courseInfo['password'] != $password){
|
||||
$this->error(__('兑换密码错误'));
|
||||
}
|
||||
|
||||
//判断是否订阅过了
|
||||
$userCourse = $this->SubscriptionModel->getUserCourse($userId,$courseId);
|
||||
if($userCourse){
|
||||
$this->error(__('该课程已经订阅过了'));
|
||||
}
|
||||
|
||||
//添加订阅记录
|
||||
$result = $this->SubscriptionModel->setUserCourse($userId,$courseId);
|
||||
|
||||
if(!$result){
|
||||
$this->error(__('订阅失败'));
|
||||
}
|
||||
|
||||
$this->success('订阅成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 免费加入学习
|
||||
* 适用以下任一情形:
|
||||
* 1. 课程本身免费(pay_type=free 或 pay_type=pay 且价格为 0)→ get_type=free
|
||||
* 2. 用户付费会员卡免费权益命中该课程 → get_type=vip
|
||||
* 仅支持 article/video/audio/live/column 类型课程
|
||||
* @return void
|
||||
*/
|
||||
public function free(){
|
||||
$courseId = $this->request->post('course_id','');
|
||||
|
||||
$userId = $this->auth->id;
|
||||
|
||||
if(empty($courseId)){
|
||||
$this->error('请完善参数后再提交');
|
||||
}
|
||||
|
||||
//获取课程详情
|
||||
$courseInfo = $this->courseModel->where([
|
||||
'id'=>$courseId,
|
||||
'status'=>1
|
||||
])->find();
|
||||
|
||||
|
||||
if (!$courseInfo) {
|
||||
$this->error(__('未找到相关课程信息'));
|
||||
}
|
||||
|
||||
//仅支持指定类型的课程加入学习
|
||||
$allowTypes = ['article','video','audio','live','column'];
|
||||
if (!in_array($courseInfo['type'], $allowTypes)) {
|
||||
$this->error(__('当前课程类型不支持加入学习'));
|
||||
}
|
||||
|
||||
//加入学习的资格判定:课程免费 或 用户付费会员卡免费权益命中
|
||||
$isFreeCourse = \app\common\model\user\Subscription::isFreeCourse($courseId);
|
||||
$hasVipFreeAccess = false;
|
||||
if (!$isFreeCourse) {
|
||||
$hasVipFreeAccess = \app\common\model\user\Subscription::getVipFreeAccess($userId, $courseId);
|
||||
}
|
||||
|
||||
if (!$isFreeCourse && !$hasVipFreeAccess) {
|
||||
$this->error(__('当前没有加入学习的资格'));
|
||||
}
|
||||
|
||||
\app\common\model\course\Course::checkSalesType($courseInfo);
|
||||
|
||||
//判断是否订阅过了
|
||||
$userCourse = $this->SubscriptionModel->getUserCourse($userId,$courseId);
|
||||
if($userCourse){
|
||||
$this->error(__('该课程已经加入学习'));
|
||||
}
|
||||
|
||||
$validityTime = $this->SubscriptionModel->getValidityTime($courseInfo);
|
||||
|
||||
if($validityTime < time()){
|
||||
$this->error(__('该课程有效时间不支持订阅'));
|
||||
}
|
||||
|
||||
//标记 get_type:课程免费时为 free,VIP 权益命中时为 vip
|
||||
$getType = $isFreeCourse ? 'free' : 'vip';
|
||||
|
||||
//添加订阅记录
|
||||
$result = $this->SubscriptionModel->setUserCourse($userId,$courseId,$getType);
|
||||
|
||||
if(!$result){
|
||||
$this->error(__('加入学习失败'));
|
||||
}
|
||||
|
||||
$this->success('加入学习成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取我的订阅
|
||||
* @return void
|
||||
*/
|
||||
public function getMySubscription(){
|
||||
$limit = input('limit',10);
|
||||
$page = input('page',1);
|
||||
$type = input('type','all');
|
||||
$list = $this->SubscriptionModel->getMyCourse($this->auth->id,[
|
||||
'limit'=>$limit,
|
||||
'page'=>$page,
|
||||
'type'=>$type
|
||||
]);
|
||||
$this->success('订阅成功',$list);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user