添加评论功能

This commit is contained in:
陈淳
2022-12-01 16:06:53 +08:00
parent e9bcd29e36
commit 44db5ab150
13 changed files with 289 additions and 41 deletions

View File

@@ -162,6 +162,25 @@
<param name="entity"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.CommentController.GetListByArticleId(System.Int64)">
<summary>
获取文章的全部一级评论
</summary>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.CommentController.GetById(System.Int64)">
<summary>
获取一级评论详情
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.CommentController.Add(Yi.Framework.Model.Models.CommentEntity)">
<summary>
回复文章或回复评论
</summary>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.SkuController.PageList(Yi.Framework.Model.Models.SkuEntity,Yi.Framework.Common.Models.PageParModel)">
<summary>
动态条件分页查询

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
@@ -6,6 +7,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.DTOModel.Vo;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
@@ -20,19 +22,24 @@ namespace Yi.Framework.ApiMicroservice.Controllers
public class CommentController : BaseSimpleCrudController<CommentEntity>
{
private ICommentService _iCommentService;
public CommentController(ILogger<CommentEntity> logger, ICommentService iCommentService) : base(logger, iCommentService)
private IMapper _mapper;
public CommentController(ILogger<CommentEntity> logger, ICommentService iCommentService, IMapper mapper) : base(logger, iCommentService)
{
_iCommentService = iCommentService;
_mapper = mapper;
}
/// <summary>
/// 获取全部一级评论
/// 获取文章的全部一级评论
/// </summary>
/// <returns></returns>
public override async Task<Result> GetList()
[HttpGet]
[Route("{articleId}")]
public async Task<Result> GetListByArticleId(long articleId)
{
var data = await _repository.GetListAsync(u=>u.UserId==null);
return Result.Success().SetData(data);
//一级评论被回复的用户id为空
var data = await _repository._DbQueryable.Where(u => u.ParentId == 0 && u.ArticleId == articleId).Includes(u => u.CreateUserInfo).OrderByDescending(u=>u.CreateTime).ToListAsync();
return Result.Success().SetData(_mapper.Map<List<CommentVo>>(data));
}
/// <summary>
@@ -40,9 +47,10 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override Task<Result> GetById([FromRoute] long id)
public override async Task<Result> GetById([FromRoute] long id)
{
return base.GetById(id);
var data = await _repository._DbQueryable.Includes(u => u.CreateUserInfo).Includes(u => u.UserInfo).FirstAsync(u => u.Id == id);
return Result.Success().SetData(_mapper.Map<CommentVo>(data));
}
/// <summary>
@@ -50,9 +58,9 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<Result> Comment()
{
public override async Task<Result> Add(CommentEntity comment)
{
return Result.Success().SetStatus(await _iCommentService.AddAsync(comment));
}
}
}

View File

@@ -4,6 +4,12 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Hangfire\**" />
<EmbeddedResource Remove="Hangfire\**" />
<None Remove="Hangfire\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="2.0.8" />
<PackageReference Include="Consul" Version="1.6.10.3" />
@@ -22,8 +28,4 @@
<ProjectReference Include="..\Yi.Framework.Task\Yi.Framework.Job.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Hangfire\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,61 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.DTOModel.Vo
{
public class CommentVo
{
public long Id { get; set; }
/// <summary>
/// 评论内容
///</summary>
public string Content { get; set; }
/// <summary>
/// 点赞数
///</summary>
public int? AgreeNum { get; set; }
/// <summary>
/// 创建时间
///</summary>
public DateTime? CreateTime { get; set; }
/// <summary>
/// 是否删除
///</summary>
public bool? IsDeleted { get; set; }
/// <summary>
/// 租户Id
///</summary>
public long? TenantId { get; set; }
/// <summary>
/// 排序字段
///</summary>
public int? OrderNum { get; set; }
/// <summary>
/// 描述
///</summary>
public string Remark { get; set; }
/// <summary>
/// 子评论数
///</summary>
public int? CommentNum { get; set; }
/// <summary>
/// 被回复的用户信息
///</summary>
public UserVo? UserInfo { get; set; }
/// <summary>
/// 创建评论的用户信息
///</summary>
public UserVo? CreateUserInfo { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
namespace Yi.Framework.Interface
{
public partial interface ICommentService
{
Task<bool> AddAsync(CommentEntity comment);
}
}

View File

@@ -0,0 +1,24 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public partial class CommentEntity
{
/// <summary>
/// 被回复的用户信息
///</summary>
[Navigate(NavigateType.OneToOne,nameof(UserId),nameof(UserEntity.Id))]
public UserEntity? UserInfo { get; set; }
/// <summary>
/// 创建评论的用户信息
///</summary>
[Navigate(NavigateType.OneToOne, nameof(CreateUser), nameof(UserEntity.Id))]
public UserEntity? CreateUserInfo { get; set; }
}
}

View File

@@ -14,6 +14,9 @@ namespace Yi.Framework.Model.Models
public CommentEntity()
{
this.CreateTime = DateTime.Now;
this.AgreeNum= 0;
this.CommentNum = 0;
this.ParentId= 0;
}
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
@@ -32,7 +35,7 @@ namespace Yi.Framework.Model.Models
/// 评论内容
///</summary>
[SugarColumn(ColumnName="Content" )]
public string Content { get; set; }
public string? Content { get; set; }
/// <summary>
/// 点赞数
///</summary>
@@ -77,7 +80,7 @@ namespace Yi.Framework.Model.Models
/// 描述
///</summary>
[SugarColumn(ColumnName="Remark" )]
public string Remark { get; set; }
public string? Remark { get; set; }
/// <summary>
/// 子评论数
///</summary>

View File

@@ -0,0 +1,30 @@
using SqlSugar;
using System.Threading.Tasks;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
namespace Yi.Framework.Service
{
public partial class CommentService : BaseService<CommentEntity>, ICommentService
{
//添加一个评论
public async Task<bool> AddAsync(CommentEntity comment)
{
//如果是一级评论:不用处理
//如果是二级评论ParentId父节点评论数+1
return await _repository.UseTranAsync(async () =>
{
if (comment.ParentId != 0)
{
var parentData = await _repository.GetByIdAsync(comment.ParentId);
parentData.CommentNum += 1;
await _repository.AsUpdateable(parentData).UpdateColumns(u => new { u.CommentNum }).ExecuteCommandAsync();
}
await _repository.InsertReturnSnowflakeIdAsync(comment);
});
}
}
}

View File

@@ -16,6 +16,7 @@ namespace Yi.Framework.WebCore.Mapper
{
CreateMap<ArticleEntity, ArticleVo > ();
CreateMap<UserEntity, UserVo>();
CreateMap<CommentEntity, CommentVo>();
}
}

View File

@@ -18,7 +18,6 @@ declare module '@vue/runtime-core' {
VanButton: typeof import('vant/es')['Button']
VanCellGroup: typeof import('vant/es')['CellGroup']
VanCol: typeof import('vant/es')['Col']
VanDivider: typeof import('vant/es')['Divider']
VanField: typeof import('vant/es')['Field']
VanGrid: typeof import('vant/es')['Grid']
VanGridItem: typeof import('vant/es')['GridItem']
@@ -34,6 +33,5 @@ declare module '@vue/runtime-core' {
VanTabbar: typeof import('vant/es')['Tabbar']
VanTabbarItem: typeof import('vant/es')['TabbarItem']
VanTabs: typeof import('vant/es')['Tabs']
VanUploader: typeof import('vant/es')['Uploader']
}
}

View File

@@ -0,0 +1,17 @@
import myaxios from '@/utils/myaxios'
export default {
add(data:any) {
return myaxios({
url: `/comment/add`,
method: 'post',
data: data
})
},
getListByArticleId(articleId:any) {
return myaxios({
url: `/comment/GetListByArticleId/${articleId}`,
method: 'get',
})
}
}

View File

@@ -1,14 +1,24 @@
<template>
<van-pull-refresh v-model="refreshing" @refresh="onRefresh">
<van-list class="list" v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
<van-list
class="list"
v-model:loading="loading"
:finished="finished"
finished-text="没有更多了"
@load="onLoad"
>
<van-row v-for="(item, index) in articleList" :key="index" class="row">
<van-col span="4" class="leftCol">
<AppUserIcon width="3rem" height="3rem" :src="item.user == null ? null : item.user.icon" />
<AppUserIcon
width="3rem"
height="3rem"
:src="item.user == null ? null : item.user.icon"
/>
</van-col>
<van-col span="14" class="centerTitle">
<span class="justtitle">{{
item.user == null ? "-" : item.user.nick ?? item.user.username
item.user == null ? "-" : item.user.nick ?? item.user.username
}}</span>
<br />
<app-createTime :time="item.createTime" />
@@ -20,9 +30,21 @@
<van-col class="rowBody" span="24">{{ item.content }}</van-col>
<van-col span="8" v-for="(image, imageIndex) in item.images" :key="imageIndex" class="imageCol"
@click="openImage(item.images, imageIndex)">
<van-image lazy-load fit="cover" width="100%" height="7rem" :src="url + image + '/true'" radius="5" />
<van-col
span="8"
v-for="(image, imageIndex) in item.images"
:key="imageIndex"
class="imageCol"
@click="openImage(item.images, imageIndex)"
>
<van-image
lazy-load
fit="cover"
width="100%"
height="7rem"
:src="url + image + '/true'"
radius="5"
/>
<template v-slot:loading>
<van-loading type="spinner" size="20" />
</template>
@@ -31,30 +53,55 @@
<van-col span="24" class="bottomRow">
<van-grid direction="horizontal" :column-num="3">
<van-grid-item icon="share-o" text="分享" />
<van-grid-item icon="comment-o" text="评论" @click="commentShow = true" />
<van-grid-item icon="good-job-o" :text="`点赞:${item.agreeNum}`" @click="aggreeHand(item.id)" />
<van-grid-item
icon="comment-o"
text="评论"
@click="openComment(item.id)"
/>
<van-grid-item
icon="good-job-o"
:text="`点赞:${item.agreeNum}`"
@click="aggreeHand(item.id)"
/>
</van-grid>
</van-col>
</van-row>
</van-list>
</van-pull-refresh>
<!-- 功能页面 -->
<van-action-sheet v-model:show="show" :actions="actions" cancel-text="取消" close-on-click-action />
<van-action-sheet
v-model:show="show"
:actions="actions"
cancel-text="取消"
close-on-click-action
/>
<!-- 图片预览 -->
<van-image-preview v-model:show="imageShow" :images="imagesPreview" :startPosition="startIndex" @change="onChange"
:closeable="true">
<van-image-preview
v-model:show="imageShow"
:images="imagesPreview"
:startPosition="startIndex"
@change="onChange"
:closeable="true"
>
<template v-slot:index>{{ index + 1 }}</template>
</van-image-preview>
<!-- 评论面板 -->
<van-action-sheet v-model:show="commentShow" title="共10条评论">
<van-row v-for="i of 10" :key="i" class="commentContent">
<van-row v-for="i in commentList" :key="i" class="commentContent">
<van-col span="4">头像</van-col>
<van-col span="16">内容</van-col>
<van-col span="16">{{ i.content }}</van-col>
<van-col span="4">点赞</van-col>
</van-row>
<van-cell-group inset>
<van-field v-model="commentData.content" placeholder="请输入评论" >
<template #button>
<van-button size="small" type="primary" @click="sendComment()">发布</van-button>
</template>
</van-field>
</van-cell-group>
</van-action-sheet>
</template>
@@ -65,6 +112,7 @@ import AppCreateTime from "@/components/AppCreateTime.vue";
import AppUserIcon from "@/components/AppUserIcon.vue";
import articleApi from "@/api/articleApi";
import agreeApi from "@/api/agreeApi";
import commentApi from "@/api/commentApi";
import { ArticleEntity } from "@/type/interface/ArticleEntity";
const VanImagePreview = ImagePreview.Component;
const url = `${import.meta.env.VITE_APP_BASE_API}/file/`;
@@ -77,14 +125,34 @@ const data = reactive({
isDeleted: false,
},
});
const { queryParams } = toRefs(data);
const commentData = reactive({
content: "",
articleId:0
});
const sendComment=()=>{
commentData.articleId=openCommentId.value;
commentApi.add(commentData).then(()=>{
getCommentList(openCommentId.value);
commentData.content="";
})
}
const { queryParams } = toRefs(data);
const {content}=toRefs(commentData);
const articleList = ref<any[]>([]);
const commentList = ref<any[]>([]);
const totol = ref<Number>(0);
const imageShow = ref(false);
const commentShow = ref(false)
const commentShow = ref<any>(false);
const index = ref(0);
let imagesPreview = ref<string[]>([]);
const openCommentId=ref(0);
const openComment = (id: any) => {
commentShow.value = true;
openCommentId.value=id;
getCommentList(id);
};
const onChange = (newIndex: any) => {
index.value = newIndex;
@@ -140,6 +208,12 @@ onMounted(() => {
// getList();
});
const getCommentList = (id: any) => {
commentApi.getListByArticleId(id).then((response: any) => {
commentList.value = response.data;
});
};
const getList = () => {
articleApi.pageList(queryParams.value).then((response: any) => {
articleList.value.push(...response.data.data);
@@ -150,16 +224,16 @@ const aggreeHand = (articleId: any) => {
agreeApi.operate(articleId).then((response: any) => {
//更改显示的值
if (response.status) {
articleList.value.filter(p => p.id == articleId)[0].agreeNum += 1
articleList.value.filter((p) => p.id == articleId)[0].agreeNum += 1;
} else {
articleList.value.filter(p => p.id == articleId)[0].agreeNum -= 1
articleList.value.filter((p) => p.id == articleId)[0].agreeNum -= 1;
}
Toast({
message: response.message,
position: 'bottom',
})
})
}
position: "bottom",
});
});
};
</script>
<style scoped>
.list {