161 lines
3.6 KiB
PHP
161 lines
3.6 KiB
PHP
<?php
|
|
|
|
|
|
namespace Yunshop\Article\models;
|
|
|
|
|
|
use app\common\models\BaseModel;
|
|
use app\common\models\Member;
|
|
|
|
class Comment extends BaseModel
|
|
{
|
|
public $table = "yz_plugin_article_comment";
|
|
|
|
protected $guarded = [''];
|
|
|
|
protected $appends = ['type_name','comment_images','head_image'];
|
|
|
|
/**
|
|
* 状态
|
|
*/
|
|
const TYPE_COMMENT = 1;
|
|
const TYPE_REPLY = 2;
|
|
const TYPE_ADD_COMMENT = 3;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
public static $StatusComment = [
|
|
self::TYPE_COMMENT => '评论',
|
|
self::TYPE_REPLY => '回复',
|
|
self::TYPE_ADD_COMMENT => '追加评论',
|
|
];
|
|
|
|
public function getTypeNameAttribute()
|
|
{
|
|
return self::$StatusComment[$this->attributes['type']] ?: '追加回复';
|
|
}
|
|
|
|
public function getCommentImagesAttribute()
|
|
{
|
|
if($this->attributes['images'])
|
|
{
|
|
$images = unserialize($this->attributes['images']);
|
|
foreach ($images as &$image)
|
|
{
|
|
$image = yz_tomedia($image);
|
|
}
|
|
return $images;
|
|
}else{
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public function getHeadImageAttribute()
|
|
{
|
|
if($this->attributes['head_img_url'])
|
|
{
|
|
return yz_tomedia($this->attributes['head_img_url']);
|
|
}else{
|
|
return '';
|
|
}
|
|
}
|
|
|
|
public static function getComments($search){
|
|
$merModel=self::uniacid();
|
|
|
|
if ($search['article_id']) {
|
|
$merModel->where('article_id',$search['article_id']);
|
|
}
|
|
|
|
|
|
if ($search['is_time']) {
|
|
if ($search['time']) {
|
|
$range = [strtotime($search['time']['start']), strtotime($search['time']['end'])];
|
|
$merModel->whereBetween('created_at', $range);
|
|
}
|
|
}
|
|
|
|
$merModel->with(['member' => function ($query) {
|
|
$query->select('uid', 'nickname', 'avatar');
|
|
}]);
|
|
$merModel->orderBy('created_at', 'desc')->orderBy('id', 'desc');
|
|
|
|
return $merModel;
|
|
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public static function getComment($id)
|
|
{
|
|
return self::with(['hasManyReply'=>function ($query) {
|
|
return $query->where('type', 2)
|
|
->orderBy('created_at', 'asc');
|
|
},'article' => function($q2){
|
|
return $q2->select('id','title');
|
|
},'member' => function ($query) {
|
|
$query->select('uid', 'nickname', 'avatar');
|
|
}])
|
|
->with(['hasManyAppend' => function ($query) {
|
|
return $query->where('type', 3)
|
|
->orderBy('created_at', 'asc');
|
|
}])
|
|
->where('id', $id);
|
|
}
|
|
|
|
public function member()
|
|
{
|
|
return $this->belongsTo(Member::class, 'uid', 'uid');
|
|
}
|
|
|
|
public function article()
|
|
{
|
|
return $this->belongsTo(Article::class, 'article_id', 'id');
|
|
}
|
|
|
|
public function hasManyReply()
|
|
{
|
|
return $this->hasMany(self::class);
|
|
}
|
|
|
|
public function hasManyAppend()
|
|
{
|
|
return $this->hasMany(self::class);
|
|
}
|
|
|
|
/**
|
|
* 定义字段名
|
|
* 可使
|
|
* @return array
|
|
*/
|
|
public function atributeNames()
|
|
{
|
|
return [
|
|
'article_id' => '文章ID',
|
|
'content' => '评论内容',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 字段规则
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'article_id' => 'required',
|
|
'content' => 'required'
|
|
];
|
|
}
|
|
|
|
public function isPass()
|
|
{
|
|
if(\Setting::get('plugin.article.enabled_review')){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
} |