Compare commits
2 Commits
feature-cc
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d9ca28ec9 | |||
| 5624e598ec |
7
pom.xml
7
pom.xml
@@ -71,6 +71,13 @@
|
|||||||
<version>3.12.0</version>
|
<version>3.12.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- OkHttp(用于调用API) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>4.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- AOP起步依赖 -->
|
<!-- AOP起步依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bao.dating.common.ip2location;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "ip2location.api")
|
||||||
|
public class Ip2LocationConfig {
|
||||||
|
private String key;
|
||||||
|
private String url;
|
||||||
|
private int timeout;
|
||||||
|
}
|
||||||
@@ -30,7 +30,10 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
// 忽略的接口
|
// 忽略的接口
|
||||||
.excludePathPatterns(
|
.excludePathPatterns(
|
||||||
"/user/login",
|
"/user/login",
|
||||||
"/api/verification/send-email-code"
|
"/user/register",
|
||||||
|
"/user/emailLogin",
|
||||||
|
"/api/verification/send-email-code",
|
||||||
|
"/ip/location"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.bao.dating.controller;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.vo.IpLocationVO;
|
||||||
|
import com.bao.dating.service.Ip2LocationClientService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/ip")
|
||||||
|
public class IpLocationController {
|
||||||
|
@Autowired
|
||||||
|
private Ip2LocationClientService ip2LocationClientService;
|
||||||
|
/**
|
||||||
|
* 前端访问接口,获取IP地址位置信息
|
||||||
|
* @param ip 可选参数,要查询的IP地址
|
||||||
|
* @return IP位置信息(JSON格式)
|
||||||
|
*/
|
||||||
|
@GetMapping("/location")
|
||||||
|
public Result<?> getIpLocation(@RequestParam(required = false) String ip) {
|
||||||
|
if (ip.isEmpty()){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 调用工具类获取API响应
|
||||||
|
IpLocationVO ipLocationVo = ip2LocationClientService.getIpLocation(ip);
|
||||||
|
return Result.success(ResultCode.SUCCESS,ipLocationVo);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 异常处理:返回错误信息(实际项目建议封装统一响应格式)
|
||||||
|
return Result.error(ResultCode.SYSTEM_ERROR,e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,13 @@ package com.bao.dating.controller;
|
|||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.entity.User;
|
import com.bao.dating.pojo.entity.User;
|
||||||
import com.bao.dating.service.PostFavoriteService;
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
import com.bao.dating.service.PostService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -15,6 +16,13 @@ import java.util.Map;
|
|||||||
public class PostFavoriteController {
|
public class PostFavoriteController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PostFavoriteService postFavoriteService;
|
private PostFavoriteService postFavoriteService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 收藏
|
||||||
|
* @param postId 动态ID
|
||||||
|
* @param user 当前登录用户对象
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
@PostMapping("/{post_id}/favorites")
|
@PostMapping("/{post_id}/favorites")
|
||||||
public Result<Map<String,Long>> addPostFavorite(@PathVariable("post_id")Long postId, User user){
|
public Result<Map<String,Long>> addPostFavorite(@PathVariable("post_id")Long postId, User user){
|
||||||
if (user == null){
|
if (user == null){
|
||||||
@@ -23,6 +31,13 @@ public class PostFavoriteController {
|
|||||||
Long userId = user.getUserId();
|
Long userId = user.getUserId();
|
||||||
return postFavoriteService.postFavorite(userId,postId);
|
return postFavoriteService.postFavorite(userId,postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消收藏
|
||||||
|
* @param postId 动态id
|
||||||
|
* @param user 登录用户
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
@DeleteMapping("/{post_id}/favorites")
|
@DeleteMapping("/{post_id}/favorites")
|
||||||
public Result<?> deletePostFavorite(@PathVariable("post_id")Long postId, User user){
|
public Result<?> deletePostFavorite(@PathVariable("post_id")Long postId, User user){
|
||||||
if (user == null){
|
if (user == null){
|
||||||
@@ -31,4 +46,21 @@ public class PostFavoriteController {
|
|||||||
Long userId = user.getUserId();
|
Long userId = user.getUserId();
|
||||||
return postFavoriteService.deletePostFavorite(userId, postId);
|
return postFavoriteService.deletePostFavorite(userId, postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 展示所有收藏
|
||||||
|
* @param user 当前登录用户
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
@GetMapping("/showFavorites")
|
||||||
|
public Result<List<Post>> showPostFavorites(@RequestBody User user){
|
||||||
|
//校验参数
|
||||||
|
if (user == null){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
Long userId = user.getUserId();
|
||||||
|
List<Post> posts = postFavoriteService.selectAllFavorites(userId);
|
||||||
|
return Result.success(ResultCode.SUCCESS,posts);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户接口
|
* 用户接口
|
||||||
*
|
*
|
||||||
@@ -79,4 +80,42 @@ public class UserController {
|
|||||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||||
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户注册
|
||||||
|
* @param userName 用户名称
|
||||||
|
* @param userPassword 用户密码
|
||||||
|
* @return 返回
|
||||||
|
*/
|
||||||
|
@PostMapping("/register")
|
||||||
|
public Result<?> userRegister(@RequestParam String userName , @RequestParam String userPassword){
|
||||||
|
//校验参数是否为空
|
||||||
|
if (userName.isEmpty() || userPassword.isEmpty()){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
if (!userService.registerUser(userName,userPassword)){
|
||||||
|
return Result.error(ResultCode.FAIL);
|
||||||
|
}
|
||||||
|
return Result.success(ResultCode.SUCCESS,"用户注册成功",null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过邮箱登录
|
||||||
|
* @param email 邮箱
|
||||||
|
* @param code 验证码
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
|
@PostMapping("/emailLogin")
|
||||||
|
public Result<UserLoginVO> emailLogin(@RequestParam String email , @RequestParam String code){
|
||||||
|
//校验参数是否为空
|
||||||
|
if (email.isEmpty() || code.isEmpty()){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
UserLoginVO userLoginVO = userService.emailLogin(email, code);
|
||||||
|
if (userLoginVO == null){
|
||||||
|
return Result.error(ResultCode.FAIL,"请先注册用户或添加邮箱");
|
||||||
|
}
|
||||||
|
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.entity.PostFavorite;
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
@@ -10,6 +11,10 @@ import java.util.List;
|
|||||||
public interface PostFavoriteMapper {
|
public interface PostFavoriteMapper {
|
||||||
//查询当前已收藏所有用户
|
//查询当前已收藏所有用户
|
||||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||||
|
//添加收藏
|
||||||
int addPostFavorite(PostFavorite postFavorite);
|
int addPostFavorite(PostFavorite postFavorite);
|
||||||
|
//删除收藏
|
||||||
int deletePostFavorite(@Param("postId") Long postId);
|
int deletePostFavorite(@Param("postId") Long postId);
|
||||||
|
//查询用户所有收藏
|
||||||
|
List<Post> showAllFavorites(@Param("userid")Long userid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,5 +34,24 @@ public interface UserMapper {
|
|||||||
*/
|
*/
|
||||||
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加用户
|
||||||
|
* @param user 用户对象
|
||||||
|
* @return 受影响行数
|
||||||
|
*/
|
||||||
|
int saveUser(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询最大用户id
|
||||||
|
* @return 用户id
|
||||||
|
*/
|
||||||
|
Long selectMaxId();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据邮箱查询用户
|
||||||
|
* @param email 用户邮箱
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
|
User selectByUserEmailUser(@Param("userEmail") String email);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/main/java/com/bao/dating/pojo/vo/IpLocationVO.java
Normal file
30
src/main/java/com/bao/dating/pojo/vo/IpLocationVO.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.bao.dating.pojo.vo;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class IpLocationVO {
|
||||||
|
// 国家
|
||||||
|
@JsonProperty("country_name")
|
||||||
|
private String countryName;
|
||||||
|
// 省份/地区
|
||||||
|
@JsonProperty("region_name")
|
||||||
|
private String regionName;
|
||||||
|
// 城市
|
||||||
|
@JsonProperty("city_name")
|
||||||
|
private String cityName;
|
||||||
|
// 纬度
|
||||||
|
private String latitude;
|
||||||
|
// 经度
|
||||||
|
private String longitude;
|
||||||
|
// ISP运营商
|
||||||
|
@JsonProperty("isp")
|
||||||
|
private String isp;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.vo.IpLocationVO;
|
||||||
|
|
||||||
|
public interface Ip2LocationClientService {
|
||||||
|
IpLocationVO getIpLocation(String ip)throws Exception;
|
||||||
|
}
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface PostFavoriteService {
|
public interface PostFavoriteService {
|
||||||
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
||||||
Result<?> deletePostFavorite(Long userid,Long postId);
|
Result<?> deletePostFavorite(Long userid,Long postId);
|
||||||
|
List<Post> selectAllFavorites(Long userid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.bao.dating.service;
|
|||||||
|
|
||||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -45,4 +46,19 @@ public interface UserService {
|
|||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户注册
|
||||||
|
* @param userName 用户民称
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
|
Boolean registerUser(String userName,String userPassword);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮箱登录
|
||||||
|
* @param email 邮箱
|
||||||
|
* @param code 验证码
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
UserLoginVO emailLogin(String email , String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
|
import com.bao.dating.common.ip2location.Ip2LocationConfig;
|
||||||
|
import com.bao.dating.pojo.vo.IpLocationVO;
|
||||||
|
import com.bao.dating.service.Ip2LocationClientService;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class Ip2LocationClientServiceImpl implements Ip2LocationClientService {
|
||||||
|
@Autowired
|
||||||
|
private Ip2LocationConfig ip2LocationConfig;
|
||||||
|
|
||||||
|
// Jackson的ObjectMapper,用于JSON解析
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用API并只返回核心位置信息
|
||||||
|
* @param ip 要查询的IP地址
|
||||||
|
* @return 精简的位置信息实体类
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public IpLocationVO getIpLocation(String ip) throws Exception {
|
||||||
|
// 1. 构建请求URL
|
||||||
|
URIBuilder uriBuilder = new URIBuilder(ip2LocationConfig.getUrl());
|
||||||
|
uriBuilder.addParameter("key", ip2LocationConfig.getKey());
|
||||||
|
if (ip != null && !ip.trim().isEmpty()) {
|
||||||
|
uriBuilder.addParameter("ip", ip);
|
||||||
|
}
|
||||||
|
URI uri = uriBuilder.build();
|
||||||
|
|
||||||
|
// 2. 配置超时时间(逻辑和之前一致)
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(ip2LocationConfig.getTimeout())
|
||||||
|
.setSocketTimeout(ip2LocationConfig.getTimeout())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 3. 发送请求并解析响应
|
||||||
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
|
HttpGet httpGet = new HttpGet(uri);
|
||||||
|
httpGet.setConfig(requestConfig);
|
||||||
|
|
||||||
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||||||
|
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||||
|
// 核心:将完整JSON解析为只包含位置信息的VO类
|
||||||
|
return objectMapper.readValue(jsonStr, IpLocationVO.class);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("调用API失败,状态码:" + response.getStatusLine().getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import com.bao.dating.common.Result;
|
|||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
import com.bao.dating.mapper.PostFavoriteMapper;
|
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||||
import com.bao.dating.mapper.PostMapper;
|
import com.bao.dating.mapper.PostMapper;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.entity.PostFavorite;
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import com.bao.dating.service.PostFavoriteService;
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -58,4 +59,18 @@ public class PostFavoriteServiceImpl implements PostFavoriteService {
|
|||||||
postMapper.decreaseFavoriteCount(postId);
|
postMapper.decreaseFavoriteCount(postId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 展示所有收藏
|
||||||
|
* @param userid 用户id
|
||||||
|
* @return 查询到的结果
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Post> selectAllFavorites(Long userid) {
|
||||||
|
if (userid == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return postFavoriteMapper.showAllFavorites(userid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||||
@@ -13,6 +15,7 @@ import com.bao.dating.pojo.entity.User;
|
|||||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
|
import com.bao.dating.service.VerificationCodeService;
|
||||||
import com.bao.dating.util.FileUtil;
|
import com.bao.dating.util.FileUtil;
|
||||||
import com.bao.dating.util.JwtUtil;
|
import com.bao.dating.util.JwtUtil;
|
||||||
import com.bao.dating.util.MD5Util;
|
import com.bao.dating.util.MD5Util;
|
||||||
@@ -48,6 +51,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private VerificationCodeService verificationCodeService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
*
|
*
|
||||||
@@ -275,4 +281,59 @@ public class UserServiceImpl implements UserService {
|
|||||||
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
||||||
return userInfoVO;
|
return userInfoVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询用户
|
||||||
|
* @param userName 用户民称
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Boolean registerUser(String userName,String userPassword) {
|
||||||
|
//校验参数是否为空
|
||||||
|
if (userName.isEmpty() || userPassword.isEmpty()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//产看数据库是否存在已注册用户
|
||||||
|
User user = userMapper.getByUsername(userName);
|
||||||
|
if (user != null){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//将用户数据存入苏数据库
|
||||||
|
String salt = "lyy123";
|
||||||
|
String passwordHash = MD5Util.encryptWithSalt(userPassword, salt);
|
||||||
|
//查询最大用户id
|
||||||
|
Long maxId = userMapper.selectMaxId();
|
||||||
|
User saveUser = new User();
|
||||||
|
saveUser.setUserId(maxId+1);
|
||||||
|
saveUser.setUserName(userName);
|
||||||
|
saveUser.setPasswordHash(passwordHash);
|
||||||
|
saveUser.setSalt(salt);
|
||||||
|
saveUser.setUpdatedAt(LocalDateTime.now());
|
||||||
|
saveUser.setCreatedAt(LocalDateTime.now());
|
||||||
|
int count = userMapper.saveUser(saveUser);
|
||||||
|
return count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮箱登录
|
||||||
|
* @param email 邮箱
|
||||||
|
* @param code 验证码
|
||||||
|
* @return 脱敏用户信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public UserLoginVO emailLogin(String email, String code) {
|
||||||
|
User user = userMapper.selectByUserEmailUser(email);
|
||||||
|
if (user == null)
|
||||||
|
return null;
|
||||||
|
boolean flag = verificationCodeService.verifyEmailCode(email, code);
|
||||||
|
if (!flag)
|
||||||
|
return null;
|
||||||
|
// 生成token
|
||||||
|
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
|
||||||
|
UserLoginVO userLoginVO = new UserLoginVO();
|
||||||
|
userLoginVO.setUserId(user.getUserId());
|
||||||
|
userLoginVO.setNickname(user.getNickname());
|
||||||
|
userLoginVO.setToken(token);
|
||||||
|
return userLoginVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,3 +64,11 @@ aliyun:
|
|||||||
region-id: cn-hangzhou
|
region-id: cn-hangzhou
|
||||||
sign-name: 速通互联验证码
|
sign-name: 速通互联验证码
|
||||||
template-code: 100001
|
template-code: 100001
|
||||||
|
|
||||||
|
# ip2location.io 相关配置
|
||||||
|
ip2location:
|
||||||
|
api:
|
||||||
|
key: 95F4AB991174E296AFD5AD0EF927B2ED # ip2location.io API密钥
|
||||||
|
url: https://api.ip2location.io/
|
||||||
|
timeout: 5000 # 请求超时时间(毫秒)
|
||||||
|
|
||||||
|
|||||||
@@ -14,4 +14,8 @@
|
|||||||
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
||||||
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
||||||
</select>
|
</select>
|
||||||
|
<!-- 查询所有收藏动态 -->
|
||||||
|
<select id="showAllFavorites" resultType="com.bao.dating.pojo.entity.Post">
|
||||||
|
SELECT * FROM post WHERE post_id IN (SELECT post_id FROM post_favorite WHERE user_id = #{userid})
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -3,6 +3,10 @@
|
|||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||||
|
<!-- 向数据库中添加用户 -->
|
||||||
|
<insert id="saveUser">
|
||||||
|
insert into user(user_id,user_name,password_hash,salt,created_at,updated_at) values (#{userId},#{userName},#{passwordHash},#{salt},#{createdAt},#{updatedAt})
|
||||||
|
</insert>
|
||||||
|
|
||||||
<!--根据用户名查询用户-->
|
<!--根据用户名查询用户-->
|
||||||
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||||
@@ -45,6 +49,16 @@
|
|||||||
FROM user WHERE user_id = #{userId}
|
FROM user WHERE user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<!-- 查询最大用户id -->
|
||||||
|
<select id="selectMaxId" resultType="java.lang.Long">
|
||||||
|
SELECT MAX(user_id) FROM user
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- 根据邮箱查询用户信息 -->
|
||||||
|
<select id="selectByUserEmailUser" resultType="com.bao.dating.pojo.entity.User">
|
||||||
|
select * from user where user_email = #{userEmail}
|
||||||
|
</select>
|
||||||
|
|
||||||
<!--根据ID更新动态-->
|
<!--根据ID更新动态-->
|
||||||
<update id="updateUserInfoByUserId">
|
<update id="updateUserInfoByUserId">
|
||||||
UPDATE user
|
UPDATE user
|
||||||
|
|||||||
Reference in New Issue
Block a user