美文网首页
[极光推送] 短信验证码

[极光推送] 短信验证码

作者: 后端技术学习分享 | 来源:发表于2019-02-20 14:31 被阅读7次

    介绍

    本文介绍spring项目对接极光推送第三方平台的短信验证码模块。

    依赖

            <dependency>
                <groupId>cn.jpush.api</groupId>
                <artifactId>jsms-client</artifactId>
                <version>1.2.8</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version><!--极光官方的版本是2.3,但是会报错,使用高版本就不会-->
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.7</version>
            </dependency>
            <dependency>
                <groupId>cn.jpush.api</groupId>
                <artifactId>jiguang-common</artifactId>
                <version>1.0.8</version>
            </dependency>
    

    工具类

    package com.lvjian.jiyu.util;
    
    import cn.jiguang.common.resp.APIConnectionException;
    import cn.jiguang.common.resp.APIRequestException;
    import cn.jsms.api.JSMSClient;
    import cn.jsms.api.SendSMSResult;
    import cn.jsms.api.ValidSMSResult;
    import cn.jsms.api.common.SMSClient;
    import cn.jsms.api.common.model.SMSPayload;
    import com.alibaba.fastjson.JSONObject;
    import com.lvjian.jiyu.vo.Code;
    import com.lvjian.jiyu.vo.Response;
    import com.sun.org.apache.xpath.internal.operations.Bool;
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import javax.validation.constraints.NotEmpty;
    import java.util.Base64;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 极光推送短信验证码工具
     * 参考 https://docs.jiguang.cn/jsms/server/rest_api_jsms/
     * @author zp
     */
    @Slf4j
    @Component
    public class JiGuangSMSUtil {
    
        @Value("${jiGuang.appKey}")
        private String appKey;
    
        @Value("${jiGuang.masterSecret}")
        private String masterSecret;
    
        @Value("${jiGuang.tempId}")
        private String tempId; // 模板ID 必填
    
        // 短信发送接口
        private static final String SEND_SMS_URL = "https://api.sms.jpush.cn/v1/codes";
    
        // 校验短信验证码接口
        private static final String CHECK_SMS_URL = "https://api.sms.jpush.cn/v1/codes/";
    
        /**
         * 发送短信
         * @param phone
         * @return 返回成功时,会同时返回msgId,用来调用方法查询用户输入的验证码是否正确
         */
        public Response sendSms(@NotEmpty(message = "手机号不能为空") String phone){
    
            if(CommonUtil.isBlank(appKey) ||
                    CommonUtil.isBlank(masterSecret)){
                log.error("极光推送配置信息读取失败");
                return Response.error("极光推送配置信息读取失败");
            }
    
            // 调用极光sdk
            SMSClient client = new SMSClient(masterSecret, appKey);
            SMSPayload payload = SMSPayload.newBuilder()
                    .setMobileNumber(phone)
                    .setTempId(Integer.parseInt(tempId))
                    .build();
            try {
                SendSMSResult res = client.sendSMSCode(payload);
                return Response.ok(res.getMessageId());
            } catch (APIConnectionException e) {
                log.error("发送短信验证码出现异常:", e);
                return Response.error("发送短信验证码出现异常:" + e);
            } catch (APIRequestException e) {
                log.error("发送短信验证码出现异常:HTTP Status: ", e.getStatus() +
                        "  Error Message: " + e.getErrorMessage());
                return Response.error("发送短信验证码返回结果异常:" + e.getErrorMessage());
            }
        }
    
        /**
         * 校验短信验证码
         * @param code
         * @param msgId
         * @return
         */
        public Response<Boolean> checkSms(@NotEmpty(message = "验证码不能为空") String code,
                                          @NotEmpty(message = "msgId不能为空") String msgId){
            SMSClient client = new SMSClient(masterSecret, appKey);
            try {
                ValidSMSResult res = client.sendValidSMSCode(msgId,code);
                return Response.ok(res.getIsValid());
            } catch (APIConnectionException e) {
                log.error("校验短信验证码出现异常:", e);
                return Response.error("校验短信验证码出现异常:" + e,Boolean.FALSE);
            } catch (APIRequestException e) {
                log.error("校验短信验证码出现异常:HTTP Status: ", e.getStatus() +
                        "  Error Message: " + e.getMessage());
                return Response.error("校验短信验证码返回结果异常:" +  e,Boolean.FALSE);
            }
        }
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:[极光推送] 短信验证码

          本文链接:https://www.haomeiwen.com/subject/ljlhyqtx.html