Files
huangjingfen/pro_v3.5.1/app/dao/community/CommunityCommentDao.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

130 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\dao\community;
use app\dao\BaseDao;
use app\model\community\CommunityComment;
use crmeb\basic\BaseModel;
/**
* 社区评论
* Class CommunityCommentDao
* @package app\dao\community
*/
class CommunityCommentDao extends BaseDao
{
/**
* 设置模型
* @return string
*/
protected function setModel(): string
{
return CommunityComment::class;
}
/**
* @param array $where
* @param bool $search
* @return BaseModel
* @throws \ReflectionException
*/
public function search(array $where = [], bool $search = false)
{
$keyword = $where['keyword'] ?? '';
$fieldKey = $where['field_key'] ?? '';
$fieldKey = in_array($fieldKey, ['user', 'community', 'comment', 'id']) ? $fieldKey : '';
/** field_key
* user
* community
* comment
* id
*/
return parent::search($where, $search)
->when(isset($where['author_uid']), function ($query) use ($where) {
if ($where['author_uid'] == 0) {
$query->where('is_show', 1);
}
})
->when($keyword !== '' && $fieldKey !== '', function ($query) use ($fieldKey, $keyword) {
switch ($fieldKey) {
case 'user':
$query->whereIn('uid', function ($query) use ($keyword) {
$query->name('user')->whereLike('nickname', '%' . $keyword . '%')->field('uid');
});
break;
case 'community':
$query->whereIn('community_id', function ($query) use ($keyword) {
$query->name('community')->whereLike('title|content', '%' . $keyword . '%')->field('id');
});
break;
case 'comment':
$query->whereLike('content', '%' . $keyword . '%');
break;
case 'id':
$query->where('id', $keyword);
}
})->when(isset($where['is_verify']) && $where['is_verify'] !== '', function ($query) use ($where) {
if (isset($where['author_uid'])) {
$query->where(function ($query) use ($where) {
if ($where['author_uid'] == 0) {
$query->where('is_verify', 1);
} else {
$query->where(function ($query) use ($where) {
// 条件一uid 等于 author_uid且 is_verify 为 0 或 1
$query->where('uid', $where['author_uid'])
->whereIn('is_verify', [0, 1]);
})->whereOr(function ($query) use ($where) {
// 条件二uid 不等于 author_uid且 is_verify 为 1
$query->where('uid', '<>', $where['author_uid'])
->where('is_verify', 1);
});
}
});
} else {
$query->where('is_verify', $where['is_verify']);
}
});
}
/**
* 获取评论数量
* @param array $where 查询条件
* @return int
*/
public function count($where = []): int
{
return $this->search($where)->count();
}
/**
* 评论列表
* @param array $where
* @param string $field
* @param array $with
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0, $order = 'add_time DESC')
{
return $this->search($where)->field($field)
->when($with, function ($query) use ($with) {
$query->with($with);
})->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->order($order)->select()->toArray();
}
}