2020-01-01 13:17:19 +08:00
|
|
|
<?php
|
2022-08-02 21:13:36 +08:00
|
|
|
/*
|
|
|
|
* @Author: TaoLer <alipay_tao@qq.com>
|
|
|
|
* @Date: 2021-12-06 16:04:50
|
2022-08-16 15:29:04 +08:00
|
|
|
* @LastEditTime: 2022-08-15 13:37:58
|
2022-08-02 21:13:36 +08:00
|
|
|
* @LastEditors: TaoLer
|
2022-08-16 15:29:04 +08:00
|
|
|
* @Description: 评论模型
|
|
|
|
* @FilePath: \TaoLer\app\common\model\Comment.php
|
2022-08-02 21:13:36 +08:00
|
|
|
* Copyright (c) 2020~2022 https://www.aieok.com All rights reserved.
|
|
|
|
*/
|
2020-01-01 13:17:19 +08:00
|
|
|
namespace app\common\model;
|
|
|
|
|
|
|
|
use think\Model;
|
|
|
|
use think\model\concern\SoftDelete;
|
|
|
|
|
|
|
|
class Comment extends Model
|
2022-08-16 15:29:04 +08:00
|
|
|
{
|
2020-01-01 13:17:19 +08:00
|
|
|
//软删除
|
|
|
|
use SoftDelete;
|
|
|
|
protected $deleteTime = 'delete_time';
|
|
|
|
protected $defaultSoftDelete = 0;
|
|
|
|
|
|
|
|
public function article()
|
|
|
|
{
|
|
|
|
//评论关联文章
|
|
|
|
return $this->belongsTo('Article','article_id','id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
//评论关联用户
|
|
|
|
return $this->belongsTo('User','user_id','id');
|
|
|
|
}
|
2020-11-25 15:29:48 +08:00
|
|
|
|
|
|
|
//获取评论
|
2022-01-07 14:43:42 +08:00
|
|
|
public function getComment($id, $page)
|
2020-11-25 15:29:48 +08:00
|
|
|
{
|
2022-08-02 21:13:36 +08:00
|
|
|
$comments = $this::with(['user'])->where(['article_id'=>(int)$id,'status'=>1])->order(['cai'=>'asc','create_time'=>'asc'])->paginate(['list_rows'=>10, 'page'=>$page]);
|
2020-11-25 15:29:48 +08:00
|
|
|
return $comments;
|
|
|
|
}
|
2020-11-27 16:25:08 +08:00
|
|
|
|
|
|
|
//回帖榜
|
|
|
|
public function reply($num)
|
|
|
|
{
|
2022-08-16 15:29:04 +08:00
|
|
|
try {
|
|
|
|
$user = User::field('id,user_img,name,nickname')
|
|
|
|
->withCount(['comments'=> function($query) {
|
|
|
|
$query->where(['status'=>1]);
|
|
|
|
}])
|
|
|
|
->order(['comments_count'=>'desc','last_login_time'=>'desc'])
|
|
|
|
->limit($num)
|
|
|
|
->withCache(300)
|
|
|
|
->select()
|
|
|
|
->toArray();
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
return json(['status'=>-1,'msg'=>$e->getMessage()]);
|
|
|
|
}
|
2020-11-27 16:25:08 +08:00
|
|
|
|
2022-08-02 21:13:36 +08:00
|
|
|
if(count($user)) {
|
|
|
|
$res['status'] = 0;
|
|
|
|
$res['data'] = array();
|
|
|
|
foreach ($user as $key=>$v) {
|
2022-08-16 15:29:04 +08:00
|
|
|
$u['uid'] = (string) url('user_home',['id'=>$v['id']]);
|
|
|
|
// $u['uid'] = (string) \think\facade\Route::buildUrl('user_home', ['id' => $v['id']]);
|
2022-08-02 21:13:36 +08:00
|
|
|
$u['count'] = $v['comments_count'];
|
2022-08-16 15:29:04 +08:00
|
|
|
|
|
|
|
$u['user'] = ['username'=>$v['nickname'] ?: $v['name'] ,'avatar'=>$v['user_img']];
|
|
|
|
|
2022-08-02 21:13:36 +08:00
|
|
|
$res['data'][] = $u;
|
2020-11-27 16:25:08 +08:00
|
|
|
}
|
2022-08-02 21:13:36 +08:00
|
|
|
} else {
|
|
|
|
$res = ['status' => 0, 'msg' =>'no reply'];
|
2020-11-27 16:25:08 +08:00
|
|
|
}
|
|
|
|
return json($res);
|
|
|
|
}
|
2022-08-02 21:13:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户评论列表
|
|
|
|
*
|
|
|
|
* @param integer $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getUserCommentList(int $id) {
|
|
|
|
$userCommList = $this::field('id,user_id,create_time,delete_time,article_id,content')
|
|
|
|
->with(['article' => function(\think\model\Relation $query){
|
|
|
|
$query->withField('id,title,cate_id,delete_time')->where(['status' => 1,'delete_time' => 0]);
|
|
|
|
}])
|
|
|
|
->where(['user_id' => $id,'status' => 1,'delete_time'=>0])
|
|
|
|
//->append(['url'])
|
|
|
|
->order(['create_time' => 'desc'])
|
|
|
|
//->cache(3600)
|
|
|
|
->select()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
return $userCommList;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取url
|
|
|
|
public function getUrlAttr($value,$data)
|
|
|
|
{
|
|
|
|
if(config('taoler.url_rewrite.article_as') == '<ename>/') {
|
|
|
|
$cate = Cate::field('id,ename')->find($data['article']['cate_id']);
|
|
|
|
return (string) url('detail',['id' => $data['id'],'ename'=>$cate->ename]);
|
|
|
|
} else {
|
|
|
|
return (string) url('detail',['id' => $data['id']]);
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
}
|