初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\user;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\course\Comment as commonComment;
|
||||
use think\Db;
|
||||
/**
|
||||
* 课程评论
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Comment extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Comment模型对象
|
||||
* @var \app\admin\model\user\Comment
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\user\Comment;
|
||||
$this->commonCommentModel = new commonComment;
|
||||
$this->commonCommentLikeModel = new \app\common\model\course\CommentLike;
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加评论
|
||||
* @return void
|
||||
*/
|
||||
public function reply(){
|
||||
$content = $this->request->post('content');
|
||||
$replyCommentId = $this->request->post('comment_id');
|
||||
|
||||
if(empty($content)){
|
||||
$this->error("请完善评论内容");
|
||||
}
|
||||
|
||||
$data = [
|
||||
'createtime'=>time(),
|
||||
'content'=>$content,
|
||||
'user_id'=>0,
|
||||
'uniacid'=>UNIACID
|
||||
];
|
||||
if($replyCommentId){
|
||||
$replyCommentData = $this->commonCommentModel->getComment($replyCommentId);
|
||||
$data['course_id'] = $replyCommentData['course_id'];
|
||||
if(!$replyCommentData){
|
||||
return $this->error("回复的评论不存在");
|
||||
}
|
||||
$data['reply_comment_id'] = $replyCommentData['id'];
|
||||
$data['reply_user_id'] = $replyCommentData['user_id'];
|
||||
}
|
||||
$this->commonCommentModel->save($data);
|
||||
|
||||
return $this->success("评论成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//当前是否为关联查询
|
||||
$this->relationSearch = true;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
if($sort=='comment.like_count'){
|
||||
$sort = "like_count";
|
||||
}
|
||||
|
||||
$list = $this->commonCommentModel->with(['user','course'])
|
||||
->alias("comment")
|
||||
->join($this->commonCommentLikeModel->name." like","comment.id=like.comment_id","left")
|
||||
->where($where)
|
||||
->group("comment.id")
|
||||
->field(["count(like.id) as like_count","comment.content","comment.createtime","comment.id","comment.course_id"])
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
if(!empty($list)){
|
||||
$list = $list->toArray();
|
||||
foreach ($list['data'] as &$row) {
|
||||
if(!empty($row['reply_comment_id'])){
|
||||
$row['reply'] = $this->model->with(['user'])->where([
|
||||
'comment.id'=>$row['reply_comment_id']
|
||||
])->find();
|
||||
}
|
||||
if($row['user_id']==0){
|
||||
$row['user'] = [
|
||||
'nickname'=>'管理员',
|
||||
'username'=>'admin',
|
||||
'avatar'=>'/assets/logo.png'
|
||||
];
|
||||
}
|
||||
$row['content'] = str_replace("\n","<br>",$row['content']);
|
||||
|
||||
// $row->getRelation('user')->visible(\app\admin\model\User::$listShowField);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$result = array("total" => $list['total'], "rows" => $list['data']);
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param $ids
|
||||
* @return void
|
||||
* @throws DbException
|
||||
* @throws DataNotFoundException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function del($ids = null)
|
||||
{
|
||||
if (false === $this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ?: $this->request->post("ids");
|
||||
if (empty($ids)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
$pk = $this->model->getPk();
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$list = $this->model->where($pk, 'in', $ids)->select();
|
||||
|
||||
$count = 0;
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($list as $item) {
|
||||
$count += $this->commonCommentModel->del($item['id']);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success("操作成功");
|
||||
}
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换可用状态
|
||||
* @return void
|
||||
*/
|
||||
public function changeStatus($id){
|
||||
$row = $this->model->get($id);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$row->allowField(true)->save([
|
||||
'status'=>$row['status'] == 1 ? 0 : 1
|
||||
]);
|
||||
|
||||
return $this->success("操作成功");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user