改了一些bug

main
forely 4 days ago
parent 11d6d818ae
commit 59501feb4a

@ -120,6 +120,12 @@
<version>2.8.8</version>
</dependency>
<!-- es -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>

@ -1,84 +0,0 @@
package com.luojia_channel.modules.captcha.controller;
import com.luojia_channel.common.domain.Result;
import com.luojia_channel.modules.captcha.utils.CaptchaUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/user")
@Tag(name = "图形验证码", description = "图形验证码相关接口")
public class CaptchaController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
*
* @param request
* @param res
* @throws IOException
*/
@GetMapping("/captcha")
@Operation(
summary = "生成验证码图片"
)
public void generateCaptcha(HttpServletRequest request,
HttpServletResponse res) throws IOException {
CaptchaUtils captcha = new CaptchaUtils();
BufferedImage image = captcha.getImage();
String text = captcha.getText();
String captchaKey = UUID.randomUUID().toString();
redisTemplate.opsForValue().set("captcha:" + captchaKey, text, 60, TimeUnit.SECONDS);
Cookie cookie = new Cookie("captchaKey", captchaKey);
cookie.setPath("/");
res.addCookie(cookie);
CaptchaUtils.output(image,res.getOutputStream());
}
/**
*
* @param session
* @param params
* @return
*/
@PostMapping("/verify-captcha")
public Result verifyCaptcha(@RequestBody Map<String, String> params, @CookieValue(value = "captchaKey", required = false) String captchaKey, HttpSession session) {
String captcha = params.get("captcha");
if (captchaKey == null) {
return Result.fail(500, "验证码已失效,请重新获取");
}
String redisKey = "captcha:" + captchaKey;
String correctCaptcha = redisTemplate.opsForValue().get(redisKey);
if (correctCaptcha == null) {
return Result.fail(500, "验证码已过期,请重新获取");
}
if (captcha.equalsIgnoreCase(correctCaptcha)) {
redisTemplate.delete(redisKey);
return Result.success();
} else {
return Result.fail(500, "图形验证码错误");
}
}
}

@ -150,6 +150,7 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> impl
.build();
notificationProducer.sendMessage(notificationMessage);
}
redisUtil.delete("post:detail:" + comment.getPostId());
return comment.getId();
}

@ -104,7 +104,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
if(!updateById(post)){
throw new PostException("更新帖子失败");
}
// redisUtil.delete("post:detail:" + postSaveDTO.getId());
redisUtil.delete("post:detail:" + postSaveDTO.getId());
// redisUtil.delete("post:of:user:" + UserContext.getUserId());
}
@ -284,17 +284,18 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
//没点赞
if(!isLikedPost(id)){
//数据库点赞记录加一
boolean success = update().setSql("liked = liked + 1").eq("id",id).update();
boolean success = update().setSql("like_count = like_count + 1").eq("id",id).update();
if(success){
redisTemplate.opsForSet().add(likeBlogKey, userId, System.currentTimeMillis());
}
}else{
//数据库点赞记录减一
boolean success = update().setSql("liked = liked - 1").eq("id",id).update();
boolean success = update().setSql("like_count = like_count - 1").eq("id",id).update();
if(success){
redisTemplate.opsForSet().remove(likeBlogKey, userId);
}
}
redisUtil.delete("post:detail:" + id);
}
}

@ -0,0 +1,4 @@
package com.luojia_channel.modules.search.controller;
public class SearchController {
}

@ -0,0 +1,36 @@
package com.luojia_channel.modules.search.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.time.LocalDateTime;
@Data
@Document(indexName = "post_index")
public class EsPost {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title;
private String image;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
private Integer status;
private Integer likeCount;
private Integer commentCount;
private Integer favoriteCount;
private Integer viewCount;
private Long userId;
private Long categoryId;
private LocalDateTime createTime;
}

@ -0,0 +1,30 @@
package com.luojia_channel.modules.search.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "user_index")
public class EsUser {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String username;
@Field(type = FieldType.Keyword)
private String studentId;
private String avatar;
private Integer gender;
private Integer integral;
@Field(type = FieldType.Keyword)
private String college;
}

@ -0,0 +1,4 @@
package com.luojia_channel.modules.search.service;
public class SearchService {
}

@ -0,0 +1,4 @@
package com.luojia_channel.modules.search.task;
public class DataSyncTask {
}

@ -2,6 +2,8 @@ package com.luojia_channel.modules.user.controller;
import com.luojia_channel.common.domain.Result;
import com.luojia_channel.common.domain.UserDTO;
import com.luojia_channel.common.utils.RedisUtil;
import com.luojia_channel.modules.user.utils.CaptchaUtils;
import com.luojia_channel.modules.user.dto.UserLoginDTO;
import com.luojia_channel.modules.user.dto.UserRegisterDTO;
import com.luojia_channel.modules.user.service.UserLoginService;
@ -12,16 +14,26 @@ import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "用户登陆注册相关接口")
public class UserLoginController {
private final UserLoginService userLoginService;
private final RedisUtil redisUtil;
@PostMapping("/login")
@Operation(
summary = "用户登录",
@ -67,4 +79,59 @@ public class UserLoginController {
return Result.success("hello");
}
/**
*
* @param request
* @param res
* @throws IOException
*/
@GetMapping("/captcha")
@Operation(
summary = "生成验证码图片"
)
public void generateCaptcha(HttpServletRequest request,
HttpServletResponse res) throws IOException {
CaptchaUtils captcha = new CaptchaUtils();
BufferedImage image = captcha.getImage();
String text = captcha.getText();
String captchaKey = UUID.randomUUID().toString();
redisUtil.set("captcha:" + captchaKey, text, 60, TimeUnit.SECONDS);
Cookie cookie = new Cookie("captchaKey", captchaKey);
cookie.setPath("/");
res.addCookie(cookie);
CaptchaUtils.output(image,res.getOutputStream());
}
/**
*
* @param session
* @param params
* @return
*/
@PostMapping("/verify-captcha")
public Result verifyCaptcha(@RequestBody Map<String, String> params, @CookieValue(value = "captchaKey", required = false) String captchaKey, HttpSession session) {
String captcha = params.get("captcha");
if (captchaKey == null) {
return Result.fail(500, "验证码已失效,请重新获取");
}
String redisKey = "captcha:" + captchaKey;
String correctCaptcha = redisUtil.get(redisKey, String.class);
if (correctCaptcha == null) {
return Result.fail(500, "验证码已过期,请重新获取");
}
if (captcha.equalsIgnoreCase(correctCaptcha)) {
redisUtil.delete(redisKey);
return Result.success();
} else {
return Result.fail(500, "图形验证码错误");
}
}
}

Loading…
Cancel
Save