commit ac8659ec7cab783a70c13521ce3066b1807eea77 Author: jia <2744404105@qq.com> Date: Mon Feb 26 09:38:15 2024 +0800 月考5(2) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.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 \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..3f8d602 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..c32584c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c3f3b0a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bwie-auth/pom.xml b/bwie-auth/pom.xml new file mode 100644 index 0000000..542730c --- /dev/null +++ b/bwie-auth/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.bwie + test_month + 1.0.0 + + + bwie-auth + + + 17 + 17 + UTF-8 + + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + diff --git a/bwie-auth/src/main/java/com/bwie/auth/AuthApp.java b/bwie-auth/src/main/java/com/bwie/auth/AuthApp.java new file mode 100644 index 0000000..d513965 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/AuthApp.java @@ -0,0 +1,17 @@ +package com.bwie.auth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) +@EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.bwie.**") + +public class AuthApp { + public static void main(String[] args) { + SpringApplication.run(AuthApp.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..9f4b05c --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java @@ -0,0 +1,30 @@ +package com.bwie.auth.controller; + +import com.bwie.auth.service.AuthService; +import com.bwie.common.domain.request.LoginRequest; +import com.bwie.common.domain.response.Info; +import com.bwie.common.domain.response.JwtResponse; +import com.bwie.common.result.Result; +import org.springframework.web.bind.annotation.*; + +@RestController +@ResponseBody +public class AuthController { + private final AuthService authService; + + public AuthController(AuthService authService) { + this.authService = authService; + } + + @PostMapping("login") + public Result login(@RequestBody LoginRequest loginRequest){ + JwtResponse login = authService.login(loginRequest); + return Result.success(login); + } + @GetMapping("info") + public Result info(){ + Info info = authService.info(); + return Result.success(info); + } + +} 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..5a2fb87 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java @@ -0,0 +1,10 @@ +package com.bwie.auth.service; + +import com.bwie.common.domain.request.LoginRequest; +import com.bwie.common.domain.response.Info; +import com.bwie.common.domain.response.JwtResponse; + +public interface AuthService { + JwtResponse login(LoginRequest loginRequest); + Info info(); +} 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..c4eac61 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java @@ -0,0 +1,66 @@ +package com.bwie.auth.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.bwie.auth.service.AuthService; +import com.bwie.common.constant.JwtConstants; +import com.bwie.common.constant.TokenConstants; +import com.bwie.common.domain.User; +import com.bwie.common.domain.request.LoginRequest; +import com.bwie.common.domain.response.Info; +import com.bwie.common.domain.response.JwtResponse; +import com.bwie.common.redis.RedisCache; +import com.bwie.common.remote.user.UserRemoteService; +import com.bwie.common.result.Result; +import com.bwie.common.utils.IdUtils; +import com.bwie.common.utils.JwtUtils; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.concurrent.TimeUnit; + +@Service +public class AuthServiceImpl implements AuthService { + private final UserRemoteService userRemoteService; + private final RedisCache redisCache; + private final HttpServletRequest request; + + public AuthServiceImpl(UserRemoteService userRemoteService, RedisCache redisCache, HttpServletRequest request) { + this.userRemoteService = userRemoteService; + this.redisCache = redisCache; + this.request = request; + } + + + @Override + public JwtResponse login(LoginRequest loginRequest) { + Result findname = userRemoteService.findname(loginRequest.getUserName()); + Assert.isTrue(findname.isSuccess(),"登陆失败"); + User data = findname.getData(); + Assert.notNull(data,"用户不存在"); + Assert.isTrue(data.getUserPwd().equals(loginRequest.getUserPwd()),"密码不正确"); + + HashMap map = new HashMap<>(); + String key = IdUtils.genId(); + map.put(JwtConstants.USER_KEY,key); + String token = JwtUtils.createToken(map); + redisCache.setCacheObject(TokenConstants.LOGIN_TOKEN_KEY+key,data,TokenConstants.EXPIRATION, TimeUnit.SECONDS); + + return JwtResponse.builder() + .expireTime(TokenConstants.EXPIRATION) + .token(token) + .build(); + } + + @Override + public Info info() { + User cacheObject = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + request.getHeader(JwtConstants.USER_KEY)); + + return Info.builder() + .userPhone(cacheObject.getUserPhone()) + .userId(cacheObject.getUserId()) + .userName(cacheObject.getUserName()) + .userRole(cacheObject.getUserRole()) + .build(); + } +} diff --git a/bwie-auth/src/main/resources/bootstrap.yml b/bwie-auth/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..770cce2 --- /dev/null +++ b/bwie-auth/src/main/resources/bootstrap.yml @@ -0,0 +1,31 @@ +# Tomcat +server: + port: 9001 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: 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: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + 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..135ddd8 --- /dev/null +++ b/bwie-common/pom.xml @@ -0,0 +1,114 @@ + + + 4.0.0 + + com.bwie + test_month + 1.0.0 + + + bwie-common + + + 17 + 17 + UTF-8 + + + + + 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 + 0.9.1 + + + + com.alibaba + fastjson + 1.2.80 + + + + 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 + 5.8.3 + + + + com.aliyun + dysmsapi20170525 + 2.0.1 + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.4.1 + + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.42 + + + diff --git a/bwie-common/src/main/java/com/bwie/common/config/FastJson2JsonRedisSerializer.java b/bwie-common/src/main/java/com/bwie/common/config/FastJson2JsonRedisSerializer.java new file mode 100644 index 0000000..584566b --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/config/FastJson2JsonRedisSerializer.java @@ -0,0 +1,48 @@ +package com.bwie.common.config; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.filter.Filter; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * Redis使用FastJson序列化 + * + * @author ruoyi + */ +public class FastJson2JsonRedisSerializer implements RedisSerializer { + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + + static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter( + "org.springframework", "com"); + + private Class clazz; + + public FastJson2JsonRedisSerializer(Class clazz) { + super(); + this.clazz = clazz; + } + + @Override + public byte[] serialize (T t) throws SerializationException { + if (t == null) { + return new byte[0]; + } + return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET); + } + + @Override + public T deserialize (byte[] bytes) throws SerializationException { + if (bytes == null || bytes.length <= 0) { + return null; + } + String str = new String(bytes, DEFAULT_CHARSET); + + return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/config/MybatisPlusConfig.java b/bwie-common/src/main/java/com/bwie/common/config/MybatisPlusConfig.java new file mode 100644 index 0000000..1983b7e --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/config/MybatisPlusConfig.java @@ -0,0 +1,30 @@ +package com.bwie.common.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author DongZl + * @description: Mybatis-Plus配置 + * @Date 2024-1-10 下午 05:12 + */ +@Configuration +public class MybatisPlusConfig { + + public MybatisPlusConfig () { + System.out.println("初始化-----------"); + } + + /** + * 添加分页插件 + */ + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); + return interceptor; + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/config/RedisConfig.java b/bwie-common/src/main/java/com/bwie/common/config/RedisConfig.java new file mode 100644 index 0000000..5d3ecc0 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/config/RedisConfig.java @@ -0,0 +1,65 @@ +package com.bwie.common.config; + +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.script.DefaultRedisScript; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * redis配置 + * + * @author ruoyi + */ +@Configuration +@EnableCaching +public class RedisConfig extends CachingConfigurerSupport { + @Bean + @SuppressWarnings(value = {"unchecked", "rawtypes"}) + public RedisTemplate redisTemplate (RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + + FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); + + // 使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(serializer); + + // Hash的key也采用StringRedisSerializer的序列化方式 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(serializer); + + template.afterPropertiesSet(); + return template; + } + + @Bean + public DefaultRedisScript limitScript () { + DefaultRedisScript redisScript = new DefaultRedisScript<>(); + redisScript.setScriptText(limitScriptText()); + redisScript.setResultType(Long.class); + return redisScript; + } + + /** + * 限流脚本 + */ + private String limitScriptText () { + return "local key = KEYS[1]\n" + + "local count = tonumber(ARGV[1])\n" + + "local time = tonumber(ARGV[2])\n" + + "local current = redis.call('get', key);\n" + + "if current and tonumber(current) > count then\n" + + " return tonumber(current);\n" + + "end\n" + + "current = redis.call('incr', key)\n" + + "if tonumber(current) == 1 then\n" + + " redis.call('expire', key, time)\n" + + "end\n" + + "return tonumber(current);"; + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/constant/Constants.java b/bwie-common/src/main/java/com/bwie/common/constant/Constants.java new file mode 100644 index 0000000..9dd5a4e --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constant/Constants.java @@ -0,0 +1,18 @@ +package com.bwie.common.constant; + +/** + * @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/constant/JwtConstants.java b/bwie-common/src/main/java/com/bwie/common/constant/JwtConstants.java new file mode 100644 index 0000000..9def83c --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constant/JwtConstants.java @@ -0,0 +1,27 @@ +package com.bwie.common.constant; + +/** + * @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/constant/ServerNameConstants.java b/bwie-common/src/main/java/com/bwie/common/constant/ServerNameConstants.java new file mode 100644 index 0000000..f5e62e8 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constant/ServerNameConstants.java @@ -0,0 +1,9 @@ +package com.bwie.common.constant; + +public class ServerNameConstants { + + public final static String SYSTEM_NAME="bwie-system"; + public final static String ES_NAME="bwie-es"; + public final static String GOODS_NAME="bwie-goods"; + public final static String MQ_NAME="bwie-mq"; +} diff --git a/bwie-common/src/main/java/com/bwie/common/constant/TokenConstants.java b/bwie-common/src/main/java/com/bwie/common/constant/TokenConstants.java new file mode 100644 index 0000000..7e81bda --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/constant/TokenConstants.java @@ -0,0 +1,24 @@ +package com.bwie.common.constant; + +/** + * @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"; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/Car.java b/bwie-common/src/main/java/com/bwie/common/domain/Car.java new file mode 100644 index 0000000..14e7bd2 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/Car.java @@ -0,0 +1,28 @@ +package com.bwie.common.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@TableName(value = "car") +public class Car { + @TableId(value = "car_id",type = IdType.AUTO) + private Long carId; + @TableField(value = "user_id") + private Long userId; + @TableField(value = "goods_id") + private Long goodsId; + @TableField(value = "car_num") + private Integer carNum; + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/Goods.java b/bwie-common/src/main/java/com/bwie/common/domain/Goods.java new file mode 100644 index 0000000..04f120c --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/Goods.java @@ -0,0 +1,48 @@ +package com.bwie.common.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.bwie.common.domain.response.Info; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@TableName("goods") +public class Goods { + @TableId(value = "goods_id",type = IdType.AUTO) + private Long goodsId; + @TableField(value = "goods_name") + private String goodsName; + @TableField(value = "goods_price") + private Double goodsPrice; + @TableField(value = "goods_sale") + private Integer goodsSale; + @TableField(value = "goods_save") + private Integer goodsSave; + @TableField(value = "goods_status") + private Integer goodsStatus; + @TableField(value = "goods_time") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date goodsTime; + @TableField(value = "type_id") + private Long typeId; + + + + + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/Ord.java b/bwie-common/src/main/java/com/bwie/common/domain/Ord.java new file mode 100644 index 0000000..7ca5cfa --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/Ord.java @@ -0,0 +1,31 @@ +package com.bwie.common.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class Ord { + private Long ordId; + private String ordHao; + private Double ordMoney; + private Double ordTruth; + private Date ordTime; + private Long userId; + private Integer ordStatus; + private Long goodsId; + private String goodsName; + private Integer goodsNum; + private Long middId; + + + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/Type.java b/bwie-common/src/main/java/com/bwie/common/domain/Type.java new file mode 100644 index 0000000..394bcbc --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/Type.java @@ -0,0 +1,22 @@ +package com.bwie.common.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@TableName(value = "type") +public class Type { + @TableId(value = "type_id",type = IdType.AUTO) + private Integer typeId; + @TableField(value = "type_name") + private String typeName; +} 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..5431e50 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/User.java @@ -0,0 +1,37 @@ +package com.bwie.common.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.bwie.common.domain.response.Info; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@TableName(value = "user") +public class User { + @TableId(value = "user_id",type = IdType.AUTO) + private Long userId; + @TableField(value = "user_name") + private String userName; + @TableField(value = "user_pwd") + private String userPwd; + @TableField(value = "user_phone") + private String userPhone; + @TableField(value = "user_role") + private Integer userRole; + @TableField(value = "user_ye") + private Double userYe; + + + + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/AddoReq.java b/bwie-common/src/main/java/com/bwie/common/domain/request/AddoReq.java new file mode 100644 index 0000000..ddd50e1 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/AddoReq.java @@ -0,0 +1,28 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class AddoReq { + private String ordHao; + private Double goodsPrice; + private Date ordTime; + private Long userId; + private Integer ordStatus; + private Long goodsId; + private Integer carNum; + private Long middId; + + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/BackReq.java b/bwie-common/src/main/java/com/bwie/common/domain/request/BackReq.java new file mode 100644 index 0000000..b2d940d --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/BackReq.java @@ -0,0 +1,21 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class BackReq { + private Double ordTruth; + private Long goodsId; + private Long middId; + private Integer goodsNum; + + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/CusReq.java b/bwie-common/src/main/java/com/bwie/common/domain/request/CusReq.java new file mode 100644 index 0000000..0df51fd --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/CusReq.java @@ -0,0 +1,18 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CusReq { + + private Integer pageNum=1; + private Integer pageSize=3; + private String goodsName; + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/LoginRequest.java b/bwie-common/src/main/java/com/bwie/common/domain/request/LoginRequest.java new file mode 100644 index 0000000..23debe4 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/LoginRequest.java @@ -0,0 +1,16 @@ +package com.bwie.common.domain.request; + +import com.baomidou.mybatisplus.annotation.TableField; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class LoginRequest { + private String userName; + private String userPwd; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/ManaReq.java b/bwie-common/src/main/java/com/bwie/common/domain/request/ManaReq.java new file mode 100644 index 0000000..b609ed4 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/ManaReq.java @@ -0,0 +1,19 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class ManaReq { + private Integer pageNum=1; + private Integer pageSize=3; + private String goodsName; + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/MqReq.java b/bwie-common/src/main/java/com/bwie/common/domain/request/MqReq.java new file mode 100644 index 0000000..4c4398d --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/MqReq.java @@ -0,0 +1,17 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class MqReq { + private Long middId; + private String userPhone; + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/OrdNum.java b/bwie-common/src/main/java/com/bwie/common/domain/request/OrdNum.java new file mode 100644 index 0000000..32ba49d --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/OrdNum.java @@ -0,0 +1,15 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class OrdNum { + private Integer goodsId; + private Integer carNum; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/PayNum.java b/bwie-common/src/main/java/com/bwie/common/domain/request/PayNum.java new file mode 100644 index 0000000..ce23f8a --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/PayNum.java @@ -0,0 +1,15 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class PayNum { + private Long goodsId; + private Integer goodsNum; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/PayReq.java b/bwie-common/src/main/java/com/bwie/common/domain/request/PayReq.java new file mode 100644 index 0000000..17ba86f --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/PayReq.java @@ -0,0 +1,18 @@ +package com.bwie.common.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class PayReq { + private Long middId; + private List payNums; + private Double price; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/response/CarRes.java b/bwie-common/src/main/java/com/bwie/common/domain/response/CarRes.java new file mode 100644 index 0000000..1eecf2b --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/response/CarRes.java @@ -0,0 +1,22 @@ +package com.bwie.common.domain.response; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class CarRes { + private Long carId; + private Long userId; + private Long goodsId; + private Integer carNum; + private String goodsName; + private Double goodsPrice; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/response/CusRes.java b/bwie-common/src/main/java/com/bwie/common/domain/response/CusRes.java new file mode 100644 index 0000000..6a0a6e1 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/response/CusRes.java @@ -0,0 +1,31 @@ +package com.bwie.common.domain.response; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class CusRes { + + private Long goodsId; + + private String goodsName; + + private Double goodsPrice; + + private Integer goodsSale; + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/response/GoodsSave.java b/bwie-common/src/main/java/com/bwie/common/domain/response/GoodsSave.java new file mode 100644 index 0000000..041af51 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/response/GoodsSave.java @@ -0,0 +1,30 @@ +package com.bwie.common.domain.response; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class GoodsSave { + + private Long goodsId; + + private String goodsName; + + private Double goodsPrice; + + private Integer goodsSave; + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/response/Info.java b/bwie-common/src/main/java/com/bwie/common/domain/response/Info.java new file mode 100644 index 0000000..d24ca6d --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/response/Info.java @@ -0,0 +1,19 @@ +package com.bwie.common.domain.response; + +import com.baomidou.mybatisplus.annotation.TableField; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class Info { + private Long userId; + private String userName; + private String userPhone; + private Integer userRole; + private Double userYe; +} diff --git a/bwie-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java b/bwie-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java new file mode 100644 index 0000000..5007f5a --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java @@ -0,0 +1,16 @@ +package com.bwie.common.domain.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class JwtResponse { + private String token; + private Long expireTime; + +} diff --git a/bwie-common/src/main/java/com/bwie/common/handler/GlobalExceptionHandle.java b/bwie-common/src/main/java/com/bwie/common/handler/GlobalExceptionHandle.java new file mode 100644 index 0000000..533cab2 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/handler/GlobalExceptionHandle.java @@ -0,0 +1,56 @@ +package com.bwie.common.handler; + +import com.alibaba.fastjson.JSONObject; +import com.bwie.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.stream.Collectors; + +@RestControllerAdvice +@Log4j2 +@Configuration +public class GlobalExceptionHandle { + + @ExceptionHandler(value = MethodArgumentNotValidException.class) + public Result runtimeException(MethodArgumentNotValidException exception){ + log.error("请求异常:[{}]",exception.getMessage(),exception); + return Result.error( + JSONObject.toJSONString( + exception.getBindingResult().getAllErrors() + .stream() + .map(ObjectError::getDefaultMessage) + .toArray() + ) + ); +// return Result.error( +// exception.getBindingResult().getAllErrors() +// .stream() +// .map(ObjectError::getDefaultMessage) +// .collect(Collectors.joining())); + } + + + @ExceptionHandler(value = IllegalArgumentException.class) + public Result illegalArgumentExceptionHandle(IllegalArgumentException exception){ + log.error("请求异常[{}]",exception.getMessage(),exception); + return Result.error(exception.getMessage()); + } + + + + + + + + + + + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/redis/RedisCache.java b/bwie-common/src/main/java/com/bwie/common/redis/RedisCache.java new file mode 100644 index 0000000..d1e4f79 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/redis/RedisCache.java @@ -0,0 +1,260 @@ +package com.bwie.common.redis; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.BoundSetOperations; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * spring redis 工具类 + * + * @author ruoyi + **/ +@SuppressWarnings(value = {"unchecked", "rawtypes"}) +@Component +public class RedisCache { + @Autowired + public RedisTemplate redisTemplate; + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + */ + public void setCacheObject (final String key, final T value) { + redisTemplate.opsForValue().set(key, value); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param timeout 时间 + * @param timeUnit 时间颗粒度 + */ + public void setCacheObject (final String key, final T value, final Long timeout, final TimeUnit timeUnit) { + redisTemplate.opsForValue().set(key, value, timeout, timeUnit); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * + * @return true=设置成功;false=设置失败 + */ + public boolean expire (final String key, final long timeout) { + return expire(key, timeout, TimeUnit.SECONDS); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @param unit 时间单位 + * + * @return true=设置成功;false=设置失败 + */ + public boolean expire (final String key, final long timeout, final TimeUnit unit) { + return Boolean.TRUE.equals(redisTemplate.expire(key, timeout, unit)); + } + + /** + * 获取有效时间 + * + * @param key Redis键 + * + * @return 有效时间 + */ + public long getExpire (final String key) { + return redisTemplate.getExpire(key); + } + + /** + * 判断 key是否存在 + * + * @param key 键 + * + * @return true 存在 false不存在 + */ + public Boolean hasKey (String key) { + return redisTemplate.hasKey(key); + } + + /** + * 获得缓存的基本对象。 + * + * @param key 缓存键值 + * + * @return 缓存键值对应的数据 + */ + public T getCacheObject (final String key) { + ValueOperations operation = redisTemplate.opsForValue(); + return operation.get(key); + } + + /** + * 删除单个对象 + * + * @param key + */ + public boolean deleteObject (final String key) { + return redisTemplate.delete(key); + } + + /** + * 删除集合对象 + * + * @param collection 多个对象 + * + * @return + */ + public boolean deleteObject (final Collection collection) { + return redisTemplate.delete(collection) > 0; + } + + /** + * 缓存List数据 + * + * @param key 缓存的键值 + * @param dataList 待缓存的List数据 + * + * @return 缓存的对象 + */ + public long setCacheList (final String key, final List dataList) { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + return count == null ? 0 : count; + } + + /** + * 获得缓存的list对象 + * + * @param key 缓存的键值 + * + * @return 缓存键值对应的数据 + */ + public List getCacheList (final String key) { + return redisTemplate.opsForList().range(key, 0, -1); + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param dataSet 缓存的数据 + * + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet (final String key, final Set dataSet) { + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + for (T t : dataSet) { + setOperation.add(t); + } + return setOperation; + } + + /** + * 获得缓存的set + * + * @param key + * + * @return + */ + public Set getCacheSet (final String key) { + return redisTemplate.opsForSet().members(key); + } + + /** + * 缓存Map + * + * @param key + * @param dataMap + */ + public void setCacheMap (final String key, final Map dataMap) { + if (dataMap != null) { + redisTemplate.opsForHash().putAll(key, dataMap); + } + } + + /** + * 获得缓存的Map + * + * @param key + * + * @return + */ + public Map getCacheMap (final String key) { + return redisTemplate.opsForHash().entries(key); + } + + /** + * 往Hash中存入数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @param value 值 + */ + public void setCacheMapValue (final String key, final String hKey, final T value) { + redisTemplate.opsForHash().put(key, hKey, value); + } + + /** + * 获取Hash中的数据 + * + * @param key Redis键 + * @param hKey Hash键 + * + * @return Hash中的对象 + */ + public T getCacheMapValue (final String key, final String hKey) { + HashOperations opsForHash = redisTemplate.opsForHash(); + return opsForHash.get(key, hKey); + } + + /** + * 获取多个Hash中的数据 + * + * @param key Redis键 + * @param hKeys Hash键集合 + * + * @return Hash对象集合 + */ + public List getMultiCacheMapValue (final String key, final Collection hKeys) { + return redisTemplate.opsForHash().multiGet(key, hKeys); + } + + /** + * 删除Hash中的某条数据 + * + * @param key Redis键 + * @param hKey Hash键 + * + * @return 是否成功 + */ + public boolean deleteCacheMapValue (final String key, final String hKey) { + return redisTemplate.opsForHash().delete(key, hKey) > 0; + } + + /** + * 获得缓存的基本对象列表 + * + * @param pattern 字符串前缀 + * + * @return 对象列表 + */ + public Collection keys (final String pattern) { + return redisTemplate.keys(pattern); + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/es/EsRemoteService.java b/bwie-common/src/main/java/com/bwie/common/remote/es/EsRemoteService.java new file mode 100644 index 0000000..0c12f4e --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/es/EsRemoteService.java @@ -0,0 +1,30 @@ +package com.bwie.common.remote.es; + +import com.bwie.common.constant.ServerNameConstants; +import com.bwie.common.domain.Goods; +import com.bwie.common.remote.es.factory.EsRemoteFactory; +import com.bwie.common.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; + +@FeignClient( + name = ServerNameConstants.ES_NAME, + fallbackFactory = EsRemoteFactory.class +) +public interface EsRemoteService { + @PostMapping("addb") + public Result addb(@RequestBody List gg); + + @PostMapping("del") + Result delb(@RequestParam Long goodsId); + + @GetMapping + void del(); + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/es/factory/EsRemoteFactory.java b/bwie-common/src/main/java/com/bwie/common/remote/es/factory/EsRemoteFactory.java new file mode 100644 index 0000000..7164be6 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/es/factory/EsRemoteFactory.java @@ -0,0 +1,36 @@ +package com.bwie.common.remote.es.factory; + +import com.bwie.common.domain.Goods; +import com.bwie.common.remote.es.EsRemoteService; +import com.bwie.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +import java.util.List; +@Log4j2 +@Component +public class EsRemoteFactory implements FallbackFactory { + @Override + public EsRemoteService create(Throwable cause) { + return new EsRemoteService() { + @Override + public Result addb(List gg) { + log.error("[{}-{}]es同步远程调用错误",gg,cause.getMessage(),cause); + return Result.error(); + } + + @Override + public Result delb(Long goodsId) { + log.error("[{}-{}]es删除远程调用错误",goodsId,cause.getMessage(),cause); + return Result.error(); + } + + @Override + public void del() { + log.error("[{}-{}]es删除远程调用错误",cause.getMessage(),cause); + Result.error(); + } + }; + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/goods/GoodsRemoteService.java b/bwie-common/src/main/java/com/bwie/common/remote/goods/GoodsRemoteService.java new file mode 100644 index 0000000..ea80a17 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/goods/GoodsRemoteService.java @@ -0,0 +1,25 @@ +package com.bwie.common.remote.goods; + +import com.bwie.common.constant.ServerNameConstants; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.remote.goods.factory.GoodsRemoteFactory; +import com.bwie.common.remote.user.factory.UserRemoteFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; + +@FeignClient( + name = ServerNameConstants.GOODS_NAME, + fallbackFactory = GoodsRemoteFactory.class +) +public interface GoodsRemoteService { + + @PostMapping("updnum") + void updnum(@RequestParam Long goodsId,Integer goodsNum); + + @PostMapping("addnum") + void addnum(@RequestParam Integer goodsNum, Long goodsId); +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/goods/factory/GoodsRemoteFactory.java b/bwie-common/src/main/java/com/bwie/common/remote/goods/factory/GoodsRemoteFactory.java new file mode 100644 index 0000000..8de66ec --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/goods/factory/GoodsRemoteFactory.java @@ -0,0 +1,31 @@ +package com.bwie.common.remote.goods.factory; + +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.remote.goods.GoodsRemoteService; +import com.bwie.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@Log4j2 +public class GoodsRemoteFactory implements FallbackFactory { + @Override + public GoodsRemoteService create(Throwable cause) { + return new GoodsRemoteService() { + @Override + public void updnum(Long goodsId,Integer goodsNum) { + log.error("[{}-{}-{}]修改库存远程调用错误",goodsId,goodsNum,cause.getMessage(),cause); + Result.error(); + } + + @Override + public void addnum(Integer goodsNum, Long goodsId) { + log.error("[{}-{}-{}]恢复库存远程调用错误",goodsId,goodsNum,cause.getMessage(),cause); + Result.error(); + } + }; + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/mq/MqRemoteService.java b/bwie-common/src/main/java/com/bwie/common/remote/mq/MqRemoteService.java new file mode 100644 index 0000000..0e9cee4 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/mq/MqRemoteService.java @@ -0,0 +1,24 @@ +package com.bwie.common.remote.mq; + +import com.bwie.common.constant.ServerNameConstants; +import com.bwie.common.domain.request.MqReq; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.remote.goods.factory.GoodsRemoteFactory; +import com.bwie.common.remote.mq.factory.MqRemoteFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; + +@FeignClient( + name = ServerNameConstants.MQ_NAME, + fallbackFactory = MqRemoteFactory.class +) +public interface MqRemoteService { + + @PostMapping("updstatus") + void updstatus(@RequestBody MqReq build); + +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/mq/factory/MqRemoteFactory.java b/bwie-common/src/main/java/com/bwie/common/remote/mq/factory/MqRemoteFactory.java new file mode 100644 index 0000000..53ff523 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/mq/factory/MqRemoteFactory.java @@ -0,0 +1,28 @@ +package com.bwie.common.remote.mq.factory; + +import com.bwie.common.domain.request.MqReq; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.remote.goods.GoodsRemoteService; +import com.bwie.common.remote.mq.MqRemoteService; +import com.bwie.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@Log4j2 +public class MqRemoteFactory implements FallbackFactory { + @Override + public MqRemoteService create(Throwable cause) { + return new MqRemoteService() { + + @Override + public void updstatus(MqReq build) { + log.error("[{}-{}-{}]退款远程调用错误",build,cause.getMessage(),cause); + Result.error(); + } + }; + } +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/user/UserRemoteService.java b/bwie-common/src/main/java/com/bwie/common/remote/user/UserRemoteService.java new file mode 100644 index 0000000..8600c73 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/user/UserRemoteService.java @@ -0,0 +1,26 @@ +package com.bwie.common.remote.user; + +import com.bwie.common.constant.ServerNameConstants; +import com.bwie.common.domain.User; +import com.bwie.common.remote.user.factory.UserRemoteFactory; +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; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient( + name = ServerNameConstants.SYSTEM_NAME, + fallbackFactory = UserRemoteFactory.class +) +public interface UserRemoteService { + + @PostMapping("findname") + public Result findname(@RequestParam String userName); + + + @PostMapping("incremoney") + void incremoney(@RequestParam Double ordMoney, Long userId); + @PostMapping("addmoney") + void addmoney(@RequestParam Long userId, Double ordTruth); +} diff --git a/bwie-common/src/main/java/com/bwie/common/remote/user/factory/UserRemoteFactory.java b/bwie-common/src/main/java/com/bwie/common/remote/user/factory/UserRemoteFactory.java new file mode 100644 index 0000000..7cd0e7e --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/remote/user/factory/UserRemoteFactory.java @@ -0,0 +1,35 @@ +package com.bwie.common.remote.user.factory; + +import com.bwie.common.domain.User; +import com.bwie.common.remote.user.UserRemoteService; +import com.bwie.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +@Component +@Log4j2 +public class UserRemoteFactory implements FallbackFactory { + @Override + public UserRemoteService create(Throwable cause) { + return new UserRemoteService() { + @Override + public Result findname(String userName) { + log.error("[{}-{}]查找姓名远程调用错误",userName,cause.getMessage(),cause); + return Result.error(); + } + + @Override + public void incremoney(Double ordMoney, Long userId) { + log.error("[{}-{}-{}]支付远程调用错误",ordMoney,userId,cause.getMessage(),cause); + Result.error(); + } + + @Override + public void addmoney(Long userId, Double ordTruth) { + log.error("[{}-{}-{}]退款远程调用错误",userId,ordTruth,cause.getMessage(),cause); + Result.error(); + } + }; + } +} 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..85ecdda --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/result/PageResult.java @@ -0,0 +1,34 @@ +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..148e0fa --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/result/Result.java @@ -0,0 +1,63 @@ +package com.bwie.common.result; + + +import com.bwie.common.constant.Constants; +import lombok.Data; + +import java.io.Serializable; + +/** + * @description: 响应信息主体 + * @author DongZl + */ +@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; + } + + public boolean isSuccess(){ + return this.code==SUCCESS; + } + public boolean isError(){ + return !isSuccess(); + } + + +} diff --git a/bwie-common/src/main/java/com/bwie/common/utils/IdUtils.java b/bwie-common/src/main/java/com/bwie/common/utils/IdUtils.java new file mode 100644 index 0000000..b013ae8 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/IdUtils.java @@ -0,0 +1,19 @@ +package com.bwie.common.utils; + +import java.util.UUID; + +/** + * @author DongZl + * @description: ID生成工具类 + * @Date 2024-1-10 下午 08:26 + */ +public class IdUtils { + + /** + * 生成UUID + * @return UUID + */ + public static String genId(){ + return UUID.randomUUID().toString().replace("-", ""); + } +} 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..79ab70f --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/JwtUtils.java @@ -0,0 +1,105 @@ +package com.bwie.common.utils; + + +import com.bwie.common.constant.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/SecurityUtils.java b/bwie-common/src/main/java/com/bwie/common/utils/SecurityUtils.java new file mode 100644 index 0000000..c2ed35f --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/SecurityUtils.java @@ -0,0 +1,54 @@ +package com.bwie.common.utils; + + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +/** + * 安全服务工具类 + * + * @author ruoyi + */ +public class SecurityUtils { + + + /** + * 生成BCryptPasswordEncoder密码 + * + * @param password 密码 + * + * @return 加密字符串 + */ + public static String encryptPassword (String password, String salt) { + return encryptMD5(password, salt); + } + + /** + * 判断密码是否相同 + * + * @param rawPassword 真实密码 + * @param encodedPassword 加密后字符 + * + * @return 结果 + */ + public static boolean matchesPassword (String rawPassword, String salt, String encodedPassword) { + return encryptMD5(rawPassword, salt).equals(encodedPassword); + } + + /** + * 计算字符串的MD5加密值,并返回Base64编码的字符串。 + * @param password 要加密的字符串 + * @return 加密后的Base64编码字符串 + */ + public static String encryptMD5(String password, String salt) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update((password + salt).getBytes()); // 加盐处理 + byte[] digest = md.digest(); + return Base64.getEncoder().encodeToString(digest); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } +} 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/resources/META-INF/spring.factories b/bwie-common/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..cb5de42 --- /dev/null +++ b/bwie-common/src/main/resources/META-INF/spring.factories @@ -0,0 +1,9 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.bwie.common.config.MybatisPlusConfig,\ + com.bwie.common.handler.GlobalExceptionHandle,\ + com.bwie.common.config.RedisConfig,\ + com.bwie.common.redis.RedisCache,\ + com.bwie.common.remote.user.factory.UserRemoteFactory,\ + com.bwie.common.remote.es.factory.EsRemoteFactory,\ + com.bwie.common.remote.mq.factory.MqRemoteFactory,\ + com.bwie.common.remote.goods.factory.GoodsRemoteFactory diff --git a/bwie-gateway/pom.xml b/bwie-gateway/pom.xml new file mode 100644 index 0000000..33b6193 --- /dev/null +++ b/bwie-gateway/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + com.bwie + test_month + 1.0.0 + + + bwie-gateway + + + 17 + 17 + UTF-8 + + + + + + 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/GatewayApp.java b/bwie-gateway/src/main/java/com/bwie/gateway/GatewayApp.java new file mode 100644 index 0000000..156356c --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/GatewayApp.java @@ -0,0 +1,14 @@ +package com.bwie.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) +@EnableDiscoveryClient +public class GatewayApp { + public static void main(String[] args) { + SpringApplication.run(GatewayApp.class); + } +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java b/bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java new file mode 100644 index 0000000..a6daed3 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java @@ -0,0 +1,71 @@ +package com.bwie.gateway.config; + +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; +import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.web.reactive.result.view.ViewResolver; + +import javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @deprecation: 网关限流控件 + * @author DongZl + */ +@Configuration +public class GatewaySentinelConfig { + /** + * 查看解析器 + */ + private final List viewResolvers; + /** + * 服务器编解码器配置 + */ + private final ServerCodecConfigurer serverCodecConfigurer; + public GatewaySentinelConfig(ObjectProvider> viewResolversProvider, + ServerCodecConfigurer serverCodecConfigurer) { + this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); + this.serverCodecConfigurer = serverCodecConfigurer; + } + /** + * Sentinel 网关块异常处理程序 + * @return + */ + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { + // 给 Spring Cloud Gateway 注册块异常处理程序。 + return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); + } + + /** + * 初始化网关配置 + */ + @PostConstruct + public void doInit() { + initGatewayRules(); + } + /** + * 配置限流规则 + */ + private void initGatewayRules() { + Set rules = new HashSet<>(); + rules.add(new GatewayFlowRule("cloud-user") + // 限流阈值 + .setCount(1) + // 统计时间窗口,单位是秒,默认是 1 秒 + .setIntervalSec(5) + ); + //添加到限流规则当中 + GatewayRuleManager.loadRules(rules); + } +} 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..5705d6f --- /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 DongZl + */ +@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/AuthFilter.java b/bwie-gateway/src/main/java/com/bwie/gateway/filters/AuthFilter.java new file mode 100644 index 0000000..05f37c6 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/filters/AuthFilter.java @@ -0,0 +1,74 @@ +package com.bwie.gateway.filters; + +import com.bwie.common.constant.JwtConstants; +import com.bwie.common.constant.TokenConstants; +import com.bwie.common.redis.RedisCache; +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 io.jsonwebtoken.Claims; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.http.HttpMethod; +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.util.concurrent.TimeUnit; + +@Component +public class AuthFilter implements GlobalFilter, Ordered { + + private final IgnoreWhiteConfig ignoreWhiteConfig; + private final RedisCache redisCache; + + public AuthFilter(IgnoreWhiteConfig ignoreWhiteConfig, RedisCache redisCache) { + this.ignoreWhiteConfig = ignoreWhiteConfig; + this.redisCache = redisCache; + } + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + ServerHttpRequest request = exchange.getRequest(); + ServerHttpRequest.Builder mutate = request.mutate(); + String path = request.getURI().getPath(); + System.out.println("path = " + path); + HttpMethod method = request.getMethod(); + System.out.println("method = " + method); + if(StringUtils.matches(path,ignoreWhiteConfig.getWhites())){ + return chain.filter(exchange); + } + String first = request.getHeaders().getFirst(TokenConstants.TOKEN); + + if(null==first){ + return GatewayUtils.errorResponse(exchange,"token不为空"); + } + + Claims claims = JwtUtils.parseToken(first); + if(null==claims){ + return GatewayUtils.errorResponse(exchange,"token格式不正确"); + } + String userKey = JwtUtils.getUserKey(claims); + if(!redisCache.hasKey(TokenConstants.LOGIN_TOKEN_KEY+userKey)){ + return GatewayUtils.errorResponse(exchange,"token过期"); + } + redisCache.expire(TokenConstants.LOGIN_TOKEN_KEY+userKey,30, TimeUnit.MINUTES); + + String userId = JwtUtils.getUserId(claims); + String userName = JwtUtils.getUserName(claims); + GatewayUtils.addHander(mutate, JwtConstants.USER_KEY,userKey); + GatewayUtils.addHander(mutate,JwtConstants.DETAILS_USERNAME,userName); + GatewayUtils.addHander(mutate,JwtConstants.DETAILS_USER_ID,userId); + GatewayUtils.removeHeader(mutate,TokenConstants.TOKEN); + + return chain.filter(exchange.mutate().request(mutate.build()).build()); + } + + @Override + public int getOrder() { + return 0; + } +} 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..cc033bf --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java @@ -0,0 +1,67 @@ +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; + +@Log4j2 +public class GatewayUtils { + public static void addHander(ServerHttpRequest.Builder mutate,String key,Object value) { + if(StringUtils.isEmpty(key)){ + log.warn("添加请求头参数键不可以为空"); + return; + } + if(value==null){ + log.warn("添加请求头参数:[{}]值为空",key); + return; + } + String string = value.toString(); + mutate.header(key,string); + log.info("添加请求头参数成功 - 键:{[]},值:{[]}",key,value); + } + + 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); + } + + public static Mono errorResponse(ServerWebExchange exchange,String msg){ + ServerHttpResponse response = exchange.getResponse(); + response.setStatusCode(HttpStatus.OK); + response.getHeaders().add(HttpHeaders.CONTENT_TYPE,"application/json"); + Result error = Result.error(msg); + String jsonString = JSONObject.toJSONString(error); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", + exchange.getRequest().getPath(),msg,jsonString); + DataBuffer wrap = response.bufferFactory().wrap(jsonString.getBytes()); + return response.writeWith(Mono.just(wrap)); + + + } + + + + + + + + + + + + + + +} diff --git a/bwie-gateway/src/main/resources/bootstrap.yml b/bwie-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..231e2a0 --- /dev/null +++ b/bwie-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# 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: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-car/pom.xml b/bwie-modules/bwie-car/pom.xml new file mode 100644 index 0000000..6793594 --- /dev/null +++ b/bwie-modules/bwie-car/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-car + + + 17 + 17 + UTF-8 + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + diff --git a/bwie-modules/bwie-car/src/main/java/com/bwie/car/CarApp.java b/bwie-modules/bwie-car/src/main/java/com/bwie/car/CarApp.java new file mode 100644 index 0000000..9628543 --- /dev/null +++ b/bwie-modules/bwie-car/src/main/java/com/bwie/car/CarApp.java @@ -0,0 +1,17 @@ +package com.bwie.car; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.bwie.**") +@MapperScan("com.bwie.car.mapper") +public class CarApp { + public static void main(String[] args) { + SpringApplication.run(CarApp.class); + } +} diff --git a/bwie-modules/bwie-car/src/main/java/com/bwie/car/controller/CarController.java b/bwie-modules/bwie-car/src/main/java/com/bwie/car/controller/CarController.java new file mode 100644 index 0000000..5d4b77d --- /dev/null +++ b/bwie-modules/bwie-car/src/main/java/com/bwie/car/controller/CarController.java @@ -0,0 +1,80 @@ +package com.bwie.car.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.bwie.car.service.CarService; +import com.bwie.common.constant.JwtConstants; +import com.bwie.common.constant.TokenConstants; +import com.bwie.common.domain.Car; +import com.bwie.common.domain.User; +import com.bwie.common.domain.response.CarRes; +import com.bwie.common.domain.response.Info; +import com.bwie.common.redis.RedisCache; +import com.bwie.common.result.Result; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; + +@RestController +@ResponseBody +public class CarController { + private final CarService carService; + private final RedisCache redisCache; + private final HttpServletRequest request; + + public CarController(CarService carService, RedisCache redisCache, HttpServletRequest request) { + this.carService = carService; + this.redisCache = redisCache; + this.request = request; + } + + @PostMapping("addcar") + public Result addcar(@RequestParam Long goodsId){ + LambdaQueryWrapper carLambdaQueryWrapper = new LambdaQueryWrapper<>(); + carLambdaQueryWrapper.eq(Car::getGoodsId,goodsId); + carLambdaQueryWrapper.eq(Car::getUserId,info().getUserId()); + + Car one = carService.getOne(carLambdaQueryWrapper); + if(null==one){ + Car build = Car.builder() + .goodsId(goodsId) + .userId(info().getUserId()).carNum(1) + .build(); + carService.save(build); + } else { + Car build = Car.builder() + .carId(one.getCarId()) + .goodsId(goodsId) + .userId(info().getUserId()) + .carNum(one.getCarNum()+1) + .build(); + carService.updateById(build); + } + return Result.success(); + } + + @GetMapping("show") + public Result show(){ + List show = carService.show(); + return Result.success(show); + } + + @PostMapping("del") + public Result del(@RequestParam Long carId){ + + carService.del(carId); + return Result.success(); + } + + public Info info() { + User cacheObject = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + request.getHeader(JwtConstants.USER_KEY)); + + return Info.builder() + .userPhone(cacheObject.getUserPhone()) + .userId(cacheObject.getUserId()) + .userName(cacheObject.getUserName()) + .userRole(cacheObject.getUserRole()) + .build(); + } +} diff --git a/bwie-modules/bwie-car/src/main/java/com/bwie/car/mapper/CarMapper.java b/bwie-modules/bwie-car/src/main/java/com/bwie/car/mapper/CarMapper.java new file mode 100644 index 0000000..4247cc3 --- /dev/null +++ b/bwie-modules/bwie-car/src/main/java/com/bwie/car/mapper/CarMapper.java @@ -0,0 +1,14 @@ +package com.bwie.car.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.bwie.common.domain.Car; +import com.bwie.common.domain.response.CarRes; + +import java.util.List; + +public interface CarMapper extends BaseMapper { + List show(Long userId); + + + void del(Long carId); +} diff --git a/bwie-modules/bwie-car/src/main/java/com/bwie/car/service/CarService.java b/bwie-modules/bwie-car/src/main/java/com/bwie/car/service/CarService.java new file mode 100644 index 0000000..96aa295 --- /dev/null +++ b/bwie-modules/bwie-car/src/main/java/com/bwie/car/service/CarService.java @@ -0,0 +1,13 @@ +package com.bwie.car.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bwie.common.domain.Car; +import com.bwie.common.domain.response.CarRes; + +import java.util.List; + +public interface CarService extends IService { + List show(); + void del(Long carId); + +} diff --git a/bwie-modules/bwie-car/src/main/java/com/bwie/car/service/CarServiceImpl.java b/bwie-modules/bwie-car/src/main/java/com/bwie/car/service/CarServiceImpl.java new file mode 100644 index 0000000..7fc5c2c --- /dev/null +++ b/bwie-modules/bwie-car/src/main/java/com/bwie/car/service/CarServiceImpl.java @@ -0,0 +1,63 @@ +package com.bwie.car.service; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bwie.car.mapper.CarMapper; +import com.bwie.common.constant.JwtConstants; +import com.bwie.common.constant.TokenConstants; +import com.bwie.common.domain.Car; +import com.bwie.common.domain.User; +import com.bwie.common.domain.response.CarRes; +import com.bwie.common.domain.response.Info; +import com.bwie.common.redis.RedisCache; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashSet; +import java.util.List; + +@Service +public class CarServiceImpl extends ServiceImpl implements CarService { + private final CarMapper carMapper; + private final RedisCache redisCache; + private final HttpServletRequest request; + + public CarServiceImpl(CarMapper carMapper, RedisCache redisCache, HttpServletRequest request) { + this.carMapper = carMapper; + this.redisCache = redisCache; + this.request = request; + } + + @Override + public List show() { + if(!redisCache.hasKey("car:")){ + List show = carMapper.show(info().getUserId()); + HashSet carRes = new HashSet<>(); + for (CarRes res : show) { + carRes.add(res); + } + + redisCache.setCacheList("car:",show); + } + List cacheList = redisCache.getCacheList("car:"); + + + return cacheList; + } + + @Override + public void del(Long carId) { + redisCache.deleteObject("car:"); + carMapper.del(carId); + } + + public Info info() { + User cacheObject = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + request.getHeader(JwtConstants.USER_KEY)); + + return Info.builder() + .userPhone(cacheObject.getUserPhone()) + .userId(cacheObject.getUserId()) + .userName(cacheObject.getUserName()) + .userRole(cacheObject.getUserRole()) + .build(); + } +} diff --git a/bwie-modules/bwie-car/src/main/resources/bootstrap.yml b/bwie-modules/bwie-car/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..be52689 --- /dev/null +++ b/bwie-modules/bwie-car/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9008 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-car + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-car/src/main/resources/mapper/CarMapper.xml b/bwie-modules/bwie-car/src/main/resources/mapper/CarMapper.xml new file mode 100644 index 0000000..c9220dd --- /dev/null +++ b/bwie-modules/bwie-car/src/main/resources/mapper/CarMapper.xml @@ -0,0 +1,15 @@ + + + + + delete + from car + where car_id=#{carId} + + + + diff --git a/bwie-modules/bwie-es/pom.xml b/bwie-modules/bwie-es/pom.xml new file mode 100644 index 0000000..7ff366b --- /dev/null +++ b/bwie-modules/bwie-es/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-es + + + 17 + 17 + UTF-8 + + + + + com.bwie + bwie-common + + + org.springframework.boot + spring-boot-starter-web + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + diff --git a/bwie-modules/bwie-es/src/main/java/com/bwie/es/EsApp.java b/bwie-modules/bwie-es/src/main/java/com/bwie/es/EsApp.java new file mode 100644 index 0000000..97dcda8 --- /dev/null +++ b/bwie-modules/bwie-es/src/main/java/com/bwie/es/EsApp.java @@ -0,0 +1,16 @@ +package com.bwie.es; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) +@EnableFeignClients(basePackages = "com.bwie.**") +@EnableDiscoveryClient +public class EsApp { + public static void main(String[] args) { + SpringApplication.run(EsApp.class); + } +} diff --git a/bwie-modules/bwie-es/src/main/java/com/bwie/es/config/InitEsRes.java b/bwie-modules/bwie-es/src/main/java/com/bwie/es/config/InitEsRes.java new file mode 100644 index 0000000..c94caa7 --- /dev/null +++ b/bwie-modules/bwie-es/src/main/java/com/bwie/es/config/InitEsRes.java @@ -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)) + ); + } +} diff --git a/bwie-modules/bwie-es/src/main/java/com/bwie/es/controller/EsController.java b/bwie-modules/bwie-es/src/main/java/com/bwie/es/controller/EsController.java new file mode 100644 index 0000000..5c5ce94 --- /dev/null +++ b/bwie-modules/bwie-es/src/main/java/com/bwie/es/controller/EsController.java @@ -0,0 +1,32 @@ +package com.bwie.es.controller; + +import com.bwie.common.domain.Goods; +import com.bwie.common.result.Result; +import com.bwie.es.service.EsService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@ResponseBody +public class EsController { + private final EsService esService; + public EsController(EsService esService) { + this.esService = esService; + } + @PostMapping("addb") + public Result addb(@RequestBody List gg){ + esService.addb(gg); + return Result.success(); + } + @PostMapping("del") + Result delb(@RequestParam Long goodsId){ + esService.delb(goodsId); + return Result.success(); + } + @GetMapping("del") + public void del(){ + esService.del(); + } + +} diff --git a/bwie-modules/bwie-es/src/main/java/com/bwie/es/service/EsService.java b/bwie-modules/bwie-es/src/main/java/com/bwie/es/service/EsService.java new file mode 100644 index 0000000..04b4c26 --- /dev/null +++ b/bwie-modules/bwie-es/src/main/java/com/bwie/es/service/EsService.java @@ -0,0 +1,13 @@ +package com.bwie.es.service; + +import com.bwie.common.domain.Goods; + +import java.util.List; + +public interface EsService { + void addb(List gg); + void delb(Long goodsId); + + void del(); + +} diff --git a/bwie-modules/bwie-es/src/main/java/com/bwie/es/service/impl/EsServiceImpl.java b/bwie-modules/bwie-es/src/main/java/com/bwie/es/service/impl/EsServiceImpl.java new file mode 100644 index 0000000..730cdfd --- /dev/null +++ b/bwie-modules/bwie-es/src/main/java/com/bwie/es/service/impl/EsServiceImpl.java @@ -0,0 +1,65 @@ +package com.bwie.es.service.impl; + +import com.bwie.common.domain.Goods; +import com.bwie.es.service.EsService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.xcontent.XContentType; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.List; + +@Service +public class EsServiceImpl implements EsService { + private final RestHighLevelClient client; + + public EsServiceImpl(RestHighLevelClient client) { + this.client = client; + } + + @Override + public void addb(List gg) { + BulkRequest bulkRequest = new BulkRequest(); + ObjectMapper objectMapper = new ObjectMapper(); + for (Goods goods : gg) { + try { + String s = objectMapper.writeValueAsString(goods); + bulkRequest.add(new IndexRequest("goods").id(goods.getGoodsId().toString()) + .source(s, XContentType.JSON)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + try { + client.bulk(bulkRequest, RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void delb(Long goodsId) { + DeleteRequest deleteRequest = new DeleteRequest("goods",goodsId.toString()); + try { + client.delete(deleteRequest,RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void del() { + DeleteRequest deleteRequest = new DeleteRequest("goods"); + try { + client.delete(deleteRequest,RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/bwie-modules/bwie-es/src/main/resources/bootstrap.yml b/bwie-modules/bwie-es/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..bc86b38 --- /dev/null +++ b/bwie-modules/bwie-es/src/main/resources/bootstrap.yml @@ -0,0 +1,40 @@ +es: + host: 124.221.177.197 + port: 9200 + scheme: http + +# Tomcat +server: + port: 9005 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: 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: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + +# 将mapper接口所在包的日志级别改成debug,可以在控制台打印es +logging: + level: + org.apache.http: trace diff --git a/bwie-modules/bwie-goods/pom.xml b/bwie-modules/bwie-goods/pom.xml new file mode 100644 index 0000000..f5fad46 --- /dev/null +++ b/bwie-modules/bwie-goods/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-goods + + + 17 + 17 + UTF-8 + + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/GoodsApp.java b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/GoodsApp.java new file mode 100644 index 0000000..23bffb9 --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/GoodsApp.java @@ -0,0 +1,38 @@ +package com.bwie.goods; + +import com.bwie.common.domain.Goods; +import com.bwie.common.remote.es.EsRemoteService; +import com.bwie.goods.service.GoodsService; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +import javax.annotation.PostConstruct; +import java.util.List; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.bwie.**") +@MapperScan("com.bwie.goods.mapper") +public class GoodsApp { + private final GoodsService goodsService; + private final EsRemoteService esRemoteService; + + public GoodsApp(GoodsService goodsService, EsRemoteService esRemoteService) { + this.goodsService = goodsService; + this.esRemoteService = esRemoteService; + } + + public static void main(String[] args) { + SpringApplication.run(GoodsApp.class); + } + + @PostConstruct + public void init(){ + goodsService.initRedis(); + List findall = goodsService.findall(); + esRemoteService.addb(findall); + } +} diff --git a/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/controller/GoodsController.java b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/controller/GoodsController.java new file mode 100644 index 0000000..93e7707 --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/controller/GoodsController.java @@ -0,0 +1,70 @@ +package com.bwie.goods.controller; + +import com.bwie.common.domain.Goods; +import com.bwie.common.domain.request.CusReq; +import com.bwie.common.domain.request.ManaReq; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.domain.response.CusRes; +import com.bwie.common.remote.es.EsRemoteService; +import com.bwie.common.result.PageResult; +import com.bwie.common.result.Result; +import com.bwie.goods.service.GoodsService; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@ResponseBody +public class GoodsController { + private final GoodsService goodsService; + private final EsRemoteService esRemoteService; + + public GoodsController(GoodsService goodsService, EsRemoteService esRemoteService) { + this.goodsService = goodsService; + this.esRemoteService = esRemoteService; + } + + @PostMapping("showMana") + public Result showMana(@RequestBody ManaReq manaReq){ + Result result = goodsService.showMana(manaReq); + return result; + } + + @PostMapping("addgoods") + public Result addgoods (@RequestBody Goods goods){ + goodsService.addgoods(goods); + return Result.success(); + } + + @PostMapping("updgoods") + public Result updgoods(@RequestBody Goods gg){ + goodsService.updateById(gg); + esRemoteService.delb(gg.getGoodsId()); + ArrayList goods1 = new ArrayList<>(); + goods1.add(gg); + esRemoteService.addb(goods1); + return Result.success(); + } + + @PostMapping("delgoods") + public Result delgoods(@RequestBody Goods gg){ + goodsService.delgoods(gg); + return Result.success(); + } + + @PostMapping("showCus") + public Result showCus(@RequestBody CusReq cusReq){ + Result> pageResultResult = goodsService.showCus(cusReq); + return pageResultResult; + } + @PostMapping("updnum") + void updnum(@RequestParam Long goodsId,Integer goodsNum){ + goodsService.updnum(goodsId,goodsNum); + } + @PostMapping("addnum") + void addnum(@RequestParam Integer goodsNum, Long goodsId){ + goodsService.addnum(goodsNum,goodsId); + } + +} diff --git a/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/mapper/GoodsMapper.java b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/mapper/GoodsMapper.java new file mode 100644 index 0000000..2cc755d --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/mapper/GoodsMapper.java @@ -0,0 +1,27 @@ +package com.bwie.goods.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.bwie.common.domain.Goods; +import com.bwie.common.domain.request.CusReq; +import com.bwie.common.domain.request.ManaReq; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.domain.response.CusRes; +import com.bwie.common.domain.response.GoodsSave; +import com.bwie.common.result.PageResult; + +import java.util.List; + +public interface GoodsMapper extends BaseMapper { + + + List showMana(ManaReq manaReq); + + void add(Goods gg); + + List showCus(CusReq cusReq); + + void updnum(Long goodsId,Integer goodsNum); + + void addnum(Long goodsId, Integer goodsNum); + +} diff --git a/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/service/GoodsService.java b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/service/GoodsService.java new file mode 100644 index 0000000..afcbbe7 --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/service/GoodsService.java @@ -0,0 +1,27 @@ +package com.bwie.goods.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bwie.common.domain.Goods; +import com.bwie.common.domain.request.CusReq; +import com.bwie.common.domain.request.ManaReq; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.domain.response.CusRes; +import com.bwie.common.result.PageResult; +import com.bwie.common.result.Result; + +import java.util.List; + +public interface GoodsService extends IService { + List findall(); + void initRedis(); + Result showMana(ManaReq manaReq); + + void addgoods(Goods gg); + + void delgoods(Goods gg); + Result> showCus(CusReq cusReq); + + void updnum(Long goodsId,Integer goodsNum); + + void addnum(Integer goodsNum, Long goodsId); +} diff --git a/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/service/GoodsServiceImpl.java b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/service/GoodsServiceImpl.java new file mode 100644 index 0000000..0d2c672 --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/java/com/bwie/goods/service/GoodsServiceImpl.java @@ -0,0 +1,126 @@ +package com.bwie.goods.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bwie.common.constant.JwtConstants; +import com.bwie.common.constant.TokenConstants; +import com.bwie.common.domain.Goods; +import com.bwie.common.domain.User; +import com.bwie.common.domain.request.CusReq; +import com.bwie.common.domain.request.ManaReq; +import com.bwie.common.domain.request.PayNum; +import com.bwie.common.domain.response.CusRes; +import com.bwie.common.domain.response.GoodsSave; +import com.bwie.common.domain.response.Info; +import com.bwie.common.redis.RedisCache; +import com.bwie.common.remote.es.EsRemoteService; +import com.bwie.common.result.PageResult; +import com.bwie.common.result.Result; +import com.bwie.goods.mapper.GoodsMapper; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; + +@Service +public class GoodsServiceImpl extends ServiceImpl implements GoodsService { + private final GoodsMapper goodsMapper; + private final EsRemoteService esRemoteService; + private final RedisCache redisCache; + private final HttpServletRequest request; + + public GoodsServiceImpl(GoodsMapper goodsMapper, EsRemoteService esRemoteService, RedisCache redisCache, HttpServletRequest request) { + this.goodsMapper = goodsMapper; + this.esRemoteService = esRemoteService; + this.redisCache = redisCache; + this.request = request; + } + + @Override + public List findall() { + LambdaQueryWrapper goodsLambdaQueryWrapper = new LambdaQueryWrapper<>(); + List goods = goodsMapper.selectList(goodsLambdaQueryWrapper); + return goods; + } + + @Override + public void initRedis() { + + List findall = findall(); + ArrayList goodsSaves = new ArrayList<>(); + for (Goods goods : findall) { + GoodsSave build = GoodsSave.builder().goodsId(goods.getGoodsId()) + .goodsPrice(goods.getGoodsPrice()) + .goodsSave(goods.getGoodsSave()) + .goodsName(goods.getGoodsName()) + .build(); + goodsSaves.add(build); + } + redisCache.setCacheList("goodsSave:",goodsSaves); + } + + @Override + public Result showMana(ManaReq manaReq) { + Assert.isTrue(info().getUserRole()==1,"抱歉,用户不开放管理界面"); + + PageHelper.startPage(manaReq.getPageNum(),manaReq.getPageSize()); + List goods = goodsMapper.showMana(manaReq); + PageInfo goodsPageInfo = new PageInfo<>(goods); + return PageResult.toResult(goodsPageInfo.getTotal(),goods); + } + + @Override + public void addgoods(Goods gg) { + goodsMapper.add(gg); + ArrayList goods1 = new ArrayList<>(); + goods1.add(gg); + esRemoteService.addb(goods1); + } + + @Override + public void delgoods(Goods gg) { + goodsMapper.deleteById(gg); + esRemoteService.delb(gg.getGoodsId()); + } + + @Override + public Result> showCus(CusReq cusReq) { + PageHelper.startPage(cusReq.getPageNum(),cusReq.getPageSize()); + List goods = goodsMapper.showCus(cusReq); + PageInfo cusResPageInfo = new PageInfo<>(goods); + return PageResult.toResult(cusResPageInfo.getTotal(),goods); + } + + @Override + public void updnum(Long goodsId,Integer goodsNum) { + + goodsMapper.updnum(goodsId,goodsNum); + esRemoteService.del(); + esRemoteService.addb(findall()); + + } + + @Override + public void addnum(Integer goodsNum, Long goodsId) { + goodsMapper.addnum(goodsId,goodsNum); + esRemoteService.del(); + esRemoteService.addb(findall()); + } + + + public Info info() { + User cacheObject = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + request.getHeader(JwtConstants.USER_KEY)); + + return Info.builder() + .userPhone(cacheObject.getUserPhone()) + .userId(cacheObject.getUserId()) + .userName(cacheObject.getUserName()) + .userRole(cacheObject.getUserRole()) + .build(); + } + +} diff --git a/bwie-modules/bwie-goods/src/main/resources/bootstrap.yml b/bwie-modules/bwie-goods/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..21f45af --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9003 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-goods + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-goods/src/main/resources/mapper/GoodsMapper.xml b/bwie-modules/bwie-goods/src/main/resources/mapper/GoodsMapper.xml new file mode 100644 index 0000000..07dbc8e --- /dev/null +++ b/bwie-modules/bwie-goods/src/main/resources/mapper/GoodsMapper.xml @@ -0,0 +1,43 @@ + + + + + INSERT INTO `test_month`.`goods` + (`goods_name`, `goods_price`, `goods_sale`, `goods_save`, `goods_status`, `goods_time`, `type_id`) + VALUES + (#{goodsName}, #{goodsPrice}, #{goodsSale}, #{goodsSave}, 1,now(), #{typeId}); + + + + update goods set goods_sale=goods_sale-#{goodsNum} ,goods_save=goods_save+#{goodsNum} + where goods_id=#{goodsId} + + + update goods set goods_sale=goods_sale+#{goodsNum} ,goods_save=goods_save-#{goodsNum} + where goods_id=#{goodsId} + + + + + + diff --git a/bwie-modules/bwie-mq/pom.xml b/bwie-modules/bwie-mq/pom.xml new file mode 100644 index 0000000..8823378 --- /dev/null +++ b/bwie-modules/bwie-mq/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-mq + + + 17 + 17 + UTF-8 + + + + + + com.bwie + bwie-common + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + com.alibaba + fastjson + 1.2.15 + + + org.apache.httpcomponents + httpclient + 4.5.3 + + + org.apache.httpcomponents + httpcore + 4.4.15 + + + commons-lang + commons-lang + 2.6 + + + org.eclipse.jetty + jetty-util + 9.3.7.v20160115 + + + + + diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/MqApp.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/MqApp.java new file mode 100644 index 0000000..e609570 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/MqApp.java @@ -0,0 +1,17 @@ +package com.bwie.mq; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.bwie.**") +@MapperScan("com.bwie.mq.mapper") +public class MqApp { + public static void main(String[] args) { + SpringApplication.run(MqApp.class); + } +} diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/config/MqConfig.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/config/MqConfig.java new file mode 100644 index 0000000..0cbb1dc --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/config/MqConfig.java @@ -0,0 +1,71 @@ +package com.bwie.mq.config; + +import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.core.*; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +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; +import org.springframework.context.annotation.Primary; + +@Configuration +@Log4j2 +public class MqConfig implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback { + + public static final String QUEUE="queue"; + public static final String EXCHANGE="exchange"; + public static final String KEY="key"; + private RabbitTemplate rabbitTemplate; + + @Bean + public MessageConverter messageConverter(){ + return new Jackson2JsonMessageConverter(); + } + + @Bean + public Queue queue(){ + return new Queue(QUEUE,true); + } + + @Bean("exchange") + public DirectExchange directExchange(){ + return new DirectExchange(EXCHANGE); + } + + @Bean + public Binding binding(){ + return BindingBuilder.bind(queue()).to(directExchange()).with(KEY); + } + + @Primary + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){ + RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); + this.rabbitTemplate=rabbitTemplate; + rabbitTemplate.setMessageConverter(messageConverter()); + rabbitTemplate(); + return rabbitTemplate; + } + + private void rabbitTemplate() { + rabbitTemplate.setConfirmCallback(this); + rabbitTemplate.setReturnsCallback(this); + } + + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + if(ack){ + log.info("{}信息到达交换机",correlationData.getId()); + }else { + log.info("{}信息丢失",correlationData.getId()); + } + } + + @Override + public void returnedMessage(ReturnedMessage returned) { + log.info("{}信息未到达交换机",returned.getMessage().getMessageProperties().getMessageId()); + } +} diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/consumer/MqConsumer.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/consumer/MqConsumer.java new file mode 100644 index 0000000..dc8d8f9 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/consumer/MqConsumer.java @@ -0,0 +1,76 @@ +package com.bwie.mq.consumer; + +import com.bwie.common.domain.request.MqReq; +import com.bwie.common.redis.RedisCache; +import com.bwie.mq.mapper.MqMapper; +import com.bwie.mq.util.MsgUtil; +import com.rabbitmq.client.Channel; +import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.HashSet; + +@Component +@Log4j2 +@RabbitListener(queues = "queue") +public class MqConsumer { + private final RedisCache redisCache; + private final MqMapper mqMapper; + private final MsgUtil msgUtil; + + public MqConsumer(RedisCache redisCache, MqMapper mqMapper, MsgUtil msgUtil) { + this.redisCache = redisCache; + this.mqMapper = mqMapper; + this.msgUtil = msgUtil; + } + + @RabbitHandler + public void send(MqReq mqReq, Channel channel, Message message) throws IOException { + log.info("{}信息为:",mqReq,message.getMessageProperties().getDeliveryTag()); + String messageId = message.getMessageProperties().getMessageId(); + long deliveryTag = message.getMessageProperties().getDeliveryTag(); + if(!redisCache.hasKey("id:"+messageId)){ + redisCache.setCacheObject("id:"+messageId,deliveryTag); + } + int size = redisCache.getCacheSet(messageId).size(); + try { + if(0==size){ + HashSet strings = new HashSet<>(); + strings.add(messageId); + redisCache.setCacheSet(messageId,strings); + mqMapper.upd(mqReq.getMiddId(),5); + log.info("已提交退款"); + Thread.sleep(1800); + mqMapper.upd(mqReq.getMiddId(),6); + msgUtil.sendMsg(mqReq.getUserPhone(),"1111"); + log.info("客户您好:已经成功退款"); + channel.basicAck(deliveryTag,false); + + }else { + log.info("消息重复"); + channel.basicReject(deliveryTag,false); + } + } catch (InterruptedException e) { + Long cacheObject = redisCache.getCacheObject("id:" + messageId); + if((cacheObject+2)==deliveryTag){ + log.info("消息消费不了 不入队了"); + channel.basicNack(deliveryTag,false,false); + }else { + log.info("消息消费不了 重新入队了"); + channel.basicNack(deliveryTag,false,true); + } + } + } + + + + + + + + +} diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/controller/MqController.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/controller/MqController.java new file mode 100644 index 0000000..006405c --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/controller/MqController.java @@ -0,0 +1,30 @@ +package com.bwie.mq.controller; + +import com.bwie.common.domain.request.MqReq; +import com.bwie.mq.config.MqConfig; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.UUID; + +@RestController +@ResponseBody +public class MqController { + private final RabbitTemplate rabbitTemplate; + + public MqController(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + } + + @PostMapping("updstatus") + void updstatus(@RequestBody MqReq build){ + rabbitTemplate.convertAndSend(MqConfig.EXCHANGE,MqConfig.KEY,build,message -> { + message.getMessageProperties().setMessageId(UUID.randomUUID().toString()); + return message; + },new CorrelationData(UUID.randomUUID().toString())); + } +} diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/mapper/MqMapper.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/mapper/MqMapper.java new file mode 100644 index 0000000..4389a83 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/mapper/MqMapper.java @@ -0,0 +1,5 @@ +package com.bwie.mq.mapper; + +public interface MqMapper { + void upd(Long middId,Integer status); +} diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/util/HttpUtils.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/util/HttpUtils.java new file mode 100644 index 0000000..1f2d952 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/util/HttpUtils.java @@ -0,0 +1,319 @@ +package com.bwie.mq.util; + +import com.alibaba.fastjson.JSON; +import org.apache.commons.lang.StringUtils; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class HttpUtils { + + /** + * get + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @return + * @throws Exception + */ + public static List doGet(String host, String path, String method, + Map headers, + Map querys + ,Class clazz) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpGet request = new HttpGet(buildUrl(host, path, querys)); + if(headers!=null){ + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + } + HttpResponse execute = httpClient.execute(request); + String s = EntityUtils.toString(execute.getEntity(),"UTF-8"); + List ts = JSON.parseArray(s, clazz); + return ts; + } + + /** + * post form + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param bodys + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, String method, + Map headers, + Map querys, + Map bodys) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (bodys != null) { + List nameValuePairList = new ArrayList(); + + for (String key : bodys.keySet()) { + nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); + } + UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); + formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); + request.setEntity(formEntity); + } + + return httpClient.execute(request); + } + + /** + * Post String + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, String method, + Map headers, + Map querys, + String body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (StringUtils.isNotBlank(body)) { + request.setEntity(new StringEntity(body, "utf-8")); + } + + return httpClient.execute(request); + } + + /** + * Post stream + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, String method, + Map headers, + Map querys, + byte[] body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (body != null) { + request.setEntity(new ByteArrayEntity(body)); + } + + return httpClient.execute(request); + } + + /** + * Put String + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPut(String host, String path, String method, + Map headers, + Map querys, + String body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPut request = new HttpPut(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (StringUtils.isNotBlank(body)) { + request.setEntity(new StringEntity(body, "utf-8")); + } + + return httpClient.execute(request); + } + + /** + * Put stream + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPut(String host, String path, String method, + Map headers, + Map querys, + byte[] body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPut request = new HttpPut(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (body != null) { + request.setEntity(new ByteArrayEntity(body)); + } + + return httpClient.execute(request); + } + + /** + * Delete + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @return + * @throws Exception + */ + public static HttpResponse doDelete(String host, String path, String method, + Map headers, + Map querys) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + return httpClient.execute(request); + } + + private static String buildUrl(String host, String path, Map querys) throws UnsupportedEncodingException { + StringBuilder sbUrl = new StringBuilder(); + sbUrl.append(host); + if (!StringUtils.isBlank(path)) { + sbUrl.append(path); + } + if (null != querys) { + StringBuilder sbQuery = new StringBuilder(); + for (Map.Entry query : querys.entrySet()) { + if (0 < sbQuery.length()) { + sbQuery.append("&"); + } + if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { + sbQuery.append(query.getValue()); + } + if (!StringUtils.isBlank(query.getKey())) { + sbQuery.append(query.getKey()); + if (!StringUtils.isBlank(query.getValue())) { + sbQuery.append("="); + sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); + } + } + } + if (0 < sbQuery.length()) { + sbUrl.append("?").append(sbQuery); + } + } + + return sbUrl.toString(); + } + + private static HttpClient wrapClient(String host) { + HttpClient httpClient = new DefaultHttpClient(); + if (host.startsWith("https://")) { + sslClient(httpClient); + } + + return httpClient; + } + + private static void sslClient(HttpClient httpClient) { + try { + SSLContext ctx = SSLContext.getInstance("TLS"); + X509TrustManager tm = new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted(X509Certificate[] xcs, String str) { + + } + public void checkServerTrusted(X509Certificate[] xcs, String str) { + + } + }; + ctx.init(null, new TrustManager[] { tm }, null); + SSLSocketFactory ssf = new SSLSocketFactory(ctx); + ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + ClientConnectionManager ccm = httpClient.getConnectionManager(); + SchemeRegistry registry = ccm.getSchemeRegistry(); + registry.register(new Scheme("https", 443, ssf)); + } catch (KeyManagementException ex) { + throw new RuntimeException(ex); + } catch (NoSuchAlgorithmException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/util/MsgUtil.java b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/util/MsgUtil.java new file mode 100644 index 0000000..a783868 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/java/com/bwie/mq/util/MsgUtil.java @@ -0,0 +1,80 @@ +package com.bwie.mq.util; + + +import org.apache.http.HttpResponse; +import org.apache.http.util.EntityUtils; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author + * @version 1.0.0 + * @ClassName MsgUtil.java + * @Description TODO + * @createTime 2022年05月26日 15:49:00 + */ +@Component +public class MsgUtil { + + +// //列表添加 +// //@CacheConfig(cacheNames = "aaa") +// //列表 +// //@Cacheable +// //实时更新 +// //@CacheEvict(allEntries = true) +// +// @Cacheable(key = "#phone",value = "bbb") +// public String getCacheCode(String phone){ +// +// return null; +// } +// +// @CachePut(key = "#codeEntity.phone",value = "bbb") +// public String saveCacheCode(CodeEntity codeEntity){ +// +// return codeEntity.getCode(); +// } + +// 858f87d76b7f4e618a5f7e1557f1d529 + + public void sendMsg(String phone,String code){ + String host = "https://gyytz.market.alicloudapi.com"; + String path = "/sms/smsSend"; + String method = "POST"; + String appcode = "13a8b5b781bc4c458ce2de65ed79be80"; + Map headers = new HashMap(); + //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 + headers.put("Authorization", "APPCODE " + appcode); + Map querys = new HashMap(); + querys.put("mobile", phone); + querys.put("param", "**code**:"+code+",**minute**:5"); + +//smsSignId(短信前缀)和templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html + + querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2"); + querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad"); + Map bodys = new HashMap(); + + + try { + /** + * 重要提示如下: + * HttpUtils请从\r\n\t \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t \t* 下载 + * + * 相应的依赖请参照 + * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml + */ + HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); + System.out.println(response.toString()); + //获取response的body + System.out.println(EntityUtils.toString(response.getEntity())); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/bwie-modules/bwie-mq/src/main/resources/bootstrap.yml b/bwie-modules/bwie-mq/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..1e8c586 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9006 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-mq + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-mq/src/main/resources/mapper/MqMapper.xml b/bwie-modules/bwie-mq/src/main/resources/mapper/MqMapper.xml new file mode 100644 index 0000000..40273e8 --- /dev/null +++ b/bwie-modules/bwie-mq/src/main/resources/mapper/MqMapper.xml @@ -0,0 +1,8 @@ + + + + + + UPDATE midd set ord_status=#{status} where midd_id=#{middId} + + diff --git a/bwie-modules/bwie-ord/pom.xml b/bwie-modules/bwie-ord/pom.xml new file mode 100644 index 0000000..aa18504 --- /dev/null +++ b/bwie-modules/bwie-ord/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-ord + + + 17 + 17 + UTF-8 + + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/OrdApp.java b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/OrdApp.java new file mode 100644 index 0000000..b055f0f --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/OrdApp.java @@ -0,0 +1,19 @@ +package com.bwie.ord; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.bwie.**") +@MapperScan("com.bwie.ord.mapper") +@EnableScheduling +public class OrdApp { + public static void main(String[] args) { + SpringApplication.run(OrdApp.class); + } +} diff --git a/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/controller/OrdController.java b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/controller/OrdController.java new file mode 100644 index 0000000..bd71223 --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/controller/OrdController.java @@ -0,0 +1,39 @@ +package com.bwie.ord.controller; + +import com.bwie.common.domain.request.AddoReq; +import com.bwie.common.domain.request.BackReq; +import com.bwie.common.domain.request.PayReq; +import com.bwie.common.result.Result; +import com.bwie.ord.service.OrdService; +import org.springframework.web.bind.annotation.*; + +@RestController +@ResponseBody +public class OrdController { + private final OrdService ordService; + public OrdController(OrdService ordService) { + this.ordService = ordService; + } + @PostMapping("addord") + public Result addord(@RequestBody AddoReq addoReq){ + Result addord = ordService.addord(addoReq); + return addord; + } + @PostMapping("pay") + public Result pay(@RequestBody PayReq payReq){ + Result addord = ordService.pay(payReq); + return addord; + } + @GetMapping("show") + public Result show(){ + Result addord = ordService.show(); + return addord; + } + + @PostMapping("back") + public Result back(@RequestBody BackReq backReq){ + ordService.back(backReq); + return Result.success(); + } + +} diff --git a/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/mapper/OrdMapper.java b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/mapper/OrdMapper.java new file mode 100644 index 0000000..959b554 --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/mapper/OrdMapper.java @@ -0,0 +1,23 @@ +package com.bwie.ord.mapper; + +import com.bwie.common.domain.Ord; +import com.bwie.common.domain.request.AddoReq; +import com.bwie.common.domain.request.OrdNum; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface OrdMapper { + void addord(AddoReq addoReq); + + void addmidd(@Param("s") String s, @Param("carNum") Integer carNum, @Param("goodsId") Long goodsId, @Param("ordStatus") Integer ordStatus); + + void findback(); + + + List findwill(String s); + + void updstatus(Integer ordStatus,String s); + + List show(Long userId); +} diff --git a/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/service/OrdService.java b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/service/OrdService.java new file mode 100644 index 0000000..c70de26 --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/service/OrdService.java @@ -0,0 +1,18 @@ +package com.bwie.ord.service; + +import com.bwie.common.domain.request.AddoReq; +import com.bwie.common.domain.request.BackReq; +import com.bwie.common.domain.request.PayReq; +import com.bwie.common.result.Result; + +import java.util.List; + +public interface OrdService { + + Result addord(AddoReq addoReq); + Result pay(PayReq payReq); + + Result show(); + + void back(BackReq backReq); +} diff --git a/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/service/OrdServiceImpl.java b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/service/OrdServiceImpl.java new file mode 100644 index 0000000..fd87e0d --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/service/OrdServiceImpl.java @@ -0,0 +1,124 @@ +package com.bwie.ord.service; + +import cn.hutool.core.util.RandomUtil; +import com.bwie.common.constant.JwtConstants; +import com.bwie.common.constant.TokenConstants; +import com.bwie.common.domain.Ord; +import com.bwie.common.domain.User; +import com.bwie.common.domain.request.AddoReq; +import com.bwie.common.domain.request.BackReq; +import com.bwie.common.domain.request.MqReq; +import com.bwie.common.domain.request.PayReq; +import com.bwie.common.domain.response.Info; +import com.bwie.common.redis.RedisCache; +import com.bwie.common.remote.goods.GoodsRemoteService; +import com.bwie.common.remote.mq.MqRemoteService; +import com.bwie.common.remote.user.UserRemoteService; +import com.bwie.common.result.Result; +import com.bwie.ord.mapper.OrdMapper; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; + +@Service +public class OrdServiceImpl implements OrdService{ + private final RedisCache redisCache; + private final HttpServletRequest request; + private final UserRemoteService userRemoteService; + private final GoodsRemoteService goodsRemoteService; + private final MqRemoteService mqRemoteService; + private final OrdMapper ordMapper; + + public OrdServiceImpl(RedisCache redisCache, HttpServletRequest request, UserRemoteService userRemoteService, GoodsRemoteService goodsRemoteService, MqRemoteService mqRemoteService, OrdMapper ordMapper) { + this.redisCache = redisCache; + this.request = request; + this.userRemoteService = userRemoteService; + this.goodsRemoteService = goodsRemoteService; + this.mqRemoteService = mqRemoteService; + this.ordMapper = ordMapper; + } + @Override + public Result addord(AddoReq addoReq) { + + addoReq.setUserId(info().getUserId()); + String s = RandomUtil.randomNumbers(6); + addoReq.setOrdHao(s); + + ordMapper.addord(addoReq); + ordMapper.addmidd(s,addoReq.getCarNum(),addoReq.getGoodsId(),1); +// List findwill = ordMapper.findwill(s); +// return Result.success(findwill); + if(addoReq.getGoodsPrice()>info().getUserYe()){ + return Result.error("余额不足"); + } + userRemoteService.incremoney(addoReq.getGoodsPrice(), info().getUserId()); + ordMapper.updstatus(2,s); + goodsRemoteService.updnum(addoReq.getGoodsId(), addoReq.getCarNum()); + return Result.success(); + + +// } +// userRemoteService.incremoney(addoReq.getOrdMoney(),info().getUserId()); +// +// addoReq.setUserId(info().getUserId()); +// String s = RandomUtil.randomNumbers(6); +// addoReq.setOrdHao(s); +// addoReq.setOrdStatus(2); +// ordMapper.addord(addoReq); +// ordMapper.addmidd(s,addoReq.getGoodsIds()); +// return Result.success(); + } + + @Override + public Result pay(PayReq payReq) { +return Result.success(); + } + + @Override + public Result show() { + List show = ordMapper.show(info().getUserId()); + return Result.success(show); + } + + @Override + public void back(BackReq backReq) { + MqReq build = MqReq.builder() + .middId(backReq.getMiddId()) + .userPhone(info().getUserPhone()) + .build(); + mqRemoteService.updstatus(build); + goodsRemoteService.addnum(backReq.getGoodsNum(),backReq.getGoodsId()); + userRemoteService.addmoney(info().getUserId(),backReq.getOrdTruth()); + + } + + + public Info info() { + User cacheObject = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + request.getHeader(JwtConstants.USER_KEY)); + + return Info.builder() + .userPhone(cacheObject.getUserPhone()) + .userId(cacheObject.getUserId()) + .userName(cacheObject.getUserName()) + .userRole(cacheObject.getUserRole()) + .userYe(cacheObject.getUserYe()) + .build(); + } +} + + + + + + + + + + + + + + + + diff --git a/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/task/MyTask.java b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/task/MyTask.java new file mode 100644 index 0000000..9ea3320 --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/java/com/bwie/ord/task/MyTask.java @@ -0,0 +1,20 @@ +package com.bwie.ord.task; + +import com.bwie.ord.mapper.OrdMapper; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class MyTask { + private final OrdMapper ordMapper; + + public MyTask(OrdMapper ordMapper) { + this.ordMapper = ordMapper; + } + + @Scheduled(cron = "0/30 * * * * *") + public void back(){ + ordMapper.findback(); + } + +} diff --git a/bwie-modules/bwie-ord/src/main/resources/bootstrap.yml b/bwie-modules/bwie-ord/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..c2547a6 --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9007 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-ord + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-ord/src/main/resources/mapper/OrdMapper.xml b/bwie-modules/bwie-ord/src/main/resources/mapper/OrdMapper.xml new file mode 100644 index 0000000..36e7b85 --- /dev/null +++ b/bwie-modules/bwie-ord/src/main/resources/mapper/OrdMapper.xml @@ -0,0 +1,34 @@ + + + + + + insert into ord (`ord_hao`,`ord_money`,`ord_truth`,`user_id`) + values (#{ordHao},#{goodsPrice},#{goodsPrice},#{userId}) + + + insert into midd (`ord_hao`,`goods_id`,`goods_num`,`ord_time`,`ord_status`) + values + (#{s},#{goodsId},#{carNum},now(),#{ordStatus}) + + + + UPDATE midd set ord_status=8 where ord_status=1 and TIMESTAMPDIFF(MINUTE,ord_time,now())>=20 + + + UPDATE midd set ord_status=#{ordStatus} where ord_hao=#{s} + + + + diff --git a/bwie-modules/bwie-system/pom.xml b/bwie-modules/bwie-system/pom.xml new file mode 100644 index 0000000..247900e --- /dev/null +++ b/bwie-modules/bwie-system/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-system + + + 17 + 17 + UTF-8 + + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/bwie-modules/bwie-system/src/main/java/com/bwie/system/SysApp.java b/bwie-modules/bwie-system/src/main/java/com/bwie/system/SysApp.java new file mode 100644 index 0000000..0fa9a65 --- /dev/null +++ b/bwie-modules/bwie-system/src/main/java/com/bwie/system/SysApp.java @@ -0,0 +1,18 @@ +package com.bwie.system; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.bwie.**") +@MapperScan("com.bwie.system.mapper") +public class SysApp { + public static void main(String[] args) { + SpringApplication.run(SysApp.class); + } +} diff --git a/bwie-modules/bwie-system/src/main/java/com/bwie/system/controller/SysController.java b/bwie-modules/bwie-system/src/main/java/com/bwie/system/controller/SysController.java new file mode 100644 index 0000000..5be42b7 --- /dev/null +++ b/bwie-modules/bwie-system/src/main/java/com/bwie/system/controller/SysController.java @@ -0,0 +1,35 @@ +package com.bwie.system.controller; + +import com.bwie.common.domain.User; +import com.bwie.common.result.Result; +import com.bwie.system.service.SysService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@ResponseBody +public class SysController { + private final SysService sysService; + + public SysController(SysService sysService) { + this.sysService = sysService; + } + + + @PostMapping("findname") + public Result findname(@RequestParam String userName){ + User findname = sysService.findname(userName); + return Result.success(findname); + } + + @PostMapping("incremoney") + void incremoney(@RequestParam Double ordMoney, Long userId){ + sysService.incremoney(ordMoney,userId); + } + @PostMapping("addmoney") + void addmoney(@RequestParam Long userId, Double ordTruth){ + sysService.addmoney(ordTruth,userId); + } +} diff --git a/bwie-modules/bwie-system/src/main/java/com/bwie/system/mapper/SysMapper.java b/bwie-modules/bwie-system/src/main/java/com/bwie/system/mapper/SysMapper.java new file mode 100644 index 0000000..fda2942 --- /dev/null +++ b/bwie-modules/bwie-system/src/main/java/com/bwie/system/mapper/SysMapper.java @@ -0,0 +1,10 @@ +package com.bwie.system.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.bwie.common.domain.User; + +public interface SysMapper extends BaseMapper { + void incremoney(Double ordMoney, Long userId); + + void addmoney(Double ordTruth, Long userId); +} diff --git a/bwie-modules/bwie-system/src/main/java/com/bwie/system/service/SysService.java b/bwie-modules/bwie-system/src/main/java/com/bwie/system/service/SysService.java new file mode 100644 index 0000000..fa39217 --- /dev/null +++ b/bwie-modules/bwie-system/src/main/java/com/bwie/system/service/SysService.java @@ -0,0 +1,14 @@ +package com.bwie.system.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bwie.common.domain.User; + +public interface SysService extends IService { + User findname(String userName); + + void incremoney(Double ordMoney, Long userId); + + void addmoney(Double ordTruth, Long userId); + + +} diff --git a/bwie-modules/bwie-system/src/main/java/com/bwie/system/service/SysServiceImpl.java b/bwie-modules/bwie-system/src/main/java/com/bwie/system/service/SysServiceImpl.java new file mode 100644 index 0000000..963b194 --- /dev/null +++ b/bwie-modules/bwie-system/src/main/java/com/bwie/system/service/SysServiceImpl.java @@ -0,0 +1,35 @@ +package com.bwie.system.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bwie.common.domain.User; +import com.bwie.system.mapper.SysMapper; +import org.springframework.stereotype.Service; + +@Service +public class SysServiceImpl extends ServiceImpl implements SysService { + private final SysMapper sysMapper; + + public SysServiceImpl(SysMapper sysMapper) { + this.sysMapper = sysMapper; + } + + @Override + public User findname(String userName) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(User::getUserName,userName); + User user = sysMapper.selectOne(queryWrapper); + return user; + } + + @Override + public void incremoney(Double ordMoney, Long userId) { + sysMapper.incremoney(ordMoney,userId); + } + + @Override + public void addmoney(Double ordTruth, Long userId) { + + sysMapper.addmoney(ordTruth,userId); + } +} diff --git a/bwie-modules/bwie-system/src/main/resources/bootstrap.yml b/bwie-modules/bwie-system/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..51939cf --- /dev/null +++ b/bwie-modules/bwie-system/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9002 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-system + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-system/src/main/resources/mapper/SysMapper.xml b/bwie-modules/bwie-system/src/main/resources/mapper/SysMapper.xml new file mode 100644 index 0000000..5dc3be7 --- /dev/null +++ b/bwie-modules/bwie-system/src/main/resources/mapper/SysMapper.xml @@ -0,0 +1,11 @@ + + + + + update user set user_ye=user_ye+#{ordTruth} where user_id=#{userId} + + + + update user set user_ye=user_ye-#{ordMoney} where user_id=#{userId} + + diff --git a/bwie-modules/bwie-type/pom.xml b/bwie-modules/bwie-type/pom.xml new file mode 100644 index 0000000..b11d4df --- /dev/null +++ b/bwie-modules/bwie-type/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.bwie + bwie-modules + 1.0.0 + + + bwie-type + + + 17 + 17 + UTF-8 + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + diff --git a/bwie-modules/bwie-type/src/main/java/com/bwie/type/TypeApp.java b/bwie-modules/bwie-type/src/main/java/com/bwie/type/TypeApp.java new file mode 100644 index 0000000..154e70b --- /dev/null +++ b/bwie-modules/bwie-type/src/main/java/com/bwie/type/TypeApp.java @@ -0,0 +1,29 @@ +package com.bwie.type; + +import com.bwie.type.service.TypeService; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +import javax.annotation.PostConstruct; + +@SpringBootApplication +@EnableDiscoveryClient +@MapperScan("com.bwie.type.mapper") +public class TypeApp { + private final TypeService typeService; + + public TypeApp(TypeService typeService) { + this.typeService = typeService; + } + + public static void main(String[] args) { + SpringApplication.run(TypeApp.class); + } + @PostConstruct + public void init(){ + typeService.show(); + } + +} diff --git a/bwie-modules/bwie-type/src/main/java/com/bwie/type/controller/TypeController.java b/bwie-modules/bwie-type/src/main/java/com/bwie/type/controller/TypeController.java new file mode 100644 index 0000000..5977fdd --- /dev/null +++ b/bwie-modules/bwie-type/src/main/java/com/bwie/type/controller/TypeController.java @@ -0,0 +1,31 @@ +package com.bwie.type.controller; + +import com.bwie.common.domain.Type; +import com.bwie.common.result.Result; +import com.bwie.type.service.TypeService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@ResponseBody +public class TypeController { + private final TypeService typeService; + + + public TypeController(TypeService typeService) { + this.typeService = typeService; + } + @GetMapping ("show") + public Result show(){ + List show = typeService.show(); + return Result.success(show); + } + + + + +} diff --git a/bwie-modules/bwie-type/src/main/java/com/bwie/type/mapper/TypeMapper.java b/bwie-modules/bwie-type/src/main/java/com/bwie/type/mapper/TypeMapper.java new file mode 100644 index 0000000..36f1779 --- /dev/null +++ b/bwie-modules/bwie-type/src/main/java/com/bwie/type/mapper/TypeMapper.java @@ -0,0 +1,7 @@ +package com.bwie.type.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.bwie.common.domain.Type; + +public interface TypeMapper extends BaseMapper { +} diff --git a/bwie-modules/bwie-type/src/main/java/com/bwie/type/service/TypeService.java b/bwie-modules/bwie-type/src/main/java/com/bwie/type/service/TypeService.java new file mode 100644 index 0000000..ce90b77 --- /dev/null +++ b/bwie-modules/bwie-type/src/main/java/com/bwie/type/service/TypeService.java @@ -0,0 +1,10 @@ +package com.bwie.type.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bwie.common.domain.Type; + +import java.util.List; + +public interface TypeService extends IService { + List show(); +} diff --git a/bwie-modules/bwie-type/src/main/java/com/bwie/type/service/TypeServiceImpl.java b/bwie-modules/bwie-type/src/main/java/com/bwie/type/service/TypeServiceImpl.java new file mode 100644 index 0000000..16d9f69 --- /dev/null +++ b/bwie-modules/bwie-type/src/main/java/com/bwie/type/service/TypeServiceImpl.java @@ -0,0 +1,35 @@ +package com.bwie.type.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bwie.common.domain.Type; +import com.bwie.common.redis.RedisCache; +import com.bwie.type.mapper.TypeMapper; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class TypeServiceImpl extends ServiceImpl implements TypeService{ + private final TypeMapper typeMapper; + private final RedisCache redisCache; + + public TypeServiceImpl(TypeMapper typeMapper, RedisCache redisCache) { + this.typeMapper = typeMapper; + this.redisCache = redisCache; + } + + @Override + public List show() { + if(!redisCache.hasKey("type:")){ + LambdaQueryWrapper typeLambdaQueryWrapper = new LambdaQueryWrapper<>(); + List types = typeMapper.selectList(typeLambdaQueryWrapper); + redisCache.setCacheList("type:",types); + } + + List cacheList = redisCache.getCacheList("type:"); + + return cacheList; + } +} diff --git a/bwie-modules/bwie-type/src/main/resources/bootstrap.yml b/bwie-modules/bwie-type/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..98b4238 --- /dev/null +++ b/bwie-modules/bwie-type/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9004 +# Spring +spring: + main: + allow-circular-references: true + allow-bean-definition-overriding: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-type + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.177.197:8848 + config: + # 配置中心地址 + server-addr: 124.221.177.197:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-modules/bwie-type/src/main/resources/mapper/TypeMapper.xml b/bwie-modules/bwie-type/src/main/resources/mapper/TypeMapper.xml new file mode 100644 index 0000000..23b995d --- /dev/null +++ b/bwie-modules/bwie-type/src/main/resources/mapper/TypeMapper.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/bwie-modules/pom.xml b/bwie-modules/pom.xml new file mode 100644 index 0000000..ff46d57 --- /dev/null +++ b/bwie-modules/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + com.bwie + test_month + 1.0.0 + + + bwie-modules + pom + + bwie-system + bwie-mq + bwie-es + bwie-goods + bwie-type + bwie-car + bwie-ord + + + + 17 + 17 + UTF-8 + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2f1a4b0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.bwie + test_month + 1.0.0 + pom + + bwie-common + bwie-auth + bwie-gateway + bwie-modules + + + + 17 + 17 + UTF-8 + + + + + + spring-boot-starter-parent + org.springframework.boot + 2.6.2 + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2021.0.0 + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2021.1 + pom + import + + + + com.alibaba.nacos + nacos-client + 2.0.4 + + + + + com.bwie + bwie-common + 1.0.0 + + + + + +