commit fd5938cae3361e68a7b153a4c1099a47ca746a7e Author: Administrator Date: Mon Oct 31 23:05:03 2022 +0800 视频024~030(接口模块基本操作) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8450319 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + diff --git a/beacon-api/pom.xml b/beacon-api/pom.xml new file mode 100644 index 0000000..b694304 --- /dev/null +++ b/beacon-api/pom.xml @@ -0,0 +1,58 @@ + + + + beacon-cloud + com.mashibing + 1.0-SNAPSHOT + + 4.0.0 + + beacon-api + + + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + org.springframework.boot + spring-boot-starter-amqp + + + + org.projectlombok + lombok + + + + org.springframework.boot + spring-boot-starter-test + + + + org.springframework.boot + spring-boot-starter-validation + + + + \ No newline at end of file diff --git a/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java b/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java new file mode 100644 index 0000000..709637e --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java @@ -0,0 +1,18 @@ +package com.mashibing.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author zjw + * @description + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ApiStarterApp { + + public static void main(String[] args) { + SpringApplication.run(ApiStarterApp.class,args); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java b/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java new file mode 100644 index 0000000..6b33843 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java @@ -0,0 +1,35 @@ +package com.mashibing.api.controller; + +import com.mashibing.api.enums.SmsCodeEnum; +import com.mashibing.api.form.SingleSendForm; +import com.mashibing.api.util.R; +import com.mashibing.api.vo.ResultVO; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author zjw + * @description + */ +@RestController +@RequestMapping("/sms") +@Slf4j +public class SmsController { + + + @PostMapping(value = "/single_send",produces = "application/json;charset=utf-8") + public ResultVO singleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult){ + //1. 校验参数 + if (bindingResult.hasErrors()){ + String msg = bindingResult.getFieldError().getDefaultMessage(); + log.info("【接口模块-单条短信Controller】 参数不合法 msg = {}",msg); + return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(),msg); + } + return R.ok(); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/controller/TestController.java b/beacon-api/src/main/java/com/mashibing/api/controller/TestController.java new file mode 100644 index 0000000..dee065d --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/controller/TestController.java @@ -0,0 +1,23 @@ +package com.mashibing.api.controller; + +import com.mashibing.api.filter.CheckFilterContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * @author zjw + * @description 测试校验链的效果 + */ +@Controller +public class TestController { + + @Autowired + private CheckFilterContext checkFilterContext; + + @GetMapping("/api/test") + public void test(){ + System.out.println("===================================="); + checkFilterContext.check(new Object()); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/enums/SmsCodeEnum.java b/beacon-api/src/main/java/com/mashibing/api/enums/SmsCodeEnum.java new file mode 100644 index 0000000..2b8eb5e --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/enums/SmsCodeEnum.java @@ -0,0 +1,21 @@ +package com.mashibing.api.enums; + +import lombok.Getter; + +/** + * 一些响应信息中code和msg的对应 + * @author zjw + * @description + */ +@Getter +public enum SmsCodeEnum { + PARAMETER_ERROR(-10,"参数不合法!"); + + private Integer code; + private String msg; + + SmsCodeEnum(Integer code, String msg) { + this.code = code; + this.msg = msg; + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/CheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/CheckFilter.java new file mode 100644 index 0000000..4779baa --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/CheckFilter.java @@ -0,0 +1,17 @@ +package com.mashibing.api.filter; + +import com.mashibing.api.form.SmsForm; + +/** + * + * @author zjw + * @description 做策略模式的父接口 + */ +public interface CheckFilter { + + /** + * 校验!!!! + * @param smsForm + */ + void check(SmsForm smsForm); +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/CheckFilterContext.java b/beacon-api/src/main/java/com/mashibing/api/filter/CheckFilterContext.java new file mode 100644 index 0000000..d4c76ce --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/CheckFilterContext.java @@ -0,0 +1,44 @@ +package com.mashibing.api.filter; + +import com.mashibing.api.form.SingleSendForm; +import com.mashibing.api.form.SmsForm; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @author zjw + * @description 策略模式的上下文对象 + */ +@Component +@RefreshScope +public class CheckFilterContext { + + // Spring的IOC会将对象全部都放到Map集合中 + // 基于4.x中Spring提供的反省注解,基于Map只拿到需要的类型对象即可 + @Autowired + private Map checkFiltersMap; + + // 基于Nacos获取到执行的顺序和需要执行的校验对象 + @Value("${singleSendFilters:apikey,ip,sign,template}") + private String singleSendFilters; + + /** + * 当前check方法用于管理校验链的顺序 + */ + public void check(SmsForm smsForm){ + if (smsForm instanceof SingleSendForm) { + // 单挑短信发送的策略 + //1. 将获取到filters基于,做切分 + String[] filterArray = singleSendFilters.split(","); + //2. 遍历数组即可 + for (String filter : filterArray) { + CheckFilter checkFilter = checkFiltersMap.get(filter); + checkFilter.check(smsForm); + } + } + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/impl/ApiKeyCheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/impl/ApiKeyCheckFilter.java new file mode 100644 index 0000000..b03c4f1 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/impl/ApiKeyCheckFilter.java @@ -0,0 +1,21 @@ +package com.mashibing.api.filter.impl; + +import com.mashibing.api.filter.CheckFilter; +import com.mashibing.api.form.SmsForm; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description 校验客户的apikey是否合法 + */ +@Service(value = "apikey") +@Slf4j +public class ApiKeyCheckFilter implements CheckFilter { + + + @Override + public void check(SmsForm smsForm) { + log.info("【接口模块-校验apikey】 校验ing…………"); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/impl/FeeCheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/impl/FeeCheckFilter.java new file mode 100644 index 0000000..8e4a0a0 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/impl/FeeCheckFilter.java @@ -0,0 +1,21 @@ +package com.mashibing.api.filter.impl; + +import com.mashibing.api.filter.CheckFilter; +import com.mashibing.api.form.SmsForm; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description 校验客户剩余的金额是否充足 + */ +@Service(value = "fee") +@Slf4j +public class FeeCheckFilter implements CheckFilter { + + + @Override + public void check(SmsForm smsForm) { + log.info("【接口模块-校验客户余额】 校验ing…………"); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/impl/IPCheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/impl/IPCheckFilter.java new file mode 100644 index 0000000..130a973 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/impl/IPCheckFilter.java @@ -0,0 +1,21 @@ +package com.mashibing.api.filter.impl; + +import com.mashibing.api.filter.CheckFilter; +import com.mashibing.api.form.SmsForm; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description 校验请求的ip地址是否是白名单 + */ +@Service(value = "ip") +@Slf4j +public class IPCheckFilter implements CheckFilter { + + + @Override + public void check(SmsForm smsForm) { + log.info("【接口模块-校验ip】 校验ing…………"); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/impl/MobileCheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/impl/MobileCheckFilter.java new file mode 100644 index 0000000..6956069 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/impl/MobileCheckFilter.java @@ -0,0 +1,21 @@ +package com.mashibing.api.filter.impl; + +import com.mashibing.api.filter.CheckFilter; +import com.mashibing.api.form.SmsForm; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description 校验手机号的格式合法性 + */ +@Service(value = "mobile") +@Slf4j +public class MobileCheckFilter implements CheckFilter { + + + @Override + public void check(SmsForm smsForm) { + log.info("【接口模块-校验手机号】 校验ing…………"); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/impl/SignCheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/impl/SignCheckFilter.java new file mode 100644 index 0000000..2a1810a --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/impl/SignCheckFilter.java @@ -0,0 +1,21 @@ +package com.mashibing.api.filter.impl; + +import com.mashibing.api.filter.CheckFilter; +import com.mashibing.api.form.SmsForm; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description 校验短信的签名 + */ +@Service(value = "sign") +@Slf4j +public class SignCheckFilter implements CheckFilter { + + + @Override + public void check(SmsForm smsForm) { + log.info("【接口模块-校验签名】 校验ing…………"); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/filter/impl/TemplateCheckFilter.java b/beacon-api/src/main/java/com/mashibing/api/filter/impl/TemplateCheckFilter.java new file mode 100644 index 0000000..518f9ef --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/filter/impl/TemplateCheckFilter.java @@ -0,0 +1,21 @@ +package com.mashibing.api.filter.impl; + +import com.mashibing.api.filter.CheckFilter; +import com.mashibing.api.form.SmsForm; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description 校验短信的模板 + */ +@Service(value = "template") +@Slf4j +public class TemplateCheckFilter implements CheckFilter { + + + @Override + public void check(SmsForm smsForm) { + log.info("【接口模块-校验模板】 校验ing…………"); + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/form/SingleSendForm.java b/beacon-api/src/main/java/com/mashibing/api/form/SingleSendForm.java new file mode 100644 index 0000000..1ca5a9c --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/form/SingleSendForm.java @@ -0,0 +1,26 @@ +package com.mashibing.api.form; + +import lombok.Data; +import org.hibernate.validator.constraints.Range; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @author zjw + * @description + */ +@Data +public class SingleSendForm extends SmsForm { + + + /** 短信内容 */ + @NotBlank(message = "短信内容不能为空!") + private String text; + + /** 0-验证码短信 1-通知类短信 2-营销类短信 */ + @Range(min = 0,max = 2,message = "短信类型只能是0~2的整数!") + @NotNull(message = "短信类型不允许非空!") + private Integer state; + +} diff --git a/beacon-api/src/main/java/com/mashibing/api/form/SmsForm.java b/beacon-api/src/main/java/com/mashibing/api/form/SmsForm.java new file mode 100644 index 0000000..a2f6aa6 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/form/SmsForm.java @@ -0,0 +1,25 @@ +package com.mashibing.api.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * 发送短信必备的参数 + * @author zjw + * @description + */ +@Data +public class SmsForm { + + /** 客户的apikey */ + @NotBlank(message = "apikey不允许为空!") + private String apikey; + + /** 手机号 */ + @NotBlank(message = "手机号不能为空!") + private String mobile; + + /** 客户业务内的uid */ + private String uid; +} diff --git a/beacon-api/src/main/java/com/mashibing/api/util/R.java b/beacon-api/src/main/java/com/mashibing/api/util/R.java new file mode 100644 index 0000000..b5e90a3 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/util/R.java @@ -0,0 +1,24 @@ +package com.mashibing.api.util; + +import com.mashibing.api.vo.ResultVO; + +/** + * @author zjw + * @description + */ +public class R { + + public static ResultVO ok(){ + ResultVO r = new ResultVO(); + r.setCode(0); + r.setMsg("接收成功"); + return r; + } + + public static ResultVO error(Integer code ,String msg) { + ResultVO r = new ResultVO(); + r.setCode(code); + r.setMsg(msg); + return r; + } +} diff --git a/beacon-api/src/main/java/com/mashibing/api/vo/ResultVO.java b/beacon-api/src/main/java/com/mashibing/api/vo/ResultVO.java new file mode 100644 index 0000000..0ed4eb9 --- /dev/null +++ b/beacon-api/src/main/java/com/mashibing/api/vo/ResultVO.java @@ -0,0 +1,23 @@ +package com.mashibing.api.vo; + +import lombok.Data; + +/** + * @author zjw + * @description + */ +@Data +public class ResultVO { + + private Integer code; + + private String msg; + + private Integer count; + + private Long fee; + + private String uid; + + private String sid; +} diff --git a/beacon-api/src/main/resources/bootstrap.yml b/beacon-api/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..8a8837c --- /dev/null +++ b/beacon-api/src/main/resources/bootstrap.yml @@ -0,0 +1,18 @@ +# 服务名称 +spring: + application: + name: beacon-api +# 多环境 + profiles: + active: dev +# nacos注册中心地址 + cloud: + nacos: + discovery: + server-addr: 114.116.226.76:8848 + # nacos配置中心地址: + config: + server-addr: 114.116.226.76:8848 + file-extension: yml + # beacon-api-dev.yml + diff --git a/beacon-api/src/test/java/com/mashibing/api/filter/CheckFilterContextTest.java b/beacon-api/src/test/java/com/mashibing/api/filter/CheckFilterContextTest.java new file mode 100644 index 0000000..a8b95bd --- /dev/null +++ b/beacon-api/src/test/java/com/mashibing/api/filter/CheckFilterContextTest.java @@ -0,0 +1,22 @@ +package com.mashibing.api.filter; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.*; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class CheckFilterContextTest { + + @Autowired + private CheckFilterContext checkFilterContext; + + @org.junit.Test + public void check() { + Object obj = new Object(); + checkFilterContext.check(obj); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..720d28c --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + beacon-api + + + org.springframework.boot + spring-boot-starter-parent + 2.3.12.RELEASE + + + com.mashibing + beacon-cloud + 1.0-SNAPSHOT + pom + + + Hoxton.SR12 + 2.2.6.RELEASE + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud-version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring.cloud.alibaba-version} + pom + import + + + + + \ No newline at end of file