1、pom.xml
    <parent>
        <artifactId>base-service</artifactId>
        <groupId>com.southwind</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
        <!-- 短信接口 -->
        <dependency>
            <groupId>com.jdwx</groupId>
            <artifactId>sms</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- Gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <!-- Common Service -->
        <dependency>
            <groupId>com.southwind</groupId>
            <artifactId>common-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>
2、application.yml
server:
  port: 8083
  
jdwx:
  url: https://way.jd.com/chonry/smsapi
  appkey: yourappkey
  sign: UUShop
  
spring:
  application:
    name: sms-service
    
  redis:
    database: 0
    host: 192.168.248.138
    port: 6379
3、sms-1.0.jar 打包到本地项目
(start启动器)
4、util
RandomUtil --》生成随机数
public class RandomUtil {
    private static final Random random = new Random();
    private static final DecimalFormat fourdf = new DecimalFormat("0000");
    private static final DecimalFormat sixdf = new DecimalFormat("000000");
    //生成四位的随机数字
    public static String getFourBitRandom() {
        return fourdf.format(random.nextInt(10000));
    }
    //生成六位的随机数字
    public static String getSixBitRandom() {
        return sixdf.format(random.nextInt(1000000));
    }
}
SmsUtil–读取yml文件
@Setter
@Component
@ConfigurationProperties(prefix = "jdwx")
public class SmsUtil implements InitializingBean {
    private String url;
    private String appkey;
    private String sign;
    public static String Url;
    public static String Appkey;
    public static String Sign;
    @Override
    public void afterPropertiesSet() throws Exception {
        Url = this.url;
        Appkey = this.appkey;
        Sign = this.sign;
    }
}
service
public interface SmsService {
    public boolean send(String mobile,String code);
}
impl–》SmsServiceImpl
@Service
public class SmsServiceImpl implements SmsService {
    @Override
    public boolean send(String mobile, String code) {
        RequestModel model = new RequestModel();
        model.setGwUrl(SmsUtil.Url);
        model.setAppkey(SmsUtil.Appkey);
        Map queryMap = new HashMap();
        queryMap.put("sign",SmsUtil.Sign); //访问参数
        queryMap.put("mobile",mobile); //访问参数
        queryMap.put("content","您本次的验证码是:"+code); //访问参数
        model.setQueryParams(queryMap);
        try {
            //call 发送
            WxApiCall call = new WxApiCall();
            call.setModel(model);
            call.request(); //发出去
            String request = call.request();
            Gson gson = new Gson();
            //返回一个map对象
            Map<String,String> map = gson.fromJson(request,
                    new TypeToken<Map<String,String>>(){}.getType());
            System.out.println(map);
            if(map.get("code").equals("10010"))return true;
        } catch (JsonSyntaxException e) {
            throw new ShopException(ResponseEnum.SMS_SEND_ERROR.getMsg());
        }
        return false;
    }
}
controller
@RestController
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/send/{mobile}")
    public ResultVO send(@PathVariable("mobile") String mobile){
     //手机号不能为空
        Assert.notNull(mobile, ResponseEnum.PARAMETER_NULL);
        //手机号是否合法
        Assert.isTrue(RegexValidateUtil.checkMobile(mobile), ResponseEnum.MOBILE_ERROR);
        //随机生成6位数
        String code = RandomUtil.getSixBitRandom();
        //发送
        boolean send = this.smsService.send(mobile, code);
        if(send){
            this.redisTemplate.opsForValue().set("uushop-sms-code-"+mobile, code);
            return ResultVOUtil.success("短信发送成功!");
        }
        return ResultVOUtil.fail(null);
    }
}










