TaoLer/app/common/model/Comment.php

136 lines
4.0 KiB
PHP
Raw Normal View History

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
* @LastEditTime: 2022-08-16 12:18:29
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;
}
//回帖榜
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()]);
}
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;
}
2022-08-02 21:13:36 +08:00
} else {
$res = ['status' => 0, 'msg' =>'no reply'];
}
return json($res);
}
2022-08-02 21:13:36 +08:00
/**
* 获取用户评论列表
*
* @param integer $id
* @return void
*/
2022-11-18 10:31:44 +08:00
public function getUserCommentList1(int $id) {
$userCommList = $this::field('id,user_id,create_time,article_id,content')
->with(['article' => function($query){
$query->withField('id,title,create_time')->where(['delete_time'=>0,'status' => 1]);
2022-08-02 21:13:36 +08:00
}])
->where(['user_id' => $id,'status' => 1])
2022-11-18 10:31:44 +08:00
->append(['url'])
->order(['create_time' => 'desc'])
//->cache(3600)
->select()
->toArray();
return $userCommList;
}
/**
* 获取用户评论列表
*
* @param integer $id
* @return void
*/
public function getUserCommentList(int $id) {
$userCommList = Article::field('Article.id,title,Article.create_time')->hasWhere('comments',['status'=>1,'delete_time'=>0])
->with(['comments' => function($query) use($id){
$query->withField('id,content')->where(['user_id'=>$id,'delete_time'=>0,'status' => 1]);
}])
->append(['url'])
2022-08-02 21:13:36 +08:00
->order(['create_time' => 'desc'])
//->cache(3600)
->select()
->toArray();
return $userCommList;
}
2022-11-18 10:31:44 +08:00
2022-08-02 21:13:36 +08:00
// 获取url
public function getUrlAttr($value,$data)
{
2022-11-18 10:31:44 +08:00
// dump($data);
2022-08-02 21:13:36 +08:00
if(config('taoler.url_rewrite.article_as') == '<ename>/') {
2022-11-18 10:31:44 +08:00
$article = Article::field('id,cate_id')->with(['cate' => function($query){
$query->withField('id,ename')->where(['status' => 1]);
}])->find($data['article_id']);
return (string) url('detail',['id' => $data['article_id'],'ename'=>$article->cate->ename]);
2022-08-02 21:13:36 +08:00
} else {
return (string) url('detail',['id' => $data['id']]);
}
}
2020-01-01 13:17:19 +08:00
}