调整测试案例

This commit is contained in:
Anyon 2020-09-03 11:03:09 +08:00
parent 4c8ecbf501
commit 71d301c162
4 changed files with 75 additions and 68 deletions

View File

@ -55,8 +55,9 @@ class News extends Controller
*/
public function getLike()
{
$data = $this->_vali(['cid.require' => '文章ID不能为空']);
$query = $this->app->db->name('DataNewsXLike')->where($data);
$query = $this->app->db->name('DataNewsXCollect')->where($this->_vali([
'cid.require' => '文章ID不能为空', 'type.value' => 2,
]));
$this->success('获取已点赞的会员', ['list' => $query->order('mid asc')->column('mid')]);
}
@ -65,8 +66,9 @@ class News extends Controller
*/
public function getCollect()
{
$data = $this->_vali(['cid.require' => '文章ID不能为空']);
$query = $this->app->db->name('DataNewsXCollect')->where($data);
$query = $this->app->db->name('DataNewsXCollect')->where($this->_vali([
'cid.require' => '文章ID不能为空', 'type.value' => 1,
]));
$this->success('获取已收藏的会员', ['list' => $query->order('mid asc')->column('mid')]);
}

View File

@ -31,6 +31,19 @@ class News extends Auth
}
}
/**
* 获取我的评论
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getComment()
{
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']);
$query = $this->app->db->name('DataNewsXComment')->where($data)->order('id desc');
$this->success('获取评论列表成功', ['list' => $query->select()->toArray()]);
}
/**
* 删除内容评论
* @throws \think\db\exception\DbException
@ -49,31 +62,18 @@ class News extends Auth
}
}
/**
* 获取我的评论
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getComment()
{
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']);
$query = $this->app->db->name('DataNewsXComment')->where($data)->order('id desc');
$this->success('获取评论列表成功', ['list' => $query->select()->toArray()]);
}
/**
* 添加内容收藏
* @throws \think\db\exception\DbException
*/
public function addCollect()
{
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']);
if ($this->app->db->name('DataNewsXCollect')->where($data)->count() > 0) {
$map = $this->_getCollectWhere(1);
if ($this->app->db->name('DataNewsXCollect')->where($map)->count() > 0) {
$this->success('您已收藏!');
}
if ($this->app->db->name('DataNewsXCollect')->insert($data) !== false) {
NewsService::instance()->syncNewsTotal($data['cid']);
if ($this->app->db->name('DataNewsXCollect')->insert($map) !== false) {
NewsService::instance()->syncNewsTotal($map['cid']);
$this->success('收藏成功!');
} else {
$this->error('收藏失败!');
@ -86,7 +86,7 @@ class News extends Auth
*/
public function delCollect()
{
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '文章ID不能为空']);
$data = $this->_getCollectWhere(1);
if ($this->app->db->name('DataNewsXCollect')->where($data)->delete() !== false) {
NewsService::instance()->syncNewsTotal($data['cid']);
$this->success('取消收藏成功!');
@ -96,12 +96,13 @@ class News extends Auth
}
/**
* 获取收藏的资讯
* 获取会员收藏的资讯
* @throws \think\db\exception\DbException
*/
public function getMyCollect()
public function getCollect()
{
$query = $this->_query('DataNewsXCollect')->where(['mid' => $this->mid]);
$query = $this->_query('DataNewsXCollect');
$query->where(['mid' => $this->mid, 'type' => 1]);
$result = $query->order('id desc')->page(true, false, false, 15);
NewsService::instance()->buildListByCid($result['list']);
$this->success('获取收藏记录成功!', $result);
@ -113,12 +114,12 @@ class News extends Auth
*/
public function addLike()
{
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']);
if ($this->app->db->name('DataNewsXLike')->where($data)->count() > 0) {
$map = $this->_getCollectWhere(2);
if ($this->app->db->name('DataNewsXCollect')->where($map)->count() > 0) {
$this->success('您已点赞!');
}
if ($this->app->db->name('DataNewsXLike')->insert($data) !== false) {
NewsService::instance()->syncNewsTotal($data['cid']);
if ($this->app->db->name('DataNewsXCollect')->insert($map) !== false) {
NewsService::instance()->syncNewsTotal($map['cid']);
$this->success('点赞成功!');
} else {
$this->error('点赞失败!');
@ -131,9 +132,9 @@ class News extends Auth
*/
public function delLike()
{
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']);
if ($this->app->db->name('DataNewsXLike')->where($data)->delete() !== false) {
NewsService::instance()->syncNewsTotal($data['cid']);
$map = $this->_getCollectWhere(2);
if ($this->app->db->name('DataNewsXCollect')->where($map)->delete() !== false) {
NewsService::instance()->syncNewsTotal($map['cid']);
$this->success('取消点赞成功!');
} else {
$this->error('取消点赞失败!');
@ -141,18 +142,31 @@ class News extends Auth
}
/**
* 获取浏览历史
* 获取会员的浏览历史
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getHistory()
{
$query = $this->_query('DataNewsXHistory');
$query->where(['mid' => $this->mid])->order('id desc');
$result = $query->page(true, false, false, 15);
$query = $this->_query('DataNewsXHistory')->where(['mid' => $this->mid]);
$result = $query->order('id desc')->page(true, false, false, 15);
NewsService::instance()->buildListByCid($result['list']);
$this->success('获取浏览历史成功!', $result);
}
/**
* 获取收藏点赞
* @param integer $type 数据类型
* @return array
*/
private function _getCollectWhere($type = 1): array
{
return $this->_vali([
'mid.value' => $this->mid,
'type.value' => $type,
'cid.require' => '文章ID不能为空',
]);
}
}

View File

@ -138,15 +138,16 @@ CREATE TABLE `data_news_mark` (
-- ----------------------------
DROP TABLE IF EXISTS `data_news_x_collect`;
CREATE TABLE `data_news_x_collect` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`cid` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '文章编号',
`mid` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '会员MID',
`content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '评论内容',
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`cid` bigint(20) unsigned DEFAULT '0' COMMENT '文章编号',
`mid` bigint(20) unsigned DEFAULT '0' COMMENT '会员MID',
`type` tinyint(1) unsigned DEFAULT '1' COMMENT '记录类型(1收藏,2点赞)',
`create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_data_news_x_collect_cid`(`cid`) USING BTREE,
INDEX `idx_data_news_x_collect_mid`(`mid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 CHARACTER SET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='数据-文章-会员-收藏' ROW_FORMAT=Compact;
KEY `idx_data_news_x_collect_cid` (`cid`) USING BTREE,
KEY `idx_data_news_x_collect_mid` (`mid`) USING BTREE,
KEY `idx_data_news_x_collect_type` (`type`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='数据-文章-会员-收藏';
-- ----------------------------
-- Table structure for data_news_x_comment
@ -177,18 +178,4 @@ CREATE TABLE `data_news_x_history` (
INDEX `idx_data_news_x_history_mid`(`mid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 CHARACTER SET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='数据-文章-会员-历史' ROW_FORMAT=Compact;
-- ----------------------------
-- Table structure for data_news_x_like
-- ----------------------------
DROP TABLE IF EXISTS `data_news_x_like`;
CREATE TABLE `data_news_x_like` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`cid` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '文章编号',
`mid` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '会员MID',
`create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_data_news_x_like_cid`(`cid`) USING BTREE,
INDEX `idx_data_news_x_like_mid`(`mid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 CHARACTER SET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='数据-文章-会员-点赞' ROW_FORMAT=Compact;
SET FOREIGN_KEY_CHECKS=1;

View File

@ -13,21 +13,25 @@ class NewsService extends Service
{
/**
* 同步文章数据统计
* @param integer $cid 文章ID
* @param integer $cid 文章记录ID
* @throws \think\db\exception\DbException
*/
public function syncNewsTotal($cid)
public function syncNewsTotal(int $cid)
{
$this->app->db->name('DataNewsItem')->where(['id' => $cid])->update([
'num_like' => $this->app->db->name('DataNewsXLike')->where(['cid' => $cid])->count(),
'num_comment' => $this->app->db->name('DataNewsXComment')->where(['cid' => $cid])->count(),
'num_collect' => $this->app->db->name('DataNewsXCollect')->where(['cid' => $cid])->count(),
[$map, $total] = [['cid' => $cid], []];
$query = $this->app->db->name('DataNewsXCollect')->field('count(1) count,type');
$query->where($map)->group('type')->select()->map(function ($item) use (&$total) {
$total[$item['type']] = $item['count'];
});
$this->app->db->name('DataNewsItem')->where($map)->update([
'num_collect' => $total[2] ?? 0, 'num_like' => $total[1] ?? 0,
'num_comment' => $this->app->db->name('DataNewsXComment')->where($map)->count(),
]);
}
/**
* 根据CID绑定列表数据
* @param array $list
* @param array $list 数据列表
* @return array
*/
public function buildListByCid(array &$list = []): array
@ -43,7 +47,7 @@ class NewsService extends Service
/**
* 根据MID绑定列表数据
* @param array $list
* @param array $list 数据列表
* @return array
*/
public function buildListByMid(array &$list = []): array
@ -59,8 +63,8 @@ class NewsService extends Service
/**
* 获取列表状态
* @param array $list
* @param integer $mid
* @param array $list 数据列表
* @param integer $mid 会员MID
* @return array
*/
public function buildListState(array &$list, int $mid = 0): array