diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/Community/AgreeController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/Community/AgreeController.cs index 721882b3..ef52a5c0 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/Community/AgreeController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/Community/AgreeController.cs @@ -23,11 +23,11 @@ namespace Yi.Framework.ApiMicroservice.Controllers public class AgreeController : ControllerBase { [Autowired] - public IAgreeService _iAgreeService { get; set; } + public IAgreeService? _iAgreeService { get; set; } [Autowired] - public IArticleService _iArticleService { get; set; } + public IArticleService? _iArticleService { get; set; } [Autowired] - public ILogger _logger { get; set; } + public ILogger? _logger { get; set; } /// /// 点赞操作 @@ -37,30 +37,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers [HttpGet] public async Task Operate(long articleId) { - //long userId = HttpContext.GetUserIdInfo(); - long userId = 1L; - var article = await _iArticleService._repository.GetByIdAsync(articleId); - if (await _iAgreeService._repository.IsAnyAsync(u => u.UserId == userId && u.ArticleId == articleId)) + long userId = HttpContext.GetUserIdInfo(); + if (await _iAgreeService!.OperateAsync(articleId, userId)) { - //已点赞,取消点赞 - await _iAgreeService._repository.UseTranAsync(async () => - { - await _iAgreeService._repository.DeleteAsync(u => u.UserId == userId && u.ArticleId == articleId); - await _iArticleService._repository.UpdateIgnoreNullAsync(new ArticleEntity { Id = articleId, AgreeNum = article.AgreeNum - 1 }); - - }); + return Result.Success("点赞成功"); + } else { - //未点赞,添加点赞记录 - await _iAgreeService._repository.UseTranAsync(async () => - { - await _iAgreeService._repository.InsertAsync(new AgreeEntity { UserId = userId, ArticleId = articleId }); - await _iArticleService._repository.UpdateIgnoreNullAsync(new ArticleEntity { Id = articleId, AgreeNum = article.AgreeNum + 1 }); - }); - + return Result.Success("已点赞,取消点赞").StatusFalse(); } - return Result.Success("这里业务全部拆开放service层去"); } } } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db index 25d6f215..6e7b7aa8 100644 Binary files a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db differ diff --git a/Yi.Framework.Net6/Yi.Framework.DTOModel/Vo/ArticleVo.cs b/Yi.Framework.Net6/Yi.Framework.DTOModel/Vo/ArticleVo.cs index bbe8fd48..721de83d 100644 --- a/Yi.Framework.Net6/Yi.Framework.DTOModel/Vo/ArticleVo.cs +++ b/Yi.Framework.Net6/Yi.Framework.DTOModel/Vo/ArticleVo.cs @@ -33,6 +33,7 @@ namespace Yi.Framework.DTOModel.Vo public string Remark { get; set; } public List Images { get; set; } + public int? AgreeNum { get; set; } public UserVo User { get; set; } } } diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IAgreeService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IAgreeService.cs new file mode 100644 index 00000000..5c8c99b2 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Interface/IAgreeService.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; +using Yi.Framework.Model.Models; +using Yi.Framework.Repository; + +namespace Yi.Framework.Interface +{ + public partial interface IAgreeService : IBaseService + { + Task OperateAsync(long articleId, long userId); + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IAgreeService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IAgreeService.cs index 6d7ccb97..80309158 100644 --- a/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IAgreeService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IAgreeService.cs @@ -1,9 +1,10 @@ -using Yi.Framework.Model.Models; +using System.Threading.Tasks; +using Yi.Framework.Model.Models; using Yi.Framework.Repository; namespace Yi.Framework.Interface { - public partial interface IAgreeService:IBaseService - { + public partial interface IAgreeService : IBaseService + { } } diff --git a/Yi.Framework.Net6/Yi.Framework.Model/ModelsTemplate/ArticleEntity.cs b/Yi.Framework.Net6/Yi.Framework.Model/ModelsTemplate/ArticleEntity.cs index 7151223a..2fd22c2e 100644 --- a/Yi.Framework.Net6/Yi.Framework.Model/ModelsTemplate/ArticleEntity.cs +++ b/Yi.Framework.Net6/Yi.Framework.Model/ModelsTemplate/ArticleEntity.cs @@ -14,6 +14,7 @@ namespace Yi.Framework.Model.Models public ArticleEntity() { this.CreateTime = DateTime.Now; + this.AgreeNum = 0; } [JsonConverter(typeof(ValueToStringConverter))] [SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )] @@ -22,12 +23,12 @@ namespace Yi.Framework.Model.Models /// 文章标题 /// [SugarColumn(ColumnName="Title" )] - public string Title { get; set; } + public string? Title { get; set; } /// /// 文章内容 /// [SugarColumn(ColumnName="Content" )] - public string Content { get; set; } + public string? Content { get; set; } /// /// 用户id /// @@ -72,16 +73,16 @@ namespace Yi.Framework.Model.Models /// 描述 /// [SugarColumn(ColumnName="Remark" )] - public string Remark { get; set; } + public string? Remark { get; set; } /// /// 图片列表 /// - [SugarColumn(ColumnName="Images" )] - public string Images { get; set; } + [SugarColumn(ColumnName = "Images", IsJson = true)] + public List? Images { get; set; } /// /// 点赞数量 /// [SugarColumn(ColumnName="AgreeNum" )] - public int? AgreeNum { get; set; } + public int AgreeNum { get; set; } } } diff --git a/Yi.Framework.Net6/Yi.Framework.Service/AgreeService.cs b/Yi.Framework.Net6/Yi.Framework.Service/AgreeService.cs new file mode 100644 index 00000000..ba61029d --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Service/AgreeService.cs @@ -0,0 +1,42 @@ +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 AgreeService : BaseService, IAgreeService + { + /// + /// 点赞操作 + /// + /// + public async Task OperateAsync(long articleId, long userId) + { + var _articleRepositoty = _repository.ChangeRepository>(); + var article = await _articleRepositoty.GetByIdAsync(articleId); + if (await _repository.IsAnyAsync(u => u.UserId == userId && u.ArticleId == articleId)) + { + //已点赞,取消点赞 + await _repository.UseTranAsync(async () => + { + await _repository.DeleteAsync(u => u.UserId == userId && u.ArticleId == articleId); + await _articleRepositoty.UpdateIgnoreNullAsync(new ArticleEntity { Id = articleId, AgreeNum = article.AgreeNum - 1 }); + + }); + return false; + } + else + { + //未点赞,添加点赞记录 + await _repository.UseTranAsync(async () => + { + await _repository.InsertReturnSnowflakeIdAsync(new AgreeEntity { UserId = userId, ArticleId = articleId }); + await _articleRepositoty.UpdateIgnoreNullAsync(new ArticleEntity { Id = articleId, AgreeNum = article.AgreeNum + 1 }); + }); + return true; + } + } + } +} diff --git a/Yi.Vue3.x.Vant/components.d.ts b/Yi.Vue3.x.Vant/components.d.ts index a63a89cb..72a77002 100644 --- a/Yi.Vue3.x.Vant/components.d.ts +++ b/Yi.Vue3.x.Vant/components.d.ts @@ -16,7 +16,6 @@ declare module '@vue/runtime-core' { RouterView: typeof import('vue-router')['RouterView'] VanActionSheet: typeof import('vant/es')['ActionSheet'] VanButton: typeof import('vant/es')['Button'] - VanCell: typeof import('vant/es')['Cell'] VanCellGroup: typeof import('vant/es')['CellGroup'] VanCol: typeof import('vant/es')['Col'] VanDivider: typeof import('vant/es')['Divider'] @@ -28,12 +27,9 @@ declare module '@vue/runtime-core' { VanList: typeof import('vant/es')['List'] VanLoading: typeof import('vant/es')['Loading'] VanNavBar: typeof import('vant/es')['NavBar'] - VanPopup: typeof import('vant/es')['Popup'] VanPullRefresh: typeof import('vant/es')['PullRefresh'] VanRow: typeof import('vant/es')['Row'] VanSticky: typeof import('vant/es')['Sticky'] - VanSwipe: typeof import('vant/es')['Swipe'] - VanSwipeItem: typeof import('vant/es')['SwipeItem'] VanTab: typeof import('vant/es')['Tab'] VanTabbar: typeof import('vant/es')['Tabbar'] VanTabbarItem: typeof import('vant/es')['TabbarItem'] diff --git a/Yi.Vue3.x.Vant/src/api/agreeApi.ts b/Yi.Vue3.x.Vant/src/api/agreeApi.ts new file mode 100644 index 00000000..a9b2d164 --- /dev/null +++ b/Yi.Vue3.x.Vant/src/api/agreeApi.ts @@ -0,0 +1,11 @@ +import myaxios from '@/utils/myaxios' + +export default { + operate(data:any) { + return myaxios({ + url: `/agree/operate`, + method: 'get', + params: {articleId:data} + }) + }, +} \ No newline at end of file diff --git a/Yi.Vue3.x.Vant/src/utils/myaxios.ts b/Yi.Vue3.x.Vant/src/utils/myaxios.ts index b25bde49..8b8ee565 100644 --- a/Yi.Vue3.x.Vant/src/utils/myaxios.ts +++ b/Yi.Vue3.x.Vant/src/utils/myaxios.ts @@ -55,6 +55,12 @@ myaxios.interceptors.response.use(async function(response) { return resp; }, async function(error) { //未授权、失败 +if(error.response==undefined) +{ + Notify({ type: 'danger', message: `服务器异常:${error.message}` }); + return Promise.reject(error);; +} + const resp = error.response.data if (resp.code == undefined && resp.message == undefined) { Notify({ type: 'danger', message: '未知错误' }); diff --git a/Yi.Vue3.x.Vant/src/view/main/recommend.vue b/Yi.Vue3.x.Vant/src/view/main/recommend.vue index 2df5bb04..7ebe74d0 100644 --- a/Yi.Vue3.x.Vant/src/view/main/recommend.vue +++ b/Yi.Vue3.x.Vant/src/view/main/recommend.vue @@ -1,24 +1,14 @@ @@ -95,6 +64,7 @@ import { ImagePreview, Toast } from "vant"; import AppCreateTime from "@/components/AppCreateTime.vue"; import AppUserIcon from "@/components/AppUserIcon.vue"; import articleApi from "@/api/articleApi"; +import agreeApi from "@/api/agreeApi"; import { ArticleEntity } from "@/type/interface/ArticleEntity"; const VanImagePreview = ImagePreview.Component; const url = `${import.meta.env.VITE_APP_BASE_API}/file/`; @@ -112,7 +82,7 @@ const { queryParams } = toRefs(data); const articleList = ref([]); const totol = ref(0); const imageShow = ref(false); -const commentShow=ref(false) +const commentShow = ref(false) const index = ref(0); let imagesPreview = ref([]); @@ -176,11 +146,26 @@ const getList = () => { totol.value = response.data.totol; }); }; +const aggreeHand = (articleId: any) => { + agreeApi.operate(articleId).then((response: any) => { + //更改显示的值 + if (response.status) { + articleList.value.filter(p => p.id == articleId)[0].agreeNum += 1 + } else { + articleList.value.filter(p => p.id == articleId)[0].agreeNum -= 1 + } + Toast({ + message: response.message, + position: 'bottom', + }) + }) +} \ No newline at end of file