parent
7d2c60b567
commit
1be12ec0bf
@ -0,0 +1,89 @@
|
||||
package com.example.flower.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.flower.entity.Comment;
|
||||
import com.example.flower.service.CommentService;
|
||||
import com.example.flower.unit.JWTUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/comment")
|
||||
public class CommentController {
|
||||
@Resource
|
||||
private CommentService commentService;
|
||||
|
||||
@PostMapping("/list") //commentList获取所有评论列表
|
||||
public JSONObject commentList(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
int page=param.getIntValue("page");
|
||||
int page_size=param.getIntValue("page_size");
|
||||
int flower_id=param.getIntValue("flower_id");
|
||||
|
||||
List<Comment> commentList = commentService.commentList(page,page_size,flower_id).getList();
|
||||
|
||||
jsonObject1.put("page_number",commentService.commentList(page,page_size,flower_id).getPages());
|
||||
jsonObject1.put("total",commentService.commentList(page,page_size,flower_id).getTotal());
|
||||
|
||||
int size=commentList.size();
|
||||
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++){
|
||||
objects[i]=new JSONObject();
|
||||
objects[i].put("comment_id", commentList.get(i).getComment_id());
|
||||
objects[i].put("flower_id", commentList.get(i).getFlower_id());
|
||||
objects[i].put("user_id", commentList.get(i).getUser_id());
|
||||
objects[i].put("comment_content", commentList.get(i).getComment_content());
|
||||
objects[i].put("comment_time", commentList.get(i).getComment_time());
|
||||
}
|
||||
jsonObject1.put("comments",objects);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","token认证成功!" );
|
||||
jsonObject.put("data",jsonObject1);
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/add") //commentAdd添加新评论
|
||||
public JSONObject commentAdd(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
Claims claims = JWTUtil.getBodyByToken(Authorization);
|
||||
int user_id = (int) claims.get("id");
|
||||
|
||||
Comment comment = new Comment();
|
||||
comment.setUser_id(user_id);
|
||||
comment.setFlower_id(param.getIntValue("flower_id"));
|
||||
comment.setComment_content(param.getString("comment_content"));
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 格式化
|
||||
Date date = new Date();
|
||||
String time = sdf.format(date);
|
||||
comment.setComment_time(time);
|
||||
|
||||
commentService.commentAdd(comment);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","成功" );
|
||||
jsonObject.put("comment_id",comment.getComment_id());
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue