From 8da8e77c1542f212788f619f0c8d0f843e0361ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=E5=93=80?= <2076029107@qq.com> Date: Tue, 6 Aug 2024 19:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=A8=E8=80=8386?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 35 ++++++ bwie-auth/pom.xml | 27 ++++ .../java/com/bwie/auth/AuthApplication.java | 22 ++++ .../bwie/auth/controller/AuthController.java | 52 ++++++++ .../java/com/bwie/auth/feign/UserFeign.java | 23 ++++ .../auth/feign/factory/UserFeignFactory.java | 25 ++++ .../com/bwie/auth/service/AuthService.java | 23 ++++ .../auth/service/impl/AuthServiceImpl.java | 108 ++++++++++++++++ bwie-auth/src/main/resources/bootstrap.yml | 34 ++++++ bwie-common/pom.xml | 94 ++++++++++++++ .../com/bwie/common/constants/Constants.java | 18 +++ .../bwie/common/constants/JwtConstants.java | 29 +++++ .../bwie/common/constants/TokenConstants.java | 28 +++++ .../java/com/bwie/common/domain/Firm.java | 27 ++++ .../main/java/com/bwie/common/domain/Job.java | 23 ++++ .../java/com/bwie/common/domain/People.java | 30 +++++ .../java/com/bwie/common/domain/User.java | 28 +++++ .../com/bwie/common/result/PageResult.java | 38 ++++++ .../java/com/bwie/common/result/Result.java | 76 ++++++++++++ .../java/com/bwie/common/utils/JwtUtils.java | 109 +++++++++++++++++ .../com/bwie/common/utils/StringUtils.java | 68 +++++++++++ .../com/bwie/common/utils/TelSmsUtils.java | 87 +++++++++++++ bwie-gateway/pom.xml | 38 ++++++ .../com/bwie/gateway/GatewayApplication.java | 20 +++ .../gateway/config/IgnoreWhiteConfig.java | 32 +++++ .../bwie/gateway/filters/GatewayFilters.java | 79 ++++++++++++ .../com/bwie/gateway/utils/GatewayUtils.java | 98 +++++++++++++++ bwie-gateway/src/main/resources/bootstrap.yml | 33 +++++ bwie-modules/bwie-firm/pom.xml | 66 ++++++++++ .../java/com/bwie/firm/FirmApplication.java | 20 +++ .../firm/config/ConfirmCallbackConfig.java | 40 ++++++ .../bwie/firm/config/RabbitAdminConfig.java | 50 ++++++++ .../com/bwie/firm/config/RabbitmqConfig.java | 15 +++ .../firm/config/ReturnsCallbackConfig.java | 37 ++++++ .../bwie/firm/consumer/FirmConsumerBai.java | 55 +++++++++ .../bwie/firm/consumer/FirmConsumerGong.java | 54 ++++++++ .../bwie/firm/controller/FirmController.java | 49 ++++++++ .../java/com/bwie/firm/mapper/FirmMapper.java | 40 ++++++ .../com/bwie/firm/service/FirmService.java | 24 ++++ .../firm/service/impl/FirmServiceImpl.java | 115 ++++++++++++++++++ .../firm/util/GlobalExceptionHandler.java | 24 ++++ .../java/com/bwie/firm/utils/DLXQueue.java | 77 ++++++++++++ .../com/bwie/firm/utils/DelayedQueue.java | 79 ++++++++++++ .../java/com/bwie/firm/utils/TtlQueue.java | 66 ++++++++++ .../src/main/resources/bootstrap.yml | 50 ++++++++ .../src/main/resources/mappers/FirmMapper.xml | 71 +++++++++++ bwie-modules/bwie-message/pom.xml | 27 ++++ .../com/bwie/message/MessageApplication.java | 20 +++ .../src/main/resources/bootstrap.yml | 37 ++++++ bwie-modules/bwie-user/pom.xml | 59 +++++++++ .../java/com/bwie/user/UserApplication.java | 23 ++++ .../bwie/user/controller/UserController.java | 31 +++++ .../java/com/bwie/user/mapper/UserMapper.java | 17 +++ .../com/bwie/user/service/UserService.java | 16 +++ .../user/service/impl/UserServiceImpl.java | 29 +++++ .../src/main/resources/bootstrap.yml | 33 +++++ .../src/main/resources/mappers/UserMapper.xml | 15 +++ bwie-modules/pom.xml | 26 ++++ pom.xml | 113 +++++++++++++++++ 59 files changed, 2682 insertions(+) create mode 100644 .gitignore create mode 100644 bwie-auth/pom.xml create mode 100644 bwie-auth/src/main/java/com/bwie/auth/AuthApplication.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/feign/UserFeign.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/feign/factory/UserFeignFactory.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java create mode 100644 bwie-auth/src/main/resources/bootstrap.yml create mode 100644 bwie-common/pom.xml create mode 100644 bwie-common/src/main/java/com/bwie/common/constants/Constants.java create mode 100644 bwie-common/src/main/java/com/bwie/common/constants/JwtConstants.java create mode 100644 bwie-common/src/main/java/com/bwie/common/constants/TokenConstants.java create mode 100644 bwie-common/src/main/java/com/bwie/common/domain/Firm.java create mode 100644 bwie-common/src/main/java/com/bwie/common/domain/Job.java create mode 100644 bwie-common/src/main/java/com/bwie/common/domain/People.java create mode 100644 bwie-common/src/main/java/com/bwie/common/domain/User.java create mode 100644 bwie-common/src/main/java/com/bwie/common/result/PageResult.java create mode 100644 bwie-common/src/main/java/com/bwie/common/result/Result.java create mode 100644 bwie-common/src/main/java/com/bwie/common/utils/JwtUtils.java create mode 100644 bwie-common/src/main/java/com/bwie/common/utils/StringUtils.java create mode 100644 bwie-common/src/main/java/com/bwie/common/utils/TelSmsUtils.java create mode 100644 bwie-gateway/pom.xml create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/GatewayApplication.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/filters/GatewayFilters.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java create mode 100644 bwie-gateway/src/main/resources/bootstrap.yml create mode 100644 bwie-modules/bwie-firm/pom.xml create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/FirmApplication.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ConfirmCallbackConfig.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitAdminConfig.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitmqConfig.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ReturnsCallbackConfig.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerBai.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerGong.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/controller/FirmController.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/mapper/FirmMapper.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/FirmService.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/impl/FirmServiceImpl.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/util/GlobalExceptionHandler.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DLXQueue.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DelayedQueue.java create mode 100644 bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/TtlQueue.java create mode 100644 bwie-modules/bwie-firm/src/main/resources/bootstrap.yml create mode 100644 bwie-modules/bwie-firm/src/main/resources/mappers/FirmMapper.xml create mode 100644 bwie-modules/bwie-message/pom.xml create mode 100644 bwie-modules/bwie-message/src/main/java/com/bwie/message/MessageApplication.java create mode 100644 bwie-modules/bwie-message/src/main/resources/bootstrap.yml create mode 100644 bwie-modules/bwie-user/pom.xml create mode 100644 bwie-modules/bwie-user/src/main/java/com/bwie/user/UserApplication.java create mode 100644 bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java create mode 100644 bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java create mode 100644 bwie-modules/bwie-user/src/main/java/com/bwie/user/service/UserService.java create mode 100644 bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java create mode 100644 bwie-modules/bwie-user/src/main/resources/bootstrap.yml create mode 100644 bwie-modules/bwie-user/src/main/resources/mappers/UserMapper.xml create mode 100644 bwie-modules/pom.xml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..65f20b1 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/bwie-auth/pom.xml b/bwie-auth/pom.xml new file mode 100644 index 0000000..6267222 --- /dev/null +++ b/bwie-auth/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.bwie + yp_zhoukao86 + 1.0-SNAPSHOT + + + bwie-auth + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/bwie-auth/src/main/java/com/bwie/auth/AuthApplication.java b/bwie-auth/src/main/java/com/bwie/auth/AuthApplication.java new file mode 100644 index 0000000..42deaba --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/AuthApplication.java @@ -0,0 +1,22 @@ +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_zhoukao86 + * @name:AuthApplication + * @Date:2024/8/6 10:24 + */ +@EnableFeignClients +@SpringBootApplication +public class AuthApplication { + + public static void main(String[] args) { + SpringApplication.run(AuthApplication.class); + } + +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java b/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java new file mode 100644 index 0000000..29609e2 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java @@ -0,0 +1,52 @@ +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.*; + +/** + * @Author:杨鹏 + * @Package:com.bwie.auth.controller + * @Project:yp_zhoukao86 + * @name:AuthController + * @Date:2024/8/6 9:54 + */ +@Log4j2 +@RestController +@RequestMapping("auth") +public class AuthController { + + @Autowired + private AuthService authService; + + @PostMapping("getCode") + public Result getCode(@RequestBody User user){ + Result result = authService.getCode(user); + log.info("响应解决:{}",result); + return result; + } + + + @PostMapping("login") + public Result login(@RequestBody User user){ + Result result = authService.login(user); + log.info("响应解决:{}",result); + return result; + } + + @GetMapping("info") + public Result info( ){ + Result result = authService.info(); + log.info("响应解决:{}",result); + return result; + } + + @PostMapping("logout") + public void logout(){ + authService.logout(); + } + +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/feign/UserFeign.java b/bwie-auth/src/main/java/com/bwie/auth/feign/UserFeign.java new file mode 100644 index 0000000..9de7544 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/feign/UserFeign.java @@ -0,0 +1,23 @@ +package com.bwie.auth.feign; + +import com.bwie.common.domain.User; +import com.bwie.common.result.Result; +import org.springframework.cloud.openfeign.FallbackFactory; +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_zhoukao86 + * @name:UserFeign + * @Date:2024/8/6 10:02 + */ +@FeignClient("bwie-user") +public interface UserFeign{ + + @PostMapping("user/login") + public Result login(@RequestBody User user); + +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/feign/factory/UserFeignFactory.java b/bwie-auth/src/main/java/com/bwie/auth/feign/factory/UserFeignFactory.java new file mode 100644 index 0000000..23f7553 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/feign/factory/UserFeignFactory.java @@ -0,0 +1,25 @@ +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_zhoukao86 + * @name:UserFeignFactory + * @Date:2024/8/6 11:02 + */ +public class UserFeignFactory implements FallbackFactory { + @Override + public UserFeign create(Throwable cause) { + return new UserFeign() { + @Override + public Result login(User user) { + return Result.error(cause.getMessage()); + } + }; + } +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java b/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java new file mode 100644 index 0000000..8872cc4 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java @@ -0,0 +1,23 @@ +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_zhoukao86 +*@name:AuthService +*@Date:2024/8/6 9:55 +*/ +public interface AuthService { + Result login(User user); + + Result info(); + + void logout(); + + Result getCode(User user); + + +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java b/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java new file mode 100644 index 0000000..3dd6343 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java @@ -0,0 +1,108 @@ +package com.bwie.auth.service.impl; + +import cn.hutool.core.util.RandomUtil; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.client.naming.utils.RandomUtils; +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.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.Random; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * @Author:杨鹏 + * @Package:com.bwie.auth.service.impl + * @Project:yp_zhoukao86 + * @name:AuthServiceImpl + * @Date:2024/8/6 9:55 + */ +@Service +public class AuthServiceImpl implements AuthService { + + + @Autowired + private UserFeign userFeign; + + @Autowired + private HttpServletRequest request; + + @Autowired + private StringRedisTemplate redisTemplate; + + @Override + public Result getCode(User user) { + if (StringUtils.isAllEmpty(user.getUserPhone())){ + return Result.error("手机号不能为空"); + } + + Result login = userFeign.login(user); + if (login == null ){ + return Result.error("手机号不存在"); + } + int code = RandomUtils.nextInt(9999); + redisTemplate.opsForValue().set(TokenConstants.LOGIN_CODE_KEY+user.getUserPhone(), + JSONObject.toJSONString(code), + TokenConstants.REFRESH_TIME, + TimeUnit.MINUTES); + return Result.success(code,"验证码发送成功"); + } + + @Override + public Result login(User user) { + if (StringUtils.isAllEmpty(user.getUserPhone(),user.getCode())){ + return Result.error("手机号/验证码不能为空"); + } + + Result login = userFeign.login(user); + User data = login.getData(); + if (data == null ){ + return Result.error("手机号不存在"); + } + + String code = redisTemplate.opsForValue().get(TokenConstants.LOGIN_CODE_KEY + user.getUserPhone()); + + if (!user.getCode().equals(code)){ + return Result.error("验证码错"); + } + + String userKey = UUID.randomUUID().toString().replace("-", ""); + Map map = new HashMap<>(); + map.put(JwtConstants.USER_KEY,userKey); + String token = JwtUtils.createToken(map); + data.setToken(token); + + redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey,JSONObject.toJSONString(data), + TokenConstants.EXPIRATION, TimeUnit.MINUTES); + return Result.success(data); + } + + @Override + public Result info() { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + return Result.success(JSONObject.parseObject(s, User.class)); + } + + @Override + public void logout() { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY + userKey); + } + + +} diff --git a/bwie-auth/src/main/resources/bootstrap.yml b/bwie-auth/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..573174f --- /dev/null +++ b/bwie-auth/src/main/resources/bootstrap.yml @@ -0,0 +1,34 @@ +# Tomcat +server: + port: 9001 +# Spring +spring: + 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} + diff --git a/bwie-common/pom.xml b/bwie-common/pom.xml new file mode 100644 index 0000000..b0c44a1 --- /dev/null +++ b/bwie-common/pom.xml @@ -0,0 +1,94 @@ + + + 4.0.0 + + com.bwie + yp_zhoukao86 + 1.0-SNAPSHOT + + + bwie-common + + + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + io.jsonwebtoken + jjwt + + + + com.alibaba + fastjson + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.springframework.boot + spring-boot-starter-validation + + + + org.apache.commons + commons-lang3 + + + + org.projectlombok + lombok + + + + cn.hutool + hutool-all + + + + com.aliyun + dysmsapi20170525 + + + + com.github.tobato + fastdfs-client + 1.26.5 + + + + diff --git a/bwie-common/src/main/java/com/bwie/common/constants/Constants.java b/bwie-common/src/main/java/com/bwie/common/constants/Constants.java new file mode 100644 index 0000000..2fdc9fe --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constants/Constants.java @@ -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 = "操作异常"; +} diff --git a/bwie-common/src/main/java/com/bwie/common/constants/JwtConstants.java b/bwie-common/src/main/java/com/bwie/common/constants/JwtConstants.java new file mode 100644 index 0000000..03692c1 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constants/JwtConstants.java @@ -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"; + +} diff --git a/bwie-common/src/main/java/com/bwie/common/constants/TokenConstants.java b/bwie-common/src/main/java/com/bwie/common/constants/TokenConstants.java new file mode 100644 index 0000000..406298e --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constants/TokenConstants.java @@ -0,0 +1,28 @@ +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_tokens1:"; + /** + * 权限缓存前缀 + */ + public final static String LOGIN_CODE_KEY = "login_code1:"; + /** + * token标识 + */ + public static final String TOKEN = "token"; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/Firm.java b/bwie-common/src/main/java/com/bwie/common/domain/Firm.java new file mode 100644 index 0000000..52002bb --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/Firm.java @@ -0,0 +1,27 @@ +package com.bwie.common.domain; + +import lombok.Data; + +/** + * @Author:杨鹏 + * @Package:com.bwie.common.domain + * @Project:yp_zhoukao86 + * @name:Firm + * @Date:2024/8/6 9:41 + */ +@Data +public class Firm { + + private Long firmId; + private String firmJob; + private String firmJobDescription; + private String firmPriceMin; + private String firmPriceMax; + private Long jobId; + private String firmDate; + private String firmCount; + private String firmStatus; + + private String jobName; + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/Job.java b/bwie-common/src/main/java/com/bwie/common/domain/Job.java new file mode 100644 index 0000000..452c622 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/Job.java @@ -0,0 +1,23 @@ +package com.bwie.common.domain; + +import lombok.Data; + +/** + * @Author:杨鹏 + * @Package:com.bwie.common.domain + * @Project:yp_zhoukao86 + * @name:Job + * @Date:2024/8/6 9:44 + */ +@Data +public class Job { + + private String jobId; + private String jobName; + + private String userPriceMin; + private String userPriceMax; + + private String userDate; + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/People.java b/bwie-common/src/main/java/com/bwie/common/domain/People.java new file mode 100644 index 0000000..fb6bc5d --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/People.java @@ -0,0 +1,30 @@ +package com.bwie.common.domain; + + +import lombok.Data; + +/** + * @Author:杨鹏 + * @Package:com.bwie.common.domain + * @Project:yp_zhoukao86 + * @name:People + * @Date:2024/8/6 9:42 + */ +@Data +public class People { + + + private Long peopleId; + private Long firmId; + private Long userId; + private String peopleDate; + private String peopleStatus; + + private String firmJob; + private String userName; + private String userPhone; + private String userJob; + private String userPriceMin; + private String userPriceMax; + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/User.java b/bwie-common/src/main/java/com/bwie/common/domain/User.java new file mode 100644 index 0000000..e5971fc --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/User.java @@ -0,0 +1,28 @@ +package com.bwie.common.domain; + +import lombok.Data; + +/** + * @Author:杨鹏 + * @Package:com.bwie.common.domain + * @Project:yp_zhoukao86 + * @name:User + * @Date:2024/8/6 9:39 + */ +@Data +public class User { + + private Long userId; + private String userName; + private String userPhone; + private String userJob; + private String userCount; + private String userPriceMin; + private String userPriceMax; + + private String userDate; + + private String code; + private String token; + +} diff --git a/bwie-common/src/main/java/com/bwie/common/result/PageResult.java b/bwie-common/src/main/java/com/bwie/common/result/PageResult.java new file mode 100644 index 0000000..e284263 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/result/PageResult.java @@ -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 implements Serializable { + /** + * 总条数 + */ + private long total; + /** + * 结果集合 + */ + private List list; + + public PageResult() { + } + + public PageResult(long total, List list) { + this.total = total; + this.list = list; + } + + public static PageResult toPageResult(long total, List list) { + return new PageResult(total, list); + } + + public static Result> toResult(long total, List list) { + return Result.success(PageResult.toPageResult(total, list)); + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/result/Result.java b/bwie-common/src/main/java/com/bwie/common/result/Result.java new file mode 100644 index 0000000..30b1e73 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/result/Result.java @@ -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 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 Result success() { + return restResult(null, SUCCESS, Constants.SUCCESS_MSG); + } + + public static Result success(T data) { + return restResult(data, SUCCESS, Constants.SUCCESS_MSG); + } + + public static Result success(T data, String msg) { + return restResult(data, SUCCESS, msg); + } + + public static Result error() { + return restResult(null, FAIL, Constants.ERROR_MSG); + } + + public static Result error(String msg) { + return restResult(null, FAIL, msg); + } + + public static Result error(T data) { + return restResult(data, FAIL, Constants.ERROR_MSG); + } + + public static Result error(T data, String msg) { + return restResult(data, FAIL, msg); + } + + public static Result error(int code, String msg) { + return restResult(null, code, msg); + } + + private static Result restResult(T data, int code, String msg) { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/utils/JwtUtils.java b/bwie-common/src/main/java/com/bwie/common/utils/JwtUtils.java new file mode 100644 index 0000000..f560aa9 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/JwtUtils.java @@ -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 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(); + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/utils/StringUtils.java b/bwie-common/src/main/java/com/bwie/common/utils/StringUtils.java new file mode 100644 index 0000000..93c47fd --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/StringUtils.java @@ -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 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); + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/utils/TelSmsUtils.java b/bwie-common/src/main/java/com/bwie/common/utils/TelSmsUtils.java new file mode 100644 index 0000000..49e8c81 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/TelSmsUtils.java @@ -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 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()); + } + +} diff --git a/bwie-gateway/pom.xml b/bwie-gateway/pom.xml new file mode 100644 index 0000000..a11150c --- /dev/null +++ b/bwie-gateway/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.bwie + yp_zhoukao86 + 1.0-SNAPSHOT + + + bwie-gateway + + + + + com.bwie + bwie-common + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + com.alibaba.cloud + spring-cloud-alibaba-sentinel-gateway + + + + com.alibaba.csp + sentinel-spring-cloud-gateway-adapter + + + + diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/GatewayApplication.java b/bwie-gateway/src/main/java/com/bwie/gateway/GatewayApplication.java new file mode 100644 index 0000000..8f5fde7 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/GatewayApplication.java @@ -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_zhoukao86 + * @name:GatewayApplication + * @Date:2024/8/6 10:29 + */ +@SpringBootApplication +public class GatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class); + } + +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java b/bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java new file mode 100644 index 0000000..8462d82 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java @@ -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 whites = new ArrayList<>(); + + public void setWhites(List whites) { + log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites)); + this.whites = whites; + } +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/filters/GatewayFilters.java b/bwie-gateway/src/main/java/com/bwie/gateway/filters/GatewayFilters.java new file mode 100644 index 0000000..36c8553 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/filters/GatewayFilters.java @@ -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 filter(ServerWebExchange exchange, GatewayFilterChain chain) { + List 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 annotationType() { + return null; + } + + +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java b/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java new file mode 100644 index 0000000..7b789e5 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java @@ -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 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 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)); + } + + +} diff --git a/bwie-gateway/src/main/resources/bootstrap.yml b/bwie-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..ffb6927 --- /dev/null +++ b/bwie-gateway/src/main/resources/bootstrap.yml @@ -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} diff --git a/bwie-modules/bwie-firm/pom.xml b/bwie-modules/bwie-firm/pom.xml new file mode 100644 index 0000000..6b7b541 --- /dev/null +++ b/bwie-modules/bwie-firm/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0-SNAPSHOT + + + bwie-firm + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba + druid-spring-boot-starter + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + + + com.github.pagehelper + pagehelper-spring-boot-starter + + + + com.sun.mail + javax.mail + 1.5.6 + + + + com.aliyun.oss + aliyun-sdk-oss + 3.12.0 + + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/FirmApplication.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/FirmApplication.java new file mode 100644 index 0000000..d622d68 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/FirmApplication.java @@ -0,0 +1,20 @@ +package com.bwie.firm; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @Author:杨鹏 + * @Package:com.bwie.firm + * @Project:yp_zhoukao86 + * @name:FirmApplication + * @Date:2024/8/6 11:01 + */ +@SpringBootApplication +public class FirmApplication { + + public static void main(String[] args) { + SpringApplication.run(FirmApplication.class); + } + +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ConfirmCallbackConfig.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ConfirmCallbackConfig.java new file mode 100644 index 0000000..e157178 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ConfirmCallbackConfig.java @@ -0,0 +1,40 @@ +package com.bwie.firm.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); + } + } + +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitAdminConfig.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitAdminConfig.java new file mode 100644 index 0000000..54a2201 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitAdminConfig.java @@ -0,0 +1,50 @@ +package com.bwie.firm.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; + } +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitmqConfig.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitmqConfig.java new file mode 100644 index 0000000..7963c73 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/RabbitmqConfig.java @@ -0,0 +1,15 @@ +package com.bwie.firm.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(); + } +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ReturnsCallbackConfig.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ReturnsCallbackConfig.java new file mode 100644 index 0000000..43b1e2e --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/config/ReturnsCallbackConfig.java @@ -0,0 +1,37 @@ +package com.bwie.firm.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 回退了所有的信息,可做补偿机制 + } +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerBai.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerBai.java new file mode 100644 index 0000000..5e4ca7f --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerBai.java @@ -0,0 +1,55 @@ +package com.bwie.firm.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.firm.consumer + * @Project:yp_zhoukao86 + * @name:FirmConsumer + * @Date:2024/8/6 15:34 + */ +@Log4j2 +@Component +public class FirmConsumerBai { + + @Autowired + private StringRedisTemplate redisTemplate; + + private static final String INDEX_SET = "INDEX_SET"; + + @RabbitListener(queuesToDeclare = {@Queue("INDEX_MQ_BAI")}) + public void firmConsumerBai(String userPhone, Message message, Channel channel){ + + //iv. 考虑消息队列中消息重复消费以及消息丢失问题(5分) + try { + String messageId = message.getMessageProperties().getMessageId(); + Long add = redisTemplate.opsForSet().add(INDEX_SET, messageId); + if (add == 1){ + log.info("消费者开始消费,消费内容为{},",userPhone); + log.info("{}",userPhone); + log.info("消费者消费成功,消费内容为{},",userPhone); + channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); + } + } catch (Exception e) { + log.info("报错为:{}",e); + try { + channel.basicReject(message.getMessageProperties().getDeliveryTag(),true); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + + } + +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerGong.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerGong.java new file mode 100644 index 0000000..edce0d3 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/consumer/FirmConsumerGong.java @@ -0,0 +1,54 @@ +package com.bwie.firm.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.firm.consumer + * @Project:yp_zhoukao86 + * @name:FirmConsumer + * @Date:2024/8/6 15:34 + */ +@Log4j2 +@Component +public class FirmConsumerGong { + + @Autowired + private StringRedisTemplate redisTemplate; + + private static final String INDEX_SET = "INDEX_SET_GONG"; + + @RabbitListener(queuesToDeclare = {@Queue("INDEX_MQ_GONG")}) + public void firmConsumerBai(String userPhone, Message message, Channel channel){ + //iv. 考虑消息队列中消息重复消费以及消息丢失问题(5分) + try { + String messageId = message.getMessageProperties().getMessageId(); + Long add = redisTemplate.opsForSet().add(INDEX_SET, messageId); + if (add == 1){ + log.info("消费者开始消费,消费内容为{},",userPhone); + log.info("{}",userPhone); + log.info("消费者消费成功,消费内容为{},",userPhone); + channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); + } + } catch (Exception e) { + log.info("报错为:{}",e); + try { + channel.basicReject(message.getMessageProperties().getDeliveryTag(),true); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + + } + +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/controller/FirmController.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/controller/FirmController.java new file mode 100644 index 0000000..a8025be --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/controller/FirmController.java @@ -0,0 +1,49 @@ +package com.bwie.firm.controller; + +import com.bwie.common.domain.Firm; +import com.bwie.common.domain.People; +import com.bwie.common.result.Result; +import com.bwie.firm.service.FirmService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +/** + * @Author:杨鹏 + * @Package:com.bwie.user.controller + * @Project:yp_zhoukao86 + * @name:FirmController + * @Date:2024/8/6 10:39 + */ +@RestController +@RequestMapping("firm") +public class FirmController { + + @Autowired + private FirmService firmService; + + @PostMapping("firmList") + public Result firmList(@RequestBody Firm firm){ + return firmService.firmList(firm); + } + + @PostMapping("peopleList") + public Result peopleList(@RequestBody People people){ + return firmService.peopleList(people); + } + + @PostMapping("peopleAdd") + public Result peopleAdd(@RequestBody People people){ + return firmService.peopleAdd(people); + } + + @PostMapping("peopleUpd") + public Result peopleUpd(@RequestBody People people){ + return firmService.peopleUpd(people); + } + + @GetMapping("jobList") + public Result jobList(){ + return firmService.jobList(); + } + +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/mapper/FirmMapper.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/mapper/FirmMapper.java new file mode 100644 index 0000000..e59b48d --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/mapper/FirmMapper.java @@ -0,0 +1,40 @@ +package com.bwie.firm.mapper; + +import com.bwie.common.domain.Firm; +import com.bwie.common.domain.Job; +import com.bwie.common.domain.People; +import com.bwie.common.domain.User; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * @Author:杨鹏 + * @Package:com.bwie.user.mapper + * @Project:yp_zhoukao86 + * @name:FirmMapper + * @Date:2024/8/6 10:41 + */ +@Mapper +public interface FirmMapper { + + + List firmList(Firm firm); + + List peopleList(People people); + + List jobList(); + + + People listPeopleId(People people); + + void peopleAdd(People people); + + void updFirmCount(Long firmId); + + void updUserCount(Long userId); + + void updPeopleStatus(People people); + + User listUserId(Long userId); +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/FirmService.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/FirmService.java new file mode 100644 index 0000000..481f6ef --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/FirmService.java @@ -0,0 +1,24 @@ +package com.bwie.firm.service; + +import com.bwie.common.domain.Firm; +import com.bwie.common.domain.People; +import com.bwie.common.result.Result; + +/** + * @Author:杨鹏 + * @Package:com.bwie.user.service + * @Project:yp_zhoukao86 + * @name:FirmService + * @Date:2024/8/6 10:40 + */ +public interface FirmService { + Result firmList(Firm firm); + + Result peopleList(People people); + + Result peopleAdd(People people); + + Result peopleUpd(People people); + + Result jobList(); +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/impl/FirmServiceImpl.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/impl/FirmServiceImpl.java new file mode 100644 index 0000000..141ee39 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/service/impl/FirmServiceImpl.java @@ -0,0 +1,115 @@ +package com.bwie.firm.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.bwie.common.constants.TokenConstants; +import com.bwie.common.domain.Firm; +import com.bwie.common.domain.Job; +import com.bwie.common.domain.People; +import com.bwie.common.domain.User; +import com.bwie.common.result.Result; +import com.bwie.common.utils.JwtUtils; +import com.bwie.firm.mapper.FirmMapper; +import com.bwie.firm.service.FirmService; +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 org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * @Author:杨鹏 + * @Package:com.bwie.user.service.impl + * @Project:yp_zhoukao86 + * @name:FirmServiceImpl + * @Date:2024/8/6 10:40 + */ +@Service +public class FirmServiceImpl implements FirmService { + private static final String LIST_JOB = "job"; + + @Autowired + private FirmMapper firmMapper; + + @Autowired + private HttpServletRequest request; + + @Autowired + private RabbitTemplate rabbitTemplate; + + @Autowired + private StringRedisTemplate redisTemplate; + + @Override + public Result firmList(Firm firm) { + List firmList = firmMapper.firmList(firm); + return Result.success(firmList); + } + + @Override + public Result peopleList(People people) { + List peopleList = firmMapper.peopleList(people); + return Result.success(peopleList); + } + + @Transactional + @Override + public Result peopleAdd(People people) { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + User user = JSONObject.parseObject(s, User.class); + people.setUserId(user.getUserId()); + + //g) 点击投个简历按钮执行以下逻辑 + People people1 = firmMapper.listPeopleId(people); + if (people1 == null){ + //i. 在该岗位的候选人列表中插入一条记录(2分) + firmMapper.peopleAdd(people); + //ii. 插入成功后,更新企业招聘列表中的候选人数加一(2分) + firmMapper.updFirmCount(people.getFirmId()); + //iii. 更新个人简历表中的已投递岗位数量加一,最近投递时间列更新为系统当前时间(3分) + firmMapper.updUserCount(people.getUserId()); + return Result.success(); + } + //iv. 如果该岗位当前登录用户已投递,则提示用户:该岗位您已投递过,请耐心等待!(5分) + return Result.error("该岗位您已投递过,请耐心等待!"); + } + + @Override + public Result peopleUpd(People people) { + //1. 把该条记录的状态改为已通过(2分) + //1. 把该条记录的状态改为不通过(2分) + firmMapper.updPeopleStatus(people); + User user = firmMapper.listUserId(people.getUserId()); + if (people.getPeopleStatus().equals("1")){ + //2. 通过中间件异步调用消息服务发送短信给候选人,提示:恭喜你,你的简历已通过,请前来面试!(注意:在消费者打印短信内容并且有演示,短信内容自拟,能够成功发送即可,否则0分)(3分) + rabbitTemplate.convertAndSend("INDEX_MQ_GONG",user.getUserPhone(),message -> { + message.getMessageProperties().setMessageId(UUID.randomUUID().toString()); + return message; + }); + } + //2. 通过中间件异步调用消息服务发送短信给候选人,提示:很遗憾,你的简历不符合岗位要求,请再接再厉!(注意:在消费者打印短信内容并且有演示,短信内容自拟,能够成功发送即可,否则0分)(3分) + rabbitTemplate.convertAndSend("INDEX_MQ_BAI",user.getUserPhone(),message -> { + message.getMessageProperties().setMessageId(UUID.randomUUID().toString()); + return message; + }); + return Result.success(); + } + + @Override + public Result jobList() { + String s = redisTemplate.opsForValue().get(LIST_JOB); + if (s == null){ + return Result.success(JSONObject.parseObject(s, Job.class)); + } + List jobList = firmMapper.jobList(); + redisTemplate.opsForValue().set(LIST_JOB,JSONObject.toJSONString(jobList), + TokenConstants.REFRESH_TIME, TimeUnit.MINUTES); + return Result.success(jobList); + } +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/util/GlobalExceptionHandler.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/util/GlobalExceptionHandler.java new file mode 100644 index 0000000..167a965 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/util/GlobalExceptionHandler.java @@ -0,0 +1,24 @@ +package com.bwie.firm.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 exceptionHandler(HttpServletRequest request, Exception e){//处理异常方法 + Map map=new HashMap(); + map.put("errorCode","101"); + map.put("errorMsg","已捕获到全局异常,系统错误!"); + map.put("requestURL", request.getRequestURL().toString()); // 获取请求的URL + map.put("exception", e.getMessage()); // 获取异常的消息(可能不是具体原因) + return map; + } +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DLXQueue.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DLXQueue.java new file mode 100644 index 0000000..eaa7c5e --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DLXQueue.java @@ -0,0 +1,77 @@ +package com.bwie.firm.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 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); + } +} diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DelayedQueue.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DelayedQueue.java new file mode 100644 index 0000000..ffb67b7 --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/DelayedQueue.java @@ -0,0 +1,79 @@ +package com.bwie.firm.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 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); + } + +} + diff --git a/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/TtlQueue.java b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/TtlQueue.java new file mode 100644 index 0000000..9a65dfd --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/java/com/bwie/firm/utils/TtlQueue.java @@ -0,0 +1,66 @@ +package com.bwie.firm.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 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); + } +} + diff --git a/bwie-modules/bwie-firm/src/main/resources/bootstrap.yml b/bwie-modules/bwie-firm/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..c8ed05c --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/resources/bootstrap.yml @@ -0,0 +1,50 @@ +# Tomcat +server: + port: 10009 +# 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) + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + 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} diff --git a/bwie-modules/bwie-firm/src/main/resources/mappers/FirmMapper.xml b/bwie-modules/bwie-firm/src/main/resources/mappers/FirmMapper.xml new file mode 100644 index 0000000..c95f13b --- /dev/null +++ b/bwie-modules/bwie-firm/src/main/resources/mappers/FirmMapper.xml @@ -0,0 +1,71 @@ + + + + + + INSERT INTO `yp_zhoukao86`.`people` ( `firm_id`, `user_id`, `people_date`) + VALUES (#{firmId}, #{userId}, now()); + + + update firm set firm_count = firm_count + 1 where firm_id = #{firmId} + + + UPDATE `yp_zhoukao86`.`user` + SET + `user_count` = user_count + 1, + `user_date` = now() + WHERE + `user_id` = #{userId} + + + update people set people_status = #{peopleStatus} where people_id = #{peopleId} + + + + + + + + + + + diff --git a/bwie-modules/bwie-message/pom.xml b/bwie-modules/bwie-message/pom.xml new file mode 100644 index 0000000..2d3944d --- /dev/null +++ b/bwie-modules/bwie-message/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0-SNAPSHOT + + + bwie-message + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/bwie-modules/bwie-message/src/main/java/com/bwie/message/MessageApplication.java b/bwie-modules/bwie-message/src/main/java/com/bwie/message/MessageApplication.java new file mode 100644 index 0000000..00fb7a2 --- /dev/null +++ b/bwie-modules/bwie-message/src/main/java/com/bwie/message/MessageApplication.java @@ -0,0 +1,20 @@ +package com.bwie.message; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @Author:杨鹏 + * @Package:com.bwie.message + * @Project:yp_zhoukao86 + * @name:MessageApplication + * @Date:2024/8/6 15:45 + */ +@SpringBootApplication +public class MessageApplication { + + public static void main(String[] args) { + SpringApplication.run(MessageApplication.class); + } + +} diff --git a/bwie-modules/bwie-message/src/main/resources/bootstrap.yml b/bwie-modules/bwie-message/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..0459e1c --- /dev/null +++ b/bwie-modules/bwie-message/src/main/resources/bootstrap.yml @@ -0,0 +1,37 @@ +# Tomcat +server: + port: 10008 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-message + 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 diff --git a/bwie-modules/bwie-user/pom.xml b/bwie-modules/bwie-user/pom.xml new file mode 100644 index 0000000..fc926ca --- /dev/null +++ b/bwie-modules/bwie-user/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0-SNAPSHOT + + + bwie-user + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba + druid-spring-boot-starter + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + + + com.github.pagehelper + pagehelper-spring-boot-starter + + + + com.sun.mail + javax.mail + 1.5.6 + + + + com.aliyun.oss + aliyun-sdk-oss + 3.12.0 + + + + diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/UserApplication.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/UserApplication.java new file mode 100644 index 0000000..a05e499 --- /dev/null +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/UserApplication.java @@ -0,0 +1,23 @@ +package com.bwie.user; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @Author:杨鹏 + * @Package:com.bwie.user + * @Project:yp_zhoukao86 + * @name:UserApplication + * @Date:2024/8/6 9:53 + */ +@SpringBootApplication +public class UserApplication { + + public static void main(String[] args) { + + SpringApplication.run(UserApplication.class); + + } + +} + diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java new file mode 100644 index 0000000..a6b6681 --- /dev/null +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java @@ -0,0 +1,31 @@ +package com.bwie.user.controller; + +import com.bwie.user.service.UserService; +import com.bwie.common.domain.User; +import com.bwie.common.result.Result; +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.book.controller + * @Project:yp_zhoukao86 + * @name:UserController + * @Date:2024/8/6 9:48 + */ +@RestController +@RequestMapping("user") +public class UserController { + + @Autowired + private UserService userService; + + @PostMapping("login") + public Result login(@RequestBody User user){ + return userService.login(user); + } + +} diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java new file mode 100644 index 0000000..e651917 --- /dev/null +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java @@ -0,0 +1,17 @@ +package com.bwie.user.mapper; + +import com.bwie.common.domain.User; +import org.apache.ibatis.annotations.Mapper; + +/** + * @Author:杨鹏 + * @Package:com.bwie.book.mapper + * @Project:yp_zhoukao86 + * @name:UserMapper + * @Date:2024/8/6 9:46 + */ +@Mapper +public interface UserMapper { + User login(String userPhone); + +} diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/UserService.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/UserService.java new file mode 100644 index 0000000..95dd406 --- /dev/null +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/UserService.java @@ -0,0 +1,16 @@ +package com.bwie.user.service; + +import com.bwie.common.domain.User; +import com.bwie.common.result.Result; + +/** + * @Author:杨鹏 + * @Package:com.bwie.book.service + * @Project:yp_zhoukao86 + * @name:UserMapper + * @Date:2024/8/6 9:47 + */ +public interface UserService { + Result login(User user); + +} diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..e545fab --- /dev/null +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java @@ -0,0 +1,29 @@ +package com.bwie.user.service.impl; + +import com.bwie.user.mapper.UserMapper; +import com.bwie.user.service.UserService; +import com.bwie.common.domain.User; +import com.bwie.common.result.Result; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @Author:杨鹏 + * @Package:com.bwie.book.service.impl + * @Project:yp_zhoukao86 + * @name:UserMapperImpl + * @Date:2024/8/6 9:46 + */ +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserMapper userMapper; + + + @Override + public Result login(User user) { + User user1 = userMapper.login(user.getUserPhone()); + return Result.success(user1); + } +} diff --git a/bwie-modules/bwie-user/src/main/resources/bootstrap.yml b/bwie-modules/bwie-user/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..f1e4087 --- /dev/null +++ b/bwie-modules/bwie-user/src/main/resources/bootstrap.yml @@ -0,0 +1,33 @@ +# Tomcat +server: + port: 10007 +# 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} diff --git a/bwie-modules/bwie-user/src/main/resources/mappers/UserMapper.xml b/bwie-modules/bwie-user/src/main/resources/mappers/UserMapper.xml new file mode 100644 index 0000000..deb138b --- /dev/null +++ b/bwie-modules/bwie-user/src/main/resources/mappers/UserMapper.xml @@ -0,0 +1,15 @@ + + + + + + + diff --git a/bwie-modules/pom.xml b/bwie-modules/pom.xml new file mode 100644 index 0000000..4cd675a --- /dev/null +++ b/bwie-modules/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + com.bwie + yp_zhoukao86 + 1.0-SNAPSHOT + + + bwie-modules + pom + + bwie-user + bwie-firm + bwie-message + + + + 17 + 17 + UTF-8 + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..dfd4380 --- /dev/null +++ b/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + + com.bwie + yp_zhoukao86 + 1.0-SNAPSHOT + pom + + bwie-common + bwie-gateway + bwie-auth + bwie-modules + + + + 8 + 8 + UTF-8 + 2021.0.0 + 2021.1 + 0.9.1 + 1.2.80 + 5.8.3 + 2.0.1 + 1.0-SNAPSHOT + 1.2.8 + 2.2.2 + 1.4.1 + + + + + spring-boot-starter-parent + org.springframework.boot + 2.6.2 + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud-version} + pom + import + + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring.cloud.alibaba-version} + pom + import + + + + io.jsonwebtoken + jjwt + ${jwt.version} + + + + com.alibaba + fastjson + ${fastjson.version} + + + + cn.hutool + hutool-all + ${hutool.version} + + + + com.aliyun + dysmsapi20170525 + ${dysms.version} + + + + com.bwie + bwie-common + ${common.version} + + + + com.alibaba + druid-spring-boot-starter + ${druid.version} + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.version} + + + + com.github.pagehelper + pagehelper-spring-boot-starter + ${pagehelper.version} + + + + +