初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\course;
|
||||
use app\common\model\course\CommentLike;
|
||||
use think\Model;
|
||||
class Comment extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'comment';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
/**
|
||||
* 获取评论详情
|
||||
* @param $commentId
|
||||
* @return mixed
|
||||
*/
|
||||
public function getComment($commentId){
|
||||
$data = self::where([
|
||||
'id'=>$commentId
|
||||
])->find();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取课程的评论数量
|
||||
* @param $course
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCourseCommentCount($course){
|
||||
return self::where([
|
||||
'course_id'=>$course,
|
||||
'status'=>1
|
||||
])->count();
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
||||
}
|
||||
|
||||
public function course()
|
||||
{
|
||||
return $this->belongsTo('app\admin\model\Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自关联
|
||||
* @return mixed
|
||||
*/
|
||||
public function like()
|
||||
{
|
||||
return $this->hasMany('comment_like');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取评论列表
|
||||
* @param $courseId 课程ID
|
||||
* @param $params 参数
|
||||
* @return array
|
||||
*/
|
||||
public function getList($courseId = 0,$params = []){
|
||||
|
||||
$commentLikeModel = new CommentLike();
|
||||
$where = [
|
||||
'comment.status'=>1,
|
||||
'comment.uniacid'=>UNIACID
|
||||
];
|
||||
|
||||
if($courseId){
|
||||
$where['comment.course_id'] = $courseId;
|
||||
}
|
||||
|
||||
if(isset($params['sort']) && !empty($params['sort'])){
|
||||
if($params['sort']=='time'){
|
||||
$orderField = 'comment.createtime';
|
||||
}else{
|
||||
$orderField = 'like_count';
|
||||
}
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
$list = self::with(['user'])
|
||||
->alias("comment")
|
||||
->join($commentLikeModel->name." like","comment.id=like.comment_id","left")
|
||||
->where($where)
|
||||
->limit($params['limit'])->page($params['page'])
|
||||
->order($orderField,'desc')
|
||||
->group("comment.id")
|
||||
->field(["count(like.id) as like_count","comment.content","comment.createtime","comment.id","comment.course_id"])
|
||||
->select();
|
||||
|
||||
if(!empty($list)){
|
||||
foreach ($list as $item){
|
||||
|
||||
if(!empty($item['reply_comment_id'])){
|
||||
$item['reply'] = self::with(['user'])->where([
|
||||
'comment.id'=>$item['reply_comment_id'],
|
||||
'comment.status'=>1
|
||||
])->find();
|
||||
}
|
||||
|
||||
if(isset($item['reply']) && !empty($item['reply'])){
|
||||
$item['reply']['user']->visible(["nickname","avatar"]);
|
||||
$item['reply'] = $item['reply']->visible(["content","createtime","id","user"]);
|
||||
}
|
||||
|
||||
$item['content'] = str_replace("\n","<br>",$item['content']);
|
||||
$item['user'] = $item['user']->visible(["nickname","avatar"]);
|
||||
|
||||
$item['islike'] = false;
|
||||
$item['candel'] = false;
|
||||
|
||||
if(isset($params['user_id']) && $params['user_id']){
|
||||
$item['islike'] = $commentLikeModel->getIsLike($item['id'],$params['user_id']);
|
||||
$item['candel'] = $params['user_id']==$item['user_id'];
|
||||
}
|
||||
|
||||
$item->user->avatar = cdnurl($item->user->avatar);
|
||||
|
||||
if($item->user_id==0){
|
||||
$item->user->nickname = '管理员';
|
||||
$item->user->avatar = '/assets/logo.png';
|
||||
}
|
||||
|
||||
$data[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
* @param $commentId
|
||||
* @return void
|
||||
*/
|
||||
public function del($commentId){
|
||||
self::where([
|
||||
'id'=>$commentId
|
||||
])->delete();
|
||||
|
||||
CommentLike::where([
|
||||
'comment_id'=>$commentId
|
||||
])->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\course;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class CommentLike extends Model
|
||||
{
|
||||
// 表名
|
||||
public $name = 'comment_like';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
/**
|
||||
* 判断某用户是否点赞
|
||||
* @param $commentId
|
||||
* @param $userId
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsLike($commentId,$userId){
|
||||
$res = self::where([
|
||||
'comment_id'=>$commentId,
|
||||
'user_id'=>$userId,
|
||||
'uniacid'=>UNIACID
|
||||
])->find();
|
||||
|
||||
if($res){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 点赞
|
||||
* @param $commentId
|
||||
* @param $userId
|
||||
* @return bool
|
||||
*/
|
||||
public function setLike($commentId,$userId){
|
||||
$isLike = $this->getIsLike($commentId,$userId);
|
||||
|
||||
if($isLike){
|
||||
self::where([
|
||||
'comment_id'=>$commentId,
|
||||
'user_id'=>$userId,
|
||||
])->delete();
|
||||
}else{
|
||||
self::save([
|
||||
'comment_id'=>$commentId,
|
||||
'user_id'=>$userId,
|
||||
'uniacid'=>UNIACID,
|
||||
'createtime'=>time()
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\course;
|
||||
|
||||
use think\Model;
|
||||
use app\api\model\course\ColumnBind;
|
||||
use app\common\model\user\Subscription;
|
||||
use app\common\exception\Exception;
|
||||
class Course extends Model
|
||||
{
|
||||
|
||||
// 表名
|
||||
protected $name = 'course';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
|
||||
/**
|
||||
* 获取课程价格
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
public static function getPrice($id){
|
||||
$data = self::get($id);
|
||||
|
||||
if(!$data){
|
||||
return false;
|
||||
}
|
||||
|
||||
$buyData = [
|
||||
'pay_type'=>$data['pay_type'],
|
||||
'price'=>$data['price'],
|
||||
'sales_type'=>json_decode($data['sales_type'],true)
|
||||
];
|
||||
|
||||
if($data['pay_type'] != 'free'){
|
||||
//判断是否在专栏中设置了试看
|
||||
//如果专栏是免费的,那么专栏下的所有课程都可查看
|
||||
$columnBind = ColumnBind::with([
|
||||
'column'
|
||||
])->where(function ($query) use ($data) {
|
||||
$query->where([
|
||||
'course_id'=>$data['id'],
|
||||
'try'=>1,
|
||||
'column.status'=>1,
|
||||
'column.uniacid'=>UNIACID
|
||||
]);
|
||||
})->whereOr(function ($query) use ($data) {
|
||||
$query->where([
|
||||
'course_id'=>$data['id'],
|
||||
'column.status'=>1,
|
||||
'column.pay_type'=>'free',
|
||||
'column.uniacid'=>UNIACID
|
||||
]);
|
||||
})->field(["column.id"])->select();
|
||||
|
||||
if($columnBind){
|
||||
$buyData['pay_type'] = 'free';
|
||||
}
|
||||
}
|
||||
|
||||
return $buyData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查课程销售类型
|
||||
* @param $row 课程详情
|
||||
* @return false
|
||||
*/
|
||||
public static function checkSalesType($row){
|
||||
|
||||
$row['sales_type'] = json_decode($row['sales_type'],true);
|
||||
|
||||
if($row['type'] != 'column' && (!$row['sales_type'] || !in_array("alone",$row['sales_type']))){
|
||||
throw new Exception("该课程未开启单独售卖");
|
||||
}
|
||||
|
||||
if($row['type'] == 'column' && !in_array("alone",$row['sales_type'])){
|
||||
throw new Exception("该专栏未开启单独售卖");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user