[极光推送] 短信验证码

介绍

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

依赖

        
            cn.jpush.api
            jsms-client
            1.2.8
        
        
            com.google.code.gson
            gson
            2.8.5
        
        
            org.slf4j
            slf4j-api
            1.7.7
        
        
            cn.jpush.api
            jiguang-common
            1.0.8
        

工具类

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 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);
        }
    }
}



你可能感兴趣的:([极光推送] 短信验证码)