初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller\user;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\model\user\Study as StudyModel;
|
||||
use app\common\model\user\Subscription as SubscriptionModel;
|
||||
/**
|
||||
* 学习统计
|
||||
*/
|
||||
class Study extends Api
|
||||
{
|
||||
// 无需登录的接口,*表示全部
|
||||
protected $noNeedLogin = ["setLog","getMediaProgress"];
|
||||
|
||||
protected $noNeedRight = '*';
|
||||
protected $model;
|
||||
protected $SubscriptionModel;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new StudyModel();
|
||||
$this->SubscriptionModel = new SubscriptionModel();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取学习记录
|
||||
* @return void
|
||||
*/
|
||||
public function getStudyLog(){
|
||||
$limit = input('limit',10);
|
||||
$page = input('page',1);
|
||||
|
||||
|
||||
$list = $this->model->getStudyLog($this->auth->id,[
|
||||
'limit'=>$limit,
|
||||
'page'=>$page
|
||||
]);
|
||||
|
||||
$this->success('获取成功', $list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取统计
|
||||
* @return void
|
||||
*/
|
||||
public function getStatistics(){
|
||||
$time = $this->request->post('time',0);
|
||||
|
||||
$timeLimit = [];
|
||||
$data = [];
|
||||
|
||||
$data['log'] = [];
|
||||
|
||||
if($time==0){
|
||||
//日
|
||||
$timeLimit['start'] = strtotime('today midnight');
|
||||
$timeLimit['end'] = strtotime('today 23:59:59');
|
||||
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$startOfDay = strtotime("-$i day", strtotime('today'));
|
||||
$endOfDay = strtotime("+1 day", $startOfDay) - 1;
|
||||
$date = date('m-d', $startOfDay);
|
||||
|
||||
$data['log'][$date] = $this->model->getTotal($this->auth->id,['start'=>$startOfDay,'end'=>$endOfDay]);
|
||||
}
|
||||
}elseif($time==1){
|
||||
//周
|
||||
$timeLimit['start'] = strtotime('this week');
|
||||
$timeLimit['end'] = strtotime('this week +6 days +23 hours +59 minutes +59 seconds');
|
||||
|
||||
$weeks = getPastWeeksOfMonth();
|
||||
|
||||
foreach ($weeks as $item){
|
||||
$startDate = date('m.d', $item['startDate']);
|
||||
$endDate = date('m.d', $item['endDate']);
|
||||
$data['log']["$startDate~$endDate"] = $this->model->getTotal($this->auth->id,['start'=>$item['startDate'],'end'=>$item['endDate']]);
|
||||
}
|
||||
}else{
|
||||
//月
|
||||
// 获取今年过去的月份的每月开始时间戳与结束时间戳
|
||||
for ($i = 1; $i <= date('n'); $i++) {
|
||||
// 计算每月的开始时间戳
|
||||
$startOfMonth = strtotime(date('Y-' . $i . '-01'));
|
||||
// 计算每月的结束时间戳
|
||||
$endOfMonth = strtotime(date('Y-' . $i . '-t'));
|
||||
$date = date('m月', $startOfMonth);
|
||||
$data['log'][$date] = $this->model->getTotal($this->auth->id,['start'=>$startOfMonth,'end'=>$endOfMonth]);
|
||||
}
|
||||
|
||||
$timeLimit['start'] = strtotime('first day of this month midnight');
|
||||
$timeLimit['end'] = strtotime('last day of this month 23:59:59');
|
||||
}
|
||||
|
||||
$data['total'] = $this->model->getTotal($this->auth->id);
|
||||
$data['time_total'] = $this->model->getTotal($this->auth->id,$timeLimit);
|
||||
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 设置记录
|
||||
* @return void
|
||||
*/
|
||||
public function setLog(){
|
||||
if(!$this->auth->id){
|
||||
$this->success('暂未记录');
|
||||
}
|
||||
$courseId = $this->request->post('course_id','');
|
||||
$columnId = $this->request->post('column_id','');
|
||||
$pause = $this->request->post('pause',0);
|
||||
$mediaProgress = input('media_progress',0);
|
||||
//判断课程是否已经订阅
|
||||
|
||||
$userId = $this->auth->id;
|
||||
|
||||
$subscriptionAuth = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId);
|
||||
|
||||
if(!$subscriptionAuth){
|
||||
$this->success("暂无学习权限");
|
||||
}
|
||||
|
||||
$this->model->setLog($userId,$courseId,$columnId,$mediaProgress);
|
||||
|
||||
if($pause){
|
||||
$this->model->singleSyncCache($userId,$courseId);
|
||||
}
|
||||
|
||||
$this->success("操作成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习完成
|
||||
* @return void
|
||||
*/
|
||||
public function finish(){
|
||||
$courseId = $this->request->post('course_id','');
|
||||
$columnId = $this->request->post('column_id','');
|
||||
|
||||
if(!$courseId){
|
||||
$this->error('参数错误');
|
||||
}
|
||||
|
||||
$courseSubscriptionAuth = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId);
|
||||
if(!$courseSubscriptionAuth){
|
||||
$this->success("暂无学习权限");
|
||||
}
|
||||
$this->model->singleSyncCache($this->auth->id,$courseId);
|
||||
$this->model->where(['course_id'=>$courseId,'user_id'=>$this->auth->id])->update(['finish'=>1]);
|
||||
|
||||
if($columnId){
|
||||
$columnSubscriptionAuth = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$columnId);
|
||||
if(!$columnSubscriptionAuth){
|
||||
$this->success("暂无学习权限");
|
||||
}
|
||||
|
||||
if(\app\admin\library\project\App::isInstall('cert')){
|
||||
(new \app\admin\model\app\cert\Grant)->courseCert($this->auth->id,$columnId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->success("完成");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取媒体播放进度
|
||||
* @return void
|
||||
*/
|
||||
public function getMediaProgress(){
|
||||
|
||||
if(!$this->auth->isLogin()){
|
||||
$this->success('未登录',0);
|
||||
}
|
||||
|
||||
$courseId = $this->request->post('course_id','');
|
||||
|
||||
$data = $this->model->where([
|
||||
'course_id'=>$courseId,
|
||||
'user_id'=>$this->auth->id
|
||||
])->order("id","desc")->field("media_progress")->find();
|
||||
|
||||
$this->success("获取成功",($data ? $data['media_progress'] : 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连续学习天数
|
||||
* @param $userId
|
||||
* @return int
|
||||
*/
|
||||
public function getContinuousStudyDays()
|
||||
{
|
||||
//获取连续学习天数
|
||||
|
||||
//获取加入时间
|
||||
$studyDays = $this->model->getContinuousStudyDays($this->auth->id);
|
||||
$joinDays = ceil((time() - $this->auth->createtime) / 86400);
|
||||
|
||||
|
||||
$this->success("获取成功",[
|
||||
'study_days'=>$studyDays,
|
||||
'join_days'=>$joinDays
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user