【畅购商城】详情页模块之评论

小灰灰 2024-04-01 19:00 248阅读 0赞

目录

接口

分析

后端实现:JavaBean

后端实现

前端实现

接口

GET http://localhost:10010/web-service/comments/spu/2?current=1&size=2

{

“code”: 20000,

“message”: “查询成功”,

“data”: {

“impressions”: [

{

“id”: 1,

“title”: “口感不错”,

“count”: 15326,

“spu_id”: 2,

“sku_id”: 2600242

}

],

“ratio”: {

“common”: “33.33”,

“bad”: “33.33”,

“goods”: “33.33”

},

“comment_count”: 3,

“comments”: [

{

“id”: 3,

“userId”: 1,

“user”: {

“face”: “images/user3.jpg”,

},

“spuId”: 2,

“skuId”: 2600248,

“ratio”: “2”,

“content”: “差”,

}

]

},

“other”: {}

}

分析

2b1c0196785948b892f9fb9d3c624f80.png

后端实现:JavaBean

074c584e331c40b7b358bf57efcbf7ad.png

步骤一:创建 Impression ,用于封装印象表中的数据

  1. package com.czxy.changgou4.pojo;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableField;
  4. import com.baomidou.mybatisplus.annotation.TableId;
  5. import com.baomidou.mybatisplus.annotation.TableName;
  6. import com.fasterxml.jackson.annotation.JsonProperty;
  7. import lombok.Data;
  8. /** 印象
  9. * @author 桐叔
  10. * @email liangtong@itcast.cn
  11. */
  12. @TableName("tb_impression")
  13. @Data
  14. public class Impression {
  15. @TableId(type = IdType.AUTO)
  16. private Integer id;
  17. private String title;
  18. private Integer count;
  19. @TableField("spu_id")
  20. @JsonProperty("spu_id")
  21. private Integer spuId;
  22. @TableField("sku_id")
  23. @JsonProperty("sku_id")
  24. private Integer skuId;
  25. }

步骤二:创建 CommentResult,用于封装评论相关的信息

  1. package com.czxy.changgou4.pojo;
  2. import lombok.Data;
  3. import java.util.List;
  4. import java.util.Map;
  5. /**
  6. * @author 桐叔
  7. * @email liangtong@itcast.cn
  8. */
  9. @Data
  10. public class CommentResult {
  11. private List<Impression> impressions; //印象
  12. private Map<String,Object> ratio; //好评度
  13. private Integer comment_count; //评论数
  14. private List<SkuComment> comments; //评论
  15. }

后端实现

步骤一:创建 ImpressionMapper,完成 “查询指定SpuId的所有印象”

27d4644f23c44c3c9d5bef85a3472cd1.png

  1. package com.czxy.changgou4.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.czxy.changgou4.pojo.Impression;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Param;
  6. import org.apache.ibatis.annotations.Select;
  7. import java.util.List;
  8. /**
  9. * @author 桐叔
  10. * @email liangtong@itcast.cn
  11. */
  12. @Mapper
  13. public interface ImpressionMapper extends BaseMapper<Impression> {
  14. /**
  15. * 查询指定SpuId的所有印象
  16. * @param spuid
  17. * @return
  18. */
  19. @Select("select * from tb_impression where spu_id = #{spuid}")
  20. public List<Impression> findImpressionsBySpuid(@Param("spuid") Integer spuid);
  21. }

步骤二:修改 SkuCommentMapper,完成“查询指定好评度的评论数”

a1fc95398815422abd503835cfcec6c4.png

  1. /**
  2. * 查询指定好评度的评论数
  3. * @param spuid
  4. * @param ratio 0好评、1中评、2差评
  5. * @return
  6. */
  7. @Select("select count(*) from tb_sku_comment where spu_id = #{spuid} and ratio = #{ratio}")
  8. public Integer findCommentCountByRatio(@Param("spuid")Integer spuid,@Param("ratio")Integer ratio);

步骤三:修改 SkuCommentMapper,完成“查询SpuId的评论数”

3a6789fba9c04fc0816b620e313fbe4d.png

  1. /**
  2. * 查询SpuId的评论数
  3. * @param spuid
  4. * @return
  5. */
  6. @Select("select count(*) from tb_sku_comment where spu_id = #{spuid}")
  7. public Integer findTotalCommentBySpuid(@Param("spuid") Integer spuid);

步骤四:修改 skuCommentMapper,完成“查询指定spu的所有评论”

da781f98d437469faecc243be4666625.png

  1. /**
  2. * 查询指定spu的所有评论
  3. * @param spuid
  4. * @return
  5. */
  6. @Select("select * from tb_sku_comment where spu_id = #{spuid} limit #{startIndex},#{size}")
  7. @Results({
  8. @Result(property = "createdAt" , column = "created_at"),
  9. @Result(property = "updatedAt" , column = "updated_at"),
  10. @Result(property = "userId" , column = "user_id"),
  11. @Result(property = "skuId" , column = "sku_id"),
  12. @Result(property = "specList" , column = "spec_list"),
  13. @Result(property = "user" , one = @One(select = "com.czxy.changgou4.mapper.UserMapper.selectById"), column = "user_id"),
  14. })
  15. public List<SkuComment> findCommentsBySpuid(@Param("spuid") Integer spuid, @Param("startIndex") Integer startIndex, @Param("size") Integer size);

步骤五:创建 SkuCommentService接口,定义“查询指定spu的所有评论”方法

  1. package com.czxy.changgou4.service;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.czxy.changgou4.pojo.SkuComment;
  4. import com.czxy.changgou4.vo.CommentResult;
  5. import com.czxy.changgou4.vo.PageRequest;
  6. /**
  7. * @author 桐叔
  8. * @email liangtong@itcast.cn
  9. */
  10. public interface SkuCommentService extends IService<SkuComment> {
  11. /**
  12. *
  13. * @param spuid
  14. * @param pageRequest
  15. * @return
  16. */
  17. public CommentResult findComments(Integer spuid, PageRequest pageRequest);
  18. }

步骤六:创建 SkuCommentServiceImpl实现类

  1. package com.czxy.changgou4.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.czxy.changgou4.mapper.ImpressionMapper;
  5. import com.czxy.changgou4.mapper.SkuCommentMapper;
  6. import com.czxy.changgou4.mapper.SkuMapper;
  7. import com.czxy.changgou4.pojo.Impression;
  8. import com.czxy.changgou4.pojo.SkuComment;
  9. import com.czxy.changgou4.service.SkuCommentService;
  10. import com.czxy.changgou4.vo.CommentResult;
  11. import com.czxy.changgou4.vo.PageRequest;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import javax.annotation.Resource;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * @author 桐叔
  21. * @email liangtong@itcast.cn
  22. */
  23. @Service
  24. @Transactional
  25. public class SkuCommentServiceImpl extends ServiceImpl<SkuCommentMapper, SkuComment> implements SkuCommentService {
  26. @Resource
  27. private ImpressionMapper impressionMapper;
  28. public CommentResult findComments(Integer spuid, PageRequest pageRequest){
  29. CommentResult commentResult = new CommentResult();
  30. //查询所有印象
  31. List<Impression> impressionList = impressionMapper.findImpressionsBySpuid(spuid);
  32. commentResult.setImpressions(impressionList);
  33. //好评度
  34. Integer goodCount = baseMapper.findCommentCountByRatio(spuid,0);// 好评
  35. Integer commonCount = baseMapper.findCommentCountByRatio(spuid,1);// 中评
  36. Integer badCount = baseMapper.findCommentCountByRatio(spuid,2);// 差评
  37. Integer totalCount = baseMapper.findTotalCommentBySpuid(spuid);//
  38. Map<String,Object> ratio = new HashMap<>();
  39. ratio.put("goods", String.format("%.2f" , goodCount * 100.0 / totalCount ));
  40. ratio.put("common",String.format("%.2f" , commonCount * 100.0 / totalCount ));
  41. ratio.put("bad",String.format("%.2f" , badCount * 100.0 / totalCount ));
  42. commentResult.setRatio( ratio );
  43. //总评论数
  44. Integer comment_count = baseMapper.findNumBySpuId(spuid);
  45. commentResult.setComment_count(comment_count);
  46. //查询所有
  47. int startIndex = (pageRequest.getCurrent() - 1) * pageRequest.getSize();
  48. List<SkuComment> comments = baseMapper.findCommentsBySpuid(spuid, startIndex ,pageRequest.getSize());
  49. commentResult.setComments(comments);
  50. return commentResult;
  51. }
  52. }

步骤七:创建 SkuCommentController,完成“查询指定spu的所有评论”

  1. package com.czxy.changgou4.controller;
  2. import com.czxy.changgou4.service.SkuCommentService;
  3. import com.czxy.changgou4.vo.BaseResult;
  4. import com.czxy.changgou4.vo.CommentResult;
  5. import com.czxy.changgou4.vo.PageRequest;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.annotation.Resource;
  12. /**
  13. * @author 桐叔
  14. * @email liangtong@itcast.cn
  15. */
  16. @RestController
  17. @RequestMapping("/comments")
  18. public class SkuCommentController {
  19. @Resource
  20. private SkuCommentService skuCommentService;
  21. @GetMapping("/spu/{spuid}")
  22. public BaseResult findCommentsByPage(@PathVariable("spuid") Integer spuid, PageRequest pageRequest){
  23. CommentResult comments = skuCommentService.findComments(spuid, pageRequest);
  24. return BaseResult.ok("查询成功", comments);
  25. }
  26. }

前端实现

步骤一:修改 apiclient.js,添加 getComments 函数,完成查询评论操作

94e45d687b34405daede23c1d3cc3d89.png

  1. //评论
  2. getComments : (spuid , size, current) => {
  3. return axios.get(`/web-service/comments/spu/${spuid}`,{
  4. params: {
  5. size: size,
  6. current: current
  7. }
  8. })
  9. },

步骤二:修改 Goods.vue ,编写查询评论函数,用于支持分页

512b63b327b94aef9f0bb72d94c0fe6e.png

  1. data() {
  2. return {
  3. commentVo: {
  4. size: 2,
  5. current: 1,
  6. },
  7. commentResult: {
  8. ratio: {}
  9. }
  10. }
  11. },
  12. async pageChanged (num) {
  13. this.commentVo.current = num
  14. // ajax 查询
  15. let { data } = await this.$request.getComments( this.goodsInfo.spuid, this.commentVo.current, this.commentVo.size)
  16. // 处理结果
  17. this.commentResult = data.data
  18. }

步骤三:修改 Goods.vue ,页面加载成功后,查询评论(第一页)

49348bb7f69141b3b43c7706cce42b7a.png

  1. //评论
  2. this.pageChanged(1)

步骤四:修改 Goods.vue ,展示“好评度”

9df8bd3c0e3c4de6aae45245989d2c58.png

d8dccbcaa78e4064bf674ba68cebe5a5.png

  1. <div class="rate fl">
  2. <strong><em>{
  3. { commentResult.ratio.goods}}</em>%</strong> <br />
  4. <span>好评度</span>
  5. </div>
  6. <div class="percent fl">
  7. <dl>
  8. <dt>好评({
  9. { commentResult.ratio.goods}}%)</dt>
  10. <dd><div :style="{'width': comments.ratio.goods + 'px'}"></div></dd>
  11. </dl>
  12. <dl>
  13. <dt>中评({
  14. { commentResult.ratio.common}}%)</dt>
  15. <dd><div :style="{'width':comments.ratio.common + 'px'}"></div></dd>
  16. </dl>
  17. <dl>
  18. <dt>差评({
  19. { commentResult.ratio.bad}}%)</dt>
  20. <dd><div :style="{'width': commentResult.ratio.bad + 'px'}" ></div></dd>
  21. </dl>
  22. </div>

步骤五:修改 Goods.vue ,展示“买家印象”

28899cd06dee44889c1ee2061f4aab77.png

  1. <dl>
  2. <dt>买家印象:</dt>
  3. <dd v-for="(ci,cii) in commentResult.impressions" :key="cii">
  4. <span>{
  5. {ci.title}}</span><em>({
  6. {ci.count}})</em>
  7. </dd>
  8. </dl>

步骤六:修改 Goods.vue ,展示“评论”

a10fef97408d495faee9c1263c404057.png

  1. <!-- 评论开始 -->
  2. <div v-for="(cc,cci) in commentResult.comments" :key="cci" class="comment_items mt10">
  3. <div class="user_pic">
  4. <dl>
  5. <dt><a href=""><img :src="cc.user.face" alt="" /></a></dt>
  6. <dd><a href="">{
  7. {cc.user.name}}</a></dd>
  8. </dl>
  9. </div>
  10. <div class="item">
  11. <div class="title">
  12. <span>{
  13. {cc.created_at}}</span>
  14. <strong class="star" :class="'star' + cc.star"></strong> <!-- star5表示5星级 start4表示4星级,以此类推 -->
  15. </div>
  16. <div class="comment_content">
  17. {
  18. {cc.content}}
  19. </div>
  20. <div class="btns">
  21. <a href="" class="reply">回复(0)</a>
  22. <a href="" class="useful">有用(0)</a>
  23. </div>
  24. </div>
  25. <div class="cornor"></div>
  26. </div>
  27. <!-- 评论结束 -->

步骤七:修改 Goods.vue ,展示“分页条”

a7a94b4ff83040b8857d0a2c823af467.png

  1. import Pagination from '../components/Pagination'

0f6e8cb17d714c19b0d6af9fcb8ade5f.png

  1. <!-- 分页信息 start -->
  2. <pagination :total="commentResult.comment_count"
  3. :page_size="commentVo.size"
  4. @page_changed="pageChanged" ></pagination>
  5. <!-- 分页信息 end -->

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读