master
commit
7804c601ef
|
@ -0,0 +1,35 @@
|
|||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
|
@ -0,0 +1,31 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>month</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bw-auth</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 项目公共 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.bw</groupId>
|
||||
<artifactId>bw-common</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringBoot Web-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,28 @@
|
|||
package com.bw;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Auth
|
||||
* @Description: 启动器
|
||||
* @CreatedDate: 2024/8/12 下午4:29
|
||||
* @FilePath: com.bw
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@EnableFeignClients
|
||||
public class AuthApplication {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
SpringApplication.run(AuthApplication.class, args);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("Auth 模块启动成功!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送确认配置
|
||||
* 消息发送到交换机的回调
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setConfirmCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换机不管是否收到消息的一个回调方法
|
||||
*
|
||||
* @param correlationData 消息相关数据
|
||||
* @param ack 交换机是否收到消息
|
||||
* @param cause 失败原因
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (!ack) {
|
||||
String exchange = correlationData.getReturned().getExchange();
|
||||
String message = correlationData.getReturned().getMessage().getBody().toString();
|
||||
// 发送异常
|
||||
log.error("消息:{},发送到交换机:{}失败,原因是:{}", message, exchange, cause);
|
||||
// TODO 可以把异常信息 以及 消息的内容直接添加到 MYSQL
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.ReturnedMessage;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送到队列的确认
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnsCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送失败 则会执行这个方法
|
||||
*
|
||||
* @param returnedMessage the returned message and metadata.
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||
log.error("消息:{},被交换机:{} 回退!退回原因为:{}",
|
||||
returnedMessage.getMessage().toString(), returnedMessage.getExchange(), returnedMessage.getReplyText());
|
||||
// TODO 回退了所有的信息,可做补偿机制
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,203 @@
|
|||
package com.bw.controller;
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.service.AuthService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Auth
|
||||
* @Description: 远程调用控制层
|
||||
* @CreatedDate: 2024/8/12 下午3:49
|
||||
* @FilePath: com.bw.controller
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class AuthController {
|
||||
@Resource
|
||||
private AuthService service;
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*/
|
||||
@GetMapping("/sendCode")
|
||||
public Result<Boolean> sendCode(@RequestParam String phone){
|
||||
return service.sendCode(phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*/
|
||||
@PostMapping("/insUser")
|
||||
public Result<Boolean> insUser(@RequestBody UserReq userReq){
|
||||
return service.insUser(userReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result<JwtResponse> login(@RequestBody UserReq userReq){
|
||||
return service.login(userReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
public Result<Map<String,Object>> info(){
|
||||
return service.info(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
@GetMapping("/logout")
|
||||
public Result<Boolean> logout(){
|
||||
return service.logout(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户余额调整
|
||||
*/
|
||||
@GetMapping("/updUserMoney")
|
||||
public Result<Boolean> updUserMoney(
|
||||
@RequestParam("uid") Integer uid,
|
||||
@RequestParam("money") Double money){
|
||||
return service.updUserMoney(uid,money);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户ID精确查询
|
||||
*/
|
||||
@GetMapping("/findUserById/{uid}")
|
||||
public Result<User> findUserById(@PathVariable("uid") Integer uid){
|
||||
return service.findUserById(uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看所有商家
|
||||
*/
|
||||
@GetMapping("/selTypeTcodeAll")
|
||||
Result<List<String>> selTypeTcodeAll(){
|
||||
return service.selTypeTcodeAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有售卖类型
|
||||
*/
|
||||
@GetMapping("/selTypeList")
|
||||
Result<List<Type>> selTypeList(String tname){
|
||||
return service.selTypeAll(tname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改库存
|
||||
*/
|
||||
// @GetMapping("/updTypeNumber")
|
||||
Result<Boolean> updTypeNumber(
|
||||
@RequestParam("tid") Integer tid,
|
||||
@RequestParam("number") Integer number){
|
||||
return service.updTypeNumber(tid, number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
*/
|
||||
@GetMapping("/selOrderAll/{uid}")
|
||||
Result<List<OrderResp>> selOrderAll(@PathVariable Integer uid){
|
||||
return service.selOrderAll(uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单状态修改
|
||||
*/
|
||||
@GetMapping("/updOrderState")
|
||||
Result<Boolean> updOrderState(
|
||||
@RequestParam("oid") Integer oid,
|
||||
@RequestParam("state") Integer state){
|
||||
return service.updOrderState(oid, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单精确查询
|
||||
*/
|
||||
@GetMapping("/findOrderByOid/{oid}")
|
||||
Result<OrderResp> findOrderByOid(@PathVariable Integer oid){
|
||||
return service.findOrderByOid(oid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商铺ID精确查询
|
||||
*/
|
||||
@GetMapping("/findTypeByTid/{tid}")
|
||||
Result<Type> findTypeByTid(@PathVariable Integer tid){
|
||||
return service.findTypeByTid(tid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增订单
|
||||
*/
|
||||
@PostMapping("/insOrder")
|
||||
Result<Boolean> insOrder(@RequestBody Order order){
|
||||
return service.insOrder(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* ES表单查询
|
||||
*/
|
||||
@PostMapping("/selSaleList")
|
||||
public Result<PageResult<SaleResp>> selSaleList(@RequestBody SaleReq saleReq) {
|
||||
return service.selSaleList(saleReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销售ID精确查询ES
|
||||
*/
|
||||
@PostMapping("/findSaleBySid/{sid}")
|
||||
public Result<SaleResp> findSaleBySid(@PathVariable Integer sid) {
|
||||
return service.findSaleBySid(sid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改售卖信息状态
|
||||
*/
|
||||
@GetMapping("/updSaleState")
|
||||
public Result<Boolean> updSaleState(
|
||||
@RequestParam("sid") Integer sid,
|
||||
@RequestParam("state") Integer state) {
|
||||
return service.updSaleStateMysql(sid, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增售卖信息
|
||||
*/
|
||||
@PostMapping("/insSale")
|
||||
public Result<Boolean> insSale(@RequestBody Sale sale) {
|
||||
return service.insSaleMysql(sale);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.bw.factory;
|
||||
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.feign.OrderFeign;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: OrderFeignFallbackFactory
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 下午4:22
|
||||
* @FilePath: com.bw.factory
|
||||
*/
|
||||
@Component
|
||||
public class OrderFeignFallbackFactory implements FallbackFactory<OrderFeign> {
|
||||
@Override
|
||||
public OrderFeign create(Throwable cause) {
|
||||
return new OrderFeign() {
|
||||
|
||||
@Override
|
||||
public Result<List<Type>> selTypeList(String tname) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updTypeNumber(Integer tid, Integer number) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<OrderResp>> selOrderAll(Integer uid) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updOrderState(Integer oid, Integer state) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<OrderResp> findOrderByOid(Integer oid) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Type> findTypeByTid(Integer tid) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> insOrder(Order order) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Order>> selOrderByTimeAndState() {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<String>> selTypeTcodeAll() {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.bw.factory;
|
||||
|
||||
|
||||
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.feign.SaleFeign;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: SaleFeignFallbackFactory
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 下午4:24
|
||||
* @FilePath: com.bw.factory
|
||||
*/
|
||||
@Component
|
||||
public class SaleFeignFallbackFactory implements FallbackFactory<SaleFeign> {
|
||||
@Override
|
||||
public SaleFeign create(Throwable cause) {
|
||||
return new SaleFeign() {
|
||||
@Override
|
||||
public Result<PageResult<SaleResp>> selSaleList(SaleReq saleReq) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<SaleResp> findSaleBySid(Integer sid) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updSaleState(Integer sid, Integer state) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> insSale(Sale sale) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.bw.factory;
|
||||
|
||||
|
||||
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.feign.UserFeign;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: UserFeignFallbackFactory
|
||||
* @Description: 用户模块远程调用熔断
|
||||
* @CreatedDate: 2024/8/12 下午4:20
|
||||
* @FilePath: com.bw.factory
|
||||
*/
|
||||
@Component
|
||||
public class UserFeignFallbackFactory implements FallbackFactory<UserFeign> {
|
||||
@Override
|
||||
public UserFeign create(Throwable cause) {
|
||||
return new UserFeign() {
|
||||
@Override
|
||||
public Result<Boolean> sendCode(String phone) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> insUser(UserReq userReq) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<JwtResponse> login(UserReq userReq) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> updUserMoney(Integer uid, Double money) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<User> findUserById(Integer uid) {
|
||||
return Result.error(null,"网络异常,请重试...");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.bw.feign;
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.factory.OrderFeignFallbackFactory;
|
||||
import com.bw.factory.UserFeignFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: UserFeign
|
||||
* @Description: 远程调用接口
|
||||
* @CreatedDate: 2024/8/12 下午4:18
|
||||
* @FilePath: com.bw.feign
|
||||
*/
|
||||
|
||||
@FeignClient(name = "month-order",value = "month-order", fallbackFactory = OrderFeignFallbackFactory.class)
|
||||
public interface OrderFeign {
|
||||
/**
|
||||
* 查询所有售卖类型
|
||||
*/
|
||||
@GetMapping("/Order/selTypeList")
|
||||
Result<List<Type>> selTypeList(@RequestParam String tname);
|
||||
|
||||
/**
|
||||
* 修改库存
|
||||
*/
|
||||
@GetMapping("/Order/updTypeNumber")
|
||||
Result<Boolean> updTypeNumber(
|
||||
@RequestParam("tid") Integer tid,
|
||||
@RequestParam("number") Integer number);
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
*/
|
||||
@GetMapping("/Order/selOrderAll")
|
||||
Result<List<OrderResp>> selOrderAll(@RequestParam Integer uid);
|
||||
|
||||
/**
|
||||
* 订单状态修改
|
||||
*/
|
||||
@GetMapping("/Order/updOrderState")
|
||||
Result<Boolean> updOrderState(
|
||||
@RequestParam("oid") Integer oid,
|
||||
@RequestParam("state") Integer state);
|
||||
|
||||
/**
|
||||
* 订单精确查询
|
||||
*/
|
||||
@GetMapping("/Order/findOrderByOid/{oid}")
|
||||
Result<OrderResp> findOrderByOid(@PathVariable Integer oid);
|
||||
|
||||
/**
|
||||
* 商铺ID精确查询
|
||||
*/
|
||||
@GetMapping("/Order/findTypeByTid/{tid}")
|
||||
Result<Type> findTypeByTid(@PathVariable Integer tid);
|
||||
|
||||
/**
|
||||
* 新增订单
|
||||
*/
|
||||
@PostMapping("/Order/insOrder")
|
||||
Result<Boolean> insOrder(@RequestBody Order order);
|
||||
|
||||
@GetMapping("/Order/selOrderByTimeAndState")
|
||||
public Result<List<Order>> selOrderByTimeAndState();
|
||||
|
||||
/**
|
||||
* 查看所有商家
|
||||
*/
|
||||
@GetMapping("/Order/selTypeTcodeAll")
|
||||
Result<List<String>> selTypeTcodeAll();
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.bw.feign;
|
||||
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.factory.SaleFeignFallbackFactory;
|
||||
import com.bw.factory.UserFeignFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: UserFeign
|
||||
* @Description: 用户远程调用接口
|
||||
* @CreatedDate: 2024/8/12 下午4:18
|
||||
* @FilePath: com.bw.feign
|
||||
*/
|
||||
|
||||
@FeignClient(name = "month-sale",value = "month-sale", fallbackFactory = SaleFeignFallbackFactory.class)
|
||||
public interface SaleFeign {
|
||||
/**
|
||||
* ES表单查询
|
||||
*/
|
||||
@PostMapping("/Sale/selSaleList")
|
||||
public Result<PageResult<SaleResp>> selSaleList(@RequestBody SaleReq saleReq);
|
||||
|
||||
/**
|
||||
* 销售ID精确查询ES
|
||||
*/
|
||||
@PostMapping("/Sale/{sid}")
|
||||
public Result<SaleResp> findSaleBySid(@PathVariable Integer sid);
|
||||
|
||||
/**
|
||||
* 修改售卖信息状态
|
||||
*/
|
||||
@GetMapping("/Sale/updSaleState")
|
||||
public Result<Boolean> updSaleState(
|
||||
@RequestParam("sid") Integer sid,
|
||||
@RequestParam("state") Integer state);
|
||||
|
||||
/**
|
||||
* 新增售卖信息
|
||||
*/
|
||||
@PostMapping("/Sale/insSale")
|
||||
public Result<Boolean> insSale(@RequestBody Sale sale);
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.bw.feign;
|
||||
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.factory.UserFeignFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: UserFeign
|
||||
* @Description: 用户远程调用接口
|
||||
* @CreatedDate: 2024/8/12 下午4:18
|
||||
* @FilePath: com.bw.feign
|
||||
*/
|
||||
|
||||
@FeignClient(name = "month-user",value = "month-user", fallbackFactory = UserFeignFallbackFactory.class)
|
||||
public interface UserFeign {
|
||||
/**
|
||||
* 发送验证码
|
||||
*/
|
||||
@GetMapping("/User/sendCode")
|
||||
public Result<Boolean> sendCode(@RequestParam String phone);
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*/
|
||||
@PostMapping("/User/insUser")
|
||||
public Result<Boolean> insUser(@RequestBody UserReq userReq);
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
@PostMapping("/User/login")
|
||||
public Result<JwtResponse> login(@RequestBody UserReq userReq);
|
||||
|
||||
/**
|
||||
* 用户余额调整
|
||||
*/
|
||||
@GetMapping("/User/updUserMoney")
|
||||
public Result<Boolean> updUserMoney(
|
||||
@RequestParam("uid") Integer uid,
|
||||
@RequestParam("money") Double money);
|
||||
|
||||
/**
|
||||
* 用户ID精确查询
|
||||
*/
|
||||
@GetMapping("/User/{uid}")
|
||||
public Result<User> findUserById(@PathVariable("uid") Integer uid);
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.bw.service;
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Auth
|
||||
* @Description: 远程调用业务层
|
||||
* @CreatedDate: 2024/8/12 下午3:49
|
||||
* @FilePath: com.bw.service.impl
|
||||
*/
|
||||
|
||||
public interface AuthService {
|
||||
Result<Boolean> sendCode(String phone);
|
||||
|
||||
Result<Boolean> insUser(UserReq userReq);
|
||||
|
||||
Result<JwtResponse> login(UserReq userReq);
|
||||
|
||||
Result<Boolean> updUserMoney(Integer uid, Double money);
|
||||
|
||||
Result<User> findUserById(Integer uid);
|
||||
|
||||
Result<List<Type>> selTypeAll(String tname);
|
||||
|
||||
Result<Boolean> updTypeNumber(Integer tid, Integer number);
|
||||
|
||||
Result<List<OrderResp>> selOrderAll(Integer uid);
|
||||
|
||||
Result<Boolean> updOrderState(Integer oid, Integer state);
|
||||
|
||||
Result<OrderResp> findOrderByOid(Integer oid);
|
||||
|
||||
Result<Type> findTypeByTid(Integer tid);
|
||||
|
||||
Result<Boolean> insOrder(Order order);
|
||||
|
||||
Result<PageResult<SaleResp>> selSaleList(SaleReq saleReq);
|
||||
|
||||
Result<SaleResp> findSaleBySid(Integer sid);
|
||||
|
||||
Result<Boolean> updSaleStateMysql(Integer sid, Integer state);
|
||||
|
||||
Result<Boolean> insSaleMysql(Sale sale);
|
||||
|
||||
Result<Map<String, Object>> info(HttpServletRequest request);
|
||||
|
||||
Result<Boolean> logout(HttpServletRequest request);
|
||||
|
||||
Result<List<String>> selTypeTcodeAll();
|
||||
}
|
|
@ -0,0 +1,321 @@
|
|||
package com.bw.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.Money;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.common.utils.JwtConstants;
|
||||
import com.bw.common.utils.JwtUtils;
|
||||
import com.bw.common.utils.StringUtils;
|
||||
import com.bw.feign.OrderFeign;
|
||||
import com.bw.feign.SaleFeign;
|
||||
import com.bw.feign.UserFeign;
|
||||
import com.bw.service.AuthService;
|
||||
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Auth
|
||||
* @Description: 远程调用实现层
|
||||
* @CreatedDate: 2024/8/12 下午3:49
|
||||
* @FilePath: com.bw.service.impl
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
@Resource
|
||||
private UserFeign userFeign;
|
||||
@Resource
|
||||
private SaleFeign saleFeign;
|
||||
@Resource
|
||||
private OrderFeign orderFeign;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
* @param phone
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Boolean> sendCode(String phone) {
|
||||
return userFeign.sendCode(phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param userReq
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Boolean> insUser(UserReq userReq) {
|
||||
return userFeign.insUser(userReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param userReq
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<JwtResponse> login(UserReq userReq) {
|
||||
return userFeign.login(userReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户余额调整
|
||||
* @param uid
|
||||
* @param money
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Boolean> updUserMoney(Integer uid, Double money) {
|
||||
return userFeign.updUserMoney(uid, money);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户ID精确查询
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<User> findUserById(Integer uid) {
|
||||
return userFeign.findUserById(uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有售卖类型
|
||||
* @param tname
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<List<Type>> selTypeAll(String tname) {
|
||||
return orderFeign.selTypeList(tname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改库存
|
||||
* @param tid
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Boolean> updTypeNumber(Integer tid, Integer number) {
|
||||
return orderFeign.updTypeNumber(tid, number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<List<OrderResp>> selOrderAll(Integer uid) {
|
||||
return orderFeign.selOrderAll(uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单状态修改
|
||||
* @param oid
|
||||
* @param state
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public Result<Boolean> updOrderState(Integer oid, Integer state) {
|
||||
OrderResp data = orderFeign.findOrderByOid(oid).getData();
|
||||
// 付款
|
||||
if (state == 2) {
|
||||
User user = userFeign.findUserById(data.getUid()).getData();
|
||||
if (user.getUmoney() < data.getOprice()){
|
||||
return Result.error("余额不足");
|
||||
}
|
||||
updUserMoney(data.getUid(), 0 - data.getOprice());
|
||||
// rabbitTemplate.convertAndSend("upd_money",
|
||||
// new Money(data.getUid(), 0 - data.getOprice()),
|
||||
// message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// });
|
||||
// 付款完成后 售卖状态更改为 3.待使用
|
||||
updSaleStateMysql(data.getSid(),3);
|
||||
}else if (state == 3){
|
||||
// 确认收货 库存-1
|
||||
orderFeign.updTypeNumber(data.getTid(),-1);
|
||||
// 确认收货后 售卖状态更改为 5.完成
|
||||
updSaleStateMysql(data.getSid(),5);
|
||||
}
|
||||
return orderFeign.updOrderState(oid, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单精确查询
|
||||
* @param oid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<OrderResp> findOrderByOid(Integer oid) {
|
||||
return orderFeign.findOrderByOid(oid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商铺ID精确查询
|
||||
* @param tid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Type> findTypeByTid(Integer tid) {
|
||||
return orderFeign.findTypeByTid(tid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增订单
|
||||
* @param order
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public Result<Boolean> insOrder(Order order) {
|
||||
Result<Boolean> booleanResult = orderFeign.insOrder(order);
|
||||
// 购买后售卖状态更改为 2.已销售
|
||||
updSaleStateMysql(order.getSid(),2);
|
||||
return booleanResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* ES表单查询
|
||||
* @param saleReq
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<PageResult<SaleResp>> selSaleList(SaleReq saleReq) {
|
||||
return saleFeign.selSaleList(saleReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销售ID精确查询ES
|
||||
* @param sid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<SaleResp> findSaleBySid(Integer sid) {
|
||||
return saleFeign.findSaleBySid(sid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改售卖信息状态
|
||||
* @param sid
|
||||
* @param state
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public Result<Boolean> updSaleStateMysql(Integer sid, Integer state) {
|
||||
// 完成
|
||||
if (state == 5){
|
||||
SaleResp data = findSaleBySid(sid).getData();
|
||||
// 余额增加
|
||||
updUserMoney(data.getUid(), data.getSprice());
|
||||
// rabbitTemplate.convertAndSend("upd_money",
|
||||
// new Money(data.getUid(), data.getSprice()),
|
||||
// message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// });
|
||||
}
|
||||
return saleFeign.updSaleState(sid, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增售卖信息
|
||||
* @param sale
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public Result<Boolean> insSaleMysql(Sale sale) {
|
||||
updTypeNumber(sale.getTid(),1);
|
||||
return saleFeign.insSale(sale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Map<String, Object>> info(HttpServletRequest request) {
|
||||
String token = request.getHeader("token");
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
String userString = redisTemplate.opsForValue().get(JwtConstants.LOGIN_TOKEN+userKey);
|
||||
if (StringUtils.isEmpty(userString)){
|
||||
return Result.error("用户信息失效,请重新登录");
|
||||
}
|
||||
User user = JSONObject.parseObject(userString, User.class);
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("name",user.getUname());
|
||||
map.put("uid",user.getUid());
|
||||
return Result.success(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Boolean> logout(HttpServletRequest request) {
|
||||
String token = request.getHeader("token");
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
redisTemplate.delete(JwtConstants.LOGIN_TOKEN+userKey);
|
||||
return Result.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<String>> selTypeTcodeAll() {
|
||||
return orderFeign.selTypeTcodeAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时器 每25秒执行一次
|
||||
*/
|
||||
@Scheduled(cron = "*/25 * * * * ?")
|
||||
public void timer(){
|
||||
// 查询所有订单中 购买时间超48小时且状态为2.未确认的订单
|
||||
List<Order> orders = orderFeign.selOrderByTimeAndState().getData();
|
||||
if (orders==null || orders.size()<=0){
|
||||
System.out.println("无超时未确认订单");
|
||||
return;
|
||||
}
|
||||
// 修改订单状态
|
||||
orders.forEach(order -> updOrderState(order.getOid(),3));
|
||||
System.out.println("已修改"+orders.size()+"条,超时未确认订单");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
com.bw.factory.UserFeignFallbackFactory
|
||||
com.bw.factory.OrderFeignFallbackFactory
|
||||
com.bw.factory.SaleFeignFallbackFactory
|
|
@ -0,0 +1,42 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9000
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: GMT+8
|
||||
application:
|
||||
# 应用名称
|
||||
name: month-auth
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
|
||||
|
||||
#proxy: {
|
||||
#[process.env.VUE_APP_BASE_API]: {
|
||||
#target: `http://localhost:18080/Auth`,
|
||||
#changeOrigin: true,
|
||||
#pathRewrite: {
|
||||
# ['^' + process.env.VUE_APP_BASE_API]: ''
|
||||
#}
|
||||
#}
|
||||
#}
|
|
@ -0,0 +1,119 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>month</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bw-common</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<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>
|
||||
<!-- oss 图片上传 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- <!–mq 依赖–>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>-->
|
||||
|
||||
<!--fastDfs文件上传-->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,33 @@
|
|||
package com.bw.common.advice;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Common
|
||||
* @Description: 公共异常处理
|
||||
* @CreatedDate: 2024/8/12 上午10:09
|
||||
* @FilePath: com.bw.advice
|
||||
*/
|
||||
|
||||
|
||||
import com.bw.common.exception.CommonException;
|
||||
import com.bw.common.result.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Common
|
||||
* @Description: 公共异常处理
|
||||
* @CreatedDate: 2024/8/12 上午10:09
|
||||
* @FilePath: com.bw.advice
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CommonAdvice {
|
||||
@ExceptionHandler(CommonException.class)
|
||||
public Result<Boolean> commonException(CommonException e) {
|
||||
log.error("公共异常处理,异常信息:[{}]",e.toString());
|
||||
return Result.error(false,e.getMessage());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.bw.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<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Object> 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.bw.common.constants;
|
||||
|
||||
/**
|
||||
* @description: 系统常量
|
||||
* @author DongZl
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* 成功标记
|
||||
*/
|
||||
public static final Integer SUCCESS = 200;
|
||||
public static final String SUCCESS_MSG = "操作成功";
|
||||
/**
|
||||
* 失败标记
|
||||
*/
|
||||
public static final Integer ERROR = 500;
|
||||
public static final String ERROR_MSG = "操作异常";
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.bw.common.constants;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: Jwt常量
|
||||
*/
|
||||
public class JwtConstants {
|
||||
|
||||
/**
|
||||
* 用户ID字段
|
||||
*/
|
||||
public static final String DETAILS_USER_ID = "user_id";
|
||||
|
||||
/**
|
||||
* 用户名字段
|
||||
*/
|
||||
public static final String DETAILS_USERNAME = "username";
|
||||
|
||||
/**
|
||||
* 用户标识
|
||||
*/
|
||||
public static final String USER_KEY = "user_key";
|
||||
|
||||
/**
|
||||
* 令牌秘钥
|
||||
*/
|
||||
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.bw.common.constants;
|
||||
|
||||
public class RabbitMQConstants {
|
||||
public static final String SEND_SMS_QUEUE = "send_sms_queue";
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.bw.common.constants;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 令牌常量
|
||||
*/
|
||||
public class TokenConstants {
|
||||
/**
|
||||
* 缓存有效期,默认720(分钟)
|
||||
*/
|
||||
public final static long EXPIRATION = 720;
|
||||
/**
|
||||
* 缓存刷新时间,默认120(分钟)
|
||||
*/
|
||||
public final static long REFRESH_TIME = 120;
|
||||
/**
|
||||
* 权限缓存前缀
|
||||
*/
|
||||
public final static String LOGIN_TOKEN_KEY = "login_tokens:";
|
||||
/**
|
||||
* token标识
|
||||
*/
|
||||
public static final String TOKEN = "token";
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.bw.common.domain.pojo;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 订单表
|
||||
* @CreatedDate: 2024/8/12 上午9:41
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Order {
|
||||
|
||||
/**
|
||||
* 订单id
|
||||
*/
|
||||
private Integer oid;
|
||||
|
||||
/**
|
||||
* 订单所属类别
|
||||
*/
|
||||
private Integer tid;
|
||||
|
||||
/**
|
||||
* 订单购买用户
|
||||
*/
|
||||
private Integer uid;
|
||||
|
||||
/**
|
||||
* 订单购买商品id
|
||||
*/
|
||||
private Integer sid;
|
||||
|
||||
/**
|
||||
* 订单购买单
|
||||
*/
|
||||
private String ocode;
|
||||
|
||||
/**
|
||||
* 订单购买花费
|
||||
*/
|
||||
private Double oprice;
|
||||
|
||||
/**
|
||||
* 订单购买时间
|
||||
*/
|
||||
private String odate;
|
||||
|
||||
/**
|
||||
* 订单质保到期时间
|
||||
*/
|
||||
private String oinsurance;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 订单购买状态 1.未支付 2.待确认 3.完成
|
||||
*/
|
||||
private Integer ostate;
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.bw.common.domain.pojo;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 上午9:41
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Sale {
|
||||
|
||||
/**
|
||||
* 售卖卷码id
|
||||
*/
|
||||
private Integer sid;
|
||||
|
||||
/**
|
||||
* 售卖卷码类别
|
||||
*/
|
||||
private Integer tid;
|
||||
|
||||
/**
|
||||
* 售卖用户
|
||||
*/
|
||||
private Integer uid;
|
||||
|
||||
/**
|
||||
* 售卖单号
|
||||
*/
|
||||
private String scode;
|
||||
|
||||
/**
|
||||
* 售卖价格
|
||||
*/
|
||||
private Double sprice;
|
||||
|
||||
/**
|
||||
* 到期时间
|
||||
*/
|
||||
private String sdate;
|
||||
|
||||
/**
|
||||
* 售卖状态 1.寄售中 2.已销售 3.待使用 4.待打款 5.完成 6.关闭
|
||||
*/
|
||||
private Integer sstate;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.bw.common.domain.pojo;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Type
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 上午9:40
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Type {
|
||||
|
||||
/**
|
||||
* 券码类型ID
|
||||
*/
|
||||
private Integer tid;
|
||||
|
||||
/**
|
||||
* 券码类型名称
|
||||
*/
|
||||
private String tname;
|
||||
|
||||
/**
|
||||
* 券码所属企业码
|
||||
*/
|
||||
private String tcode;
|
||||
|
||||
/**
|
||||
* 库存数量
|
||||
*/
|
||||
private Integer tnumber;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.bw.common.domain.pojo;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description: 用户类
|
||||
* @CreatedDate: 2024/8/12 上午9:40
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Integer uid;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String uname;
|
||||
|
||||
/**
|
||||
* 用户手机号
|
||||
*/
|
||||
private String uphone;
|
||||
|
||||
/**
|
||||
* 用户余额
|
||||
*/
|
||||
private Double umoney;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.bw.common.domain.request;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Money
|
||||
* @Description: 支付类
|
||||
* @CreatedDate: 2024/8/12 下午3:38
|
||||
* @FilePath: com.bw.common.domain.request
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Money implements Serializable {
|
||||
Integer uid;
|
||||
Double money;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.bw.common.domain.request;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 上午9:41
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SaleReq {
|
||||
/**
|
||||
* 售卖卷码类别
|
||||
*/
|
||||
private Integer tid;
|
||||
|
||||
/**
|
||||
* 商户编码
|
||||
*/
|
||||
private String tcode;
|
||||
|
||||
/**
|
||||
* 关键字
|
||||
*/
|
||||
private String keyword;
|
||||
|
||||
/**
|
||||
* 售卖用户
|
||||
*/
|
||||
private Integer uid;
|
||||
|
||||
/**
|
||||
* 售卖状态 0.全部 1.寄售中 2.已销售 3.待使用 4.待打款 5.完成 6.关闭
|
||||
*/
|
||||
private Integer sstate;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.bw.common.domain.request;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description: 用户类
|
||||
* @CreatedDate: 2024/8/12 上午9:40
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UserReq {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String uname;
|
||||
|
||||
/**
|
||||
* 用户手机号
|
||||
*/
|
||||
private String uphone;
|
||||
|
||||
/**
|
||||
* 用户验证码
|
||||
*/
|
||||
private String code;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.bw.common.domain.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class JwtResponse {
|
||||
private String token;
|
||||
private String existTime;
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.bw.common.domain.response;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 订单表
|
||||
* @CreatedDate: 2024/8/12 上午9:41
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class OrderResp {
|
||||
|
||||
/**
|
||||
* 订单id
|
||||
*/
|
||||
private Integer oid;
|
||||
|
||||
/**
|
||||
* 订单所属类别
|
||||
*/
|
||||
private Integer tid;
|
||||
// 类别名称
|
||||
private String tname;
|
||||
// 类别所属商户编码
|
||||
private String tcode;
|
||||
|
||||
/**
|
||||
* 订单购买用户
|
||||
*/
|
||||
private Integer uid;
|
||||
// 用户名
|
||||
private String uname;
|
||||
|
||||
/**
|
||||
* 订单购买商品id
|
||||
*/
|
||||
private Integer sid;
|
||||
// 售卖价格
|
||||
private Double sprice;
|
||||
|
||||
/**
|
||||
* 订单购买单
|
||||
*/
|
||||
private String ocode;
|
||||
|
||||
/**
|
||||
* 订单购买花费
|
||||
*/
|
||||
private Double oprice;
|
||||
|
||||
/**
|
||||
* 订单购买时间
|
||||
*/
|
||||
private String odate;
|
||||
|
||||
/**
|
||||
* 订单质保到期时间
|
||||
*/
|
||||
private String oinsurance;
|
||||
|
||||
/**
|
||||
* 订单购买状态 1.未支付 2.待确认 3.完成
|
||||
*/
|
||||
private Integer ostate;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.bw.common.domain.response;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 上午9:41
|
||||
* @FilePath: com.bw.common.domain.pojo
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SaleResp {
|
||||
|
||||
/**
|
||||
* 售卖卷码id
|
||||
*/
|
||||
private Integer sid;
|
||||
|
||||
/**
|
||||
* 售卖卷码类别
|
||||
*/
|
||||
private Integer tid;
|
||||
// 类别名称
|
||||
private String tname;
|
||||
// 类别所属商户编码
|
||||
private String tcode;
|
||||
|
||||
/**
|
||||
* 售卖用户
|
||||
*/
|
||||
private Integer uid;
|
||||
// 用户名
|
||||
private String uname;
|
||||
|
||||
/**
|
||||
* 售卖单号
|
||||
*/
|
||||
private String scode;
|
||||
|
||||
/**
|
||||
* 售卖价格
|
||||
*/
|
||||
private Double sprice;
|
||||
|
||||
/**
|
||||
* 到期时间
|
||||
*/
|
||||
private String sdate;
|
||||
|
||||
/**
|
||||
* 售卖状态 1.寄售中 2.已销售 3.待使用 4.待打款 5.完成 6.关闭
|
||||
*/
|
||||
private Integer sstate;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.bw.common.exception;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: CommonException
|
||||
* @Description: 公共异常
|
||||
* @CreatedDate: 2024/8/12 上午10:08
|
||||
* @FilePath: com.bw.exception
|
||||
*/
|
||||
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: CommonException
|
||||
* @Description: 公共异常
|
||||
* @CreatedDate: 2024/8/12 上午10:08
|
||||
* @FilePath: com.bw.exception
|
||||
*/
|
||||
@Component
|
||||
public class CommonException extends RuntimeException {
|
||||
public CommonException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CommonException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CommonException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public CommonException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
protected CommonException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.bw.common.result;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 列表返回结果集
|
||||
*/
|
||||
@Data
|
||||
public class PageResult<T> implements Serializable {
|
||||
/**
|
||||
* 总条数
|
||||
*/
|
||||
private long total;
|
||||
/**
|
||||
* 结果集合
|
||||
*/
|
||||
private List<T> list;
|
||||
public PageResult() {
|
||||
}
|
||||
public PageResult(long total, List<T> list) {
|
||||
this.total = total;
|
||||
this.list = list;
|
||||
}
|
||||
public static <T> PageResult<T> toPageResult(long total, List<T> list){
|
||||
return new PageResult(total , list);
|
||||
}
|
||||
public static <T> Result<PageResult<T>> toResult(long total, List<T> list){
|
||||
return Result.success(PageResult.toPageResult(total,list));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.bw.common.result;
|
||||
|
||||
import com.bw.common.constants.Constants;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 响应信息主体
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final int SUCCESS = Constants.SUCCESS;
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final int FAIL = Constants.ERROR;
|
||||
/**
|
||||
* 返回状态码
|
||||
*/
|
||||
private int code;
|
||||
/**
|
||||
* 响应信息
|
||||
*/
|
||||
private String msg;
|
||||
/**
|
||||
* 响应数据
|
||||
*/
|
||||
private T data;
|
||||
|
||||
public static <T> Result<T> success() {
|
||||
return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(T data) {
|
||||
return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(T data, String msg) {
|
||||
return restResult(data, SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error() {
|
||||
return restResult(null, FAIL, Constants.ERROR_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(String msg) {
|
||||
return restResult(null, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(T data) {
|
||||
return restResult(data, FAIL, Constants.ERROR_MSG);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(T data, String msg) {
|
||||
return restResult(data, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(int code, String msg) {
|
||||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||
Result<T> apiResult = new Result<>();
|
||||
apiResult.setCode(code);
|
||||
apiResult.setData(data);
|
||||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.bw.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;
|
||||
|
||||
|
||||
@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 "删除成功";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package com.bw.common.utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @description: 生成验证码工具类
|
||||
* @Date 2023-5-11 上午 10:09
|
||||
*/
|
||||
public class GenCodeUtils {
|
||||
|
||||
/**
|
||||
* 数字类型
|
||||
*/
|
||||
private static final String NUMBER_STR = "0123456789";
|
||||
/**
|
||||
* 字母类型
|
||||
*/
|
||||
private static final String LETTERS_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
/**
|
||||
* 短信验证码长度
|
||||
*/
|
||||
private static final Integer SMS_CODE_LENGTH = 4;
|
||||
|
||||
/**
|
||||
* 生成短信四位验证码
|
||||
* @return 验证码
|
||||
*/
|
||||
public static String genLetterStrSms(){
|
||||
return genCode(LETTERS_STR, SMS_CODE_LENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成短信四位验证码
|
||||
* @return 验证码
|
||||
*/
|
||||
public static String genNumberCodeSms(){
|
||||
return genCode(NUMBER_STR, SMS_CODE_LENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
* @param codeLength 验证码长度
|
||||
* @return 验证码
|
||||
*/
|
||||
public static String genLetterStr(int codeLength){
|
||||
return genCode(LETTERS_STR, codeLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
* @param codeLength 验证码长度
|
||||
* @return 验证码
|
||||
*/
|
||||
public static String genNumberCode( int codeLength){
|
||||
return genCode(NUMBER_STR, codeLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
* @param str 验证码字符串
|
||||
* @param codeLength 验证码长度
|
||||
* @return 验证码
|
||||
*/
|
||||
public static String genCode (String str, int codeLength){
|
||||
//将字符串转换为一个新的字符数组。
|
||||
char[] verificationCodeArray = str.toCharArray();
|
||||
Random random = new Random();
|
||||
//计数器
|
||||
int count = 0;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
do {
|
||||
//随机生成一个随机数
|
||||
int index = random.nextInt(verificationCodeArray.length);
|
||||
char c = verificationCodeArray[index];
|
||||
//限制四位不重复数字
|
||||
if (stringBuilder.indexOf(String.valueOf(c)) == -1) {
|
||||
stringBuilder.append(c);
|
||||
//计数器加1
|
||||
count++;
|
||||
}
|
||||
//当count等于4时结束,随机生成四位数的验证码
|
||||
} while (count != codeLength);
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,310 @@
|
|||
package com.bw.common.utils;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doGet(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* post form
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param bodys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
Map<String, String> bodys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (bodys != null) {
|
||||
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
|
||||
|
||||
for (String key : bodys.keySet()) {
|
||||
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
|
||||
}
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
|
||||
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
|
||||
request.setEntity(formEntity);
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post String
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post stream
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put String
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put stream
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doDelete(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
||||
StringBuilder sbUrl = new StringBuilder();
|
||||
sbUrl.append(host);
|
||||
if (!StringUtils.isBlank(path)) {
|
||||
sbUrl.append(path);
|
||||
}
|
||||
if (null != querys) {
|
||||
StringBuilder sbQuery = new StringBuilder();
|
||||
for (Map.Entry<String, String> query : querys.entrySet()) {
|
||||
if (0 < sbQuery.length()) {
|
||||
sbQuery.append("&");
|
||||
}
|
||||
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append(query.getValue());
|
||||
}
|
||||
if (!StringUtils.isBlank(query.getKey())) {
|
||||
sbQuery.append(query.getKey());
|
||||
if (!StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append("=");
|
||||
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 < sbQuery.length()) {
|
||||
sbUrl.append("?").append(sbQuery);
|
||||
}
|
||||
}
|
||||
|
||||
return sbUrl.toString();
|
||||
}
|
||||
|
||||
private static HttpClient wrapClient(String host) {
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
if (host.startsWith("https://")) {
|
||||
sslClient(httpClient);
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private static void sslClient(HttpClient httpClient) {
|
||||
try {
|
||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
X509TrustManager tm = new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
};
|
||||
ctx.init(null, new TrustManager[] { tm }, null);
|
||||
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
||||
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
||||
SchemeRegistry registry = ccm.getSchemeRegistry();
|
||||
registry.register(new Scheme("https", 443, ssf));
|
||||
} catch (KeyManagementException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.bw.common.utils;
|
||||
|
||||
public class JwtConstants {
|
||||
/**
|
||||
* 用户ID字段
|
||||
*/
|
||||
public static final String DETAILS_USER_ID = "user_id";
|
||||
|
||||
/**
|
||||
* 用户名字段
|
||||
*/
|
||||
public static final String DETAILS_USERNAME = "user_name";
|
||||
|
||||
/**
|
||||
* 用户标识
|
||||
*/
|
||||
public static final String USER_KEY = "user_key";
|
||||
|
||||
/**
|
||||
* 令牌秘钥
|
||||
*/
|
||||
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
/**
|
||||
* 手机
|
||||
*/
|
||||
public static final String USER_PHONE = "user_phone";
|
||||
|
||||
|
||||
/**
|
||||
* 用户token 的键的前缀
|
||||
*/
|
||||
public static final String LOGIN_TOKEN = "login_token:";
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
package com.bw.common.utils;
|
||||
|
||||
import com.bw.common.constants.JwtConstants;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description: Jwt工具类
|
||||
*/
|
||||
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,66 @@
|
|||
package com.bw.common.utils;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author
|
||||
* @version 1.0.0
|
||||
* @ClassName MsgUtil.java
|
||||
* @Description TODO
|
||||
* @createTime 2022年05月26日 15:49:00
|
||||
*/
|
||||
public class MsgUtil {
|
||||
/**
|
||||
* 生成验证码
|
||||
* @param n 验证码位数
|
||||
* @return
|
||||
*/
|
||||
public static String getCode(Integer n){
|
||||
String code="";
|
||||
for (int i = 0; i < n; i++) {
|
||||
code+=(int)(Math.random()*10);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
public static JSONObject sendMsg(String phone,String code){
|
||||
String host = "https://duanxi.market.alicloudapi.com";
|
||||
String path = "/sendSms";
|
||||
String method = "GET";
|
||||
String appcode = "ad464d7044e340b2a49dadde7678d306";
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
||||
headers.put("Authorization", "APPCODE " + appcode);
|
||||
Map<String, String> querys = new HashMap<String, String>();
|
||||
querys.put("mobile", phone);
|
||||
querys.put("content", "{code:"+code+"}");
|
||||
querys.put("tNum", "tNum");
|
||||
querys.put("tNumAlias", "示例模板1");
|
||||
|
||||
|
||||
try {
|
||||
HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
|
||||
// {
|
||||
// "showapi_res_code": 0,
|
||||
// "showapi_res_error": "",
|
||||
// "showapi_res_body": {
|
||||
// "ret_code": "0",
|
||||
// "remark": "提交成功!",
|
||||
// "taskID": "12345",
|
||||
// "successCounts": "1"
|
||||
// }
|
||||
return JSON.parseObject(EntityUtils.toString(response.getEntity()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
package com.bw.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;
|
||||
|
||||
/**
|
||||
* Oss服务调用
|
||||
*/
|
||||
@Log4j2
|
||||
public class OssUtil {
|
||||
|
||||
/**
|
||||
* Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
|
||||
*/
|
||||
private static String endPoint = "oss-cn-shanghai.aliyuncs.com";
|
||||
private static String accessKeyId = "LTAI5tBdT6WQReZTwTLizHV7";
|
||||
private static String accessKeySecret = "4nHCxAg1sfBTmpDdS9t5RJxfua5RMK";
|
||||
private static String accessPre = "https://bawei-huyang.oss-cn-shanghai.aliyuncs.com/";
|
||||
|
||||
/**
|
||||
* bucket名称
|
||||
* @return
|
||||
*/
|
||||
private static String bucketName = "bawei-huyang";
|
||||
|
||||
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,67 @@
|
|||
package com.bw.common.utils;
|
||||
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @description: 字符串处理工具类
|
||||
*/
|
||||
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否为空
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isNull(Object object) {
|
||||
return object == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Collection<?> coll) {
|
||||
return isNull(coll) || coll.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param strs 需要检查的字符串数组
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean matches(String str, List<String> strs) {
|
||||
if (isEmpty(str) || isEmpty(strs)) {
|
||||
return false;
|
||||
}
|
||||
for (String pattern : strs) {
|
||||
if (isMatch(pattern, str))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断url是否与规则配置:
|
||||
* ? 表示单个字符;
|
||||
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||
* ** 表示任意层路径;
|
||||
*
|
||||
* @param pattern 匹配规则
|
||||
* @param url 需要匹配的url
|
||||
* @return
|
||||
*/
|
||||
public static boolean isMatch(String pattern, String url) {
|
||||
AntPathMatcher matcher = new AntPathMatcher();
|
||||
return matcher.match(pattern, url);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package com.bw.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 = "LTAI5tHU282xbcCSKZJSuKyH";
|
||||
|
||||
private static String accessKeySecret = "mX4tPoqoI55x3ACK1Z7IFiuAMVxuQr";
|
||||
|
||||
/**
|
||||
* 短信访问域名
|
||||
*/
|
||||
private static String endpoint = "dysmsapi.aliyuncs.com";
|
||||
/**
|
||||
* 短信签名
|
||||
*/
|
||||
private static String signName = "乐优购";
|
||||
|
||||
private static String templateCode = "SMS_163851467";
|
||||
|
||||
/**
|
||||
* 实例化短信对象
|
||||
*/
|
||||
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 sendDataMap
|
||||
*/
|
||||
public static String sendSms(String tel, 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,43 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>month</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bw-gateway</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 公共模块 -->
|
||||
<dependency>
|
||||
<groupId>com.bw</groupId>
|
||||
<artifactId>bw-common</artifactId>
|
||||
</dependency>
|
||||
<!-- 网关依赖 -->
|
||||
<!-- SpringCloud Gateway -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Sentinel Gateway -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>
|
||||
<!-- 引入阿里巴巴sentinel限流 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,20 @@
|
|||
package com.bw;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Gateway
|
||||
* @Description: 网关启动器
|
||||
* @CreatedDate: 2024/8/12 下午6:50
|
||||
* @FilePath: com.bw
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
System.out.println("Gateway 模块启动成功!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.bw.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<String> whites = new ArrayList<>();
|
||||
|
||||
public void setWhites(List<String> whites) {
|
||||
log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
|
||||
this.whites = whites;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package com.bw.filters;
|
||||
|
||||
import com.bw.common.utils.JwtConstants;
|
||||
import com.bw.common.utils.JwtUtils;
|
||||
import com.bw.common.utils.StringUtils;
|
||||
import com.bw.config.IgnoreWhiteConfig;
|
||||
import com.bw.utils.GatewayUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
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 javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Admin
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class AuthFilter implements GlobalFilter, Ordered {
|
||||
@Autowired
|
||||
private IgnoreWhiteConfig ignoreWhiteConfig;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
// 获取请求
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
// 获取访问路径
|
||||
String path = request.getURI().getPath();
|
||||
// 获取白名单
|
||||
List<String> whiteList = ignoreWhiteConfig.getWhites();
|
||||
if (StringUtils.matches(path, whiteList)){
|
||||
System.out.println("白名单路径:{\"+path+\"},放行");
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
// 获取token令牌
|
||||
String token = request.getHeaders().getFirst("token");
|
||||
// 非空判断
|
||||
if (StringUtils.isEmpty(token)){
|
||||
System.out.println("token为空:{"+path+"},拦截");
|
||||
return GatewayUtils.errorResponse(exchange, "token不能为空");
|
||||
}
|
||||
// 合法性校验
|
||||
String userKey = "";
|
||||
try {
|
||||
userKey = JwtUtils.getUserKey(token);
|
||||
}catch (Exception e){
|
||||
System.out.println("token不合法:{"+path+"},拦截");
|
||||
return GatewayUtils.errorResponse(exchange, "token不合法");
|
||||
}
|
||||
// 用户登录判断
|
||||
if (Boolean.FALSE.equals(redisTemplate.hasKey(JwtConstants.LOGIN_TOKEN + userKey))){
|
||||
System.out.println("用户未登录:{"+path+"},拦截");
|
||||
return GatewayUtils.errorResponse(exchange, "用户未登录");
|
||||
}
|
||||
System.out.println("token校验通过:{"+path+"},放行");
|
||||
redisTemplate.expire(JwtConstants.LOGIN_TOKEN + userKey,30, TimeUnit.MINUTES);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return -10;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package com.bw.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.common.utils.StringUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 网关处理工具类
|
||||
*/
|
||||
@Log4j2
|
||||
public class GatewayUtils {
|
||||
/**
|
||||
* 添加请求头参数
|
||||
* @param mutate 修改对象
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) {
|
||||
if (StringUtils.isEmpty(key)){
|
||||
log.warn("添加请求头参数键不可以为空");
|
||||
return;
|
||||
}
|
||||
if (value == null) {
|
||||
log.warn("添加请求头参数:[{}]值为空",key);
|
||||
return;
|
||||
}
|
||||
String valueStr = value.toString();
|
||||
mutate.header(key, valueStr);
|
||||
log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除请求头参数
|
||||
* @param mutate 修改对象
|
||||
* @param key 键
|
||||
*/
|
||||
public static void removeHeader(ServerHttpRequest.Builder mutate, String key) {
|
||||
if (StringUtils.isEmpty(key)){
|
||||
log.warn("删除请求头参数键不可以为空");
|
||||
return;
|
||||
}
|
||||
mutate.headers(httpHeaders -> httpHeaders.remove(key)).build();
|
||||
log.info("删除请求头参数 - 键:[{}]",key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误结果响应
|
||||
* @param exchange 响应上下文
|
||||
* @param msg 响应消息
|
||||
* @return
|
||||
*/
|
||||
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg, HttpStatus httpStatus) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
//设置HTTP响应头状态
|
||||
response.setStatusCode(httpStatus);
|
||||
//设置HTTP响应头文本格式
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
//定义响应内容
|
||||
Result<?> result = Result.error(msg);
|
||||
String resultJson = JSONObject.toJSONString(result);
|
||||
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||
//进行响应
|
||||
return response.writeWith(Mono.just(dataBuffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误结果响应
|
||||
* @param exchange 响应上下文
|
||||
* @param msg 响应消息
|
||||
* @return
|
||||
*/
|
||||
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
//设置HTTP响应头状态
|
||||
response.setStatusCode(HttpStatus.OK);
|
||||
//设置HTTP响应头文本格式
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
//定义响应内容
|
||||
Result<?> result = Result.error(msg);
|
||||
String resultJson = JSONObject.toJSONString(result);
|
||||
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||
//进行响应
|
||||
return response.writeWith(Mono.just(dataBuffer));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 18080
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: month-gateway
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
main:
|
||||
# 允许使用循环引用
|
||||
allow-circular-references: true
|
||||
# 允许定义相同的bean对象 去覆盖原有的
|
||||
allow-bean-definition-overriding: true
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,20 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>bw-moudles</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>month-order</artifactId>
|
||||
|
||||
<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,67 @@
|
|||
package com.bw;
|
||||
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.service.OrderService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 启动器
|
||||
* @CreatedDate: 2024/8/12 上午10:39
|
||||
* @FilePath: com.bw
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class OrderApplication {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
SpringApplication.run(OrderApplication.class, args);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("Order 模块启动成功!");
|
||||
}
|
||||
|
||||
@Resource
|
||||
private OrderService orderService;
|
||||
|
||||
private static int n = 0;
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
// 查询ES所有数据并删除
|
||||
List<Type> typeListEs = orderService.selTypeAll(null);
|
||||
if (typeListEs == null || typeListEs.size()<=0) {
|
||||
log.info("ES商铺表初始化:ES 数据为空,无需清除");
|
||||
}else{
|
||||
// 删除ES所有数据
|
||||
typeListEs.forEach(type -> {
|
||||
Boolean b = orderService.deleteType(type.getTid());
|
||||
n = b ? n+1 : n;
|
||||
});
|
||||
log.info("ES商铺表初始化:需要删除 [{}] 条,成功删除 [{}] 条",typeListEs.size(),n);
|
||||
n = 0;
|
||||
}
|
||||
|
||||
// 查询数据库所有售卖数据并插入ES
|
||||
List<Type> typeListMysql = orderService.selTypeAllMysql();
|
||||
// 插入ES
|
||||
typeListMysql.forEach(type -> {
|
||||
Boolean b = orderService.insType(type);
|
||||
n = b ? n+1 : n;
|
||||
});
|
||||
log.info("ES商铺表初始化:需要添加 [{}] 条,成功添加 [{}] 条",typeListMysql.size(),n);
|
||||
n = 0;
|
||||
log.info("ES商铺表初始化:初始化完成");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送确认配置
|
||||
* 消息发送到交换机的回调
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setConfirmCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换机不管是否收到消息的一个回调方法
|
||||
*
|
||||
* @param correlationData 消息相关数据
|
||||
* @param ack 交换机是否收到消息
|
||||
* @param cause 失败原因
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (!ack) {
|
||||
String exchange = correlationData.getReturned().getExchange();
|
||||
String message = correlationData.getReturned().getMessage().getBody().toString();
|
||||
// 发送异常
|
||||
log.error("消息:{},发送到交换机:{}失败,原因是:{}", message, exchange, cause);
|
||||
// TODO 可以把异常信息 以及 消息的内容直接添加到 MYSQL
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @ClassName:
|
||||
* @Description:
|
||||
* @Author: zhuwenqiang
|
||||
* @Date: 2024/4/7
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "es")
|
||||
@Data
|
||||
public class InitEsRes {
|
||||
|
||||
/**InitEsRes
|
||||
* es服务 地址
|
||||
*/
|
||||
@Value("${es.host}")
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口
|
||||
*/
|
||||
@Value("${es.port}")
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* 请求方式
|
||||
*/
|
||||
@Value("${es.scheme}")
|
||||
private String scheme;
|
||||
|
||||
/**
|
||||
* 构建 RestHighLevelClient 用来做 es 操作
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RestHighLevelClient restHighLevelClient() {
|
||||
return new RestHighLevelClient(
|
||||
RestClient.builder(new HttpHost(host, port, scheme))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.ReturnedMessage;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送到队列的确认
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnsCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送失败 则会执行这个方法
|
||||
*
|
||||
* @param returnedMessage the returned message and metadata.
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||
log.error("消息:{},被交换机:{} 回退!退回原因为:{}",
|
||||
returnedMessage.getMessage().toString(), returnedMessage.getExchange(), returnedMessage.getReplyText());
|
||||
// TODO 回退了所有的信息,可做补偿机制
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
package com.bw.controller;
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.service.OrderService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 订单控制层
|
||||
* @CreatedDate: 2024/8/12 上午9:45
|
||||
* @FilePath: com.bw.controller
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/Order")
|
||||
public class OrderController {
|
||||
@Resource
|
||||
private OrderService service;
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* 查询所有售卖类型
|
||||
*/
|
||||
@GetMapping("/selTypeList")
|
||||
Result<List<Type>> selTypeList(@RequestParam String tname){
|
||||
return Result.success(service.selTypeAll(tname));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改库存
|
||||
*/
|
||||
@GetMapping("/updTypeNumber")
|
||||
Result<Boolean> updTypeNumber(
|
||||
@RequestParam("tid") Integer tid,
|
||||
@RequestParam("number") Integer number){
|
||||
return Result.success(service.updTypeNumber(tid, number));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
*/
|
||||
@GetMapping("/selOrderAll")
|
||||
Result<List<OrderResp>> selOrderAll(@RequestParam Integer uid){
|
||||
return Result.success(service.selOrderAll(uid));
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单状态修改
|
||||
*/
|
||||
@GetMapping("/updOrderState")
|
||||
Result<Boolean> updOrderState(
|
||||
@RequestParam("oid") Integer oid,
|
||||
@RequestParam("state") Integer state){
|
||||
return Result.success(service.updOrderState(oid, state));
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单精确查询
|
||||
*/
|
||||
@GetMapping("/findOrderByOid/{oid}")
|
||||
Result<OrderResp> findOrderByOid(@PathVariable Integer oid){
|
||||
return Result.success(service.findOrderByOid(oid));
|
||||
}
|
||||
|
||||
/**
|
||||
* 商铺ID精确查询
|
||||
*/
|
||||
@GetMapping("/findTypeByTid/{tid}")
|
||||
Result<Type> findTypeByTid(@PathVariable Integer tid){
|
||||
return Result.success(service.findTypeByTid(tid));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增订单
|
||||
*/
|
||||
@PostMapping("/insOrder")
|
||||
Result<Boolean> insOrder(@RequestBody Order order){
|
||||
return Result.success(service.insOrder(order));
|
||||
}
|
||||
|
||||
@GetMapping("/selOrderByTimeAndState")
|
||||
public Result<List<Order>> selOrderByTimeAndState(){
|
||||
// 查询所有订单中 购买时间超48小时且状态为2.未确认的订单
|
||||
return Result.success(service.selOrderByTimeAndState());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看所有商家
|
||||
*/
|
||||
@GetMapping("/selTypeTcodeAll")
|
||||
Result<List<String>> selTypeTcodeAll(){
|
||||
return service.selTypeTcodeAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.bw.mapper;
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 订单持久层
|
||||
* @CreatedDate: 2024/8/12 上午9:45
|
||||
* @FilePath: com.bw.mapper
|
||||
*/
|
||||
|
||||
|
||||
@Repository
|
||||
@Mapper
|
||||
public interface OrderMapper {
|
||||
List<Type> selTypeAllMysql();
|
||||
|
||||
Integer updTypeNumber(@Param("tid") Integer tid, @Param("number") Integer number);
|
||||
|
||||
List<OrderResp> selOrderAll(@Param("uid") Integer uid);
|
||||
|
||||
Integer updOrderState(@Param("oid") Integer oid, @Param("state") Integer state);
|
||||
|
||||
OrderResp findOrderByOid(@Param("oid") Integer oid);
|
||||
|
||||
Integer insOrder(Order order);
|
||||
|
||||
List<Order> selOrderByTimeAndState();
|
||||
|
||||
List<Type> selTypeTcodeAll();
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.bw.service;
|
||||
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.result.Result;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: OrderService
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 上午9:45
|
||||
* @FilePath: com.bw.service
|
||||
*/
|
||||
|
||||
|
||||
public interface OrderService {
|
||||
List<Type> selTypeAll(String tname);
|
||||
|
||||
Boolean updTypeNumber(Integer tid, Integer number);
|
||||
|
||||
List<Type> selTypeAllMysql();
|
||||
|
||||
Boolean deleteType(Integer tid);
|
||||
|
||||
Boolean insType(Type saleResp);
|
||||
|
||||
List<OrderResp> selOrderAll(Integer uid);
|
||||
|
||||
Boolean updOrderState(Integer oid, Integer state);
|
||||
|
||||
OrderResp findOrderByOid(Integer oid);
|
||||
|
||||
Type findTypeByTid(Integer tid);
|
||||
|
||||
Boolean insOrder(Order order);
|
||||
|
||||
List<Order> selOrderByTimeAndState();
|
||||
|
||||
Result<List<String>> selTypeTcodeAll();
|
||||
}
|
|
@ -0,0 +1,212 @@
|
|||
package com.bw.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bw.common.domain.pojo.Order;
|
||||
import com.bw.common.domain.pojo.Type;
|
||||
import com.bw.common.domain.response.OrderResp;
|
||||
import com.bw.common.exception.CommonException;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.common.utils.StringUtils;
|
||||
import com.bw.mapper.OrderMapper;
|
||||
import com.bw.service.OrderService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.common.text.Text;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
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.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 订单实现层
|
||||
* @CreatedDate: 2024/8/12 上午9:45
|
||||
* @FilePath: com.bw.service.impl
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
@Resource
|
||||
private OrderMapper mapper;
|
||||
@Resource
|
||||
private RestHighLevelClient restHighLevelClient;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
private final static String INDEX_NAME = "month_type";
|
||||
|
||||
@Override
|
||||
public List<Type> selTypeAll(String tname) {
|
||||
// 获取复合查询请求
|
||||
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
|
||||
// 获取复合条件编辑器
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
// 获取基础条件编辑器
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
// 添加基础条件
|
||||
if (StringUtils.isNotEmpty(tname)){
|
||||
// 关键字查询
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery(
|
||||
"tname", tname));
|
||||
}
|
||||
searchSourceBuilder.query(boolQueryBuilder);
|
||||
// 添加高亮
|
||||
searchSourceBuilder.highlighter(new HighlightBuilder()
|
||||
.field("tname")
|
||||
.preTags("<span style='color: red'>")
|
||||
.postTags("</span>"));
|
||||
searchRequest.source(searchSourceBuilder);
|
||||
try {
|
||||
// 执行请求
|
||||
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||
ArrayList<Type> typeList = new ArrayList<>();
|
||||
SearchHits hits = search.getHits();
|
||||
for (SearchHit hit : hits.getHits()) {
|
||||
String typeString = hit.getSourceAsString();
|
||||
// 反序列化
|
||||
Type type = JSONObject.parseObject(typeString, Type.class);
|
||||
// 高亮处理
|
||||
HighlightField tnameHigh = hit.getHighlightFields().get("tname");
|
||||
if (tnameHigh != null) {
|
||||
String name = "";
|
||||
for (Text fragment : tnameHigh.getFragments()) {
|
||||
name += fragment;
|
||||
}
|
||||
type.setTname(name);
|
||||
}
|
||||
typeList.add(type);
|
||||
}
|
||||
return typeList;
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("查询失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Boolean updTypeNumber(Integer tid, Integer number) {
|
||||
Integer n = mapper.updTypeNumber(tid, number);
|
||||
if (n<=0){
|
||||
throw new CommonException("更新失败");
|
||||
}
|
||||
UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, String.valueOf(tid));
|
||||
try {
|
||||
restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("更新失败", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Type> selTypeAllMysql() {
|
||||
return mapper.selTypeAllMysql();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteType(Integer tid) {
|
||||
DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, String.valueOf(tid));
|
||||
try {
|
||||
restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("删除失败", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean insType(Type type) {
|
||||
IndexRequest indexRequest = new IndexRequest(INDEX_NAME)
|
||||
.id(String.valueOf(type.getTid()))
|
||||
.source(JSONObject.toJSONString(type), XContentType.JSON);
|
||||
try {
|
||||
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("删除失败", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrderResp> selOrderAll(Integer uid) {
|
||||
return mapper.selOrderAll(uid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updOrderState(Integer oid, Integer state) {
|
||||
Integer i = mapper.updOrderState(oid, state);
|
||||
if (i<=0){
|
||||
throw new CommonException("更新失败");
|
||||
}else if (state == 3){
|
||||
OrderResp order = findOrderByOid(oid);
|
||||
// 删除redis信息
|
||||
redisTemplate.delete("Sale:sid="+order.getSid());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrderResp findOrderByOid(Integer oid) {
|
||||
return mapper.findOrderByOid(oid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type findTypeByTid(Integer tid) {
|
||||
GetRequest getRequest = new GetRequest(INDEX_NAME, String.valueOf(tid));
|
||||
try {
|
||||
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
|
||||
String sourceAsString = getResponse.getSourceAsString();
|
||||
return JSONObject.parseObject(sourceAsString, Type.class);
|
||||
}catch (Exception e){
|
||||
throw new CommonException("查询失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean insOrder(Order order) {
|
||||
Integer n = mapper.insOrder(order);
|
||||
if (n<=0){
|
||||
throw new CommonException("插入失败");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> selOrderByTimeAndState() {
|
||||
return mapper.selOrderByTimeAndState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<String>> selTypeTcodeAll() {
|
||||
return Result.success(mapper.selTypeTcodeAll()
|
||||
.stream()
|
||||
.map(Type::getTcode)
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
# 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: month-order
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,53 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bw.mapper.OrderMapper">
|
||||
<insert id="insOrder">
|
||||
INSERT INTO `high-six-exam`.`month_order`
|
||||
(`tid`, `uid`, `sid`, `ocode`, `oprice`, `odate`, `oinsurance`, `ostate` )
|
||||
VALUES
|
||||
(#{tid}, #{uid}, #{sid}, #{ocode}, #{oprice}, #{odate}, #{oinsurance}, 1 );
|
||||
</insert>
|
||||
<update id="updTypeNumber">
|
||||
update month_type
|
||||
set tnumber = tnumber + #{number}
|
||||
where tid = #{tid};
|
||||
</update>
|
||||
<update id="updOrderState">
|
||||
update month_order
|
||||
set ostate = #{state}
|
||||
where oid = #{oid};
|
||||
</update>
|
||||
|
||||
<select id="selTypeAllMysql" resultType="com.bw.common.domain.pojo.Type">
|
||||
select *
|
||||
from month_type
|
||||
</select>
|
||||
<select id="selOrderAll" resultType="com.bw.common.domain.response.OrderResp">
|
||||
SELECT o.*,t.tname,t.tcode,u.uname,s.sprice FROM
|
||||
month_order o
|
||||
LEFT JOIN month_type t ON o.tid = t.tid
|
||||
LEFT JOIN month_user u ON o.uid = u.uid
|
||||
LEFT JOIN month_sale s ON o.sid = s.sid
|
||||
where u.uid = #{uid}
|
||||
</select>
|
||||
<select id="findOrderByOid" resultType="com.bw.common.domain.response.OrderResp">
|
||||
SELECT o.*,t.tname,t.tcode,u.uname,s.sprice FROM
|
||||
month_order o
|
||||
LEFT JOIN month_type t ON o.tid = t.tid
|
||||
LEFT JOIN month_user u ON o.uid = u.uid
|
||||
LEFT JOIN month_sale s ON o.sid = s.sid
|
||||
where oid = #{oid}
|
||||
</select>
|
||||
<select id="selOrderByTimeAndState" resultType="com.bw.common.domain.pojo.Order">
|
||||
select *
|
||||
from month_order
|
||||
where TIMESTAMPDIFF(hour,odate,NOW()) > 48 and ostate = 2;
|
||||
</select>
|
||||
<select id="selTypeTcodeAll" resultType="com.bw.common.domain.pojo.Type">
|
||||
SELECT * FROM
|
||||
month_type
|
||||
GROUP BY tcode
|
||||
</select>
|
||||
</mapper>
|
|
@ -0,0 +1,20 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>bw-moudles</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>month-sale</artifactId>
|
||||
|
||||
<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,64 @@
|
|||
package com.bw;
|
||||
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.service.SaleService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Order
|
||||
* @Description: 启动器
|
||||
* @CreatedDate: 2024/8/12 上午10:39
|
||||
* @FilePath: com.bw
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class SaleApplication { public static void main(String[] args) {
|
||||
try {
|
||||
SpringApplication.run(SaleApplication.class, args);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("Sale 模块启动成功!");
|
||||
}
|
||||
@Resource
|
||||
private SaleService saleService;
|
||||
|
||||
private static int n = 0;
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
// 查询ES所有数据并删除
|
||||
PageResult<SaleResp> pageResult = saleService.selSaleList(new SaleReq());
|
||||
if (pageResult == null || pageResult.getList().isEmpty()){
|
||||
log.info("ES售卖卷表初始化:ES 数据为空,无需清除");
|
||||
}else{
|
||||
// 删除ES所有数据
|
||||
pageResult.getList().forEach(saleResp -> {
|
||||
Boolean b = saleService.deleteSale(saleResp.getSid());
|
||||
n = b ? n+1 : n;
|
||||
});
|
||||
log.info("ES售卖卷表初始化:需要删除 [{}] 条,成功删除 [{}] 条",pageResult.getTotal(),n);
|
||||
n = 0;
|
||||
}
|
||||
|
||||
// 查询数据库所有售卖数据并插入ES
|
||||
List<SaleResp> saleRespList = saleService.selSaleListByStateMysql("1");
|
||||
// 插入ES
|
||||
saleRespList.forEach(saleResp -> {
|
||||
Boolean b = saleService.insSale(saleResp);
|
||||
n = b ? n+1 : n;
|
||||
});
|
||||
log.info("ES售卖卷表初始化:需要添加 [{}] 条,成功添加 [{}] 条",saleRespList.size(),n);
|
||||
n = 0;
|
||||
log.info("ES售卖卷表初始化:初始化完成");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送确认配置
|
||||
* 消息发送到交换机的回调
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setConfirmCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换机不管是否收到消息的一个回调方法
|
||||
*
|
||||
* @param correlationData 消息相关数据
|
||||
* @param ack 交换机是否收到消息
|
||||
* @param cause 失败原因
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (!ack) {
|
||||
String exchange = correlationData.getReturned().getExchange();
|
||||
String message = correlationData.getReturned().getMessage().getBody().toString();
|
||||
// 发送异常
|
||||
log.error("消息:{},发送到交换机:{}失败,原因是:{}", message, exchange, cause);
|
||||
// TODO 可以把异常信息 以及 消息的内容直接添加到 MYSQL
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @ClassName:
|
||||
* @Description:
|
||||
* @Author: zhuwenqiang
|
||||
* @Date: 2024/4/7
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "es")
|
||||
@Data
|
||||
public class InitEsRes {
|
||||
|
||||
/**InitEsRes
|
||||
* es服务 地址
|
||||
*/
|
||||
@Value("${es.host}")
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口
|
||||
*/
|
||||
@Value("${es.port}")
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* 请求方式
|
||||
*/
|
||||
@Value("${es.scheme}")
|
||||
private String scheme;
|
||||
|
||||
/**
|
||||
* 构建 RestHighLevelClient 用来做 es 操作
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RestHighLevelClient restHighLevelClient() {
|
||||
return new RestHighLevelClient(
|
||||
RestClient.builder(new HttpHost(host, port, scheme))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.bw.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.ReturnedMessage;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送到队列的确认
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnsCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送失败 则会执行这个方法
|
||||
*
|
||||
* @param returnedMessage the returned message and metadata.
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||
log.error("消息:{},被交换机:{} 回退!退回原因为:{}",
|
||||
returnedMessage.getMessage().toString(), returnedMessage.getExchange(), returnedMessage.getReplyText());
|
||||
// TODO 回退了所有的信息,可做补偿机制
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package com.bw.controller;
|
||||
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.service.SaleService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description: 寄售表控制层
|
||||
* @CreatedDate: 2024/8/12 上午9:47
|
||||
* @FilePath: com.bw.controller
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/Sale")
|
||||
public class SaleController {
|
||||
@Resource
|
||||
private SaleService service;
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* ES表单查询
|
||||
*/
|
||||
@PostMapping("/selSaleList")
|
||||
public Result<PageResult<SaleResp>> selSaleList(@RequestBody SaleReq saleReq) {
|
||||
return Result.success(service.selSaleList(saleReq));
|
||||
}
|
||||
|
||||
/**
|
||||
* 销售ID精确查询ES
|
||||
*/
|
||||
@PostMapping("/{sid}")
|
||||
public Result<SaleResp> findSaleBySid(@PathVariable String sid) {
|
||||
return Result.success(service.findSaleBySid(sid));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改售卖信息状态
|
||||
*/
|
||||
@GetMapping("/updSaleState")
|
||||
public Result<Boolean> updSaleState(
|
||||
@RequestParam("sid") Integer sid,
|
||||
@RequestParam("state") Integer state) {
|
||||
return Result.success(service.updSaleStateMysql(sid, state));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增售卖信息
|
||||
*/
|
||||
@PostMapping("/insSale")
|
||||
public Result<Boolean> insSale(@RequestBody Sale sale) {
|
||||
return Result.success(service.insSaleMysql(sale));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.bw.mapper;
|
||||
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description: 寄售表持久层
|
||||
* @CreatedDate: 2024/8/12 上午9:46
|
||||
* @FilePath: com.bw.mapper
|
||||
*/
|
||||
|
||||
|
||||
@Repository
|
||||
@Mapper
|
||||
public interface SaleMapper {
|
||||
|
||||
List<SaleResp> selSaleListByStateMysql(@Param("state") String state);
|
||||
|
||||
SaleResp findSaleBySid(@Param("sid") String sid);
|
||||
|
||||
Integer updSaleState(@Param("sid") Integer sid, @Param("state") Integer state);
|
||||
|
||||
Integer insSaleMysql(Sale sale);
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package com.bw.rabbit;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: ESRabbit
|
||||
* @Description: ES数据同步
|
||||
* @CreatedDate: 2024/8/12 上午11:42
|
||||
* @FilePath: com.bw.rabbit
|
||||
*/
|
||||
|
||||
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.service.SaleService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: ESRabbit
|
||||
* @Description: ES数据同步
|
||||
* @CreatedDate: 2024/8/12 上午11:42
|
||||
* @FilePath: com.bw.rabbit
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ESRabbit {
|
||||
@Resource
|
||||
private SaleService service;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
/**
|
||||
* 更新数据状态
|
||||
* 每次数据发生变化时都发送对应的id
|
||||
* 查询id 更新数据
|
||||
*/
|
||||
@RabbitListener(queuesToDeclare = @Queue("month_updSale"))
|
||||
public void updateSale(String sid, Message message, Channel channel) {
|
||||
// 重复消费判断
|
||||
String messageId = message.getMessageProperties().getMessageId();
|
||||
try {
|
||||
Long add = redisTemplate.opsForSet().add("month_updSale_message", messageId);
|
||||
if (add > 0){
|
||||
System.out.println("正常消费信息:["+sid+"]");
|
||||
// 查询mysql数据
|
||||
SaleResp saleMysql = service.findSaleBySidMysql(sid);
|
||||
// 查询ES的数据
|
||||
SaleResp saleEs = service.findSaleBySid(sid);
|
||||
if (saleEs==null){
|
||||
// 添加
|
||||
Boolean b = service.insSale(saleMysql);
|
||||
}else{
|
||||
// 修改
|
||||
Boolean b = service.updSale(saleMysql);
|
||||
}
|
||||
}else{
|
||||
System.out.println("重复消费信息:["+sid+"]");
|
||||
}
|
||||
// 确认消费
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
|
||||
System.out.println("消费成功,消费信息:["+sid+"]");
|
||||
} catch (IOException e) {
|
||||
System.out.println("消费失败,消费信息:["+sid+"]");
|
||||
try {
|
||||
redisTemplate.opsForSet().remove("month_updSale_message", messageId);
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.bw.service;
|
||||
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.result.PageResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description: 寄售表业务层
|
||||
* @CreatedDate: 2024/8/12 上午9:46
|
||||
* @FilePath: com.bw.service
|
||||
*/
|
||||
|
||||
public interface SaleService {
|
||||
PageResult<SaleResp> selSaleList(SaleReq saleReq);
|
||||
|
||||
Boolean deleteSale(Integer sid);
|
||||
|
||||
List<SaleResp> selSaleListByStateMysql(String states);
|
||||
|
||||
Boolean insSale(SaleResp saleResp);
|
||||
|
||||
SaleResp findSaleBySid(String sid);
|
||||
|
||||
Boolean updSaleStateMysql(Integer sid, Integer state);
|
||||
|
||||
SaleResp findSaleBySidMysql(String sid);
|
||||
|
||||
Boolean updSale(SaleResp saleMysql);
|
||||
|
||||
Boolean insSaleMysql(Sale sale);
|
||||
}
|
|
@ -0,0 +1,222 @@
|
|||
package com.bw.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bw.common.domain.pojo.Sale;
|
||||
import com.bw.common.domain.request.SaleReq;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.common.exception.CommonException;
|
||||
import com.bw.common.result.PageResult;
|
||||
import com.bw.common.utils.StringUtils;
|
||||
import com.bw.mapper.SaleMapper;
|
||||
import com.bw.service.SaleService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.common.text.Text;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
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.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: Sale
|
||||
* @Description: 寄售表实现层
|
||||
* @CreatedDate: 2024/8/12 上午9:46
|
||||
* @FilePath: com.bw.service.impl
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SaleServiceImpl implements SaleService {
|
||||
@Resource
|
||||
private SaleMapper mapper;
|
||||
@Resource
|
||||
private RestHighLevelClient restHighLevelClient;
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
private final static String INDEX_NAME = "month_sale";
|
||||
|
||||
@Override
|
||||
public PageResult<SaleResp> selSaleList(SaleReq saleReq) {
|
||||
// 获取复合查询请求
|
||||
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
|
||||
// 获取复合条件编辑器
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
// 获取基础条件编辑器
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
// 添加基础条件
|
||||
if (StringUtils.isNotEmpty(saleReq.getKeyword())){
|
||||
// 关键字查询
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery(
|
||||
"tname", saleReq.getKeyword()));
|
||||
}
|
||||
if (StringUtils.isNotEmpty(saleReq.getTcode())){
|
||||
// 添加分类条件
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery(
|
||||
"tcode", saleReq.getTcode()));
|
||||
}
|
||||
if (saleReq.getSstate()!=null && saleReq.getSstate()!=0){
|
||||
// 添加状态条件
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery(
|
||||
"sstate", saleReq.getSstate()));
|
||||
}
|
||||
if (saleReq.getUid()!=null){
|
||||
// 添加状态条件
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery(
|
||||
"uid", saleReq.getUid()));
|
||||
}
|
||||
searchSourceBuilder.query(boolQueryBuilder);
|
||||
// 添加高亮
|
||||
searchSourceBuilder.highlighter(new HighlightBuilder()
|
||||
.field("tname")
|
||||
.preTags("<span style='color: red'>")
|
||||
.postTags("</span>"));
|
||||
searchRequest.source(searchSourceBuilder);
|
||||
try {
|
||||
// 执行请求
|
||||
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||
ArrayList<SaleResp> saleRespList = new ArrayList<>();
|
||||
long total = 0;
|
||||
SearchHits hits = search.getHits();
|
||||
total = hits.getTotalHits().value;
|
||||
for (SearchHit hit : hits.getHits()) {
|
||||
String saleString = hit.getSourceAsString();
|
||||
// 反序列化
|
||||
SaleResp saleResp = JSONObject.parseObject(saleString, SaleResp.class);
|
||||
// 高亮处理
|
||||
HighlightField tnameHigh = hit.getHighlightFields().get("tname");
|
||||
if (tnameHigh != null) {
|
||||
String tname = "";
|
||||
for (Text fragment : tnameHigh.getFragments()) {
|
||||
tname += fragment;
|
||||
}
|
||||
saleResp.setTname(tname);
|
||||
}
|
||||
saleRespList.add(saleResp);
|
||||
}
|
||||
return new PageResult<>(total, saleRespList);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("查询失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteSale(Integer sid) {
|
||||
DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, String.valueOf(sid));
|
||||
try {
|
||||
restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("删除失败",e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SaleResp> selSaleListByStateMysql(String states) {
|
||||
return mapper.selSaleListByStateMysql(states);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean insSale(SaleResp saleResp) {
|
||||
IndexRequest indexRequest = new IndexRequest(INDEX_NAME)
|
||||
.id(String.valueOf(saleResp.getSid()))
|
||||
.source(JSONObject.toJSONString(saleResp), XContentType.JSON);
|
||||
try {
|
||||
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("添加失败",e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaleResp findSaleBySid(String sid) {
|
||||
GetRequest getRequest = new GetRequest(INDEX_NAME, sid);
|
||||
try {
|
||||
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
|
||||
String saleString = documentFields.getSourceAsString();
|
||||
return JSONObject.parseObject(saleString, SaleResp.class);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("查询失败",e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updSaleStateMysql(Integer sid, Integer state) {
|
||||
Integer n = mapper.updSaleState(sid, state);
|
||||
if (n > 0){
|
||||
// 使用mq异步更新ES
|
||||
rabbitTemplate.convertAndSend("month_updSale",sid,message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaleResp findSaleBySidMysql(String sid) {
|
||||
return mapper.findSaleBySid(sid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updSale(SaleResp saleMysql) {
|
||||
UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, String.valueOf(saleMysql.getSid()));
|
||||
try {
|
||||
restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
throw new CommonException("更新失败",e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增售卖卷
|
||||
* @param sale 售卖卷信息
|
||||
* @return 返回是否成功
|
||||
*/
|
||||
|
||||
@Override
|
||||
public Boolean insSaleMysql(Sale sale) {
|
||||
Integer n = mapper.insSaleMysql(sale);
|
||||
if (n > 0){
|
||||
// 存储redis
|
||||
redisTemplate.opsForValue().set("Sale:sid="+sale.getSid(), JSONObject.toJSONString(sale));
|
||||
// 使用mq异步更新ES
|
||||
rabbitTemplate.convertAndSend("month_updSale",sale.getSid(),message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
# 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: month-sale
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,33 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bw.mapper.SaleMapper">
|
||||
<insert id="insSaleMysql" useGeneratedKeys="true" keyProperty="sid">
|
||||
INSERT INTO `high-six-exam`.`month_sale`
|
||||
(`tid`, `uid`, `scode`, `sprice`, `sdate`, `sstate` )
|
||||
VALUES
|
||||
(#{tid}, #{uid}, #{scode}, #{sprice}, #{sdate}, 1 );
|
||||
</insert>
|
||||
<update id="updSaleState">
|
||||
update month_sale
|
||||
set sstate = #{state}
|
||||
where sid = #{sid};
|
||||
</update>
|
||||
|
||||
<select id="selSaleListByStateMysql" resultType="com.bw.common.domain.response.SaleResp">
|
||||
SELECT s.*,t.tname,t.tcode,u.uname FROM
|
||||
month_sale s
|
||||
LEFT JOIN month_type t ON s.tid = t.tid
|
||||
LEFT JOIN month_user u ON s.uid = u.uid
|
||||
WHERE s.sstate in (#{state})
|
||||
</select>
|
||||
|
||||
<select id="findSaleBySid" resultType="com.bw.common.domain.response.SaleResp">
|
||||
SELECT s.*,t.tname,t.tcode,u.uname FROM
|
||||
month_sale s
|
||||
LEFT JOIN month_type t ON s.tid = t.tid
|
||||
LEFT JOIN month_user u ON s.uid = u.uid
|
||||
WHERE sid = #{sid}
|
||||
</select>
|
||||
</mapper>
|
|
@ -0,0 +1,20 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>bw-moudles</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>month-user</artifactId>
|
||||
|
||||
<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,24 @@
|
|||
package com.bw;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description:
|
||||
* @CreatedDate: 2024/8/12 上午10:38
|
||||
* @FilePath: com.bw
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
public class UserApplication {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
SpringApplication.run(UserApplication.class, args);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("User 模块启动成功!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.bw.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.result.Result;
|
||||
import com.bw.common.utils.MsgUtil;
|
||||
import com.bw.service.UserService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description: 用户类控制层
|
||||
* @CreatedDate: 2024/8/12 上午9:43
|
||||
* @FilePath: com.bw.controller
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/User")
|
||||
public class UserController {
|
||||
@Resource
|
||||
private UserService service;
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*/
|
||||
@GetMapping("/sendCode")
|
||||
public Result<Boolean> sendCode(@RequestParam String phone){
|
||||
return Result.success(service.sendCode(phone));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*/
|
||||
@PostMapping("/insUser")
|
||||
public Result<Boolean> insUser(@RequestBody UserReq userReq){
|
||||
return Result.success(service.insUser(userReq));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result<JwtResponse> login(@RequestBody UserReq userReq){
|
||||
return Result.success(service.login(userReq));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户余额调整
|
||||
*/
|
||||
@GetMapping("/updUserMoney")
|
||||
public Result<Boolean> updUserMoney(
|
||||
@RequestParam("uid") Integer uid,
|
||||
@RequestParam("money") Double money){
|
||||
return Result.success(service.updUserMoney(uid,money));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户ID精确查询
|
||||
*/
|
||||
@GetMapping("/{uid}")
|
||||
public Result<User> findUserById(@PathVariable("uid") Integer uid){
|
||||
return Result.success(service.findUserById(uid));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.bw.mapper;
|
||||
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description: 用户类持久层
|
||||
* @CreatedDate: 2024/8/12 上午9:42
|
||||
* @FilePath: com.bw.mapper
|
||||
*/
|
||||
|
||||
|
||||
@Repository
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
User findUserByUnameAndUphone(@Param("uname") String uname, @Param("uphone") String uphone);
|
||||
|
||||
Integer insUser(UserReq userReq);
|
||||
|
||||
Integer updUserMoney(@Param("uid") Integer uid, @Param("money") Double money);
|
||||
|
||||
User findUserById(@Param("uid") Integer uid);
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.bw.rabbit;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: UpdMoney
|
||||
* @Description: 余额更新
|
||||
* @CreatedDate: 2024/8/12 下午3:37
|
||||
* @FilePath: com.bw.rabbit
|
||||
*/
|
||||
|
||||
|
||||
import com.bw.common.domain.request.Money;
|
||||
import com.bw.common.domain.response.SaleResp;
|
||||
import com.bw.service.UserService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: UpdMoney
|
||||
* @Description: 余额更新
|
||||
* @CreatedDate: 2024/8/12 下午3:37
|
||||
* @FilePath: com.bw.rabbit
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UpdMoney {
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
|
||||
@RabbitListener(queuesToDeclare = @Queue("upd_money"))
|
||||
public void updMoney(Money money, Message message, Channel channel) {
|
||||
log.info("更新余额:{}", money);
|
||||
// 重复消费判断
|
||||
String messageId = message.getMessageProperties().getMessageId();
|
||||
Long add = redisTemplate.opsForSet().add("month_money_message", messageId);
|
||||
if (add>0){
|
||||
log.info("正常消费信息:[{}]",money);
|
||||
Boolean b = userService.updUserMoney(money.getUid(), money.getMoney());
|
||||
}else{
|
||||
log.info("重复消费信息:[{}]",money);
|
||||
}
|
||||
try {
|
||||
// 确认消费
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
|
||||
log.info("消费成功,消费信息:[{}] ",money);
|
||||
} catch (IOException e) {
|
||||
log.error("消费失败,消费信息:[{}] ",money);
|
||||
try {
|
||||
redisTemplate.opsForSet().remove("month_money_message", messageId);
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.bw.service;
|
||||
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description: 用户类业务层
|
||||
* @CreatedDate: 2024/8/12 上午9:42
|
||||
* @FilePath: com.bw.service.impl
|
||||
*/
|
||||
|
||||
public interface UserService {
|
||||
Boolean insUser(UserReq userReq);
|
||||
|
||||
JwtResponse login(UserReq userReq);
|
||||
|
||||
Boolean sendCode(String phone);
|
||||
|
||||
Boolean updUserMoney(Integer uid, Double money);
|
||||
|
||||
User findUserById(Integer uid);
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package com.bw.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bw.common.domain.pojo.User;
|
||||
import com.bw.common.domain.request.UserReq;
|
||||
import com.bw.common.domain.response.JwtResponse;
|
||||
import com.bw.common.exception.CommonException;
|
||||
import com.bw.common.utils.JwtConstants;
|
||||
import com.bw.common.utils.JwtUtils;
|
||||
import com.bw.common.utils.MsgUtil;
|
||||
import com.bw.mapper.UserMapper;
|
||||
import com.bw.service.UserService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: User
|
||||
* @Description: 用户类实现层
|
||||
* @CreatedDate: 2024/8/12 上午9:43
|
||||
* @FilePath: com.bw.service.impl
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Resource
|
||||
private UserMapper mapper;
|
||||
@Resource
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
* @param phone 手机号
|
||||
* @return 发送状态
|
||||
*/
|
||||
@Override
|
||||
public Boolean sendCode(String phone) {
|
||||
String code = MsgUtil.getCode(6);
|
||||
JSONObject jsonObject = MsgUtil.sendMsg(phone, code);
|
||||
if (jsonObject == null || !"0".equals(jsonObject.getString("showapi_res_code"))){
|
||||
throw new CommonException("发送失败");
|
||||
}
|
||||
// 验证码存储5分钟
|
||||
redisTemplate.opsForValue().set("code:phone="+phone,code,5, TimeUnit.MINUTES);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户余额
|
||||
* @param uid 用户id
|
||||
* @param money 更新金额
|
||||
* @return 更新状态
|
||||
*/
|
||||
@Override
|
||||
public Boolean updUserMoney(Integer uid, Double money) {
|
||||
if (mapper.updUserMoney(uid,money) <= 0){
|
||||
throw new CommonException("失败");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findUserById(Integer uid) {
|
||||
return mapper.findUserById(uid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注册用户
|
||||
* @param userReq 注册信息
|
||||
* @return 注册状态
|
||||
*/
|
||||
@Override
|
||||
public Boolean insUser(UserReq userReq) {
|
||||
// 验证非重复
|
||||
User user = mapper.findUserByUnameAndUphone(userReq.getUname(), userReq.getUphone());
|
||||
if (null != user) {
|
||||
throw new CommonException("该用户已存在");
|
||||
}
|
||||
if (mapper.insUser(userReq) <= 0){
|
||||
throw new CommonException("注册失败");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录用户
|
||||
* @param userReq 登录信息
|
||||
* @return token令牌
|
||||
*/
|
||||
|
||||
@Override
|
||||
public JwtResponse login(UserReq userReq) {
|
||||
// 获取用户信息
|
||||
User user = mapper.findUserByUnameAndUphone(
|
||||
userReq.getUname(),
|
||||
userReq.getUphone());
|
||||
if (null == user) {
|
||||
if (mapper.insUser(userReq) <= 0){
|
||||
throw new CommonException("注册失败");
|
||||
}
|
||||
}
|
||||
// 获取验证码
|
||||
String code = redisTemplate.opsForValue().get("code:phone=" + userReq.getUphone());
|
||||
if (code==null){
|
||||
// 验证码过期
|
||||
throw new CommonException("验证码已过期");
|
||||
}else if (!code.equals(userReq.getCode())){
|
||||
// 验证码错误
|
||||
throw new CommonException("验证码错误");
|
||||
}
|
||||
// 生成token
|
||||
String userKey = UUID.randomUUID().toString();
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put(JwtConstants.USER_KEY,userKey);
|
||||
String token = JwtUtils.createToken(map);
|
||||
|
||||
// 存储登录用户信息 存储30分钟
|
||||
redisTemplate.opsForValue().set(
|
||||
JwtConstants.LOGIN_TOKEN+userKey,
|
||||
JSONObject.toJSONString(user),
|
||||
30,TimeUnit.MINUTES);
|
||||
return new JwtResponse(token,"30MIN");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9001
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: GMT+8
|
||||
application:
|
||||
# 应用名称
|
||||
name: month-user
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.116.173.119:8848
|
||||
namespace: 0b55c4a7-3474-4454-9452-5210cc4320e0
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bw.mapper.UserMapper">
|
||||
<insert id="insUser">
|
||||
INSERT INTO `high-six-exam`.`month_user`
|
||||
(`uname`, `upwd`, `umoney`) VALUES
|
||||
(#{uname}, #{upwd}, 0.00);
|
||||
</insert>
|
||||
<update id="updUserMoney">
|
||||
update month_user
|
||||
set umoney = umoney + #{money}
|
||||
where uid = #{uid};
|
||||
</update>
|
||||
|
||||
<select id="findUserByUnameAndUphone" resultType="com.bw.common.domain.pojo.User">
|
||||
select *
|
||||
from month_user where uname = #{uname} and uphone = #{uphone}
|
||||
</select>
|
||||
<select id="findUserById" resultType="com.bw.common.domain.pojo.User">
|
||||
select *
|
||||
from month_user
|
||||
where uid = #{uid}
|
||||
</select>
|
||||
</mapper>
|
|
@ -0,0 +1,78 @@
|
|||
<?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.bw</groupId>
|
||||
<artifactId>month</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bw-moudles</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>month-user</module>
|
||||
<module>month-order</module>
|
||||
<module>month-sale</module>
|
||||
</modules>
|
||||
|
||||
<!--版本管理-->
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<mybatis.version>2.2.2</mybatis.version>
|
||||
<druid.version>1.2.8</druid.version>
|
||||
<pagehelper.version>1.4.1</pagehelper.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 系统公共 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.bw</groupId>
|
||||
<artifactId>bw-common</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringBoot Web-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- Druid -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>${druid.version}</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>${mybatis.version}</version>
|
||||
</dependency>
|
||||
<!-- Pagehelper -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>${pagehelper.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
</dependency>
|
||||
<!-- 引入 springboot kafka -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,66 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.bw</groupId>
|
||||
<artifactId>month</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>bw-common</module>
|
||||
<module>bw-auth</module>
|
||||
<module>bw-gateway</module>
|
||||
<module>bw-moudles</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>
|
||||
<!-- 规定SpringBoot版本 -->
|
||||
<!-- 父级pom文件 主要用于规定项目依赖的各个版本,用于进行项目版本约束 -->
|
||||
<parent>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<version>2.6.2</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<!-- 依赖声明 -->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- SpringCloud 微服务 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>2021.0.0</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba 微服务 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>2021.1</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<!-- Alibaba Nacos 配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-client</artifactId>
|
||||
<version>2.0.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 系统公共 依赖 版本号定义-->
|
||||
<dependency>
|
||||
<groupId>com.bw</groupId>
|
||||
<artifactId>bw-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
Loading…
Reference in New Issue