初始化
commit
68db59ca1d
|
@ -0,0 +1,35 @@
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>yp_yuekao812</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-auth</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 项目公共 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Web-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 引入 rabbitMQ的依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.bwie.auth;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:SuthApplication
|
||||||
|
* @Date:2024/8/12 10:10
|
||||||
|
*/
|
||||||
|
|
||||||
|
@EnableFeignClients
|
||||||
|
@SpringBootApplication
|
||||||
|
public class AuthApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(AuthApplication.class,args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.bwie.auth.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前bean初始化的时候执行
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
this.rabbitTemplate.setConfirmCallback(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 确认方法
|
||||||
|
* @param correlationData correlation data for the callback.
|
||||||
|
* @param ack true for ack, false for nack
|
||||||
|
* @param cause An optional cause, for nack, when available, otherwise null.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||||
|
if (ack) {
|
||||||
|
System.out.println("消息发送到 broker 成功");
|
||||||
|
} else {
|
||||||
|
System.out.println("消息发送到 broker 失败,失败的原因:" + cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.bwie.auth.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 RabbitAdmin
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class RabbitAdminConfig {
|
||||||
|
|
||||||
|
@Value("${spring.rabbitmq.host}")
|
||||||
|
private String host;
|
||||||
|
@Value("${spring.rabbitmq.username}")
|
||||||
|
private String username;
|
||||||
|
@Value("${spring.rabbitmq.password}")
|
||||||
|
private String password;
|
||||||
|
@Value("${spring.rabbitmq.virtual-host}")
|
||||||
|
private String virtualhost;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConnectionFactory connectionFactory() {
|
||||||
|
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
|
||||||
|
connectionFactory.setAddresses(host);
|
||||||
|
connectionFactory.setUsername(username);
|
||||||
|
connectionFactory.setPassword(password);
|
||||||
|
connectionFactory.setVirtualHost(virtualhost);
|
||||||
|
// 配置发送确认回调时,次配置必须配置,否则即使在RabbitTemplate配置了ConfirmCallback也不会生效
|
||||||
|
connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
|
||||||
|
connectionFactory.setPublisherReturns(true);
|
||||||
|
return connectionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rabbitAdmin
|
||||||
|
* @param connectionFactory
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
|
||||||
|
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
|
||||||
|
rabbitAdmin.setAutoStartup(true);
|
||||||
|
return rabbitAdmin;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.bwie.auth.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||||
|
import org.springframework.amqp.support.converter.MessageConverter;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RabbitmqConfig {
|
||||||
|
// 消息转换配置
|
||||||
|
@Bean
|
||||||
|
public MessageConverter jsonMessageConverter(){
|
||||||
|
return new Jackson2JsonMessageConverter();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.bwie.auth.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.ReturnedMessage;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ReturnsCallbackConfig implements RabbitTemplate.ReturnsCallback {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前bean初始化的时候执行
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
this.rabbitTemplate.setReturnsCallback(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息发送达到 queue 失败执行
|
||||||
|
*
|
||||||
|
* @param returnedMessage the returned message and metadata.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||||
|
System.out.println("消息" + returnedMessage.getMessage().toString() +
|
||||||
|
"被交换机" + returnedMessage.getExchange() + "回退!"
|
||||||
|
+ "退回原因为:" + returnedMessage.getReplyText());
|
||||||
|
// TODO 回退了所有的信息,可做补偿机制
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.bwie.auth.consumer;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.amqp.core.Message;
|
||||||
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth.consumer
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:messageConsumer
|
||||||
|
* @Date:2024/8/12 10:36
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class MessageConsumer {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
private static final String INDEX_SET = "INDEX_SET";
|
||||||
|
|
||||||
|
@RabbitListener(queuesToDeclare = {@Queue("http_yp_lv")})
|
||||||
|
public void messageConsumer(String userPhone, Message message, Channel channel){
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 保证消息的可靠性(2分)
|
||||||
|
String messageId = message.getMessageProperties().getMessageId();
|
||||||
|
Long add = redisTemplate.opsForSet().add(INDEX_SET, messageId);
|
||||||
|
if (add > 0){
|
||||||
|
|
||||||
|
log.info("短信开始发送....,手机号为:{}",userPhone);
|
||||||
|
log.info("短信内容为: 登录成功");
|
||||||
|
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
|
||||||
|
log.info("短信发送完成....");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
log.info("短信发送报错.... , 原因为: {}",ex.getMessage());
|
||||||
|
}
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.bwie.auth.controller;
|
||||||
|
|
||||||
|
import com.bwie.auth.service.AuthService;
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth.controller
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:AuthController
|
||||||
|
* @Date:2024/8/12 9:53
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("auth")
|
||||||
|
public class AuthController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthService authService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
*/
|
||||||
|
@PostMapping("login")
|
||||||
|
public Result<User> login(@RequestBody User user){
|
||||||
|
log.info("方法名称: 登录,URI :{}, 请求方式 :{},参数:{}",request.getRequestURI(),request.getMethod(),user);
|
||||||
|
Result<User> login = authService.login(user);
|
||||||
|
log.info("方法名称: 登录,URI :{}, 请求方式 :{},响应结果:{}",request.getRequestURI(),request.getMethod(),login);
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录认证
|
||||||
|
*/
|
||||||
|
@GetMapping("info")
|
||||||
|
public Result info(){
|
||||||
|
log.info("方法名称: 登录认证,URI :{}, 请求方式 :{}}",request.getRequestURI(),request.getMethod());
|
||||||
|
Result info = authService.info();
|
||||||
|
log.info("方法名称: 登录认证,URI :{}, 请求方式 :{},响应结果:{}",request.getRequestURI(),request.getMethod(), info);
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 退出登录
|
||||||
|
*/
|
||||||
|
@PostMapping("logout")
|
||||||
|
public Result logout(){
|
||||||
|
log.info("方法名称: 退出登录,URI :{}, 请求方式 :{}",request.getRequestURI(),request.getMethod());
|
||||||
|
authService.logout();
|
||||||
|
log.info("方法名称: 退出登录,URI :{}, 请求方式 :{}",request.getRequestURI(),request.getMethod());
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
@PostMapping("addUser")
|
||||||
|
public Result addUser(@RequestBody User user){
|
||||||
|
log.info("方法名称: 注册,URI :{}, 请求方式 :{},参数:{}",request.getRequestURI(),request.getMethod(),user);
|
||||||
|
Result addUser = authService.addUser(user);
|
||||||
|
log.info("方法名称: 注册,URI :{}, 请求方式 :{},响应结果:{}",request.getRequestURI(),request.getMethod(),addUser);
|
||||||
|
return addUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.bwie.auth.feign;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth.feign
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserFeign
|
||||||
|
* @Date:2024/8/12 9:52
|
||||||
|
*/
|
||||||
|
@FeignClient(value = "bwie-user")
|
||||||
|
public interface UserFeign {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证
|
||||||
|
*/
|
||||||
|
@PostMapping("user/login")
|
||||||
|
public Result<User> login(@RequestBody User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证 用户手机号
|
||||||
|
*/
|
||||||
|
@PostMapping("user/listUserPhone")
|
||||||
|
public Result<User> listUserPhone(@RequestBody User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
@PostMapping("user/addUser")
|
||||||
|
public Result<Integer> addUser(@RequestBody User user);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.bwie.auth.feign.factory;
|
||||||
|
|
||||||
|
import com.bwie.auth.feign.UserFeign;
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth.feign.factory
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserFeignFactory
|
||||||
|
* @Date:2024/8/12 10:45
|
||||||
|
*/
|
||||||
|
public class UserFeignFactory implements FallbackFactory<UserFeign> {
|
||||||
|
@Override
|
||||||
|
public UserFeign create(Throwable cause) {
|
||||||
|
return new UserFeign() {
|
||||||
|
@Override
|
||||||
|
public Result<User> login(User user) {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<User> listUserPhone(User user) {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<Integer> addUser(User user) {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.bwie.auth.service;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth.service
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:AuthService
|
||||||
|
* @Date:2024/8/12 9:53
|
||||||
|
*/
|
||||||
|
public interface AuthService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
*/
|
||||||
|
Result<User> login(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录认证
|
||||||
|
*/
|
||||||
|
Result info();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 退出登录
|
||||||
|
*/
|
||||||
|
void logout();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
Result addUser(User user);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,136 @@
|
||||||
|
package com.bwie.auth.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.bwie.auth.feign.UserFeign;
|
||||||
|
import com.bwie.auth.service.AuthService;
|
||||||
|
import com.bwie.common.constants.JwtConstants;
|
||||||
|
import com.bwie.common.constants.TokenConstants;
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.common.utils.JwtUtils;
|
||||||
|
import com.bwie.common.utils.StringUtils;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.auth.service.impl
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:AuthServiceImpl
|
||||||
|
* @Date:2024/8/12 9:53
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class AuthServiceImpl implements AuthService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserFeign userFeign;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result<User> login(User user) {
|
||||||
|
if (StringUtils.isAllEmpty(user.getUserName(),user.getUserPwd())){
|
||||||
|
return Result.error("用户名称或密码不能为空");
|
||||||
|
}
|
||||||
|
Result<User> login = userFeign.login(user);
|
||||||
|
User loginData = login.getData();
|
||||||
|
if (loginData == null){
|
||||||
|
return Result.error("该用户名不存在,请重新输入或者注册");
|
||||||
|
}
|
||||||
|
if (!user.getUserPwd().equals(loginData.getUserPwd())){
|
||||||
|
return Result.error("密码错误");
|
||||||
|
}
|
||||||
|
String userKey = UUID.randomUUID().toString().replace("-", "");
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put(JwtConstants.USER_KEY,userKey);
|
||||||
|
String token = JwtUtils.createToken(map);
|
||||||
|
loginData.setToken(token);
|
||||||
|
redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSONObject.toJSONString(loginData),
|
||||||
|
TokenConstants.EXPIRATION, TimeUnit.MINUTES);
|
||||||
|
// 用户每次登陆系统需要给用户发送短息(3分)
|
||||||
|
// 用户短信统一先放入MQ当中(2分)
|
||||||
|
rabbitTemplate.convertAndSend("http_yp_lv",loginData.getUserPhone(),message -> {
|
||||||
|
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||||
|
return message;
|
||||||
|
});
|
||||||
|
return Result.success(loginData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录认证
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result info() {
|
||||||
|
String token = request.getHeader(TokenConstants.TOKEN);
|
||||||
|
String userKey = JwtUtils.getUserKey(token);
|
||||||
|
String user = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||||
|
return Result.success(JSONObject.parseObject(user,User.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 退出登录
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void logout() {
|
||||||
|
String token = request.getHeader(TokenConstants.TOKEN);
|
||||||
|
String userKey = JwtUtils.getUserKey(token);
|
||||||
|
redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result addUser(User user) {
|
||||||
|
|
||||||
|
if (StringUtils.isAllEmpty(user.getUserPhone())){
|
||||||
|
return Result.error("用户手机号不能为空");
|
||||||
|
}
|
||||||
|
Result<User> listUserPhone = userFeign.listUserPhone(user);
|
||||||
|
User listUserPhoneData = listUserPhone.getData();
|
||||||
|
if (listUserPhoneData != null){
|
||||||
|
return Result.error("该用户手机号已注册,请重新输入");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isAllEmpty(user.getUserName(),user.getUserPwd())){
|
||||||
|
return Result.error("用户名称或密码不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<User> login = userFeign.login(user);
|
||||||
|
User loginData = login.getData();
|
||||||
|
if (loginData != null){
|
||||||
|
return Result.error("该用户名已经存在,请重新输入");
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<Integer> integerResult = userFeign.addUser(user);
|
||||||
|
Integer integer = integerResult.getData();
|
||||||
|
if (integer < 0){
|
||||||
|
return Result.error("注册失败");
|
||||||
|
}
|
||||||
|
// 用户使用手机号注册,注册成功后给用户发送短信(3分)
|
||||||
|
// 用户短信统一先放入MQ当中(2分)
|
||||||
|
rabbitTemplate.convertAndSend("http_yp_lv",user.getUserPhone(),message -> {
|
||||||
|
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||||
|
return message;
|
||||||
|
});
|
||||||
|
return Result.success("注册成功");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.bwie.auth.utils;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.DirectExchange;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class DLXQueue {
|
||||||
|
// routingKey
|
||||||
|
private static final String DEAD_ROUTING_KEY = "dead.routingkey";
|
||||||
|
private static final String ROUTING_KEY = "routingkey";
|
||||||
|
private static final String DEAD_EXCHANGE = "dead.exchange";
|
||||||
|
private static final String EXCHANGE = "common.exchange";
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate;
|
||||||
|
@Resource
|
||||||
|
RabbitAdmin rabbitAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送死信队列,过期后进入死信交换机,进入死信队列
|
||||||
|
*
|
||||||
|
* @param queueName 队列名称
|
||||||
|
* @param deadQueueName 死信队列名称
|
||||||
|
* @param params 消息内容
|
||||||
|
* @param expiration 过期时间 毫秒
|
||||||
|
*/
|
||||||
|
public void sendDLXQueue(String queueName, String deadQueueName, Object params, Integer expiration) {
|
||||||
|
/**
|
||||||
|
* ----------------------------------先创建一个ttl队列和死信队列--------------------------------------------
|
||||||
|
*/
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
// 队列设置存活时间,单位ms, 必须是整形数据。
|
||||||
|
map.put("x-message-ttl", expiration);
|
||||||
|
// 设置死信交换机
|
||||||
|
map.put("x-dead-letter-exchange", DEAD_EXCHANGE);
|
||||||
|
// 设置死信交换器路由
|
||||||
|
map.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
|
||||||
|
/*参数1:队列名称 参数2:持久化 参数3:是否排他 参数4:自动删除队列 参数5:队列参数*/
|
||||||
|
Queue queue = new Queue(queueName, true, false, false, map);
|
||||||
|
rabbitAdmin.declareQueue(queue);
|
||||||
|
/**
|
||||||
|
* ---------------------------------创建交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
DirectExchange directExchange = new DirectExchange(EXCHANGE, true, false);
|
||||||
|
rabbitAdmin.declareExchange(directExchange);
|
||||||
|
/**
|
||||||
|
* ---------------------------------队列绑定交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(ROUTING_KEY);
|
||||||
|
rabbitAdmin.declareBinding(binding);
|
||||||
|
/**
|
||||||
|
* ---------------------------------在创建一个死信交换机和队列,接收死信队列---------------------------------------------
|
||||||
|
*/
|
||||||
|
DirectExchange deadExchange = new DirectExchange(DEAD_EXCHANGE, true, false);
|
||||||
|
rabbitAdmin.declareExchange(deadExchange);
|
||||||
|
|
||||||
|
Queue deadQueue = new Queue(deadQueueName, true, false, false);
|
||||||
|
rabbitAdmin.declareQueue(deadQueue);
|
||||||
|
/**
|
||||||
|
* ---------------------------------队列绑定死信交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
// 将队列和交换机绑定
|
||||||
|
Binding deadbinding = BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY);
|
||||||
|
rabbitAdmin.declareBinding(deadbinding);
|
||||||
|
// 发送消息
|
||||||
|
rabbitTemplate.convertAndSend(EXCHANGE, ROUTING_KEY, params);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
package com.bwie.auth.utils;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.CustomExchange;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送延迟队列的工具类
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class DelayedQueue {
|
||||||
|
|
||||||
|
// routingKey
|
||||||
|
private static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
|
||||||
|
|
||||||
|
// 延迟队列交换机
|
||||||
|
private static final String DELAYED_EXCHANGE = "delayed.exchange";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RabbitAdmin rabbitAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送延迟队列
|
||||||
|
*
|
||||||
|
* @param queueName 队列名称
|
||||||
|
* @param params 消息内容
|
||||||
|
* @param expiration 延迟时间 毫秒
|
||||||
|
*/
|
||||||
|
public void sendDelayedQueue(String queueName, Object params, Integer expiration) {
|
||||||
|
// 先创建一个队列
|
||||||
|
Queue queue = new Queue(queueName);
|
||||||
|
rabbitAdmin.declareQueue(queue);
|
||||||
|
|
||||||
|
// 创建延迟队列交换机
|
||||||
|
CustomExchange customExchange = createCustomExchange();
|
||||||
|
rabbitAdmin.declareExchange(customExchange);
|
||||||
|
|
||||||
|
// 将队列和交换机绑定
|
||||||
|
Binding binding = BindingBuilder.bind(queue).to(customExchange).with(DELAYED_ROUTING_KEY).noargs();
|
||||||
|
rabbitAdmin.declareBinding(binding);
|
||||||
|
|
||||||
|
// 发送延迟消息
|
||||||
|
rabbitTemplate.convertAndSend(DELAYED_EXCHANGE, DELAYED_ROUTING_KEY, params, msg -> {
|
||||||
|
// 发送消息的时候 延迟时长
|
||||||
|
msg.getMessageProperties().setMessageId(UUID.randomUUID().toString().replaceAll("-", ""));
|
||||||
|
msg.getMessageProperties().setDelay(expiration);
|
||||||
|
return msg;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private CustomExchange createCustomExchange() {
|
||||||
|
Map<String, Object> arguments = new HashMap<>();
|
||||||
|
/**
|
||||||
|
* 参数说明:
|
||||||
|
* 1.交换机的名称
|
||||||
|
* 2.交换机的类型
|
||||||
|
* 3.是否需要持久化
|
||||||
|
* 4.是否自动删除
|
||||||
|
* 5.其它参数
|
||||||
|
*/
|
||||||
|
arguments.put("x-delayed-type", "direct");
|
||||||
|
return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.bwie.auth.utils;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.DirectExchange;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送TTL队列 设置 消息的存活时间 如果超过了存活时间
|
||||||
|
* 该条消息还没有被消费 则自动从队列中消息 ,如果配置了死信队列则消息会进入死信队列
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class TtlQueue {
|
||||||
|
// routingKey
|
||||||
|
private static final String TTL_KEY = "ttl.routingkey";
|
||||||
|
private static final String TTL_EXCHANGE = "ttl.exchange";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RabbitAdmin rabbitAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送TTL队列
|
||||||
|
*
|
||||||
|
* @param queueName 队列名称
|
||||||
|
* @param params 消息内容
|
||||||
|
* @param expiration 过期时间 毫秒
|
||||||
|
*/
|
||||||
|
public void sendTtlQueue(String queueName, Object params, Integer expiration) {
|
||||||
|
/**
|
||||||
|
* ----------------------------------先创建一个ttl队列--------------------------------------------
|
||||||
|
*/
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
// 队列设置存活时间,单位ms,必须是整形数据。
|
||||||
|
map.put("x-message-ttl", expiration);
|
||||||
|
/*参数1:队列名称 参数2:持久化 参数3:是否排他 参数4:自动删除队列 参数5:队列参数*/
|
||||||
|
Queue queue = new Queue(queueName, true, false, false, map);
|
||||||
|
rabbitAdmin.declareQueue(queue);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ---------------------------------创建交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
DirectExchange directExchange = new DirectExchange(TTL_EXCHANGE, true, false);
|
||||||
|
rabbitAdmin.declareExchange(directExchange);
|
||||||
|
/**
|
||||||
|
* ---------------------------------队列绑定交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
// 将队列和交换机绑定
|
||||||
|
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(TTL_KEY);
|
||||||
|
rabbitAdmin.declareBinding(binding);
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
rabbitTemplate.convertAndSend(TTL_EXCHANGE, TTL_KEY, params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9001
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
rabbitmq:
|
||||||
|
host: 123.249.110.115
|
||||||
|
port: 5672
|
||||||
|
username: guest
|
||||||
|
password: guest
|
||||||
|
virtual-host: /
|
||||||
|
listener:
|
||||||
|
simple:
|
||||||
|
prefetch: 1 # 默认每次取出一条消息消费, 消费完成取下一条
|
||||||
|
acknowledge-mode: manual # 设置消费端手动ack确认
|
||||||
|
retry:
|
||||||
|
enabled: true # 是否支持重试
|
||||||
|
publisher-confirm-type: correlated #确认消息已发送到交换机(Exchange)
|
||||||
|
publisher-returns: true #确认消息已发送到队列(Queue)
|
||||||
|
main:
|
||||||
|
allow-circular-references: true
|
||||||
|
jackson:
|
||||||
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
|
time-zone: GMT+8
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: bwie-auth
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
config:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>yp_yuekao812</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
|
||||||
|
<!-- 公共依赖 -->
|
||||||
|
<dependencies>
|
||||||
|
<!-- bootstrap 启动器 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Nacos -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Nacos Config -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Sentinel -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 负载均衡-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Openfeign -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- JWT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Alibaba Fastjson -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Boot Redis -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Hibernate Validator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Apache Lang3 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- lombok依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- hutool -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 阿里大鱼 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>dysmsapi20170525</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 图片长传 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.tobato</groupId>
|
||||||
|
<artifactId>fastdfs-client</artifactId>
|
||||||
|
<version>1.26.5</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.bwie.common.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 系统常量
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
public class Constants {
|
||||||
|
/**
|
||||||
|
* 成功标记
|
||||||
|
*/
|
||||||
|
public static final Integer SUCCESS = 200;
|
||||||
|
public static final String SUCCESS_MSG = "操作成功";
|
||||||
|
/**
|
||||||
|
* 失败标记
|
||||||
|
*/
|
||||||
|
public static final Integer ERROR = 500;
|
||||||
|
public static final String ERROR_MSG = "操作异常";
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.bwie.common.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: Jwt常量
|
||||||
|
*/
|
||||||
|
public class JwtConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户ID字段
|
||||||
|
*/
|
||||||
|
public static final String DETAILS_USER_ID = "user_id";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名字段
|
||||||
|
*/
|
||||||
|
public static final String DETAILS_USERNAME = "username";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户标识
|
||||||
|
*/
|
||||||
|
public static final String USER_KEY = "user_key";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 令牌秘钥
|
||||||
|
*/
|
||||||
|
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.bwie.common.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 令牌常量
|
||||||
|
*/
|
||||||
|
public class TokenConstants {
|
||||||
|
/**
|
||||||
|
* 缓存有效期,默认720(分钟)
|
||||||
|
*/
|
||||||
|
public final static long EXPIRATION = 720;
|
||||||
|
/**
|
||||||
|
* 缓存刷新时间,默认120(分钟)
|
||||||
|
*/
|
||||||
|
public final static long REFRESH_TIME = 120;
|
||||||
|
/**
|
||||||
|
* 权限缓存前缀
|
||||||
|
*/
|
||||||
|
public final static String LOGIN_TOKEN_KEY = "login_tokens:";
|
||||||
|
/**
|
||||||
|
* token标识
|
||||||
|
*/
|
||||||
|
public static final String TOKEN = "token";
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.bwie.common.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.common.domain
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:Instant
|
||||||
|
* @Date:2024/8/12 11:09
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Instant {
|
||||||
|
|
||||||
|
private Long instantId;
|
||||||
|
private String instantDateInitiation;
|
||||||
|
private String instantDateFinish;
|
||||||
|
private String instantRule;
|
||||||
|
private String instantStatus;
|
||||||
|
|
||||||
|
private Integer pageNum = 1;
|
||||||
|
private Integer pageSize = 3;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.bwie.common.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.common.domain
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:Product
|
||||||
|
* @Date:2024/8/12 11:08
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Product {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*/
|
||||||
|
private Long productId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品名称
|
||||||
|
*/
|
||||||
|
private String productName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品类型ID
|
||||||
|
*/
|
||||||
|
private Long typeId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品价格
|
||||||
|
*/
|
||||||
|
private String productPrice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品类型名称
|
||||||
|
*/
|
||||||
|
private String typeName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品数量
|
||||||
|
*/
|
||||||
|
private String productCount;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.bwie.common.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.common.domain
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:Shopping
|
||||||
|
* @Date:2024/8/12 16:43
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Shopping {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*/
|
||||||
|
private Long shoppingId;
|
||||||
|
/**
|
||||||
|
* 商品ID
|
||||||
|
*/
|
||||||
|
private Long productId;
|
||||||
|
/**
|
||||||
|
* 购物车中商品数量
|
||||||
|
*/
|
||||||
|
private String shoppingCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品名称
|
||||||
|
*/
|
||||||
|
private String productName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品类型ID
|
||||||
|
*/
|
||||||
|
private Long typeId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品价格
|
||||||
|
*/
|
||||||
|
private String productPrice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品类型名称
|
||||||
|
*/
|
||||||
|
private String typeName;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.bwie.common.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.common.domain
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:Type
|
||||||
|
* @Date:2024/8/12 11:03
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Type {
|
||||||
|
|
||||||
|
private Long typeId;
|
||||||
|
private String typeName;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.bwie.common.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.common.domain
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:User
|
||||||
|
* @Date:2024/8/12 9:47
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*/
|
||||||
|
private Long userId;
|
||||||
|
/**
|
||||||
|
* 用户名称
|
||||||
|
*/
|
||||||
|
private String userName;
|
||||||
|
/**
|
||||||
|
* 用户手机号
|
||||||
|
*/
|
||||||
|
private String userPhone;
|
||||||
|
/**
|
||||||
|
* 用户密码
|
||||||
|
*/
|
||||||
|
private String userPwd;
|
||||||
|
/**
|
||||||
|
* 用户权限管理
|
||||||
|
*/
|
||||||
|
private String userStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户余额
|
||||||
|
*/
|
||||||
|
private Double userPrice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* token令牌
|
||||||
|
*/
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.bwie.common.domain.response;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.common.domain.response
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductResp
|
||||||
|
* @Date:2024/8/12 17:03
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class ProductResp {
|
||||||
|
|
||||||
|
private List<String> shoppingIds;
|
||||||
|
private Double productPrice;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.bwie.common.result;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 列表返回结果集
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PageResult<T> implements Serializable {
|
||||||
|
/**
|
||||||
|
* 总条数
|
||||||
|
*/
|
||||||
|
private long total;
|
||||||
|
/**
|
||||||
|
* 结果集合
|
||||||
|
*/
|
||||||
|
private List<T> list;
|
||||||
|
|
||||||
|
public PageResult() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public PageResult(long total, List<T> list) {
|
||||||
|
this.total = total;
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageResult<T> toPageResult(long total, List<T> list) {
|
||||||
|
return new PageResult(total, list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<PageResult<T>> toResult(long total, List<T> list) {
|
||||||
|
return Result.success(PageResult.toPageResult(total, list));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.bwie.common.result;
|
||||||
|
|
||||||
|
import com.bwie.common.constants.Constants;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 响应信息主体
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Result<T> implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/**
|
||||||
|
* 成功
|
||||||
|
*/
|
||||||
|
public static final int SUCCESS = Constants.SUCCESS;
|
||||||
|
/**
|
||||||
|
* 失败
|
||||||
|
*/
|
||||||
|
public static final int FAIL = Constants.ERROR;
|
||||||
|
/**
|
||||||
|
* 返回状态码
|
||||||
|
*/
|
||||||
|
private int code;
|
||||||
|
/**
|
||||||
|
* 响应信息
|
||||||
|
*/
|
||||||
|
private String msg;
|
||||||
|
/**
|
||||||
|
* 响应数据
|
||||||
|
*/
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
public static <T> Result<T> success() {
|
||||||
|
return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> success(T data) {
|
||||||
|
return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> success(T data, String msg) {
|
||||||
|
return restResult(data, SUCCESS, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error() {
|
||||||
|
return restResult(null, FAIL, Constants.ERROR_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(String msg) {
|
||||||
|
return restResult(null, FAIL, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(T data) {
|
||||||
|
return restResult(data, FAIL, Constants.ERROR_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(T data, String msg) {
|
||||||
|
return restResult(data, FAIL, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(int code, String msg) {
|
||||||
|
return restResult(null, code, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||||
|
Result<T> apiResult = new Result<>();
|
||||||
|
apiResult.setCode(code);
|
||||||
|
apiResult.setData(data);
|
||||||
|
apiResult.setMsg(msg);
|
||||||
|
return apiResult;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,109 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import com.bwie.common.constants.JwtConstants;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Jwt工具类
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
public class JwtUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 秘钥
|
||||||
|
*/
|
||||||
|
public static String secret = JwtConstants.SECRET;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据声明生成令牌
|
||||||
|
*
|
||||||
|
* @param claims 数据声明
|
||||||
|
* @return 令牌
|
||||||
|
*/
|
||||||
|
public static String createToken(Map<String, Object> claims){
|
||||||
|
String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从令牌中获取数据声明
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 数据声明
|
||||||
|
*/
|
||||||
|
public static Claims parseToken(String token){
|
||||||
|
return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户标识
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserKey(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.USER_KEY);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户标识
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserKey(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.USER_KEY);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户ID
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取用户ID
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户名
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户名
|
||||||
|
*/
|
||||||
|
public static String getUserName(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取用户名
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户名
|
||||||
|
*/
|
||||||
|
public static String getUserName(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取键值
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
public static String getValue(Claims claims, String key){
|
||||||
|
Object obj = claims.get(key);
|
||||||
|
return obj == null ? "" : obj.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 字符串处理工具类
|
||||||
|
*/
|
||||||
|
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 判断一个对象是否为空
|
||||||
|
*
|
||||||
|
* @param object Object
|
||||||
|
* @return true:为空 false:非空
|
||||||
|
*/
|
||||||
|
public static boolean isNull(Object object) {
|
||||||
|
return object == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||||
|
*
|
||||||
|
* @param coll 要判断的Collection
|
||||||
|
* @return true:为空 false:非空
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(Collection<?> coll) {
|
||||||
|
return isNull(coll) || coll.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||||
|
*
|
||||||
|
* @param str 指定字符串
|
||||||
|
* @param strs 需要检查的字符串数组
|
||||||
|
* @return 是否匹配
|
||||||
|
*/
|
||||||
|
public static boolean matches(String str, List<String> strs) {
|
||||||
|
if (isEmpty(str) || isEmpty(strs)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (String pattern : strs) {
|
||||||
|
if (isMatch(pattern, str))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断url是否与规则配置:
|
||||||
|
* ? 表示单个字符;
|
||||||
|
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||||
|
* ** 表示任意层路径;
|
||||||
|
*
|
||||||
|
* @param pattern 匹配规则
|
||||||
|
* @param url 需要匹配的url
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isMatch(String pattern, String url) {
|
||||||
|
AntPathMatcher matcher = new AntPathMatcher();
|
||||||
|
return matcher.match(pattern, url);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.aliyun.dysmsapi20170525.Client;
|
||||||
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||||
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信工具类
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class TelSmsUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限
|
||||||
|
*/
|
||||||
|
private static String accessKeyId = "LTAIEVXszCmcd1T5";
|
||||||
|
private static String accessKeySecret = "2zHwciQXln8wExSEnkIYtRTSwLeRNd";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信访问域名
|
||||||
|
*/
|
||||||
|
private static String endpoint = "dysmsapi.aliyuncs.com";
|
||||||
|
/**
|
||||||
|
* 短信签名
|
||||||
|
*/
|
||||||
|
private static String signName = "登录验证";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实例化短信对象
|
||||||
|
*/
|
||||||
|
private static Client client;
|
||||||
|
|
||||||
|
static {
|
||||||
|
log.info("初始化短信服务开始");
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
client = initClient();
|
||||||
|
log.info("初始化短信成功:{}",signName);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
log.info("初始化短信服务结束:耗时:{}MS",(System.currentTimeMillis()-startTime));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 初始化短信对象
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static Client initClient() throws Exception{
|
||||||
|
Config config = new Config()
|
||||||
|
// 您的AccessKey ID
|
||||||
|
.setAccessKeyId(accessKeyId)
|
||||||
|
// 您的AccessKey Secret
|
||||||
|
.setAccessKeySecret(accessKeySecret);
|
||||||
|
// 访问的域名
|
||||||
|
config.endpoint = endpoint;
|
||||||
|
return new Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送单条短信
|
||||||
|
* @param tel
|
||||||
|
* @param templateCode SMS_153991546
|
||||||
|
* @param sendDataMap
|
||||||
|
*/
|
||||||
|
public static String sendSms(String tel , String templateCode , Map<String,String> sendDataMap){
|
||||||
|
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||||
|
.setPhoneNumbers(tel)
|
||||||
|
.setSignName(signName)
|
||||||
|
.setTemplateCode(templateCode)
|
||||||
|
.setTemplateParam(JSONObject.toJSONString(sendDataMap));
|
||||||
|
SendSmsResponse sendSmsResponse = null;
|
||||||
|
try {
|
||||||
|
log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap));
|
||||||
|
sendSmsResponse = client.sendSms(sendSmsRequest);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e);
|
||||||
|
}
|
||||||
|
return JSONObject.toJSONString(sendSmsResponse.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>yp_yuekao812</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-gateway</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 公共模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 网关依赖 -->
|
||||||
|
<!-- SpringCloud Gateway -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Sentinel Gateway -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 引入阿里巴巴sentinel限流 依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.bwie.gateway;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.gateway
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:GatewayApplication
|
||||||
|
* @Date:2024/8/12 11:37
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GatewayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GatewayApplication.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.bwie.gateway.config;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 放行白名单配置
|
||||||
|
* @author sx
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@RefreshScope
|
||||||
|
@ConfigurationProperties(prefix = "ignore")
|
||||||
|
@Data
|
||||||
|
@Log4j2
|
||||||
|
public class IgnoreWhiteConfig {
|
||||||
|
/**
|
||||||
|
* 放行白名单配置,网关不校验此处的白名单
|
||||||
|
*/
|
||||||
|
private List<String> whites = new ArrayList<>();
|
||||||
|
|
||||||
|
public void setWhites(List<String> whites) {
|
||||||
|
log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
|
||||||
|
this.whites = whites;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
package com.bwie.gateway.filters;
|
||||||
|
|
||||||
|
import com.bwie.common.constants.TokenConstants;
|
||||||
|
import com.bwie.common.utils.JwtUtils;
|
||||||
|
import com.bwie.common.utils.StringUtils;
|
||||||
|
import com.bwie.gateway.config.IgnoreWhiteConfig;
|
||||||
|
import com.bwie.gateway.utils.GatewayUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class GatewayFilters implements GatewayFilter,Override {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IgnoreWhiteConfig ignoreWhiteConfig;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
List<String> whites = ignoreWhiteConfig.getWhites();
|
||||||
|
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
String path = request.getURI().getPath();
|
||||||
|
if (StringUtils.matches(path,whites)){
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断 token 是否为空
|
||||||
|
String token = request.getHeaders().getFirst(TokenConstants.TOKEN);
|
||||||
|
if (StringUtils.isNotBlank(token)){
|
||||||
|
return GatewayUtils.errorResponse(exchange,"token 不能为空", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断 token 是否合法
|
||||||
|
try {
|
||||||
|
JwtUtils.parseToken(token);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return GatewayUtils.errorResponse(exchange,"token 不合法");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断 token 是否过期
|
||||||
|
String userKey = JwtUtils.getUserKey(token);
|
||||||
|
if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY+userKey)){
|
||||||
|
return GatewayUtils.errorResponse(exchange,"token 已过期");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 每次访问后台gateway进行拦截10分钟内访问后台自动续期Token到15分钟(3分)
|
||||||
|
Long expire = redisTemplate.getExpire(TokenConstants.LOGIN_TOKEN_KEY + userKey, TimeUnit.MINUTES);
|
||||||
|
if (expire>5){
|
||||||
|
redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY+userKey,15,TimeUnit.MINUTES);
|
||||||
|
}else {
|
||||||
|
return GatewayUtils.errorResponse(exchange,"10分钟内为登录,请重新登录");
|
||||||
|
}
|
||||||
|
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends Annotation> annotationType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
package com.bwie.gateway.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.common.utils.StringUtils;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 网关处理工具类
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class GatewayUtils {
|
||||||
|
/**
|
||||||
|
* 添加请求头参数
|
||||||
|
* @param mutate 修改对象
|
||||||
|
* @param key 键
|
||||||
|
* @param value 值
|
||||||
|
*/
|
||||||
|
public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) {
|
||||||
|
if (StringUtils.isEmpty(key)){
|
||||||
|
log.warn("添加请求头参数键不可以为空");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (value == null) {
|
||||||
|
log.warn("添加请求头参数:[{}]值为空",key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String valueStr = value.toString();
|
||||||
|
mutate.header(key, valueStr);
|
||||||
|
log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除请求头参数
|
||||||
|
* @param mutate 修改对象
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public static void removeHeader(ServerHttpRequest.Builder mutate, String key) {
|
||||||
|
if (StringUtils.isEmpty(key)){
|
||||||
|
log.warn("删除请求头参数键不可以为空");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mutate.headers(httpHeaders -> httpHeaders.remove(key)).build();
|
||||||
|
log.info("删除请求头参数 - 键:[{}]",key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误结果响应
|
||||||
|
* @param exchange 响应上下文
|
||||||
|
* @param msg 响应消息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg, HttpStatus httpStatus) {
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
//设置HTTP响应头状态
|
||||||
|
response.setStatusCode(httpStatus);
|
||||||
|
//设置HTTP响应头文本格式
|
||||||
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||||
|
//定义响应内容
|
||||||
|
Result<?> result = Result.error(msg);
|
||||||
|
String resultJson = JSONObject.toJSONString(result);
|
||||||
|
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||||
|
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||||
|
//进行响应
|
||||||
|
return response.writeWith(Mono.just(dataBuffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误结果响应
|
||||||
|
* @param exchange 响应上下文
|
||||||
|
* @param msg 响应消息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg) {
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
//设置HTTP响应头状态
|
||||||
|
response.setStatusCode(HttpStatus.OK);
|
||||||
|
//设置HTTP响应头文本格式
|
||||||
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||||
|
//定义响应内容
|
||||||
|
Result<?> result = Result.error(msg);
|
||||||
|
String resultJson = JSONObject.toJSONString(result);
|
||||||
|
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||||
|
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||||
|
//进行响应
|
||||||
|
return response.writeWith(Mono.just(dataBuffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 18080
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: bwie-gateway
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
main:
|
||||||
|
# 允许使用循环引用
|
||||||
|
allow-circular-references: true
|
||||||
|
# 允许定义相同的bean对象 去覆盖原有的
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
config:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-modules</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-es</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<!-- es:-->
|
||||||
|
<!-- hostname: 106.54.222.192-->
|
||||||
|
<!-- port: 9200-->
|
||||||
|
<!-- scheme: http-->
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.bwie.es;
|
||||||
|
|
||||||
|
import com.bwie.es.sync.ProductSync;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:EsApplication
|
||||||
|
* @Date:2024/8/12 11:51
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@EnableFeignClients
|
||||||
|
@SpringBootApplication
|
||||||
|
public class EsApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(EsApplication.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProductSync productSync;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void cons(){
|
||||||
|
// 项目启动时需要把数据初始化到ES当中(4分)
|
||||||
|
log.info("ES项目已启动,开始同步.....");
|
||||||
|
productSync.productSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.bwie.es.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.elasticsearch.client.RestClient;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "es")
|
||||||
|
@Data
|
||||||
|
public class InitEsRes {
|
||||||
|
private String host;
|
||||||
|
private int port;
|
||||||
|
private String scheme;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestHighLevelClient restHighLevelClient(){
|
||||||
|
return new RestHighLevelClient(
|
||||||
|
RestClient.builder(new HttpHost(host,port,scheme))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.bwie.es.controller;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.result.PageResult;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.es.service.EsService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es.controller
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:EsController
|
||||||
|
* @Date:2024/8/12 13:35
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("es")
|
||||||
|
public class EsController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EsService esService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表查询
|
||||||
|
*/
|
||||||
|
@PostMapping("listProduce")
|
||||||
|
public Result<PageResult<Product>> listProduce(@RequestBody Product product){
|
||||||
|
return esService.listProduce(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表添加
|
||||||
|
*/
|
||||||
|
@PostMapping("addProduce")
|
||||||
|
public void addProduce(@RequestBody Product product){
|
||||||
|
esService.addProduce(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表修改
|
||||||
|
*/
|
||||||
|
@PostMapping("updProduce")
|
||||||
|
public void updProduce(@RequestBody Product product){
|
||||||
|
esService.updProduce(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.bwie.es.feign;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es.feign
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductFeign
|
||||||
|
* @Date:2024/8/12 11:52
|
||||||
|
*/
|
||||||
|
@FeignClient("bwie-car")
|
||||||
|
public interface ProductFeign {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*/
|
||||||
|
@GetMapping("product/listProduct")
|
||||||
|
public Result<List<Product>> listProduct();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.bwie.es.feign.factory;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.es.feign.ProductFeign;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es.feign.factory
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductFeignFactpry
|
||||||
|
* @Date:2024/8/12 11:53
|
||||||
|
*/
|
||||||
|
public class ProductFeignFactory implements FallbackFactory<ProductFeign> {
|
||||||
|
@Override
|
||||||
|
public ProductFeign create(Throwable cause) {
|
||||||
|
return new ProductFeign() {
|
||||||
|
@Override
|
||||||
|
public Result<List<Product>> listProduct() {
|
||||||
|
return Result.error(cause.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.bwie.es.service;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.result.PageResult;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es.service
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:EsService
|
||||||
|
* @Date:2024/8/12 13:34
|
||||||
|
*/
|
||||||
|
public interface EsService {
|
||||||
|
/**
|
||||||
|
* es列表查询
|
||||||
|
*/
|
||||||
|
// List<Product> listProduce(Product product);
|
||||||
|
|
||||||
|
Result<PageResult<Product>> listProduce(Product product);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表添加
|
||||||
|
*/
|
||||||
|
void addProduce(Product product);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量同步添加
|
||||||
|
*/
|
||||||
|
void adds(List<Product> productList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除
|
||||||
|
*/
|
||||||
|
void del();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表修改
|
||||||
|
*/
|
||||||
|
void updProduce(Product product);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,149 @@
|
||||||
|
package com.bwie.es.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.result.PageResult;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.common.utils.StringUtils;
|
||||||
|
import com.bwie.es.service.EsService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.elasticsearch.action.bulk.BulkRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.action.update.UpdateRequest;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.index.query.QueryStringQueryBuilder;
|
||||||
|
import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
||||||
|
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||||
|
import org.elasticsearch.search.SearchHit;
|
||||||
|
import org.elasticsearch.search.SearchHits;
|
||||||
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es.service.impl
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:EsServiceImpl
|
||||||
|
* @Date:2024/8/12 13:34
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Service
|
||||||
|
public class EsServiceImpl implements EsService {
|
||||||
|
|
||||||
|
|
||||||
|
private static final String INDEX_ES ="produce_yp";
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表查询
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result<PageResult<Product>> listProduce(Product product) {
|
||||||
|
ArrayList<Product> productList = new ArrayList<>();
|
||||||
|
Long total = 0L;
|
||||||
|
try {
|
||||||
|
SearchRequest searchRequest = new SearchRequest(INDEX_ES);
|
||||||
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||||
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
|
if (!StringUtils.isAllEmpty(product.getProductName())){
|
||||||
|
boolQueryBuilder.must(QueryBuilders.matchQuery("productName",product.getProductName()));
|
||||||
|
}
|
||||||
|
searchSourceBuilder.query(boolQueryBuilder);
|
||||||
|
searchRequest.source(searchSourceBuilder);
|
||||||
|
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
SearchHits hits = search.getHits();
|
||||||
|
total = hits.getTotalHits().value;
|
||||||
|
SearchHit[] hitsHits = hits.getHits();
|
||||||
|
for (SearchHit hitsHit : hitsHits) {
|
||||||
|
String sourceAsString = hitsHit.getSourceAsString();
|
||||||
|
Product product1 = JSONObject.parseObject(sourceAsString, Product.class);
|
||||||
|
product1.setProductId(Long.valueOf(hitsHit.getId()));
|
||||||
|
productList.add(product1);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("方法名称: es列表查询 ,报错原因 {}",e.getMessage());
|
||||||
|
}
|
||||||
|
return PageResult.toResult(total, productList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表添加
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addProduce(Product product) {
|
||||||
|
try {
|
||||||
|
IndexRequest indexRequest = new IndexRequest(INDEX_ES);
|
||||||
|
indexRequest.id(product.getProductId()+"")
|
||||||
|
.source(JSONObject.toJSONString(product));
|
||||||
|
restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("方法名称: es列表添加 ,报错原因 {}",e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量同步添加
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void adds(List<Product> productList) {
|
||||||
|
try {
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
|
||||||
|
productList.forEach(product -> {
|
||||||
|
bulkRequest.add(new IndexRequest(INDEX_ES)
|
||||||
|
.id(product.getProductId()+"")
|
||||||
|
.source(XContentType.JSON,JSONObject.toJSONString(product)));
|
||||||
|
});
|
||||||
|
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("方法名称: 批量同步添加 ,报错原因 {}",e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void del() {
|
||||||
|
try {
|
||||||
|
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest();
|
||||||
|
deleteByQueryRequest.setQuery(new QueryStringQueryBuilder(INDEX_ES));
|
||||||
|
restHighLevelClient.deleteByQuery(deleteByQueryRequest,RequestOptions.DEFAULT);
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* es列表修改
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updProduce(Product product) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
UpdateRequest updateRequest = new UpdateRequest();
|
||||||
|
updateRequest.id(product.getProductId()+"")
|
||||||
|
.doc(XContentType.JSON,JSONObject.toJSONString(product));
|
||||||
|
restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("方法名称: es列表修改 ,报错原因 {}",e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.bwie.es.sync;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.es.feign.ProductFeign;
|
||||||
|
import com.bwie.es.service.EsService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.es.sync
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductSync
|
||||||
|
* @Date:2024/8/12 12:01
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class ProductSync {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EsService esService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProductFeign productFeign;
|
||||||
|
|
||||||
|
public void productSync(){
|
||||||
|
|
||||||
|
try {
|
||||||
|
log.info("es同步已经开始...,正在从数据库中读取数据");
|
||||||
|
Result<List<Product>> listResult = productFeign.listProduct();
|
||||||
|
List<Product> productList = listResult.getData();
|
||||||
|
log.info("es同步正在进行中...,从数据库中读取数据完毕,数据为{}", JSONObject.toJSONString(productList));
|
||||||
|
// esService.del();
|
||||||
|
log.info("es同步正在进行中...,正在将数据同步到es中");
|
||||||
|
esService.adds(productList);
|
||||||
|
log.info("es同步已经结束...,es数据同步已完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("es同步报错...,报错原因为:{}",e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9005
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
main:
|
||||||
|
allow-circular-references: true
|
||||||
|
jackson:
|
||||||
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
|
time-zone: GMT+8
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: bwie-es
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
es:
|
||||||
|
host: 123.249.110.115
|
||||||
|
port: 9200
|
||||||
|
scheme: http
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-modules</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-product</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 系统公共 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Druid -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mysql Connector -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mybatis 依赖配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Pagehelper -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.pagehelper</groupId>
|
||||||
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 引入 javax 短信 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.mail</groupId>
|
||||||
|
<artifactId>javax.mail</artifactId>
|
||||||
|
<version>1.5.6</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 引入 oss 图片上传 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.bwie.product;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.product
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductApplication
|
||||||
|
* @Date:2024/8/12 11:12
|
||||||
|
*/
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ProductApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ProductApplication.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.bwie.product.controller;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Instant;
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.domain.Shopping;
|
||||||
|
import com.bwie.common.domain.response.ProductResp;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.product.service.ProductService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.product.controller
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductController
|
||||||
|
* @Date:2024/8/12 11:14
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("product")
|
||||||
|
public class ProductController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProductService productService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*/
|
||||||
|
@GetMapping("listProduct")
|
||||||
|
public Result<List<Product>> listProduct(){
|
||||||
|
return Result.success(productService.listProduct());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询秒杀规则列表
|
||||||
|
*/
|
||||||
|
@PostMapping("listInstant")
|
||||||
|
public Result listInstant(@RequestBody Instant instant){
|
||||||
|
return productService.listInstant(instant);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加秒杀规则列表
|
||||||
|
*/
|
||||||
|
@PostMapping("addInstant")
|
||||||
|
public Result addInstant(@RequestBody Instant instant){
|
||||||
|
return productService.addInstant(instant);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*/
|
||||||
|
@GetMapping("listShopping")
|
||||||
|
public Result listShopping(){
|
||||||
|
return productService.listShopping();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加购物车
|
||||||
|
*/
|
||||||
|
@PostMapping("addShopping")
|
||||||
|
public Result addShopping(@RequestBody Shopping shopping){
|
||||||
|
return productService.addShopping(shopping);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 购物车结算
|
||||||
|
*/
|
||||||
|
@PostMapping("payment")
|
||||||
|
public Result payment(@RequestBody ProductResp resp){
|
||||||
|
return productService.payment(resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.bwie.product.mapper;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Instant;
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.domain.Shopping;
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.product.mapper
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductMapper
|
||||||
|
* @Date:2024/8/12 11:13
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ProductMapper {
|
||||||
|
|
||||||
|
List<Product> listProduct();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询秒杀规则列表
|
||||||
|
*/
|
||||||
|
List<Instant> listInstant(Instant instant);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加秒杀规则列表
|
||||||
|
*/
|
||||||
|
Integer addInstant(Instant instant);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*/
|
||||||
|
List<Shopping> listShopping();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车中是否有该商品
|
||||||
|
*/
|
||||||
|
Product listProductId(Long productId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如购物车当中有此商品则购物车商品数量
|
||||||
|
*/
|
||||||
|
Integer updShopping(Shopping shopping);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加购物车
|
||||||
|
*/
|
||||||
|
Integer addShopping(Shopping shopping);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车中的商品
|
||||||
|
*/
|
||||||
|
Shopping listShoppingId(String shoppingId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除购物车中的商品
|
||||||
|
*/
|
||||||
|
void delShoppingId(String shoppingId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改商品的数量
|
||||||
|
*/
|
||||||
|
void updProduct(Shopping shopping);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扣除用户余额
|
||||||
|
*/
|
||||||
|
void updUser(User user1);
|
||||||
|
|
||||||
|
Shopping listShoppingProdcutId(Long productId);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.bwie.product.service;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.Instant;
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.domain.Shopping;
|
||||||
|
import com.bwie.common.domain.response.ProductResp;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.product.service
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductService
|
||||||
|
* @Date:2024/8/12 11:13
|
||||||
|
*/
|
||||||
|
public interface ProductService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*/
|
||||||
|
List<Product> listProduct();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询秒杀规则列表
|
||||||
|
*/
|
||||||
|
Result listInstant(Instant instant);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加秒杀规则列表
|
||||||
|
*/
|
||||||
|
Result addInstant(Instant instant);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*/
|
||||||
|
Result listShopping();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加购物车
|
||||||
|
*/
|
||||||
|
Result addShopping(Shopping shopping);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 购物车结算
|
||||||
|
*/
|
||||||
|
Result payment(ProductResp resp);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,135 @@
|
||||||
|
package com.bwie.product.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.PageUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.bwie.common.constants.TokenConstants;
|
||||||
|
import com.bwie.common.domain.Instant;
|
||||||
|
import com.bwie.common.domain.Product;
|
||||||
|
import com.bwie.common.domain.Shopping;
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.domain.response.ProductResp;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.common.utils.JwtUtils;
|
||||||
|
import com.bwie.product.mapper.ProductMapper;
|
||||||
|
import com.bwie.product.service.ProductService;
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
|
import com.github.pagehelper.PageInfo;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.product.service.impl
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:ProductServiceImpl
|
||||||
|
* @Date:2024/8/12 11:13
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ProductServiceImpl implements ProductService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProductMapper productMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Product> listProduct() {
|
||||||
|
return productMapper.listProduct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询秒杀规则列表
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result listInstant(Instant instant) {
|
||||||
|
PageHelper.startPage(instant.getPageNum(),instant.getPageSize());
|
||||||
|
List<Instant> instantList = productMapper.listInstant(instant);
|
||||||
|
PageInfo<Instant> instantPageInfo = new PageInfo<>(instantList);
|
||||||
|
return Result.success(instantPageInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加秒杀规则列表
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result addInstant(Instant instant) {
|
||||||
|
Integer i = productMapper.addInstant(instant);
|
||||||
|
return i>0?Result.success():Result.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result listShopping() {
|
||||||
|
List<Shopping> shoppingList = productMapper.listShopping();
|
||||||
|
return Result.success(shoppingList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加购物车
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result addShopping(Shopping shopping) {
|
||||||
|
// 查询购物车中是否有该商品
|
||||||
|
Shopping shopping1 = productMapper.listShoppingProdcutId(shopping.getProductId());
|
||||||
|
if (shopping1 != null){
|
||||||
|
// 如购物车当中有此商品则购物车商品数量+1(2分)
|
||||||
|
Integer i = productMapper.updShopping(shopping);
|
||||||
|
return i>0?Result.success():Result.error();
|
||||||
|
}
|
||||||
|
//如购物车无此商品则加入购物车,
|
||||||
|
Integer i = productMapper.addShopping(shopping);
|
||||||
|
return i>0?Result.success():Result.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 购物车结算
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
@Override
|
||||||
|
public Result payment(ProductResp resp) {
|
||||||
|
String token = request.getHeader(TokenConstants.TOKEN);
|
||||||
|
String userKey = JwtUtils.getUserKey(token);
|
||||||
|
String user = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||||
|
User user1 = JSONObject.parseObject(user, User.class);
|
||||||
|
// 使用余额进行结算,如果余额不足则支付失败(2分)
|
||||||
|
if (user1.getUserPrice() < resp.getProductPrice()){
|
||||||
|
return Result.error("余额不足,支付失败");
|
||||||
|
}
|
||||||
|
// 订单支付,商品redis、数据库库存减一(2分)
|
||||||
|
// 订单支付过程中,请使用redis分布式锁解决超卖的问题(2分)
|
||||||
|
List<String> shoppingIds = resp.getShoppingIds();
|
||||||
|
for (String shoppingId : shoppingIds) {
|
||||||
|
// 查询购物车中的商品
|
||||||
|
Shopping shopping = productMapper.listShoppingId(shoppingId);
|
||||||
|
// 删除购物车中的商品
|
||||||
|
productMapper.delShoppingId(shoppingId);
|
||||||
|
// 修改商品的数量
|
||||||
|
productMapper.updProduct(shopping);
|
||||||
|
}
|
||||||
|
// 扣除用户余额
|
||||||
|
user1.setUserPrice(resp.getProductPrice());
|
||||||
|
productMapper.updUser(user1);
|
||||||
|
// 当商品发生更改的时候需要异步与ES进行同步 (2分)
|
||||||
|
|
||||||
|
|
||||||
|
return Result.success("","支付成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.bwie.product.util;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(RuntimeException.class)//捕获运行时异常
|
||||||
|
@ResponseBody
|
||||||
|
public Map<String,Object> exceptionHandler(HttpServletRequest request, Exception e){//处理异常方法
|
||||||
|
Map<String,Object> map=new HashMap<String, Object>();
|
||||||
|
map.put("errorCode","101");
|
||||||
|
map.put("errorMsg","已捕获到全局异常,系统错误!");
|
||||||
|
map.put("requestURL", request.getRequestURL().toString()); // 获取请求的URL
|
||||||
|
map.put("exception", e.getMessage()); // 获取异常的消息(可能不是具体原因)
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 10006
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: bwie-car
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
main:
|
||||||
|
# 允许使用循环引用
|
||||||
|
allow-circular-references: true
|
||||||
|
# 允许定义相同的bean对象 去覆盖原有的
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,73 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<!--
|
||||||
|
1.在mybats的开发中namespace有特殊的意思,一定要是对应接口的全限定名
|
||||||
|
通过namespace可以简历mapper.xml和接口之间的关系(名字不重要,位置不重要)
|
||||||
|
-->
|
||||||
|
<mapper namespace="com.bwie.product.mapper.ProductMapper">
|
||||||
|
<insert id="addInstant">
|
||||||
|
INSERT INTO `yp_yuekao812`.`instant` (`instant_date_initiation`, `instant_date_finish`, `instant_rule`)
|
||||||
|
VALUES (#{instantDateInitiation}, #{instantDateFinish}, #{instantRule});
|
||||||
|
|
||||||
|
</insert>
|
||||||
|
<insert id="addShopping">
|
||||||
|
INSERT INTO `yp_yuekao812`.`shopping` (`product_id`)
|
||||||
|
VALUES (#{productId})
|
||||||
|
</insert>
|
||||||
|
<update id="updShopping">
|
||||||
|
update shopping
|
||||||
|
set shopping_count = shopping_count + 1
|
||||||
|
where product_id = #{productId}
|
||||||
|
</update>
|
||||||
|
<update id="updProduct">
|
||||||
|
update product
|
||||||
|
set product_count = product_count - #{shoppingCount}
|
||||||
|
where product_id = #{productId}
|
||||||
|
</update>
|
||||||
|
<update id="updUser">
|
||||||
|
update user
|
||||||
|
set user_price = user_price - #{userPrice}
|
||||||
|
where user_id = #{userId}
|
||||||
|
</update>
|
||||||
|
<delete id="delShoppingId">
|
||||||
|
delete
|
||||||
|
from shopping
|
||||||
|
where shopping_id = #{shoppingId}
|
||||||
|
</delete>
|
||||||
|
<!-- 添加 -->
|
||||||
|
|
||||||
|
|
||||||
|
<select id="listProduct" resultType="com.bwie.common.domain.Product">
|
||||||
|
select p.*,t.type_name
|
||||||
|
from product p left join type t on p.type_id = t.type_id
|
||||||
|
</select>
|
||||||
|
<select id="listInstant" resultType="com.bwie.common.domain.Instant">
|
||||||
|
select *
|
||||||
|
from instant
|
||||||
|
<where>
|
||||||
|
<if test="instantRule != null and instantRule !='' ">
|
||||||
|
and INSTR(instant_rule, #{instantRule} )
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
|
<select id="listShopping" resultType="com.bwie.common.domain.Shopping">
|
||||||
|
select s.shopping_id,s.shopping_count,p.*,t.type_name
|
||||||
|
from shopping s
|
||||||
|
left join product p on s.product_id = p.product_id
|
||||||
|
left join type t on p.type_id = t.type_id
|
||||||
|
</select>
|
||||||
|
<select id="listProductId" resultType="com.bwie.common.domain.Product">
|
||||||
|
select *
|
||||||
|
from product where product_id = #{productId}
|
||||||
|
</select>
|
||||||
|
<select id="listShoppingId" resultType="com.bwie.common.domain.Shopping">
|
||||||
|
select *
|
||||||
|
from shopping where shopping_id = #{shoppingId}
|
||||||
|
</select>
|
||||||
|
<select id="listShoppingProdcutId" resultType="com.bwie.common.domain.Shopping">
|
||||||
|
select *
|
||||||
|
from shopping where product_id = #{productId}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-modules</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-user</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 系统公共 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Druid -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mysql Connector -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mybatis 依赖配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Pagehelper -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.pagehelper</groupId>
|
||||||
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 引入 javax 短信 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.mail</groupId>
|
||||||
|
<artifactId>javax.mail</artifactId>
|
||||||
|
<version>1.5.6</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 引入 oss 图片上传 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.bwie.user;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.user
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserApplication
|
||||||
|
* @Date:2024/8/12 9:42
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class UserApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(UserApplication.class,args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.bwie.user.controller;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.bwie.user.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.user.controller
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserController
|
||||||
|
* @Date:2024/8/12 9:45
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("user")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证 用户名
|
||||||
|
*/
|
||||||
|
@PostMapping("login")
|
||||||
|
public Result<User> login(@RequestBody User user){
|
||||||
|
return Result.success(userService.login(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
@PostMapping("addUser")
|
||||||
|
public Result<Integer> addUser(@RequestBody User user){
|
||||||
|
return Result.success(userService.addUser(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证 用户手机号
|
||||||
|
*/
|
||||||
|
@PostMapping("listUserPhone")
|
||||||
|
public Result<User> listUserPhone(@RequestBody User user){
|
||||||
|
return Result.success(userService.listUserPhone(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.bwie.user.mapper;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.user.mapper
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserMapper
|
||||||
|
* @Date:2024/8/12 9:43
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface UserMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证
|
||||||
|
*/
|
||||||
|
User login(String userName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
Integer addUser(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证 用户手机号
|
||||||
|
*/
|
||||||
|
User listUserPhone(String userPhone);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.bwie.user.service;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.user.service
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserService
|
||||||
|
* @Date:2024/8/12 9:44
|
||||||
|
*/
|
||||||
|
public interface UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证
|
||||||
|
*/
|
||||||
|
User login(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
Integer addUser(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证 用户手机号
|
||||||
|
*/
|
||||||
|
User listUserPhone(User user);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.bwie.user.service.impl;
|
||||||
|
|
||||||
|
import com.bwie.common.domain.User;
|
||||||
|
import com.bwie.user.mapper.UserMapper;
|
||||||
|
import com.bwie.user.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:杨鹏
|
||||||
|
* @Package:com.bwie.user.service.impl
|
||||||
|
* @Project:yp_yuekao812
|
||||||
|
* @name:UserServiceImpl
|
||||||
|
* @Date:2024/8/12 9:44
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public User login(User user) {
|
||||||
|
return userMapper.login(user.getUserName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Integer addUser(User user) {
|
||||||
|
return userMapper.addUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录信息验证 用户手机号
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public User listUserPhone(User user) {
|
||||||
|
return userMapper.listUserPhone(user.getUserPhone());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 10005
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: bwie-user
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
main:
|
||||||
|
# 允许使用循环引用
|
||||||
|
allow-circular-references: true
|
||||||
|
# 允许定义相同的bean对象 去覆盖原有的
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 123.249.110.115:8848
|
||||||
|
# 命名空间
|
||||||
|
namespace: yp
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<!--
|
||||||
|
1.在mybats的开发中namespace有特殊的意思,一定要是对应接口的全限定名
|
||||||
|
通过namespace可以简历mapper.xml和接口之间的关系(名字不重要,位置不重要)
|
||||||
|
-->
|
||||||
|
<mapper namespace="com.bwie.user.mapper.UserMapper">
|
||||||
|
<insert id="addUser">
|
||||||
|
INSERT INTO `yp_yuekao812`.`user` (`user_name`, `user_phone`, `user_pwd`)
|
||||||
|
VALUES (#{userName}, #{userPhone}, #{userPwd})
|
||||||
|
</insert>
|
||||||
|
<!-- 添加 -->
|
||||||
|
|
||||||
|
|
||||||
|
<select id="login" resultType="com.bwie.common.domain.User">
|
||||||
|
select *
|
||||||
|
from user where user_name = #{userName}
|
||||||
|
</select>
|
||||||
|
<select id="listUserPhone" resultType="com.bwie.common.domain.User">
|
||||||
|
select *
|
||||||
|
from user where user_phone = #{userPhone}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>yp_yuekao812</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>bwie-modules</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>bwie-user</module>
|
||||||
|
<module>bwie-product</module>
|
||||||
|
<module>bwie-es</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,113 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>yp_yuekao812</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>bwie-common</module>
|
||||||
|
<module>bwie-auth</module>
|
||||||
|
<module>bwie-gateway</module>
|
||||||
|
<module>bwie-modules</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<spring.cloud-version>2021.0.0</spring.cloud-version>
|
||||||
|
<spring.cloud.alibaba-version>2021.1</spring.cloud.alibaba-version>
|
||||||
|
<jwt.version>0.9.1</jwt.version>
|
||||||
|
<fastjson.version>1.2.80</fastjson.version>
|
||||||
|
<hutool.version>5.8.3</hutool.version>
|
||||||
|
<dysms.version>2.0.1</dysms.version>
|
||||||
|
<common.version>1.0-SNAPSHOT</common.version>
|
||||||
|
<druid.version>1.2.8</druid.version>
|
||||||
|
<mybatis.version>2.2.2</mybatis.version>
|
||||||
|
<pagehelper.version>1.4.1</pagehelper.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<!-- 规定SpringBoot版本 -->
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<version>2.6.2</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<!-- 依赖 -->
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<!-- SpringCloud -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring.cloud-version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloudAlibaba -->
|
||||||
|
<!-- SpringCloud Alibaba 微服务 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>${spring.cloud.alibaba-version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- JWT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
<version>${jwt.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Alibaba Fastjson -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>${fastjson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- hutool -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>${hutool.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 阿里大鱼 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>dysmsapi20170525</artifactId>
|
||||||
|
<version>${dysms.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 公共模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>bwie-common</artifactId>
|
||||||
|
<version>${common.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Druid -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
<version>${druid.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mybatis 依赖配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>${mybatis.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Pagehelper -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.pagehelper</groupId>
|
||||||
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
|
<version>${pagehelper.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
</project>
|
Loading…
Reference in New Issue