commit 3aea2b6e097cf5b34fbbf6ffb96c22e1d6ac72af Author: 86199 Date: Tue Nov 7 21:26:41 2023 +0800 初始化 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..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..b9127c1 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://165.154.161.166:3306/lx + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..4454fed --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4871806 --- /dev/null +++ b/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + pom + + wzy-auth + wzy-common + wzy-gateway + wzy-models + wzy-models/wzy-user + wzy-models/wzy-product + wzy-models/wzy-es + + + + 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.wzy + wzy-common + 1.0-SNAPSHOT + + + + diff --git a/wzy-auth/.gitignore b/wzy-auth/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-auth/.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/wzy-auth/pom.xml b/wzy-auth/pom.xml new file mode 100644 index 0000000..d06229c --- /dev/null +++ b/wzy-auth/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + + + wzy-auth + + + 17 + 17 + UTF-8 + + + + + com.wzy + wzy-common + + + + org.springframework.boot + spring-boot-starter-web + + + diff --git a/wzy-auth/src/main/java/com/wzy/auth/AuthApplication.java b/wzy-auth/src/main/java/com/wzy/auth/AuthApplication.java new file mode 100644 index 0000000..5936fc5 --- /dev/null +++ b/wzy-auth/src/main/java/com/wzy/auth/AuthApplication.java @@ -0,0 +1,13 @@ +package com.wzy.auth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients +public class AuthApplication { + public static void main(String[] args) { + SpringApplication.run(AuthApplication.class); + } +} diff --git a/wzy-auth/src/main/java/com/wzy/auth/controller/AuthController.java b/wzy-auth/src/main/java/com/wzy/auth/controller/AuthController.java new file mode 100644 index 0000000..52540e2 --- /dev/null +++ b/wzy-auth/src/main/java/com/wzy/auth/controller/AuthController.java @@ -0,0 +1,46 @@ +package com.wzy.auth.controller; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.auth.service.AuthService; +import com.wzy.common.domain.User; +import com.wzy.common.domain.request.LoginRequest; +import com.wzy.common.domain.response.JwtResponse; +import com.wzy.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@Log4j2 +public class AuthController { + + @Autowired + private AuthService authService; + + @Autowired + private HttpServletRequest request; + + @PostMapping("/login") + public Result login(@RequestBody LoginRequest loginRequest){ + log.info("功能:登录,请求UIR:{},请求方式:{},请求参数:{}",request.getRequestURI(), + request.getMethod(),loginRequest); + Result result = authService.login(loginRequest); + log.info("功能:登录,请求UIR:{},请求方式:{},响应结果:{}",request.getRequestURI(), + request.getMethod(), JSONObject.toJSONString(result)); + return result; + } + + @GetMapping("/user/info") + public Result userInfo(){ + User user = authService.userInfo(); + return Result.success(user); + } + + @PostMapping("/logout") + public Result logout(){ + authService.logout(); + return Result.success(); + } +} diff --git a/wzy-auth/src/main/java/com/wzy/auth/feign/UserFeignService.java b/wzy-auth/src/main/java/com/wzy/auth/feign/UserFeignService.java new file mode 100644 index 0000000..4ccd9e7 --- /dev/null +++ b/wzy-auth/src/main/java/com/wzy/auth/feign/UserFeignService.java @@ -0,0 +1,13 @@ +package com.wzy.auth.feign; + +import com.wzy.common.domain.User; +import com.wzy.common.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +@FeignClient("wzy-user") +public interface UserFeignService { + @PostMapping("/login/{username}") + Result login(@PathVariable String username); +} diff --git a/wzy-auth/src/main/java/com/wzy/auth/service/AuthService.java b/wzy-auth/src/main/java/com/wzy/auth/service/AuthService.java new file mode 100644 index 0000000..a9713e4 --- /dev/null +++ b/wzy-auth/src/main/java/com/wzy/auth/service/AuthService.java @@ -0,0 +1,14 @@ +package com.wzy.auth.service; + +import com.wzy.common.domain.User; +import com.wzy.common.domain.request.LoginRequest; +import com.wzy.common.domain.response.JwtResponse; +import com.wzy.common.result.Result; + +public interface AuthService { + Result login(LoginRequest loginRequest); + + User userInfo(); + + void logout(); +} diff --git a/wzy-auth/src/main/java/com/wzy/auth/service/impl/AuthServiceImpl.java b/wzy-auth/src/main/java/com/wzy/auth/service/impl/AuthServiceImpl.java new file mode 100644 index 0000000..9d0a1f7 --- /dev/null +++ b/wzy-auth/src/main/java/com/wzy/auth/service/impl/AuthServiceImpl.java @@ -0,0 +1,72 @@ +package com.wzy.auth.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.auth.feign.UserFeignService; +import com.wzy.auth.service.AuthService; +import com.wzy.common.constants.JwtConstants; +import com.wzy.common.constants.TokenConstants; +import com.wzy.common.domain.User; +import com.wzy.common.domain.request.LoginRequest; +import com.wzy.common.domain.response.JwtResponse; +import com.wzy.common.result.Result; +import com.wzy.common.utils.JwtUtils; +import com.wzy.common.utils.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +@Service +public class AuthServiceImpl implements AuthService { + + @Autowired + private UserFeignService userFeignService; + + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private HttpServletRequest request; + @Override + public Result login(LoginRequest loginRequest) { + Result byName = userFeignService.login(loginRequest.getUsername()); + User data = byName.getData(); + if (null == data) { + return Result.error("用户不存在!"); + } + if (!loginRequest.getPassword().equals(data.getPassword())) { + return Result.error("密码错误!"); + } + if (StringUtils.isAllBlank(loginRequest.getUsername(),loginRequest.getPassword())) { + return Result.error("用户或密码不能为空!"); + } + String userKey = UUID.randomUUID().toString().replaceAll("-", ""); + HashMap map = new HashMap<>(); + map.put(JwtConstants.USER_KEY,userKey); + String token = JwtUtils.createToken(map); + redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSONObject.toJSONString(data),30, TimeUnit.MINUTES); + JwtResponse jwtResponse = new JwtResponse(); + jwtResponse.setExpireTime("30MIN"); + jwtResponse.setToken(token); + return Result.success(jwtResponse); + } + + @Override + public User userInfo() { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + return JSONObject.parseObject(s,User.class); + } + + @Override + public void logout() { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY+userKey); + } +} diff --git a/wzy-auth/src/main/resources/bootstrap.yml b/wzy-auth/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..bcbb870 --- /dev/null +++ b/wzy-auth/src/main/resources/bootstrap.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9003 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: wzy-auth + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 165.154.161.166:8848 + config: + # 配置中心地址 + server-addr: 165.154.161.166:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + diff --git a/wzy-common/.gitignore b/wzy-common/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-common/.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/wzy-common/pom.xml b/wzy-common/pom.xml new file mode 100644 index 0000000..b4457e3 --- /dev/null +++ b/wzy-common/pom.xml @@ -0,0 +1,116 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + + + wzy-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 + + + + com.aliyun.oss + aliyun-sdk-oss + 3.12.0 + + + + org.springframework.boot + spring-boot-starter-amqp + + + + com.github.tobato + fastdfs-client + 1.26.5 + + + diff --git a/wzy-common/src/main/java/com/wzy/common/config/RedisConfig.java b/wzy-common/src/main/java/com/wzy/common/config/RedisConfig.java new file mode 100644 index 0000000..02462ef --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/config/RedisConfig.java @@ -0,0 +1,40 @@ +package com.wzy.common.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +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.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new + Jackson2JsonRedisSerializer(Object.class); + ObjectMapper om = new ObjectMapper(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(om); + + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + // key采用String的序列化方式 + template.setKeySerializer(stringRedisSerializer); + // hash的key也采用String的序列化方式 + template.setHashKeySerializer(stringRedisSerializer); + // value序列化方式采用jackson + template.setValueSerializer(jackson2JsonRedisSerializer); + // hash的value序列化方式采用jackson + template.setHashValueSerializer(jackson2JsonRedisSerializer); + template.afterPropertiesSet(); + + return template; + } +} diff --git a/wzy-common/src/main/java/com/wzy/common/constants/Constants.java b/wzy-common/src/main/java/com/wzy/common/constants/Constants.java new file mode 100644 index 0000000..41f728a --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/constants/Constants.java @@ -0,0 +1,18 @@ +package com.wzy.common.constants; + +/** + * @description: 系统常量 + * @author DongZl + */ +public class Constants { + /** + * 成功标记 + */ + public static final Integer SUCCESS = 200; + public static final String SUCCESS_MSG = "操作成功"; + /** + * 失败标记 + */ + public static final Integer ERROR = 500; + public static final String ERROR_MSG = "操作异常"; +} diff --git a/wzy-common/src/main/java/com/wzy/common/constants/JwtConstants.java b/wzy-common/src/main/java/com/wzy/common/constants/JwtConstants.java new file mode 100644 index 0000000..b8d22af --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/constants/JwtConstants.java @@ -0,0 +1,29 @@ +package com.wzy.common.constants; + +/** + * @author DongZl + * @description: Jwt常量 + */ +public class JwtConstants { + + /** + * 用户ID字段 + */ + public static final String DETAILS_USER_ID = "user_id"; + + /** + * 用户名字段 + */ + public static final String DETAILS_USERNAME = "username"; + + /** + * 用户标识 + */ + public static final String USER_KEY = "user_key"; + + /** + * 令牌秘钥 + */ + public final static String SECRET = "abcdefghijklmnopqrstuvwxyz"; + +} diff --git a/wzy-common/src/main/java/com/wzy/common/constants/TokenConstants.java b/wzy-common/src/main/java/com/wzy/common/constants/TokenConstants.java new file mode 100644 index 0000000..51402e6 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/constants/TokenConstants.java @@ -0,0 +1,24 @@ +package com.wzy.common.constants; + +/** + * @author DongZl + * @description: 令牌常量 + */ +public class TokenConstants { + /** + * 缓存有效期,默认720(分钟) + */ + public final static long EXPIRATION = 720; + /** + * 缓存刷新时间,默认120(分钟) + */ + public final static long REFRESH_TIME = 120; + /** + * 权限缓存前缀 + */ + public final static String LOGIN_TOKEN_KEY = "login_tokens:"; + /** + * token标识 + */ + public static final String TOKEN = "token"; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/Check.java b/wzy-common/src/main/java/com/wzy/common/domain/Check.java new file mode 100644 index 0000000..5036676 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/Check.java @@ -0,0 +1,18 @@ +package com.wzy.common.domain; + +import lombok.Data; + +import java.util.Date; + +@Data +public class Check { + private Integer checkId; + private Date checkDate; + private Double bargainPrice; + + private Integer userId; + private String username; + + private Integer productId; + private String productName; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/Product.java b/wzy-common/src/main/java/com/wzy/common/domain/Product.java new file mode 100644 index 0000000..d9def61 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/Product.java @@ -0,0 +1,29 @@ +package com.wzy.common.domain; + +import lombok.Data; + +import java.util.Date; + +@Data +public class Product { + private String productId; + private String productName; + private String productType; + private String productPic; + private String productDesc; + private Double productPrice; + private Integer bargainNum; + private Double bargainPrice; + private Date bargainDate; + private Integer joinNum; + private Integer successNum; + private Integer beBeingNum; + private Integer status; + + private Integer userId; + private Integer number; + + private Integer ruleId; + private String ruleName; + private Integer ruleStatus; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/Rule.java b/wzy-common/src/main/java/com/wzy/common/domain/Rule.java new file mode 100644 index 0000000..facceb8 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/Rule.java @@ -0,0 +1,16 @@ +package com.wzy.common.domain; + +import lombok.Data; + +@Data +public class Rule { + private Integer ruleId; + private Integer startNum; + private Integer endNum; + private Double startRange; + private Double sendRange; + private Integer typeId; + private String typeName; + private String ruleName; + private Integer ruleStatus; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/Type.java b/wzy-common/src/main/java/com/wzy/common/domain/Type.java new file mode 100644 index 0000000..6f60942 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/Type.java @@ -0,0 +1,9 @@ +package com.wzy.common.domain; + +import lombok.Data; + +@Data +public class Type { + private Integer typeId; + private String typeName; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/User.java b/wzy-common/src/main/java/com/wzy/common/domain/User.java new file mode 100644 index 0000000..15561ef --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/User.java @@ -0,0 +1,11 @@ +package com.wzy.common.domain; + +import lombok.Data; + +@Data +public class User { + private Integer userId; + private String username; + private String password; + private Integer number; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/request/LoginRequest.java b/wzy-common/src/main/java/com/wzy/common/domain/request/LoginRequest.java new file mode 100644 index 0000000..1cb2d78 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/request/LoginRequest.java @@ -0,0 +1,9 @@ +package com.wzy.common.domain.request; + +import lombok.Data; + +@Data +public class LoginRequest { + private String username; + private String password; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/request/Req.java b/wzy-common/src/main/java/com/wzy/common/domain/request/Req.java new file mode 100644 index 0000000..e5620f5 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/request/Req.java @@ -0,0 +1,11 @@ +package com.wzy.common.domain.request; + +import lombok.Data; + +@Data +public class Req { + private Integer pageNum=1; + private Integer pageSize=5; + private String productName; + private String productType; +} diff --git a/wzy-common/src/main/java/com/wzy/common/domain/response/JwtResponse.java b/wzy-common/src/main/java/com/wzy/common/domain/response/JwtResponse.java new file mode 100644 index 0000000..8c3bd78 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/domain/response/JwtResponse.java @@ -0,0 +1,9 @@ +package com.wzy.common.domain.response; + +import lombok.Data; + +@Data +public class JwtResponse { + private String token; + private String expireTime; +} diff --git a/wzy-common/src/main/java/com/wzy/common/result/PageResult.java b/wzy-common/src/main/java/com/wzy/common/result/PageResult.java new file mode 100644 index 0000000..9c7424a --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/result/PageResult.java @@ -0,0 +1,34 @@ +package com.wzy.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/wzy-common/src/main/java/com/wzy/common/result/Result.java b/wzy-common/src/main/java/com/wzy/common/result/Result.java new file mode 100644 index 0000000..bbb8cb2 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/result/Result.java @@ -0,0 +1,76 @@ +package com.wzy.common.result; + +import com.wzy.common.constants.Constants; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author DongZl + * @description: 响应信息主体 + */ +@Data +public class Result implements Serializable { + + private static final long serialVersionUID = 1L; + /** + * 成功 + */ + public static final int SUCCESS = Constants.SUCCESS; + /** + * 失败 + */ + public static final int FAIL = Constants.ERROR; + /** + * 返回状态码 + */ + private int code; + /** + * 响应信息 + */ + private String msg; + /** + * 响应数据 + */ + private T data; + + public static Result success() { + return restResult(null, SUCCESS, Constants.SUCCESS_MSG); + } + + public static Result success(T data) { + return restResult(data, SUCCESS, Constants.SUCCESS_MSG); + } + + public static Result success(T data, String msg) { + return restResult(data, SUCCESS, msg); + } + + public static Result error() { + return restResult(null, FAIL, Constants.ERROR_MSG); + } + + public static Result error(String msg) { + return restResult(null, FAIL, msg); + } + + public static Result error(T data) { + return restResult(data, FAIL, Constants.ERROR_MSG); + } + + public static Result error(T data, String msg) { + return restResult(data, FAIL, msg); + } + + public static Result error(int code, String msg) { + return restResult(null, code, msg); + } + + private static Result restResult(T data, int code, String msg) { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } +} diff --git a/wzy-common/src/main/java/com/wzy/common/utils/FastUtil.java b/wzy-common/src/main/java/com/wzy/common/utils/FastUtil.java new file mode 100644 index 0000000..a5b065e --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/utils/FastUtil.java @@ -0,0 +1,55 @@ +package com.wzy.common.utils; + +import org.springframework.stereotype.Component; +import com.github.tobato.fastdfs.domain.fdfs.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; + +/** + * @BelongsProject: 0107day02 + * @BelongsPackage: com.bw.config + * @Author: zhupengfei + * @CreateTime: 2023-02-01 08:52 + */ +@Component +public class FastUtil { + private static final Logger log = LoggerFactory.getLogger(FastUtil.class); + + @Resource + private FastFileStorageClient storageClient ; + + /** + * 上传文件 + */ + public String upload(MultipartFile multipartFile) throws Exception{ + String originalFilename = multipartFile.getOriginalFilename(). + substring(multipartFile.getOriginalFilename(). + lastIndexOf(".") + 1); + StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( + multipartFile.getInputStream(), + multipartFile.getSize(),originalFilename , null); + return storePath.getFullPath() ; + } + /** + * 删除文件 + */ + public String deleteFile(String fileUrl) { + if (StringUtils.isEmpty(fileUrl)) { + log.info("fileUrl == >>文件路径为空..."); + return "文件路径不能为空"; + } + try { + StorePath storePath = StorePath.parseFromUrl(fileUrl); + storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); + } catch (Exception e) { + log.error(e.getMessage()); + } + return "删除成功"; + } + +} diff --git a/wzy-common/src/main/java/com/wzy/common/utils/JwtUtils.java b/wzy-common/src/main/java/com/wzy/common/utils/JwtUtils.java new file mode 100644 index 0000000..5358ed5 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/utils/JwtUtils.java @@ -0,0 +1,109 @@ +package com.wzy.common.utils; + +import com.wzy.common.constants.JwtConstants; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; + +import java.util.Map; + +/** + * @description: Jwt工具类 + * @author DongZl + */ +public class JwtUtils { + + /** + * 秘钥 + */ + public static String secret = JwtConstants.SECRET; + + /** + * 从数据声明生成令牌 + * + * @param claims 数据声明 + * @return 令牌 + */ + public static String createToken(Map claims){ + String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact(); + return token; + } + + /** + * 从令牌中获取数据声明 + * + * @param token 令牌 + * @return 数据声明 + */ + public static Claims parseToken(String token) { + return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + } + /** + * 根据令牌获取用户标识 + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserKey(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户标识 + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserKey(Claims claims){ + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户ID + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserId(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据身份信息获取用户ID + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserId(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据令牌获取用户名 + * + * @param token 令牌 + * @return 用户名 + */ + public static String getUserName(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取用户名 + * + * @param claims 身份信息 + * @return 用户名 + */ + public static String getUserName(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取键值 + * + * @param claims 身份信息 + * @param key 键 + * @return 值 + */ + public static String getValue(Claims claims, String key){ + Object obj = claims.get(key); + return obj == null ? "" : obj.toString(); + } +} diff --git a/wzy-common/src/main/java/com/wzy/common/utils/StringUtils.java b/wzy-common/src/main/java/com/wzy/common/utils/StringUtils.java new file mode 100644 index 0000000..fbc3917 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/utils/StringUtils.java @@ -0,0 +1,68 @@ +package com.wzy.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/wzy-common/src/main/java/com/wzy/common/utils/TelSmsUtils.java b/wzy-common/src/main/java/com/wzy/common/utils/TelSmsUtils.java new file mode 100644 index 0000000..bb1a106 --- /dev/null +++ b/wzy-common/src/main/java/com/wzy/common/utils/TelSmsUtils.java @@ -0,0 +1,87 @@ +package com.wzy.common.utils; + +import com.alibaba.fastjson.JSONObject; +import com.aliyun.dysmsapi20170525.Client; +import com.aliyun.dysmsapi20170525.models.SendSmsRequest; +import com.aliyun.dysmsapi20170525.models.SendSmsResponse; +import com.aliyun.teaopenapi.models.Config; +import lombok.extern.log4j.Log4j2; + +import java.util.Map; + +/** + * 短信工具类 + */ +@Log4j2 +public class TelSmsUtils { + + /** + * 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 + */ + private static String accessKeyId = "LTAIEVXszCmcd1T5"; + private static String accessKeySecret = "2zHwciQXln8wExSEnkIYtRTSwLeRNd"; + + /** + * 短信访问域名 + */ + private static String endpoint = "dysmsapi.aliyuncs.com"; + /** + * 短信签名 + */ + private static String signName = "登录验证"; + + /** + * 实例化短信对象 + */ + private static Client client; + + static { + log.info("初始化短信服务开始"); + long startTime = System.currentTimeMillis(); + try { + client = initClient(); + log.info("初始化短信成功:{}",signName); + } catch (Exception e) { + e.printStackTrace(); + } + log.info("初始化短信服务结束:耗时:{}MS",(System.currentTimeMillis()-startTime)); + } + /** + * 初始化短信对象 + * @return + * @throws Exception + */ + private static Client initClient() throws Exception{ + Config config = new Config() + // 您的AccessKey ID + .setAccessKeyId(accessKeyId) + // 您的AccessKey Secret + .setAccessKeySecret(accessKeySecret); + // 访问的域名 + config.endpoint = endpoint; + return new Client(config); + } + + /** + * 发送单条短信 + * @param tel + * @param templateCode SMS_153991546 + * @param sendDataMap + */ + public static String sendSms(String tel , String templateCode , Map sendDataMap){ + SendSmsRequest sendSmsRequest = new SendSmsRequest() + .setPhoneNumbers(tel) + .setSignName(signName) + .setTemplateCode(templateCode) + .setTemplateParam(JSONObject.toJSONString(sendDataMap)); + SendSmsResponse sendSmsResponse = null; + try { + log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap)); + sendSmsResponse = client.sendSms(sendSmsRequest); + } catch (Exception e) { + log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e); + } + return JSONObject.toJSONString(sendSmsResponse.getBody()); + } + +} diff --git a/wzy-common/src/main/resources/META-INF/spring.factories b/wzy-common/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..465b3c9 --- /dev/null +++ b/wzy-common/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ + com.wzy.common.config.RedisConfig,\ + com.wzy.common.utils.FastUtil \ No newline at end of file diff --git a/wzy-gateway/.gitignore b/wzy-gateway/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-gateway/.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/wzy-gateway/pom.xml b/wzy-gateway/pom.xml new file mode 100644 index 0000000..68f4f13 --- /dev/null +++ b/wzy-gateway/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + + + wzy-gateway + + + 17 + 17 + UTF-8 + + + + + com.wzy + wzy-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/wzy-gateway/src/main/java/com/wzy/gateway/GatewayApplication.java b/wzy-gateway/src/main/java/com/wzy/gateway/GatewayApplication.java new file mode 100644 index 0000000..ffc34e9 --- /dev/null +++ b/wzy-gateway/src/main/java/com/wzy/gateway/GatewayApplication.java @@ -0,0 +1,11 @@ +package com.wzy.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GatewayApplication { + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class); + } +} diff --git a/wzy-gateway/src/main/java/com/wzy/gateway/config/IgnoreWhiteConfig.java b/wzy-gateway/src/main/java/com/wzy/gateway/config/IgnoreWhiteConfig.java new file mode 100644 index 0000000..cff8f0b --- /dev/null +++ b/wzy-gateway/src/main/java/com/wzy/gateway/config/IgnoreWhiteConfig.java @@ -0,0 +1,32 @@ +package com.wzy.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/wzy-gateway/src/main/java/com/wzy/gateway/filters/AuthFilter.java b/wzy-gateway/src/main/java/com/wzy/gateway/filters/AuthFilter.java new file mode 100644 index 0000000..0ab7224 --- /dev/null +++ b/wzy-gateway/src/main/java/com/wzy/gateway/filters/AuthFilter.java @@ -0,0 +1,58 @@ +package com.wzy.gateway.filters; + +import com.wzy.common.constants.TokenConstants; +import com.wzy.common.utils.JwtUtils; +import com.wzy.common.utils.StringUtils; +import com.wzy.gateway.config.IgnoreWhiteConfig; +import com.wzy.gateway.utils.GatewayUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.data.redis.core.RedisTemplate; +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.List; + +@Component +public class AuthFilter implements GlobalFilter, Ordered { + + @Autowired + private IgnoreWhiteConfig ignoreWhiteConfig; + + @Autowired + private RedisTemplate redisTemplate; + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + List whites = ignoreWhiteConfig.getWhites(); + ServerHttpRequest request = exchange.getRequest(); + String path = request.getURI().getPath(); + if (StringUtils.matches(path,whites)) { + return chain.filter(exchange); + } + String token = request.getHeaders().getFirst(TokenConstants.TOKEN); + if (StringUtils.isEmpty(token)) { + return GatewayUtils.errorResponse(exchange,"token不能为空!"); + } + try { + JwtUtils.parseToken(token); + } catch (Exception e) { + return GatewayUtils.errorResponse(exchange,"token不合法!"); + } + String userKey = JwtUtils.getUserKey(token); + Boolean hasKey = redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey); + if (Boolean.FALSE.equals(hasKey)) { + return GatewayUtils.errorResponse(exchange,"token失效!"); + } + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return 0; + } +} diff --git a/wzy-gateway/src/main/java/com/wzy/gateway/utils/GatewayUtils.java b/wzy-gateway/src/main/java/com/wzy/gateway/utils/GatewayUtils.java new file mode 100644 index 0000000..3ebb092 --- /dev/null +++ b/wzy-gateway/src/main/java/com/wzy/gateway/utils/GatewayUtils.java @@ -0,0 +1,98 @@ +package com.wzy.gateway.utils; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.common.result.Result; +import com.wzy.common.utils.StringUtils; +import lombok.extern.log4j.Log4j2; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * @author DongZl + * @description: 网关处理工具类 + */ +@Log4j2 +public class GatewayUtils { + /** + * 添加请求头参数 + * @param mutate 修改对象 + * @param key 键 + * @param value 值 + */ + public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) { + if (StringUtils.isEmpty(key)){ + log.warn("添加请求头参数键不可以为空"); + return; + } + if (value == null) { + log.warn("添加请求头参数:[{}]值为空",key); + return; + } + String valueStr = value.toString(); + mutate.header(key, valueStr); + log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value); + } + + /** + * 删除请求头参数 + * @param mutate 修改对象 + * @param key 键 + */ + public static void removeHeader(ServerHttpRequest.Builder mutate, String key) { + if (StringUtils.isEmpty(key)){ + log.warn("删除请求头参数键不可以为空"); + return; + } + mutate.headers(httpHeaders -> httpHeaders.remove(key)).build(); + log.info("删除请求头参数 - 键:[{}]",key); + } + + /** + * 错误结果响应 + * @param exchange 响应上下文 + * @param msg 响应消息 + * @return + */ + public static Mono errorResponse(ServerWebExchange exchange, String msg, HttpStatus httpStatus) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(httpStatus); + //设置HTTP响应头文本格式 + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json"); + //定义响应内容 + Result result = Result.error(msg); + String resultJson = JSONObject.toJSONString(result); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson); + DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes()); + //进行响应 + return response.writeWith(Mono.just(dataBuffer)); + } + + /** + * 错误结果响应 + * @param exchange 响应上下文 + * @param msg 响应消息 + * @return + */ + public static Mono errorResponse(ServerWebExchange exchange, String msg) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(HttpStatus.OK); + //设置HTTP响应头文本格式 + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json"); + //定义响应内容 + Result result = Result.error(msg); + String resultJson = JSONObject.toJSONString(result); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson); + DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes()); + //进行响应 + return response.writeWith(Mono.just(dataBuffer)); + } + + +} diff --git a/wzy-gateway/src/main/resources/bootstrap.yml b/wzy-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..3435dc1 --- /dev/null +++ b/wzy-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# Tomcat +server: + port: 18080 +# Spring +spring: + application: + # 应用名称 + name: wzy-gateway + profiles: + # 环境配置 + active: dev + main: + # 允许使用循环引用 + allow-circular-references: true + # 允许定义相同的bean对象 去覆盖原有的 + allow-bean-definition-overriding: true + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 165.154.161.166:8848 + config: + # 配置中心地址 + server-addr: 165.154.161.166:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/wzy-models/.gitignore b/wzy-models/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-models/.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/wzy-models/pom.xml b/wzy-models/pom.xml new file mode 100644 index 0000000..95bddac --- /dev/null +++ b/wzy-models/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + + + wzy-models + + + 17 + 17 + UTF-8 + + + diff --git a/wzy-models/wzy-es/.gitignore b/wzy-models/wzy-es/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-models/wzy-es/.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/wzy-models/wzy-es/pom.xml b/wzy-models/wzy-es/pom.xml new file mode 100644 index 0000000..05fad25 --- /dev/null +++ b/wzy-models/wzy-es/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + ../../pom.xml + + + wzy-es + + + 17 + 17 + UTF-8 + + + + + + com.wzy + wzy-common + + + org.springframework.boot + spring-boot-starter-web + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + + + + + diff --git a/wzy-models/wzy-es/src/main/java/com/wzy/es/EsApplication.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/EsApplication.java new file mode 100644 index 0000000..f4f6de2 --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/EsApplication.java @@ -0,0 +1,15 @@ +package com.wzy.es; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableFeignClients +@EnableScheduling +public class EsApplication { + public static void main(String[] args) { + SpringApplication.run(EsApplication.class); + } +} diff --git a/wzy-models/wzy-es/src/main/java/com/wzy/es/config/InitEsRes.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/config/InitEsRes.java new file mode 100644 index 0000000..f5eca4d --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/config/InitEsRes.java @@ -0,0 +1,25 @@ +package com.wzy.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/wzy-models/wzy-es/src/main/java/com/wzy/es/controller/EsController.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/controller/EsController.java new file mode 100644 index 0000000..0327342 --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/controller/EsController.java @@ -0,0 +1,34 @@ +package com.wzy.es.controller; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.common.domain.request.Req; +import com.wzy.common.result.Result; +import com.wzy.es.service.EsService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@Log4j2 +public class EsController { + + @Autowired + private EsService esService; + + @Autowired + private HttpServletRequest request; + + @PostMapping("/list") + public Result list(@RequestBody Req req){ + log.info("功能:ES列表,请求UIR:{},请求方式:{},请求参数:{}",request.getRequestURI(), + request.getMethod(),req); + Result result = esService.list(req); + log.info("功能:ES列表,请求UIR:{},请求方式:{},响应结果:{}",request.getRequestURI(), + request.getMethod(), JSONObject.toJSONString(result)); + return result; + } +} diff --git a/wzy-models/wzy-es/src/main/java/com/wzy/es/feign/ProductFeignService.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/feign/ProductFeignService.java new file mode 100644 index 0000000..cc6d94b --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/feign/ProductFeignService.java @@ -0,0 +1,14 @@ +package com.wzy.es.feign; + +import com.wzy.common.domain.Product; +import com.wzy.common.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; + +@FeignClient("wzy-product") +public interface ProductFeignService { + @GetMapping("/findProductList") + Result> findProductList(); +} diff --git a/wzy-models/wzy-es/src/main/java/com/wzy/es/service/EsService.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/service/EsService.java new file mode 100644 index 0000000..3080122 --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/service/EsService.java @@ -0,0 +1,8 @@ +package com.wzy.es.service; + +import com.wzy.common.domain.request.Req; +import com.wzy.common.result.Result; + +public interface EsService { + Result list(Req req); +} diff --git a/wzy-models/wzy-es/src/main/java/com/wzy/es/service/impl/EsServiceImpl.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/service/impl/EsServiceImpl.java new file mode 100644 index 0000000..da6a4db --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/service/impl/EsServiceImpl.java @@ -0,0 +1,83 @@ +package com.wzy.es.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.common.domain.Product; +import com.wzy.common.domain.request.Req; +import com.wzy.common.result.PageResult; +import com.wzy.common.result.Result; +import com.wzy.common.utils.StringUtils; +import com.wzy.es.service.EsService; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.text.Text; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Map; + +@Service +public class EsServiceImpl implements EsService { + + @Autowired + private RestHighLevelClient restHighLevelClient; + + private static final String INDEX_NAME = "product"; + + @Override + public Result list(Req req) { + long total = 0; + ArrayList list = new ArrayList<>(); + + try { + SearchRequest searchRequest = new SearchRequest(INDEX_NAME); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); + if (StringUtils.isNotEmpty(req.getProductName())) { + boolQueryBuilder.must(QueryBuilders.matchQuery("productName",req.getProductName())); + } + if (StringUtils.isNotEmpty(req.getProductType())) { + boolQueryBuilder.must(QueryBuilders.matchQuery("productType",req.getProductType())); + } + searchSourceBuilder.query(boolQueryBuilder); + HighlightBuilder highlightBuilder = new HighlightBuilder(); + highlightBuilder.field("goodsName").preTags("").postTags(""); + searchSourceBuilder.highlighter(highlightBuilder); + searchRequest.source(searchSourceBuilder); + SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); + SearchHits hits = search.getHits(); + total = hits.getTotalHits().value; + for (SearchHit hit : hits.getHits()) { + String sourceAsString = hit.getSourceAsString(); + Product product = JSONObject.parseObject(sourceAsString, Product.class); + product.setProductId(hit.getId()); + Map highlightFields = hit.getHighlightFields(); + if (null != highlightFields) { + HighlightField highlightField = highlightFields.get("productName"); + if (null != highlightField) { + String str = ""; + for (Text fragment : highlightField.fragments()) { + str += fragment; + } + product.setProductName(str); + } + } + list.add(product); + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + return PageResult.toResult(total,list); + } +} diff --git a/wzy-models/wzy-es/src/main/java/com/wzy/es/sync/EsSync.java b/wzy-models/wzy-es/src/main/java/com/wzy/es/sync/EsSync.java new file mode 100644 index 0000000..c7b8ce8 --- /dev/null +++ b/wzy-models/wzy-es/src/main/java/com/wzy/es/sync/EsSync.java @@ -0,0 +1,69 @@ +package com.wzy.es.sync; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; +import com.wzy.common.domain.Product; +import com.wzy.common.result.Result; +import com.wzy.es.feign.ProductFeignService; +import lombok.extern.log4j.Log4j2; +import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; +import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse; +import org.elasticsearch.action.bulk.BulkRequest; +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.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Component +@Log4j2 +public class EsSync { + + @Autowired + private ProductFeignService productFeignService; + + @Autowired + private RestHighLevelClient restHighLevelClient; + + private static final String INDEX_NAME = "product"; + + @Scheduled(cron = "0 * * * * *") + public void runEs() throws IOException { + Result> productList = productFeignService.findProductList(); + List data = productList.getData(); + long l = System.currentTimeMillis(); + if (!CollectionUtils.isEmpty(data)) { + BulkRequest bulkRequest = new BulkRequest(); + data.forEach(item -> { + SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); + filter.getExcludes().addAll(new ArrayList(){{ + add("productId"); + }}); + bulkRequest.add( + new IndexRequest(INDEX_NAME) + .source(JSON.toJSONString(item,filter), XContentType.JSON) + .id(item.getProductId()) + ); + }); + restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); + log.info("功能:同步ES消耗:{}毫秒",(System.currentTimeMillis())-l); + + ClearIndicesCacheRequest cacheRequest = new ClearIndicesCacheRequest(); + cacheRequest.indices(INDEX_NAME); + cacheRequest.fieldDataCache(true); + cacheRequest.queryCache(true); + ClearIndicesCacheResponse cacheResponse = restHighLevelClient.indices().clearCache(cacheRequest, RequestOptions.DEFAULT); + log.info("清除ES索引缓存成功"+cacheResponse); + Date date = new Date(); + log.info("当前时间:"+date); + } + } +} diff --git a/wzy-models/wzy-es/src/main/resources/bootstrap.yml b/wzy-models/wzy-es/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..f267a8b --- /dev/null +++ b/wzy-models/wzy-es/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# Tomcat +server: + port: 9005 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: wzy-es + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 165.154.161.166:8848 + config: + # 配置中心地址 + server-addr: 165.154.161.166:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/wzy-models/wzy-product/.gitignore b/wzy-models/wzy-product/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-models/wzy-product/.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/wzy-models/wzy-product/pom.xml b/wzy-models/wzy-product/pom.xml new file mode 100644 index 0000000..523d902 --- /dev/null +++ b/wzy-models/wzy-product/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + ../../pom.xml + + + wzy-product + + + 17 + 17 + UTF-8 + + + + + com.wzy + wzy-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 + + + diff --git a/wzy-models/wzy-product/src/main/java/com/wzy/product/ProductApplication.java b/wzy-models/wzy-product/src/main/java/com/wzy/product/ProductApplication.java new file mode 100644 index 0000000..b09137d --- /dev/null +++ b/wzy-models/wzy-product/src/main/java/com/wzy/product/ProductApplication.java @@ -0,0 +1,13 @@ +package com.wzy.product; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableScheduling +public class ProductApplication { + public static void main(String[] args) { + SpringApplication.run(ProductApplication.class); + } +} diff --git a/wzy-models/wzy-product/src/main/java/com/wzy/product/controller/ProductController.java b/wzy-models/wzy-product/src/main/java/com/wzy/product/controller/ProductController.java new file mode 100644 index 0000000..39ab336 --- /dev/null +++ b/wzy-models/wzy-product/src/main/java/com/wzy/product/controller/ProductController.java @@ -0,0 +1,115 @@ +package com.wzy.product.controller; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.common.domain.Check; +import com.wzy.common.domain.Product; +import com.wzy.common.domain.Rule; +import com.wzy.common.domain.User; +import com.wzy.common.result.Result; +import com.wzy.product.service.ProductService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; + +@RestController +@Log4j2 +public class ProductController { + + @Autowired + private ProductService productService; + + @Autowired + private HttpServletRequest request; + + @GetMapping("/findProductList") + public Result> findProductList(){ + List findProductList = productService.findProductList(); + return Result.success(findProductList); + } + + @GetMapping("/ruleList") + @Scheduled(cron = "0 * * * * *") + public Result> ruleList(){ + List ruleList = productService.ruleList(); + return Result.success(ruleList); + } + + @PostMapping("/ruleSave") + public Result ruleSave(@RequestBody Rule rule){ + log.info("功能:新增规则,请求URI:{},请求方式:{},请求参数:{}",request.getRequestURI(), + request.getMethod(),rule); + Result result = productService.ruleSave(rule); + log.info("功能:新增规则,请求URI:{},请求方式:{},响应结果:{}",request.getRequestURI(), + request.getMethod(), JSONObject.toJSONString(result)); + return result; + } + + @GetMapping("/choiceProduct") + public Result> choiceProduct(){ + List choiceProduct = productService.choiceProduct(); + return Result.success(choiceProduct); + } + + @PostMapping("/productSave") + public Result productSave(@RequestBody Product product){ + log.info("功能:新增商品,请求URI:{},请求方式:{},请求参数:{}",request.getRequestURI(), + request.getMethod(),product); + Result result = productService.productSave(product); + log.info("功能:新增商品,请求URI:{},请求方式:{},响应结果:{}",request.getRequestURI(), + request.getMethod(), JSONObject.toJSONString(result)); + return result; + } + + @PostMapping("/startUp/{productId}") + public Result startUp(@PathVariable Integer productId){ + Result result = productService.startUp(productId); + return result; + } + + @PostMapping("/stopUp/{productId}") + public Result stopUp(@PathVariable Integer productId){ + Result result = productService.stopUp(productId); + return result; + } + + @PostMapping("/upload") + public Result upload(@RequestParam("file")MultipartFile multipartFile){ + Result result = productService.upload(multipartFile); + return result; + } + + @PostMapping("/updateProduct") + public Result updateProduct(@RequestBody Product product){ + Result result = productService.updateProduct(product); + return result; + } + + @PostMapping("/updateUser") + public Result updateUser(@RequestBody User user){ + Result result = productService.updateUser(user); + return result; + } + + @PostMapping("/checkAdd") + public Result checkAdd(@RequestBody Check check){ + Result result = productService.checkAdd(check); + return result; + } + + @GetMapping("/checkList") + public Result> checkList(){ + List checkList = productService.checkList(); + return Result.success(checkList); + } + + @PostMapping("/updateUser1") + public Result updateUser1(@RequestBody User user){ + Result result = productService.updateUser1(user); + return result; + } +} diff --git a/wzy-models/wzy-product/src/main/java/com/wzy/product/mapper/ProductMapper.java b/wzy-models/wzy-product/src/main/java/com/wzy/product/mapper/ProductMapper.java new file mode 100644 index 0000000..45cb04d --- /dev/null +++ b/wzy-models/wzy-product/src/main/java/com/wzy/product/mapper/ProductMapper.java @@ -0,0 +1,36 @@ +package com.wzy.product.mapper; + +import com.wzy.common.domain.Check; +import com.wzy.common.domain.Product; +import com.wzy.common.domain.Rule; +import com.wzy.common.domain.User; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface ProductMapper { + List findProductList(); + + List ruleList(); + + int ruleSave(Rule rule); + + List choiceProduct(); + + int productSave(Product product); + + int startUp(Integer productId); + + int stopUp(Integer productId); + + int updateProduct(Product product); + + int updateUser(User user); + + int checkAdd(Check check); + + List checkList(); + + int updateUser1(User user); +} diff --git a/wzy-models/wzy-product/src/main/java/com/wzy/product/service/ProductService.java b/wzy-models/wzy-product/src/main/java/com/wzy/product/service/ProductService.java new file mode 100644 index 0000000..0808f5c --- /dev/null +++ b/wzy-models/wzy-product/src/main/java/com/wzy/product/service/ProductService.java @@ -0,0 +1,38 @@ +package com.wzy.product.service; + +import com.wzy.common.domain.Check; +import com.wzy.common.domain.Product; +import com.wzy.common.domain.Rule; +import com.wzy.common.domain.User; +import com.wzy.common.result.Result; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +public interface ProductService { + List findProductList(); + + List ruleList(); + + Result ruleSave(Rule rule); + + List choiceProduct(); + + Result productSave(Product product); + + Result startUp(Integer productId); + + Result stopUp(Integer productId); + + Result upload(MultipartFile multipartFile); + + Result updateProduct(Product product); + + Result updateUser(User user); + + Result checkAdd(Check check); + + List checkList(); + + Result updateUser1(User user); +} diff --git a/wzy-models/wzy-product/src/main/java/com/wzy/product/service/impl/ProductServiceImpl.java b/wzy-models/wzy-product/src/main/java/com/wzy/product/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000..c89adbb --- /dev/null +++ b/wzy-models/wzy-product/src/main/java/com/wzy/product/service/impl/ProductServiceImpl.java @@ -0,0 +1,158 @@ +package com.wzy.product.service.impl; +import cn.hutool.core.util.RandomUtil; +import com.wzy.common.domain.Check; +import com.wzy.common.domain.Product; +import com.wzy.common.domain.Rule; +import com.wzy.common.domain.User; +import com.wzy.common.result.Result; +import com.wzy.common.utils.FastUtil; +import com.wzy.common.utils.StringUtils; +import com.wzy.product.mapper.ProductMapper; +import com.wzy.product.service.ProductService; +import org.apache.commons.lang3.RandomUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@Service +public class ProductServiceImpl implements ProductService { + + @Autowired + private ProductMapper productMapper; + + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private FastUtil fastUtil; + + @Override + public List findProductList() { + return productMapper.findProductList(); + } + + @Override + public List ruleList() { + return productMapper.ruleList(); + } + + @Override + public Result ruleSave(Rule rule) { + int code = RandomUtils.nextInt(10, 30); + redisTemplate.opsForValue().set("code", String.valueOf(code),1,TimeUnit.MINUTES); + rule.setStartNum(code); + if (StringUtils.isEmpty(rule.getRuleName())) { + return Result.error("规则名称不能为空!"); + } + if (rule.getStartNum() > rule.getEndNum()) { + return Result.error("截止次数不可小于起始次数!"); + } + int i = productMapper.ruleSave(rule); + if (i > 0) { + return Result.success(i,"规则新增成功!"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public List choiceProduct() { + return productMapper.choiceProduct(); + } + + @Override + public Result productSave(Product product) { + int i = productMapper.productSave(product); + if (i > 0) { + return Result.success(i,"新增成功"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public Result startUp(Integer productId) { + int i = productMapper.startUp(productId); + if (i > 0) { + return Result.success(i,"开启砍价模式"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public Result stopUp(Integer productId) { + int i = productMapper.stopUp(productId); + if (i > 0) { + return Result.success(i,"关闭砍价模式"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public Result upload(MultipartFile multipartFile) { + String upload = null; + try { + upload = fastUtil.upload(multipartFile); + if (null == upload) { + return Result.error("上传失败"); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + return Result.success("http://165.154.161.166:8888/" + upload,"上传成功"); + } + + @Override + public Result updateProduct(Product product) { + double v = RandomUtils.nextDouble(0.1, 1); + product.setBargainPrice(v); + int i = productMapper.updateProduct(product); + if (i > 0) { + return Result.success(i,"砍价成功"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public Result updateUser(User user) { + int i = productMapper.updateUser(user); + if (i > 0) { + return Result.success(i,"砍价次数更新成功"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public Result checkAdd(Check check) { + double v = RandomUtils.nextDouble(0.1, 1); + check.setBargainPrice(v); + int i = productMapper.checkAdd(check); + if (i > 0) { + return Result.success(i,"砍价记录添加成功"); + } else { + return Result.error("请求失败"); + } + } + + @Override + public List checkList() { + return productMapper.checkList(); + } + + @Override + public Result updateUser1(User user) { + int i = productMapper.updateUser1(user); + if (i > 0) { + return Result.success(i,"砍价次数更新成功"); + } else { + return Result.error("请求失败"); + } + } +} diff --git a/wzy-models/wzy-product/src/main/resources/bootstrap.yml b/wzy-models/wzy-product/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..0543663 --- /dev/null +++ b/wzy-models/wzy-product/src/main/resources/bootstrap.yml @@ -0,0 +1,42 @@ +# Tomcat +server: + port: 9002 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: wzy-product + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 165.154.161.166:8848 + config: + # 配置中心地址 + server-addr: 165.154.161.166:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + +fdfs: + so-timeout: 1500 # socket 连接时长 + connect-timeout: 600 # 连接 tracker 服务器超时时长 + # 这两个是你服务器的 IP 地址,注意 23000 端口也要打开,阿里云服务器记得配置安全组。tracker 要和 stroage 服务进行交流 + tracker-list: 165.154.161.166:22122 + web-server-url: 165.154.161.166:8888 + pool: + jmx-enabled: false + # 生成缩略图 + thumb-image: + height: 500 + width: 500 diff --git a/wzy-models/wzy-product/src/main/resources/mapper/ProductMapper.xml b/wzy-models/wzy-product/src/main/resources/mapper/ProductMapper.xml new file mode 100644 index 0000000..887856e --- /dev/null +++ b/wzy-models/wzy-product/src/main/resources/mapper/ProductMapper.xml @@ -0,0 +1,79 @@ + + + + + insert into t_rule (start_num, end_num, start_range, send_range, type_id, rule_name) + values ( + #{startNum}, + #{endNum}, + #{startRange}, + #{sendRange}, + #{typeId}, + #{ruleName} + ) + + + insert into t_product (product_name, product_type, product_pic, product_desc, product_price, + bargain_num, bargain_price, bargain_date, join_num, success_num, be_being_num, status, + user_id, rule_id) + values ( + #{productName}, + #{productType}, + #{productPic}, + #{productDesc}, + #{productPrice}, + #{bargainNum}, + #{bargainPrice}, + now(), + 5000, + 300, + 2000, + 2, + 1, + #{ruleId} + ) + + + insert into t_check (check_date, user_id, bargain_price, product_id) + values ( + #{checkDate}, + #{userId}, + #{bargainPrice}, + #{productId} + ) + + + update t_product set status=1 where product_id=#{productId} + + + update t_product set status=2 where product_id=#{productId} + + + update t_product set bargain_num=bargain_num+1,bargain_price=bargain_price-#{bargainPrice} where product_id=#{productId} + + + update t_user set number=number-1 where user_id=#{userId} + + + update t_user set number=number+1 where user_id=#{userId} + + + + + + + diff --git a/wzy-models/wzy-user/.gitignore b/wzy-models/wzy-user/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/wzy-models/wzy-user/.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/wzy-models/wzy-user/pom.xml b/wzy-models/wzy-user/pom.xml new file mode 100644 index 0000000..44f1d1e --- /dev/null +++ b/wzy-models/wzy-user/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + com.wzy + wzy-11.7yk + 1.0-SNAPSHOT + ../../pom.xml + + + wzy-user + + + 17 + 17 + UTF-8 + + + + + com.wzy + wzy-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 + + + diff --git a/wzy-models/wzy-user/src/main/java/com/wzy/user/UserApplication.java b/wzy-models/wzy-user/src/main/java/com/wzy/user/UserApplication.java new file mode 100644 index 0000000..3c76be0 --- /dev/null +++ b/wzy-models/wzy-user/src/main/java/com/wzy/user/UserApplication.java @@ -0,0 +1,11 @@ +package com.wzy.user; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class UserApplication { + public static void main(String[] args) { + SpringApplication.run(UserApplication.class); + } +} diff --git a/wzy-models/wzy-user/src/main/java/com/wzy/user/controller/UserController.java b/wzy-models/wzy-user/src/main/java/com/wzy/user/controller/UserController.java new file mode 100644 index 0000000..3f60a62 --- /dev/null +++ b/wzy-models/wzy-user/src/main/java/com/wzy/user/controller/UserController.java @@ -0,0 +1,35 @@ +package com.wzy.user.controller; + +import com.alibaba.fastjson.JSONObject; +import com.wzy.common.domain.User; +import com.wzy.common.result.Result; +import com.wzy.user.service.UserService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@Log4j2 +public class UserController { + + @Autowired + private UserService userService; + + @Autowired + private HttpServletRequest request; + + @PostMapping("/login/{username}") + public Result login(@PathVariable String username){ + log.info("功能:登录,请求UIR:{},请求方式:{},请求参数:{}",request.getRequestURI(), + request.getMethod(),username); + User user = userService.login(username); + Result result = Result.success(user); + log.info("功能:登录,请求UIR:{},请求方式:{},响应结果:{}",request.getRequestURI(), + request.getMethod(), JSONObject.toJSONString(result)); + return result; + } +} diff --git a/wzy-models/wzy-user/src/main/java/com/wzy/user/mapper/UserMapper.java b/wzy-models/wzy-user/src/main/java/com/wzy/user/mapper/UserMapper.java new file mode 100644 index 0000000..b454b5e --- /dev/null +++ b/wzy-models/wzy-user/src/main/java/com/wzy/user/mapper/UserMapper.java @@ -0,0 +1,9 @@ +package com.wzy.user.mapper; + +import com.wzy.common.domain.User; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface UserMapper { + User login(String username); +} diff --git a/wzy-models/wzy-user/src/main/java/com/wzy/user/service/UserService.java b/wzy-models/wzy-user/src/main/java/com/wzy/user/service/UserService.java new file mode 100644 index 0000000..569de49 --- /dev/null +++ b/wzy-models/wzy-user/src/main/java/com/wzy/user/service/UserService.java @@ -0,0 +1,7 @@ +package com.wzy.user.service; + +import com.wzy.common.domain.User; + +public interface UserService { + User login(String username); +} diff --git a/wzy-models/wzy-user/src/main/java/com/wzy/user/service/impl/UserServiceImpl.java b/wzy-models/wzy-user/src/main/java/com/wzy/user/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..a8b1f9e --- /dev/null +++ b/wzy-models/wzy-user/src/main/java/com/wzy/user/service/impl/UserServiceImpl.java @@ -0,0 +1,18 @@ +package com.wzy.user.service.impl; + +import com.wzy.common.domain.User; +import com.wzy.user.mapper.UserMapper; +import com.wzy.user.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserMapper userMapper; + @Override + public User login(String username) { + return userMapper.login(username); + } +} diff --git a/wzy-models/wzy-user/src/main/resources/bootstrap.yml b/wzy-models/wzy-user/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..4c256c8 --- /dev/null +++ b/wzy-models/wzy-user/src/main/resources/bootstrap.yml @@ -0,0 +1,42 @@ +# Tomcat +server: + port: 9004 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: wzy-user + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 165.154.161.166:8848 + config: + # 配置中心地址 + server-addr: 165.154.161.166:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + +fdfs: + so-timeout: 1500 # socket 连接时长 + connect-timeout: 600 # 连接 tracker 服务器超时时长 + # 这两个是你服务器的 IP 地址,注意 23000 端口也要打开,阿里云服务器记得配置安全组。tracker 要和 stroage 服务进行交流 + tracker-list: 165.154.161.166:22122 + web-server-url: 165.154.161.166:8888 + pool: + jmx-enabled: false + # 生成缩略图 + thumb-image: + height: 500 + width: 500 diff --git a/wzy-models/wzy-user/src/main/resources/mapper/UserMapper.xml b/wzy-models/wzy-user/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 0000000..5f94a05 --- /dev/null +++ b/wzy-models/wzy-user/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,9 @@ + + + + + +