<feat>
This commit is contained in:
parent
d85521717c
commit
719035dcaf
6
pom.xml
6
pom.xml
@ -29,7 +29,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
@ -66,6 +66,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
@ -103,7 +105,7 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.30</version>
|
<version>1.18.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
8
src/main/java/com/example/demo/common/StrUtil.java
Normal file
8
src/main/java/com/example/demo/common/StrUtil.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.demo.common;
|
||||||
|
|
||||||
|
public class StrUtil {
|
||||||
|
public static boolean isEmpty(String str) {
|
||||||
|
return str == null || str.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,45 +1,64 @@
|
|||||||
package com.example.demo.controller;
|
package com.example.demo.controller;
|
||||||
|
|
||||||
import com.example.demo.common.ResponseBean;
|
import com.example.demo.common.ResponseBean;
|
||||||
|
import com.example.demo.common.StrUtil;
|
||||||
|
import com.example.demo.dto.request.AddArticleRequest;
|
||||||
import com.example.demo.mapper.ArticleMapper;
|
import com.example.demo.mapper.ArticleMapper;
|
||||||
import com.example.demo.model.Article;
|
import com.example.demo.model.Article;
|
||||||
|
import com.example.demo.service.Cookie;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/auth")
|
@RequestMapping("/api/article")
|
||||||
public class ArticleController {
|
public class ArticleController {
|
||||||
@Resource
|
@Resource
|
||||||
private ArticleMapper articleMapper;
|
private ArticleMapper articleMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/add")
|
||||||
@PostMapping
|
public ResponseBean<Object> addArticle(@RequestBody AddArticleRequest addArticleRequest) {
|
||||||
public ResponseBean<Object> addUser(@RequestBody Article article) {
|
Article article = ArticleMapper.get(addArticleRequest.getTitle(), addArticleRequest.getContent(), addArticleRequest.getAuthor());
|
||||||
int insert = articleMapper.insert(article);
|
return ResponseBean.success();
|
||||||
return ResponseBean.success(insert);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping("/get")
|
||||||
public Article get(@RequestParam Integer id) {
|
public Article getArticle(@RequestParam Integer id) {
|
||||||
return articleMapper.selectById(id);
|
return articleMapper.selectById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@DeleteMapping
|
@GetMapping("/deletearticle")
|
||||||
public ResponseBean<Object> delete(@RequestParam Integer id) {
|
public ResponseBean<Object> deleteArticle(@RequestParam Integer id) {
|
||||||
int insert = articleMapper.deleteById(id);
|
int insert = articleMapper.deleteById(id);
|
||||||
return ResponseBean.success(insert);
|
return ResponseBean.success(insert);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PostMapping("/updatearticle")
|
||||||
public ResponseBean<Object> updateUser(@RequestBody Article user) {
|
public ResponseBean<Object> updateArticle(@RequestBody Article article) {
|
||||||
|
|
||||||
int update = articleMapper.updateById(user);
|
int update = articleMapper.updateById(article);
|
||||||
return ResponseBean.success(update);
|
return ResponseBean.success(update);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/readcount")
|
||||||
|
private ResponseBean<Object> ReadCount(@RequestBody Integer id, HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
Article byId = articleMapper.selectById(id);
|
||||||
|
// 如果cookie为空,说明第一次访问,访问量可以增加
|
||||||
|
if (StrUtil.isEmpty(Cookie.getCookieValue(request, Article.VIEW_COOKIES + id))) {
|
||||||
|
Integer views = byId.getViews();
|
||||||
|
views = views + 1;
|
||||||
|
byId.setViews(views);
|
||||||
|
articleMapper.updateById(byId);
|
||||||
|
Cookie.setCookie(request, response, Article.VIEW_COOKIES + id, "viewd", 60 * 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseBean.success(byId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
package com.example.demo.controller;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api/auth")
|
|
||||||
public class HelloController {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/auth")
|
@RequestMapping("/api/like")
|
||||||
public class LikeController {
|
public class LikeController {
|
||||||
private final LikeService likeService;
|
private final LikeService likeService;
|
||||||
|
|
||||||
@ -15,22 +15,22 @@ public class LikeController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{targetId}")
|
@PostMapping("/{targetId}")
|
||||||
public void like(@PathVariable("targetId") String targetId, @RequestHeader("userId") String userId) {
|
public void like(@PathVariable("targetId") Integer targetId, @RequestHeader("userId") Integer userId) {
|
||||||
likeService.like(targetId, userId);
|
likeService.like(targetId, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{targetId}")
|
@DeleteMapping("/{targetId}")
|
||||||
public void unlike(@PathVariable("targetId") String targetId, @RequestHeader("userId") String userId) {
|
public void unlike(@PathVariable("targetId") Integer targetId, @RequestHeader("userId") Integer userId) {
|
||||||
likeService.unlike(targetId, userId);
|
likeService.unlike(targetId, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{targetId}/hasLiked")
|
@GetMapping("/{targetId}/hasLiked")
|
||||||
public boolean hasLiked(@PathVariable("targetId") String targetId, @RequestHeader("userId") String userId) {
|
public boolean hasLiked(@PathVariable("targetId") Integer targetId, @RequestHeader("userId") Integer userId) {
|
||||||
return likeService.hasLiked(targetId, userId);
|
return likeService.hasLiked(targetId, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{targetId}/count")
|
@GetMapping("/{targetId}/count")
|
||||||
public long getLikeCount(@PathVariable("targetId") String targetId) {
|
public long getLikeCount(@PathVariable("targetId") Integer targetId) {
|
||||||
return likeService.getLikeCount(targetId);
|
return likeService.getLikeCount(targetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
package com.example.demo.controller;
|
package com.example.demo.controller;
|
||||||
|
|
||||||
import com.example.demo.common.ResponseBean;
|
import com.example.demo.common.ResponseBean;
|
||||||
import com.example.demo.mapper.ArticleMapper;
|
import com.example.demo.dto.request.AddArticleRequest;
|
||||||
|
import com.example.demo.dto.request.AddNewsRequest;
|
||||||
import com.example.demo.mapper.NewsMapper;
|
import com.example.demo.mapper.NewsMapper;
|
||||||
import com.example.demo.model.Article;
|
|
||||||
import com.example.demo.model.News;
|
import com.example.demo.model.News;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
//@RequestMapping("/api/auth")
|
@RequestMapping("/api/news")
|
||||||
|
|
||||||
public class NewsController {
|
public class NewsController {
|
||||||
@Resource
|
@Resource
|
||||||
@ -17,29 +17,30 @@ public class NewsController {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping("/addnews")
|
||||||
public ResponseBean<Object> addUser(@RequestBody News news) {
|
public ResponseBean<Object> addNews(@RequestBody AddNewsRequest addNewsRequest) {
|
||||||
int insert = newsMapper.insert(news);
|
News news = NewsMapper.get(addNewsRequest.getTitle(),addNewsRequest.getContent(),addNewsRequest.getAuthor());
|
||||||
return ResponseBean.success(insert);
|
return ResponseBean.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping("/getnews")
|
||||||
public News get(@RequestParam String title) {
|
public News getNews(@RequestParam String title) {
|
||||||
return (News) newsMapper.selectById(title);
|
return newsMapper.selectById(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@DeleteMapping
|
@GetMapping("/deletenews")
|
||||||
public ResponseBean<Object> delete(@RequestParam String title) {
|
public ResponseBean<Object> deleteNews(@RequestParam String title) {
|
||||||
int insert = newsMapper.deleteById(title);
|
int insert = newsMapper.deleteById(title);
|
||||||
return ResponseBean.success(insert);
|
return ResponseBean.success(insert);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PostMapping("/updatenews")
|
||||||
public ResponseBean<Object> updateUser(@RequestBody News news) {
|
public ResponseBean<Object> updateNews(@RequestBody News news) {
|
||||||
|
|
||||||
int update = newsMapper.updateById(news);
|
int update = newsMapper.updateById(news);
|
||||||
return ResponseBean.success(update);
|
return ResponseBean.success(update);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.example.demo.dto.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AddArticleRequest {
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
private String author;
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.example.demo.dto.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AddNewsRequest {
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
private String author;
|
||||||
|
}
|
@ -6,5 +6,9 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface ArticleMapper extends BaseMapper<Article> {
|
public interface ArticleMapper extends BaseMapper<Article> {
|
||||||
|
static Article get(String title,String content,String author) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package com.example.demo.mapper;
|
package com.example.demo.mapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.example.demo.model.Article;
|
import com.example.demo.model.News;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface NewsMapper extends BaseMapper {
|
public interface NewsMapper extends BaseMapper<News> {
|
||||||
|
static News get(String title, String content, String author) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,18 +4,27 @@ import com.baomidou.mybatisplus.annotation.IdType;
|
|||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@TableName("article")
|
@TableName("article")
|
||||||
@Setter
|
@Data
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Article {
|
public class Article {
|
||||||
|
public static final String VIEW_COOKIES = "ZWW_ARTICLE_VIEWS_";
|
||||||
@TableId(type = IdType.AUTO)
|
@TableId(type = IdType.AUTO)
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private String title;
|
private String title;
|
||||||
private String content;
|
private String content;
|
||||||
private String author;
|
private String author;
|
||||||
|
private Integer views;
|
||||||
|
private Integer likes;
|
||||||
|
private String created_at;
|
||||||
|
|
||||||
|
private String updated_at;
|
||||||
|
|
||||||
|
// created_at updated_at
|
||||||
|
// null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,4 +14,9 @@ public class News {
|
|||||||
private String content;
|
private String content;
|
||||||
private String author;
|
private String author;
|
||||||
private String date;
|
private String date;
|
||||||
|
private Integer views;
|
||||||
|
private Integer likes;
|
||||||
|
private String created_at;
|
||||||
|
|
||||||
|
private String updated_at;
|
||||||
}
|
}
|
||||||
|
211
src/main/java/com/example/demo/service/Cookie.java
Normal file
211
src/main/java/com/example/demo/service/Cookie.java
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
package com.example.demo.service;
|
||||||
|
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
public class Cookie {
|
||||||
|
final static Logger logger = LoggerFactory.getLogger(Cookie.class);
|
||||||
|
|
||||||
|
|
||||||
|
public static String getCookieValue(HttpServletRequest request, String cookieName) {
|
||||||
|
return getCookieValue(request, cookieName, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
|
||||||
|
jakarta.servlet.http.Cookie[] cookieList = request.getCookies();
|
||||||
|
if (cookieList == null || cookieName == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String retValue = null;
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < cookieList.length; i++) {
|
||||||
|
if (cookieList[i].getName().equals(cookieName)) {
|
||||||
|
if (isDecoder) {
|
||||||
|
retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
|
||||||
|
} else {
|
||||||
|
retValue = cookieList[i].getValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return retValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
|
||||||
|
jakarta.servlet.http.Cookie[] cookieList = request.getCookies();
|
||||||
|
if (cookieList == null || cookieName == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String retValue = null;
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < cookieList.length; i++) {
|
||||||
|
if (cookieList[i].getName().equals(cookieName)) {
|
||||||
|
retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return retValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
|
||||||
|
String cookieValue) {
|
||||||
|
setCookie(request, response, cookieName, cookieValue, 60*60);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
|
||||||
|
String cookieValue, int cookieMaxage) {
|
||||||
|
setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
|
||||||
|
String cookieValue, boolean isEncode) {
|
||||||
|
setCookie(request, response, cookieName, cookieValue, -1, isEncode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
|
||||||
|
String cookieValue, int cookieMaxage, boolean isEncode) {
|
||||||
|
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
|
||||||
|
String cookieValue, int cookieMaxage, String encodeString) {
|
||||||
|
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
String cookieName) {
|
||||||
|
doSetCookie(request, response, cookieName, null, -1, false);
|
||||||
|
// doSetCookie(request, response, cookieName, "", -1, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
|
||||||
|
try {
|
||||||
|
if (cookieValue == null) {
|
||||||
|
cookieValue = "";
|
||||||
|
} else if (isEncode) {
|
||||||
|
cookieValue = URLEncoder.encode(cookieValue, "utf-8");
|
||||||
|
}
|
||||||
|
jakarta.servlet.http.Cookie cookie = new jakarta.servlet.http.Cookie(cookieName, cookieValue);
|
||||||
|
if (cookieMaxage > 0)
|
||||||
|
cookie.setMaxAge(cookieMaxage);
|
||||||
|
if (null != request) {// 设置域名的cookie
|
||||||
|
String domainName = getDomainName(request);
|
||||||
|
logger.info("========== domainName: {} ==========", domainName);
|
||||||
|
if (!"localhost".equals(domainName)) {
|
||||||
|
cookie.setDomain(domainName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cookie.setPath("/");
|
||||||
|
response.addCookie(cookie);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
String cookieName, String cookieValue, int cookieMaxage, String encodeString) {
|
||||||
|
try {
|
||||||
|
if (cookieValue == null) {
|
||||||
|
cookieValue = "";
|
||||||
|
} else {
|
||||||
|
cookieValue = URLEncoder.encode(cookieValue, encodeString);
|
||||||
|
}
|
||||||
|
jakarta.servlet.http.Cookie cookie = new jakarta.servlet.http.Cookie(cookieName, cookieValue);
|
||||||
|
if (cookieMaxage > 0)
|
||||||
|
cookie.setMaxAge(cookieMaxage);
|
||||||
|
if (null != request) {// 设置域名的cookie
|
||||||
|
String domainName = getDomainName(request);
|
||||||
|
logger.info("========== domainName: {} ==========", domainName);
|
||||||
|
if (!"localhost".equals(domainName)) {
|
||||||
|
cookie.setDomain(domainName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cookie.setPath("/");
|
||||||
|
response.addCookie(cookie);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final String getDomainName(HttpServletRequest request) {
|
||||||
|
String domainName = null;
|
||||||
|
|
||||||
|
String serverName = request.getRequestURL().toString();
|
||||||
|
if (serverName == null || serverName.equals("")) {
|
||||||
|
domainName = "";
|
||||||
|
} else {
|
||||||
|
serverName = serverName.toLowerCase();
|
||||||
|
serverName = serverName.substring(7);
|
||||||
|
final int end = serverName.indexOf("/");
|
||||||
|
serverName = serverName.substring(0, end);
|
||||||
|
if (serverName.indexOf(":") > 0) {
|
||||||
|
String[] ary = serverName.split("\\:");
|
||||||
|
serverName = ary[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
final String[] domains = serverName.split("\\.");
|
||||||
|
int len = domains.length;
|
||||||
|
if (len > 3 && !isIp(serverName)) {
|
||||||
|
// www.xxx.com.cn
|
||||||
|
domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
|
||||||
|
} else if (len <= 3 && len > 1) {
|
||||||
|
// xxx.com or xxx.cn
|
||||||
|
domainName = "." + domains[len - 2] + "." + domains[len - 1];
|
||||||
|
} else {
|
||||||
|
domainName = serverName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return domainName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String trimSpaces(String IP){//去掉IP字符串前后所有的空格
|
||||||
|
while(IP.startsWith(" ")){
|
||||||
|
IP= IP.substring(1,IP.length()).trim();
|
||||||
|
}
|
||||||
|
while(IP.endsWith(" ")){
|
||||||
|
IP= IP.substring(0,IP.length()-1).trim();
|
||||||
|
}
|
||||||
|
return IP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isIp(String IP){//判断是否是一个IP
|
||||||
|
boolean b = false;
|
||||||
|
IP = trimSpaces(IP);
|
||||||
|
if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){
|
||||||
|
String s[] = IP.split("\\.");
|
||||||
|
if(Integer.parseInt(s[0])<255)
|
||||||
|
if(Integer.parseInt(s[1])<255)
|
||||||
|
if(Integer.parseInt(s[2])<255)
|
||||||
|
if(Integer.parseInt(s[3])<255)
|
||||||
|
b = true;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,8 +7,6 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Data
|
|
||||||
|
|
||||||
public class LikeService {
|
public class LikeService {
|
||||||
private final RedisTemplate<String, Object> redisTemplate;
|
private final RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@ -17,20 +15,20 @@ public class LikeService {
|
|||||||
this.redisTemplate = redisTemplate;
|
this.redisTemplate = redisTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void like(String targetId, String userId) {
|
public void like(Integer targetId, Integer userId) {
|
||||||
// 使用集合记录点赞用户,防止重复点赞
|
// 使用集合记录点赞用户,防止重复点赞
|
||||||
redisTemplate.opsForSet().add(targetId + ":likes", userId);
|
redisTemplate.opsForSet().add(targetId + ":likes", userId);
|
||||||
// 点赞数自增,使用字符串存储点赞数,方便后续展示
|
// 点赞数自增,使用字符串存储点赞数,方便后续展示
|
||||||
redisTemplate.opsForValue().increment(targetId + ":likeCount", 1);
|
redisTemplate.opsForValue().increment(targetId + ":likeCount", 1);
|
||||||
}
|
}
|
||||||
public void unlike(String targetId, String userId) {
|
public void unlike(Integer targetId, Integer userId) {
|
||||||
redisTemplate.opsForSet().remove(targetId + ":likes", userId);
|
redisTemplate.opsForSet().remove(targetId + ":likes", userId);
|
||||||
redisTemplate.opsForValue().decrement(targetId + ":likeCount", 1);
|
redisTemplate.opsForValue().decrement(targetId + ":likeCount", 1);
|
||||||
}
|
}
|
||||||
public boolean hasLiked(String targetId, String userId) {
|
public boolean hasLiked(Integer targetId, Integer userId) {
|
||||||
return redisTemplate.opsForSet().isMember(targetId + ":likes", userId);
|
return redisTemplate.opsForSet().isMember(targetId + ":likes", userId);
|
||||||
}
|
}
|
||||||
public long getLikeCount(String targetId) {
|
public long getLikeCount(Integer targetId) {
|
||||||
Long count = (Long) redisTemplate.opsForValue().get(targetId + ":likeCount");
|
Long count = (Long) redisTemplate.opsForValue().get(targetId + ":likeCount");
|
||||||
return count == null? 0 : count;
|
return count == null? 0 : count;
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,8 @@ public class UserService extends ServiceImpl<UserMapper, User >{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkLogin() {
|
public boolean checkLogin() {
|
||||||
return true;}
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public Object getLoginUser() {
|
public Object getLoginUser() {
|
||||||
return null;
|
return null;
|
||||||
|
@ -5,7 +5,7 @@ spring:
|
|||||||
datasource:
|
datasource:
|
||||||
url: jdbc:mysql://localhost:3306/dj-weapp?useUnicode=true&characterEncoding=utf8
|
url: jdbc:mysql://localhost:3306/dj-weapp?useUnicode=true&characterEncoding=utf8
|
||||||
username: root
|
username: root
|
||||||
password: '@liu061102'
|
password: ''
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
type: com.alibaba.druid.pool.DruidDataSource
|
type: com.alibaba.druid.pool.DruidDataSource
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user