提交。
parent
d1ba890f0f
commit
00bed24687
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>week3-1227</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>work-auth</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 项目公共 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>work-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- SpringBoot Web-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,21 @@
|
|||
package com.bwie.auth;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class AuthApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthApplication.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.bwie.auth.controller;
|
||||
|
||||
import com.bwie.auth.service.AuthService;
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.domain.request.LoginRequest;
|
||||
import com.bwie.common.domain.response.JwtResponse;
|
||||
import com.bwie.common.result.Result;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.RestController;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@RestController
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result<JwtResponse> login(@RequestBody LoginRequest loginRequest){
|
||||
return authService.login(loginRequest);
|
||||
}
|
||||
|
||||
@GetMapping("/user/info")
|
||||
public Result<User> userInfo(){
|
||||
User user = authService.userInfo();
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
public Result logout(){
|
||||
authService.logout();
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.bwie.auth.feign;
|
||||
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.result.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
@FeignClient(name = "work-user")
|
||||
public interface UserFeignService {
|
||||
|
||||
@GetMapping("/findByTel/{tel}")
|
||||
public Result<User> findByTel(@PathVariable("tel") String tel);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.bwie.auth.service;
|
||||
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.domain.request.LoginRequest;
|
||||
import com.bwie.common.domain.response.JwtResponse;
|
||||
import com.bwie.common.result.Result;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface AuthService {
|
||||
/**
|
||||
* 鉴权登录
|
||||
* @param loginRequest
|
||||
* @return
|
||||
*/
|
||||
Result<JwtResponse> login(LoginRequest loginRequest);
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
* @return
|
||||
*/
|
||||
User userInfo();
|
||||
|
||||
/**
|
||||
* 注销
|
||||
*/
|
||||
void logout();
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package com.bwie.auth.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bwie.auth.feign.UserFeignService;
|
||||
import com.bwie.auth.service.AuthService;
|
||||
import com.bwie.common.constants.JwtConstants;
|
||||
import com.bwie.common.constants.TokenConstants;
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.domain.request.LoginRequest;
|
||||
import com.bwie.common.domain.response.JwtResponse;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.common.utils.JwtUtils;
|
||||
import com.bwie.common.utils.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
|
||||
@Autowired
|
||||
private UserFeignService userFeignService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Override
|
||||
public Result<JwtResponse> login(LoginRequest loginRequest) {
|
||||
if (StringUtils.isEmpty(loginRequest.getTel()) || StringUtils.isEmpty(loginRequest.getPassword())){
|
||||
return Result.error("手机号密码不能为空");
|
||||
}
|
||||
Result<User> byTel = userFeignService.findByTel(loginRequest.getTel());
|
||||
User loginUser = byTel.getData();
|
||||
if (null == loginUser){
|
||||
return Result.error("该用户不存在");
|
||||
}
|
||||
if (!loginUser.getPassword().equals(loginRequest.getPassword())){
|
||||
return Result.error("密码错误");
|
||||
}
|
||||
// 1.3 使用JWT鉴权(5分)
|
||||
String userKey = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
Map<String, Object> jwtMap = new HashMap<>(100, 100);
|
||||
jwtMap.put(JwtConstants.USER_KEY, userKey);
|
||||
jwtMap.put(JwtConstants.DETAILS_USER_ID, loginUser.getUserId());
|
||||
String token = JwtUtils.createToken(jwtMap);
|
||||
redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY + userKey, JSONObject.toJSONString(loginUser),
|
||||
30, TimeUnit.MINUTES);
|
||||
|
||||
JwtResponse jwtResponse = new JwtResponse();
|
||||
jwtResponse.setToken(token);
|
||||
jwtResponse.setExpireTime("30MIN");
|
||||
return Result.success(jwtResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User userInfo() {
|
||||
String token = request.getHeader(TokenConstants.TOKEN);
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
String json = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
return JSONObject.parseObject(json, User.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout() {
|
||||
String token = request.getHeader(TokenConstants.TOKEN);
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 10001
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: GMT+8
|
||||
application:
|
||||
# 应用名称
|
||||
name: work-auth
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,145 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>week3-1227</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>work-common</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- bootstrap 启动器 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
<!-- 负载均衡-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Openfeign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- JWT -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.9.1</version>
|
||||
</dependency>
|
||||
<!-- Alibaba FastJson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.80</version>
|
||||
</dependency>
|
||||
<!-- SpringBoot Boot Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<!-- Hibernate Validator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<!-- Apache Lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<!-- lombok依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<!-- hutool -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.3</version>
|
||||
</dependency>
|
||||
<!-- 阿里大鱼 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<!--短信依赖 5条依赖-->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-util</artifactId>
|
||||
<version>9.3.7.v20160115</version>
|
||||
</dependency>
|
||||
<!-- rabbitMQ -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<!-- 消息转换器 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>2.9.10</version>
|
||||
</dependency>
|
||||
<!--kafka-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
</dependency>
|
||||
<!-- oss -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<!--fast dfs 上传-->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.5</version>
|
||||
</dependency>
|
||||
<!-- spring boot 邮件发送 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,20 @@
|
|||
package com.bwie.common.constants;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO 系统常量
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* 成功标记
|
||||
*/
|
||||
public static final Integer SUCCESS = 200;
|
||||
public static final String SUCCESS_MSG = "操作成功";
|
||||
/**
|
||||
* 失败标记
|
||||
*/
|
||||
public static final Integer ERROR = 500;
|
||||
public static final String ERROR_MSG = "操作异常";
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.bwie.common.constants;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO Jwt常量
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class JwtConstants {
|
||||
|
||||
/**
|
||||
* 用户ID字段
|
||||
*/
|
||||
public static final String DETAILS_USER_ID = "user_id";
|
||||
|
||||
/**
|
||||
* 用户名字段
|
||||
*/
|
||||
public static final String DETAILS_USERNAME = "username";
|
||||
|
||||
/**
|
||||
* 用户标识
|
||||
*/
|
||||
public static final String USER_KEY = "user_key";
|
||||
|
||||
/**
|
||||
* 令牌秘钥
|
||||
*/
|
||||
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.bwie.common.constants;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO 令牌常量
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class TokenConstants {
|
||||
/**
|
||||
* 缓存有效期,默认720(分钟)
|
||||
*/
|
||||
public final static long EXPIRATION = 720;
|
||||
/**
|
||||
* 缓存刷新时间,默认120(分钟)
|
||||
*/
|
||||
public final static long REFRESH_TIME = 120;
|
||||
/**
|
||||
* 权限缓存前缀
|
||||
*/
|
||||
public final static String LOGIN_TOKEN_KEY = "login_tokens:";
|
||||
/**
|
||||
* token标识
|
||||
*/
|
||||
public static final String TOKEN = "token";
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.bwie.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Goods {
|
||||
/**
|
||||
* 商品Id
|
||||
*/
|
||||
private Integer goodsId;
|
||||
/**
|
||||
* 商品名称
|
||||
*/
|
||||
private String goodsName;
|
||||
/**
|
||||
* 品牌
|
||||
*/
|
||||
private String goodsBrand;
|
||||
/**
|
||||
* 商品分类
|
||||
*/
|
||||
private Integer typeId;
|
||||
/**
|
||||
* 冗余字段
|
||||
*/
|
||||
private String typeIds;
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private BigDecimal price;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.bwie.common.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
public class Order {
|
||||
private Integer orderId;
|
||||
private Integer goodsId;
|
||||
private Integer userId;
|
||||
private Integer vipLevel;
|
||||
private BigDecimal price;
|
||||
private Integer buyNum;
|
||||
private BigDecimal amount;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.bwie.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Type implements Serializable {
|
||||
/**
|
||||
* 分类Id
|
||||
*/
|
||||
private Integer typeId;
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
private String typeName;
|
||||
/**
|
||||
* 父级Id
|
||||
*/
|
||||
private Integer parentId;
|
||||
|
||||
/**
|
||||
* 子级集合
|
||||
*/
|
||||
private List<Type> children;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.bwie.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
/**
|
||||
* 用户Id
|
||||
*/
|
||||
private Integer userId;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
private String tel;
|
||||
/**
|
||||
* 余额
|
||||
*/
|
||||
private BigDecimal balance;
|
||||
/**
|
||||
* 会员级别:0-普通用户 1-白银会员 2-黄金会员 3-白金会员 4-钻石会员
|
||||
*/
|
||||
private Integer vipLevel;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.bwie.common.domain.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
public class GoodsRequest {
|
||||
/**
|
||||
* 商品名称
|
||||
*/
|
||||
private String goodsName;
|
||||
/**
|
||||
* 品牌
|
||||
*/
|
||||
private String goodsBrand;
|
||||
/**
|
||||
* 商品分类
|
||||
*/
|
||||
private Integer typeId;
|
||||
/**
|
||||
* 冗余字段
|
||||
*/
|
||||
private String typeIds;
|
||||
|
||||
private Integer pageNum = 1;
|
||||
private Integer pageSize = 5;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.bwie.common.domain.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
public class LoginRequest {
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
private String tel;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.bwie.common.domain.response;
|
||||
|
||||
import com.bwie.common.domain.Goods;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
public class GoodsResponse extends Goods {
|
||||
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
private String typeName;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.bwie.common.domain.response;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
public class JwtResponse {
|
||||
private String token;
|
||||
private String expireTime;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.bwie.common.result;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO 列表返回结果集
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
public class PageResult<T> implements Serializable {
|
||||
/**
|
||||
* 总条数
|
||||
*/
|
||||
private long total;
|
||||
/**
|
||||
* 结果集合
|
||||
*/
|
||||
private List<T> list;
|
||||
public PageResult() {
|
||||
}
|
||||
public PageResult(long total, List<T> list) {
|
||||
this.total = total;
|
||||
this.list = list;
|
||||
}
|
||||
public static <T> PageResult<T> toPageResult(long total, List<T> list){
|
||||
return new PageResult(total , list);
|
||||
}
|
||||
public static <T> Result<PageResult<T>> toResult(long total, List<T> list){
|
||||
return Result.success(PageResult.toPageResult(total,list));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.bwie.common.result;
|
||||
|
||||
import com.bwie.common.constants.Constants;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO 响应信息主体
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final int SUCCESS = Constants.SUCCESS;
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final int FAIL = Constants.ERROR;
|
||||
/**
|
||||
* 返回状态码
|
||||
*/
|
||||
private int code;
|
||||
/**
|
||||
* 响应信息
|
||||
*/
|
||||
private String msg;
|
||||
/**
|
||||
* 响应数据
|
||||
*/
|
||||
private T data;
|
||||
|
||||
public static <T> Result<T> success() {
|
||||
return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(T data) {
|
||||
return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(T data, String msg) {
|
||||
return restResult(data, SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error() {
|
||||
return restResult(null, FAIL, Constants.ERROR_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(String msg) {
|
||||
return restResult(null, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(T data) {
|
||||
return restResult(data, FAIL, Constants.ERROR_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(T data, String msg) {
|
||||
return restResult(data, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(int code, String msg) {
|
||||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||
Result<T> apiResult = new Result<>();
|
||||
apiResult.setCode(code);
|
||||
apiResult.setData(data);
|
||||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
package com.bwie.common.utils;
|
||||
|
||||
import com.bwie.common.constants.JwtConstants;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO Jwt工具类
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class JwtUtils {
|
||||
|
||||
/**
|
||||
* 秘钥
|
||||
*/
|
||||
public static String secret = JwtConstants.SECRET;
|
||||
|
||||
/**
|
||||
* 从数据声明生成令牌
|
||||
*
|
||||
* @param claims 数据声明
|
||||
* @return 令牌
|
||||
*/
|
||||
public static String createToken(Map<String, Object> claims){
|
||||
String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从令牌中获取数据声明
|
||||
*
|
||||
* @param token 令牌
|
||||
* @return 数据声明
|
||||
*/
|
||||
public static Claims parseToken(String token){
|
||||
return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
||||
}
|
||||
/**
|
||||
* 根据令牌获取用户标识
|
||||
*
|
||||
* @param token 令牌
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getUserKey(String token){
|
||||
Claims claims = parseToken(token);
|
||||
return getValue(claims, JwtConstants.USER_KEY);
|
||||
}
|
||||
/**
|
||||
* 根据令牌获取用户标识
|
||||
*
|
||||
* @param claims 身份信息
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getUserKey(Claims claims){
|
||||
return getValue(claims, JwtConstants.USER_KEY);
|
||||
}
|
||||
/**
|
||||
* 根据令牌获取用户ID
|
||||
*
|
||||
* @param token 令牌
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getUserId(String token){
|
||||
Claims claims = parseToken(token);
|
||||
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||
}
|
||||
/**
|
||||
* 根据身份信息获取用户ID
|
||||
*
|
||||
* @param claims 身份信息
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getUserId(Claims claims){
|
||||
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||
}
|
||||
/**
|
||||
* 根据令牌获取用户名
|
||||
*
|
||||
* @param token 令牌
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getUserName(String token){
|
||||
Claims claims = parseToken(token);
|
||||
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||
}
|
||||
/**
|
||||
* 根据身份信息获取用户名
|
||||
*
|
||||
* @param claims 身份信息
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getUserName(Claims claims){
|
||||
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||
}
|
||||
/**
|
||||
* 根据身份信息获取键值
|
||||
*
|
||||
* @param claims 身份信息
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static String getValue(Claims claims, String key){
|
||||
Object obj = claims.get(key);
|
||||
return obj == null ? "" : obj.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,156 @@
|
|||
package com.bwie.common.utils;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.GetObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.utils
|
||||
* @Description: TODO Oss服务调用
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Log4j2
|
||||
public class OssUtil {
|
||||
|
||||
/**
|
||||
* Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
|
||||
*/
|
||||
private static String endPoint = "oss-cn-shanghai.aliyuncs.com";// 访问域名
|
||||
private static String accessKeyId = "LTAI5tKXvCvwq7hKpZgsY2hk";//密钥id
|
||||
private static String accessKeySecret = "HaOTCePEpeLNukQL3jhR6DZu9CAnmA";// 密钥值
|
||||
private static String accessPre = "https://arklorse.oss-cn-shanghai.aliyuncs.com/";//服务前缀URL
|
||||
|
||||
/**
|
||||
* bucket名称
|
||||
* @return
|
||||
*/
|
||||
private static String bucketName = "arklorse";
|
||||
|
||||
private static OSS ossClient ;
|
||||
|
||||
static {
|
||||
ossClient = new OSSClientBuilder().build(
|
||||
endPoint,
|
||||
accessKeyId,
|
||||
accessKeySecret);
|
||||
log.info("oss服务连接成功!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认路径上传本地文件
|
||||
* @param filePath
|
||||
*/
|
||||
public static String uploadFile(String filePath){
|
||||
return uploadFileForBucket(bucketName,getOssFilePath(filePath) ,filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认路径上传multipartFile文件
|
||||
* @param multipartFile
|
||||
*/
|
||||
public static String uploadMultipartFile(MultipartFile multipartFile) {
|
||||
return uploadMultipartFile(bucketName,getOssFilePath(multipartFile.getOriginalFilename()),multipartFile);
|
||||
}
|
||||
/**
|
||||
* 上传 multipartFile 类型文件
|
||||
* @param bucketName
|
||||
* @param ossPath
|
||||
* @param multipartFile
|
||||
*/
|
||||
public static String uploadMultipartFile(String bucketName , String ossPath , MultipartFile multipartFile){
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = multipartFile.getInputStream();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
|
||||
return accessPre+ossPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用File上传PutObject上传文件 ** 程序默认使用次方法上传
|
||||
* @param bucketName 实例名称
|
||||
* @param ossPath oss存储路径
|
||||
* @param filePath 本地文件路径
|
||||
*/
|
||||
public static String uploadFileForBucket(String bucketName , String ossPath , String filePath) {
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
|
||||
|
||||
// 上传
|
||||
ossClient.putObject(putObjectRequest);
|
||||
return accessPre+ossPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用文件流上传到指定的bucket实例
|
||||
* @param bucketName 实例名称
|
||||
* @param ossPath oss存储路径
|
||||
* @param filePath 本地文件路径
|
||||
*/
|
||||
public static String uploadFileInputStreamForBucket(String bucketName , String ossPath , String filePath){
|
||||
|
||||
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(filePath);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
|
||||
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
|
||||
return accessPre+ossPath;
|
||||
}
|
||||
|
||||
public static void uploadFileInputStreamForBucket(String bucketName , String ossPath , InputStream inputStream ){
|
||||
ossClient.putObject(bucketName, ossPath, inputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载
|
||||
* @param ossFilePath
|
||||
* @param filePath
|
||||
*/
|
||||
public static void downloadFile(String ossFilePath , String filePath ){
|
||||
downloadFileForBucket(bucketName , ossFilePath , filePath);
|
||||
}
|
||||
/**
|
||||
* 下载
|
||||
* @param bucketName 实例名称
|
||||
* @param ossFilePath oss存储路径
|
||||
* @param filePath 本地文件路径
|
||||
*/
|
||||
public static void downloadFileForBucket(String bucketName , String ossFilePath , String filePath ){
|
||||
ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getOssDefaultPath(){
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String url =
|
||||
now.getYear()+"/"+
|
||||
now.getMonth()+"/"+
|
||||
now.getDayOfMonth()+"/"+
|
||||
now.getHour()+"/"+
|
||||
now.getMinute()+"/";
|
||||
return url;
|
||||
}
|
||||
|
||||
public static String getOssFilePath(String filePath){
|
||||
String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
|
||||
return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.bwie.common.utils;
|
||||
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO 字符串处理工具类
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否为空
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isNull(Object object) {
|
||||
return object == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Collection<?> coll) {
|
||||
return isNull(coll) || coll.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param strs 需要检查的字符串数组
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean matches(String str, List<String> strs) {
|
||||
if (isEmpty(str) || isEmpty(strs)) {
|
||||
return false;
|
||||
}
|
||||
for (String pattern : strs) {
|
||||
if (isMatch(pattern, str))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断url是否与规则配置:
|
||||
* ? 表示单个字符;
|
||||
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||
* ** 表示任意层路径;
|
||||
*
|
||||
* @param pattern 匹配规则
|
||||
* @param url 需要匹配的url
|
||||
* @return
|
||||
*/
|
||||
public static boolean isMatch(String pattern, String url) {
|
||||
AntPathMatcher matcher = new AntPathMatcher();
|
||||
return matcher.match(pattern, url);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package com.bwie.common.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.common.domain
|
||||
* @Description: TODO 短信工具类
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Log4j2
|
||||
public class TelSmsUtils {
|
||||
|
||||
/**
|
||||
* 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限
|
||||
*/
|
||||
private static String accessKeyId = "LTAIEVXszCmcd1T5";
|
||||
private static String accessKeySecret = "2zHwciQXln8wExSEnkIYtRTSwLeRNd";
|
||||
|
||||
/**
|
||||
* 短信访问域名
|
||||
*/
|
||||
private static String endpoint = "dysmsapi.aliyuncs.com";
|
||||
/**
|
||||
* 短信签名
|
||||
*/
|
||||
private static String signName = "登录验证";
|
||||
|
||||
/**
|
||||
* 实例化短信对象
|
||||
*/
|
||||
private static Client client;
|
||||
|
||||
static {
|
||||
log.info("初始化短信服务开始");
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
client = initClient();
|
||||
log.info("初始化短信成功:{}",signName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info("初始化短信服务结束:耗时:{}MS",(System.currentTimeMillis()-startTime));
|
||||
}
|
||||
/**
|
||||
* 初始化短信对象
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static Client initClient() throws Exception{
|
||||
Config config = new Config()
|
||||
// 您的AccessKey ID
|
||||
.setAccessKeyId(accessKeyId)
|
||||
// 您的AccessKey Secret
|
||||
.setAccessKeySecret(accessKeySecret);
|
||||
// 访问的域名
|
||||
config.endpoint = endpoint;
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送单条短信
|
||||
* @param tel
|
||||
* @param templateCode SMS_153991546
|
||||
* @param sendDataMap
|
||||
*/
|
||||
public static String sendSms(String tel , String templateCode , Map<String, String> sendDataMap){
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setPhoneNumbers(tel)
|
||||
.setSignName(signName)
|
||||
.setTemplateCode(templateCode)
|
||||
.setTemplateParam(JSONObject.toJSONString(sendDataMap));
|
||||
SendSmsResponse sendSmsResponse = null;
|
||||
try {
|
||||
log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap));
|
||||
sendSmsResponse = client.sendSms(sendSmsRequest);
|
||||
} catch (Exception e) {
|
||||
log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e);
|
||||
}
|
||||
return JSONObject.toJSONString(sendSmsResponse.getBody());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>week3-1227</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>work-gateway</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 公共模块 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>work-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- 网关依赖 -->
|
||||
<!-- SpringCloud Gateway -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Sentinel Gateway -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>
|
||||
<!-- 引入阿里巴巴sentinel限流 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.bwie.gateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class GatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
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;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.gateway
|
||||
* @Description: TODO 放行白名单配置
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
@RefreshScope
|
||||
@ConfigurationProperties(prefix = "ignore")
|
||||
@Data
|
||||
@Log4j2
|
||||
public class IgnoreWhiteConfig {
|
||||
/**
|
||||
* 放行白名单配置,网关不校验此处的白名单
|
||||
*/
|
||||
private List<String> whites = new ArrayList<>();
|
||||
|
||||
public void setWhites(List<String> whites) {
|
||||
log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
|
||||
this.whites = whites;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.bwie.gateway.filters;
|
||||
|
||||
import com.bwie.common.constants.TokenConstants;
|
||||
import com.bwie.common.utils.JwtUtils;
|
||||
import com.bwie.common.utils.StringUtils;
|
||||
import com.bwie.gateway.config.IgnoreWhiteConfig;
|
||||
import com.bwie.gateway.utils.GatewayUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.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;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Component
|
||||
public class AuthFilter implements GlobalFilter, Ordered {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private IgnoreWhiteConfig ignoreWhiteConfig;
|
||||
|
||||
/**
|
||||
* 1.3 未登录不能访问系统资源(5分)
|
||||
* @param exchange
|
||||
* @param chain
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
List<String> whites = ignoreWhiteConfig.getWhites();
|
||||
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 aBoolean = redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
if (!aBoolean){
|
||||
return GatewayUtils.errorResponse(exchange,"token过期");
|
||||
}else {
|
||||
redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey,30, TimeUnit.MINUTES);
|
||||
}
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package com.bwie.gateway.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.common.utils.StringUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @Author: Li YongJie
|
||||
* @BelongsPackage: com.bwie.gateway
|
||||
* @Description: TODO 网关处理工具类
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Log4j2
|
||||
public class GatewayUtils {
|
||||
/**
|
||||
* 添加请求头参数
|
||||
* @param mutate 修改对象
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) {
|
||||
if (StringUtils.isEmpty(key)){
|
||||
log.warn("添加请求头参数键不可以为空");
|
||||
return;
|
||||
}
|
||||
if (value == null) {
|
||||
log.warn("添加请求头参数:[{}]值为空",key);
|
||||
return;
|
||||
}
|
||||
String valueStr = value.toString();
|
||||
mutate.header(key, valueStr);
|
||||
log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除请求头参数
|
||||
* @param mutate 修改对象
|
||||
* @param key 键
|
||||
*/
|
||||
public static void removeHeader(ServerHttpRequest.Builder mutate, String key) {
|
||||
if (StringUtils.isEmpty(key)){
|
||||
log.warn("删除请求头参数键不可以为空");
|
||||
return;
|
||||
}
|
||||
mutate.headers(httpHeaders -> httpHeaders.remove(key)).build();
|
||||
log.info("删除请求头参数 - 键:[{}]",key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误结果响应
|
||||
* @param exchange 响应上下文
|
||||
* @param msg 响应消息
|
||||
* @return
|
||||
*/
|
||||
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg, HttpStatus httpStatus) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
//设置HTTP响应头状态
|
||||
response.setStatusCode(httpStatus);
|
||||
//设置HTTP响应头文本格式
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
//定义响应内容
|
||||
Result<?> result = Result.error(msg);
|
||||
String resultJson = JSONObject.toJSONString(result);
|
||||
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||
//进行响应
|
||||
return response.writeWith(Mono.just(dataBuffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误结果响应
|
||||
* @param exchange 响应上下文
|
||||
* @param msg 响应消息
|
||||
* @return
|
||||
*/
|
||||
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
//设置HTTP响应头状态
|
||||
response.setStatusCode(HttpStatus.OK);
|
||||
//设置HTTP响应头文本格式
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
//定义响应内容
|
||||
Result<?> result = Result.error(msg);
|
||||
String resultJson = JSONObject.toJSONString(result);
|
||||
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||
//进行响应
|
||||
return response.writeWith(Mono.just(dataBuffer));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 18080
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: work-gateway
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
main:
|
||||
# 允许使用循环引用
|
||||
allow-circular-references: true
|
||||
# 允许定义相同的bean对象 去覆盖原有的
|
||||
allow-bean-definition-overriding: true
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>week3-1227</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>work-models</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>work-goods</module>
|
||||
<module>work-user</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>work-models</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>work-goods</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 系统公共 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>work-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- SpringBoot Web-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- Druid -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.2.8</version>
|
||||
</dependency>
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<!-- Mybatis 依赖配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<!-- PageHelper -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.bwie.goods;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class GoodsApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GoodsApplication.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.bwie.goods.controller;
|
||||
|
||||
import com.bwie.common.domain.Goods;
|
||||
import com.bwie.common.domain.Order;
|
||||
import com.bwie.common.domain.request.GoodsRequest;
|
||||
import com.bwie.common.domain.response.GoodsResponse;
|
||||
import com.bwie.common.result.PageResult;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.goods.service.GoodsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@RestController
|
||||
public class GoodsController {
|
||||
|
||||
@Autowired
|
||||
private GoodsService goodsService;
|
||||
|
||||
@PostMapping("/list")
|
||||
public Result<PageResult<GoodsResponse>> list(@RequestBody GoodsRequest goodsRequest){
|
||||
return goodsService.list(goodsRequest);
|
||||
}
|
||||
|
||||
@PostMapping("/findAll")
|
||||
public Result<List<GoodsResponse>> findAll(){
|
||||
List<GoodsResponse> list = goodsService.findAll();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
@GetMapping("/findById/{goodsId}")
|
||||
public Result<Goods> findById(@PathVariable("goodsId") Integer goodsId){
|
||||
Goods goods = goodsService.findById(goodsId);
|
||||
return Result.success(goods);
|
||||
}
|
||||
|
||||
@PostMapping("/buy")
|
||||
public Result buy(@RequestBody Order order){
|
||||
goodsService.buy(order);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.bwie.goods.controller;
|
||||
|
||||
import com.bwie.common.domain.Type;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.goods.service.TypeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/type")
|
||||
public class TypeController {
|
||||
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
|
||||
@PostMapping("/findTypes")
|
||||
public Result<List<Type>> findTypes(){
|
||||
List<Type> types = typeService.findTypes();
|
||||
return Result.success(types);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.bwie.goods.mapper;
|
||||
|
||||
import com.bwie.common.domain.Goods;
|
||||
import com.bwie.common.domain.Order;
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.domain.request.GoodsRequest;
|
||||
import com.bwie.common.domain.response.GoodsResponse;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface GoodsMapper {
|
||||
|
||||
/**
|
||||
* 商品列表
|
||||
* @param goodsRequest
|
||||
* @return
|
||||
*/
|
||||
List<GoodsResponse> list(GoodsRequest goodsRequest);
|
||||
|
||||
/**
|
||||
* 所有商品
|
||||
* @return
|
||||
*/
|
||||
List<GoodsResponse> findAll();
|
||||
|
||||
/**
|
||||
* 回显
|
||||
* @param goodsId
|
||||
* @return
|
||||
*/
|
||||
Goods findById(@Param("goodsId") Integer goodsId);
|
||||
|
||||
/**
|
||||
* 购买
|
||||
* @param order
|
||||
*/
|
||||
void insertOrder(Order order);
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param user
|
||||
*/
|
||||
void update(User user);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.bwie.goods.mapper;
|
||||
|
||||
import com.bwie.common.domain.Type;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface TypeMapper {
|
||||
/**
|
||||
* 分类下拉框
|
||||
* @return
|
||||
*/
|
||||
List<Type> findTypes();
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.bwie.goods.service;
|
||||
|
||||
import com.bwie.common.domain.Goods;
|
||||
import com.bwie.common.domain.Order;
|
||||
import com.bwie.common.domain.request.GoodsRequest;
|
||||
import com.bwie.common.domain.response.GoodsResponse;
|
||||
import com.bwie.common.result.PageResult;
|
||||
import com.bwie.common.result.Result;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface GoodsService {
|
||||
Result<PageResult<GoodsResponse>> list(GoodsRequest goodsRequest);
|
||||
|
||||
List<GoodsResponse> findAll();
|
||||
|
||||
Goods findById(Integer goodsId);
|
||||
|
||||
Result buy(Order order);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.bwie.goods.service;
|
||||
|
||||
import com.bwie.common.domain.Type;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface TypeService {
|
||||
/**
|
||||
* 类型下拉框
|
||||
* @return
|
||||
*/
|
||||
List<Type> findTypes();
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package com.bwie.goods.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bwie.common.constants.TokenConstants;
|
||||
import com.bwie.common.domain.Goods;
|
||||
import com.bwie.common.domain.Order;
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.domain.request.GoodsRequest;
|
||||
import com.bwie.common.domain.response.GoodsResponse;
|
||||
import com.bwie.common.result.PageResult;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.common.utils.JwtUtils;
|
||||
import com.bwie.goods.mapper.GoodsMapper;
|
||||
import com.bwie.goods.service.GoodsService;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class GoodsServiceImpl implements GoodsService {
|
||||
|
||||
@Autowired
|
||||
private GoodsMapper goodsMapper;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* 1.4 商品列表数据展示正确、分页展示成功(5分)
|
||||
* @param goodsRequest
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<PageResult<GoodsResponse>> list(GoodsRequest goodsRequest) {
|
||||
PageHelper.startPage(goodsRequest.getPageNum(), goodsRequest.getPageSize());
|
||||
List<GoodsResponse> list = goodsMapper.list(goodsRequest);
|
||||
PageInfo<GoodsResponse> info = new PageInfo<>(list);
|
||||
redisTemplate.opsForValue().set("goods",JSONObject.toJSONString(list));
|
||||
return PageResult.toResult(info.getTotal(), list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GoodsResponse> findAll() {
|
||||
return goodsMapper.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.6 商品信息回显正确(4分)
|
||||
* @param goodsId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Goods findById(Integer goodsId) {
|
||||
return goodsMapper.findById(goodsId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Result buy(Order order) {
|
||||
if (order.getBuyNum()<1){
|
||||
return Result.error("购买数量最小为1");
|
||||
}
|
||||
|
||||
String token = request.getHeader(TokenConstants.TOKEN);
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
String json = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
User user = JSONObject.parseObject(json, User.class);
|
||||
|
||||
order.setUserId(user.getUserId());
|
||||
order.setVipLevel(user.getVipLevel());
|
||||
goodsMapper.insertOrder(order);
|
||||
|
||||
user.setBalance(user.getBalance().subtract(order.getAmount()));
|
||||
goodsMapper.update(user);
|
||||
redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY + userKey, JSONObject.toJSONString(user),
|
||||
30, TimeUnit.MINUTES);
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package com.bwie.goods.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bwie.common.domain.Type;
|
||||
import com.bwie.goods.mapper.TypeMapper;
|
||||
import com.bwie.goods.service.TypeService;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class TypeServiceImpl implements TypeService {
|
||||
|
||||
@Autowired
|
||||
private TypeMapper typeMapper;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Override
|
||||
public List<Type> findTypes() {
|
||||
List<Type> types = null;
|
||||
Map<Object, Object> map = redisTemplate.opsForHash().entries("types");
|
||||
if (map.isEmpty()){
|
||||
types = typeMapper.findTypes();
|
||||
Map<String, Object> hashMap = new HashMap<>();
|
||||
for (Type type : types) {
|
||||
map.put(type.getTypeId(),type);
|
||||
redisTemplate.opsForHash().putAll("types",hashMap);
|
||||
}
|
||||
}else {
|
||||
for (Map.Entry<Object, Object> entry : map.entrySet()) {
|
||||
Type type = new Type();
|
||||
type.setTypeId((int)Long.parseLong(entry.getKey().toString()));
|
||||
type.setTypeName(entry.getValue().toString());
|
||||
types.add(type);
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
// .stream()
|
||||
// .parallel()
|
||||
// .filter(type -> type.getParentId().equals(NumberUtils.INTEGER_ZERO))
|
||||
// .map(first->{
|
||||
// first.setChildren(first.getChildren(), types) ? null : getChildren(first.getParentId(), types);
|
||||
// return first;
|
||||
// })
|
||||
// .collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// public static List<Type> getChildren(Integer parentId, List<Type> types){
|
||||
// List<Type> collect =types.stream()
|
||||
// .parallel()
|
||||
// .filter(type -> type.getParentId().equals(parentId))
|
||||
// .map(second->{
|
||||
// second.getChildren().isEmpty() ? null : getChildren(second.getParentId(), types);
|
||||
// return second;
|
||||
// })
|
||||
// .collect(Collectors.toList());
|
||||
// return collect;
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 10003
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: GMT+8
|
||||
application:
|
||||
# 应用名称
|
||||
name: work-goods
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
fdfs:
|
||||
# socket 连接时长
|
||||
so-timeout: 1500
|
||||
# 连接 tracker 服务器超时时长
|
||||
connect-timeout: 600
|
||||
# 这两个是你服务器的 IP 地址,注意 23000 端口也要打开,阿里云服务器记得配置安全组。tracker 要和 stroage 服务进行交流
|
||||
tracker-list: 121.36.202.181:22122
|
||||
web-server-url: 121.36.202.181:8888
|
||||
pool:
|
||||
jmx-enabled: false
|
||||
# 生成缩略图
|
||||
thumb-image:
|
||||
height: 500
|
||||
width: 500
|
||||
|
||||
aliyun:
|
||||
oss:
|
||||
file:
|
||||
bucketName: arklorse
|
||||
endpoint: oss-cn-shanghai.aliyuncs.com
|
||||
AccessKeyId: LTAI5tKXvCvwq7hKpZgsY2hk
|
||||
AccessKeySecret: HaOTCePEpeLNukQL3jhR6DZu9CAnmA
|
|
@ -0,0 +1,64 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- mybatis数据层 namespace命名空间-->
|
||||
<mapper namespace="com.bwie.goods.mapper.GoodsMapper">
|
||||
|
||||
|
||||
<sql id="selectGoodsVo">
|
||||
select
|
||||
g.goods_id,
|
||||
g.goods_name,
|
||||
g.goods_brand,
|
||||
t.type_name,
|
||||
g.type_ids,
|
||||
g.price
|
||||
from
|
||||
t_goods g
|
||||
left join t_type t on g.type_id = t.type_id
|
||||
</sql>
|
||||
|
||||
<select id="list" resultType="com.bwie.common.domain.response.GoodsResponse">
|
||||
<include refid="selectGoodsVo"/>
|
||||
<where>
|
||||
<if test="null !=goodsName and '' != goodsName">
|
||||
and g.goods_name like concat('%',#{goodsName},'%')
|
||||
</if>
|
||||
<if test="null != typeIds and '' != typeIds">
|
||||
and g.type_ids = #{typeIds}
|
||||
</if>
|
||||
<if test="null != typeId">
|
||||
and g.type_ids = #{typeId}
|
||||
</if>
|
||||
<if test="null != goodsBrand and ''!=goodsBrand">
|
||||
and g.goods_brand like concat('%',#{goodsBrand},'%')
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="findAll" resultType="com.bwie.common.domain.response.GoodsResponse">
|
||||
<include refid="selectGoodsVo"/>
|
||||
</select>
|
||||
|
||||
<select id="findById" resultType="com.bwie.common.domain.Goods">
|
||||
<include refid="selectGoodsVo"/> where g.goods_id = #{goodsId}
|
||||
</select>
|
||||
|
||||
<insert id="insertOrder">
|
||||
insert into t_orders (goods_id,user_id,vip_level,price,buy_num,amount)
|
||||
values (#{goodsId},#{userId},#{vipLevel},#{price},#{buyNum},#{amount})
|
||||
</insert>
|
||||
|
||||
<update id="update">
|
||||
update s_user
|
||||
<set>
|
||||
<if test="null != username and '' != username">username = #{username},</if>
|
||||
<if test="null != password and '' != password">password = #{password},</if>
|
||||
<if test="null != tel and '' != tel">tel = #{tel},</if>
|
||||
<if test="null != balance">balance = #{balance},</if>
|
||||
<if test="null != vipLevel">vip_level = #{vipLevel}</if>
|
||||
</set>
|
||||
where user_id = #{userId}
|
||||
</update>
|
||||
</mapper>
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- mybatis数据层 namespace命名空间-->
|
||||
<mapper namespace="com.bwie.goods.mapper.TypeMapper">
|
||||
|
||||
|
||||
<select id="findTypes" resultType="com.bwie.common.domain.Type">
|
||||
select type_id,type_name,parent_id from t_type where parent_id != 0
|
||||
</select>
|
||||
</mapper>
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>work-models</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>work-user</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 系统公共 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>work-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- SpringBoot Web-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- Druid -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.2.8</version>
|
||||
</dependency>
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<!-- Mybatis 依赖配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<!-- PageHelper -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.bwie.user;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class UserApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserApplication.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.bwie.user.controller;
|
||||
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.user.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/findByTel/{tel}")
|
||||
public Result<User> findByTel(@PathVariable("tel") String tel){
|
||||
User user = userService.findByTel(tel);
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
@PutMapping("/topUp/{balance}")
|
||||
public Result topUp(@PathVariable("balance")BigDecimal balance){
|
||||
// 1.5 充值金额最小为500(5分)
|
||||
if (balance.compareTo(new BigDecimal(500)) < 0){
|
||||
return Result.error("充值金额最小为500");
|
||||
}
|
||||
userService.topUp(balance);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.bwie.user.mapper;
|
||||
|
||||
import com.bwie.common.domain.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
User findByTel(@Param("tel") String tel);
|
||||
|
||||
void update(User user);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.bwie.user.service;
|
||||
|
||||
import com.bwie.common.domain.User;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface UserService {
|
||||
User findByTel(String tel);
|
||||
|
||||
/**
|
||||
* 充值
|
||||
* @param balance
|
||||
*/
|
||||
void topUp(BigDecimal balance);
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package com.bwie.user.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bwie.common.constants.TokenConstants;
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.utils.JwtUtils;
|
||||
import com.bwie.user.mapper.UserMapper;
|
||||
import com.bwie.user.service.UserService;
|
||||
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.math.BigDecimal;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author LiYonJie
|
||||
* @Date 2023/12/27
|
||||
* @Description TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Override
|
||||
public User findByTel(String tel) {
|
||||
return userMapper.findByTel(tel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void topUp(BigDecimal balance) {
|
||||
String token = request.getHeader(TokenConstants.TOKEN);
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
String json = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
User user = JSONObject.parseObject(json, User.class);
|
||||
|
||||
// 获取当前会员等级
|
||||
Integer vipLevel = user.getVipLevel();
|
||||
|
||||
// 单次充值10000赠送2000,会员级别升级为钻石会员,所有商品享受6.5折优惠
|
||||
if (balance.compareTo(new BigDecimal(10000)) >= 0){
|
||||
user.setBalance(user.getBalance().add(balance).add(new BigDecimal(2000)));
|
||||
// 充值后升级为对应的会员级别,高等级会员不会降为低等级会员
|
||||
if (vipLevel < 4){
|
||||
user.setVipLevel(4);
|
||||
}
|
||||
} else if(balance.compareTo(new BigDecimal(5000)) >= 0){
|
||||
// 单次充值5000赠500,会员级别升级为白金会员,所有商品享受8折优惠;
|
||||
user.setBalance(user.getBalance().add(balance).add(new BigDecimal(500)));
|
||||
if (vipLevel < 3){
|
||||
user.setVipLevel(3);
|
||||
}
|
||||
} else if(balance.compareTo(new BigDecimal(3000)) >= 0){
|
||||
// 单次充值满3000赠300,会员级别升级为黄金会员所有商品享受9折优惠;
|
||||
user.setBalance(user.getBalance().add(balance).add(new BigDecimal(300)));
|
||||
if (vipLevel < 2){
|
||||
user.setVipLevel(2);
|
||||
}
|
||||
} else if(balance.compareTo(new BigDecimal(2000)) >= 0){
|
||||
// 单次充值满2000赠200,会员级别升级为白银会员,所有商品享受95折优惠
|
||||
user.setBalance(user.getBalance().add(balance).add(new BigDecimal(200)));
|
||||
if (vipLevel < 1){
|
||||
user.setVipLevel(1);
|
||||
}
|
||||
}
|
||||
userMapper.update(user);
|
||||
redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY + userKey, JSONObject.toJSONString(user),
|
||||
30, TimeUnit.MINUTES);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 10002
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: GMT+8
|
||||
application:
|
||||
# 应用名称
|
||||
name: work-user
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 192.168.73.129:8848
|
||||
username: nacos
|
||||
password: nacos
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
fdfs:
|
||||
# socket 连接时长
|
||||
so-timeout: 1500
|
||||
# 连接 tracker 服务器超时时长
|
||||
connect-timeout: 600
|
||||
# 这两个是你服务器的 IP 地址,注意 23000 端口也要打开,阿里云服务器记得配置安全组。tracker 要和 stroage 服务进行交流
|
||||
tracker-list: 121.36.202.181:22122
|
||||
web-server-url: 121.36.202.181:8888
|
||||
pool:
|
||||
jmx-enabled: false
|
||||
# 生成缩略图
|
||||
thumb-image:
|
||||
height: 500
|
||||
width: 500
|
||||
|
||||
aliyun:
|
||||
oss:
|
||||
file:
|
||||
bucketName: arklorse
|
||||
endpoint: oss-cn-shanghai.aliyuncs.com
|
||||
AccessKeyId: LTAI5tKXvCvwq7hKpZgsY2hk
|
||||
AccessKeySecret: HaOTCePEpeLNukQL3jhR6DZu9CAnmA
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- mybatis数据层 namespace命名空间-->
|
||||
<mapper namespace="com.bwie.user.mapper.UserMapper">
|
||||
<select id="findByTel" resultType="com.bwie.common.domain.User">
|
||||
select user_id,username,password,tel,balance,vip_level from s_user where tel = #{tel}
|
||||
</select>
|
||||
|
||||
<update id="update">
|
||||
update s_user
|
||||
<set>
|
||||
<if test="null != username and '' != username">username = #{username},</if>
|
||||
<if test="null != password and '' != password">password = #{password},</if>
|
||||
<if test="null != tel and '' != tel">tel = #{tel},</if>
|
||||
<if test="null != balance">balance = #{balance},</if>
|
||||
<if test="null != vipLevel">vip_level = #{vipLevel}</if>
|
||||
</set>
|
||||
where user_id = #{userId}
|
||||
</update>
|
||||
</mapper>
|
Loading…
Reference in New Issue