All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.xiaoyuge5201.component.CloopenSmsClient Maven / Gradle / Ivy

The newest version!
package com.github.xiaoyuge5201.component;

import com.alibaba.fastjson.JSONObject;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.github.xiaoyuge5201.enums.CloopenSmsApiErrorCode;
import com.github.xiaoyuge5201.exception.SmsException;
import com.github.xiaoyuge5201.props.CloopenSMSProperties;

import java.util.HashMap;

/**
 * 容联云
 *
 * @author yugb
 */
public class CloopenSmsClient {

    private static CloopenSMSProperties cloopenSMSProperties = null;

    public CloopenSmsClient(CloopenSMSProperties cloopenSMSProperties) {
        this.cloopenSMSProperties = cloopenSMSProperties;
    }

    private static CCPRestSmsSDK restAPI;
    static {
        restAPI = new CCPRestSmsSDK();
        restAPI.init(cloopenSMSProperties.getSmsServer(), cloopenSMSProperties.getServerPort());
        restAPI.setAccount(cloopenSMSProperties.getSid(), cloopenSMSProperties.getToken());
        restAPI.setAppId(cloopenSMSProperties.getAppId());
    }
    /**
     * 发送短信
     *
     * @param phone        手机
     * @param templateCode 模板id
     * @param datas        数据内容 模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时
     */
    public void doSendSms(String phone, String templateCode, String[] datas) {
        JSONObject jsonObject = new JSONObject();
        try {
            HashMap result = restAPI.sendTemplateSMS(phone, templateCode, datas);
            if (!"000000".equals(result.get("statusCode"))) {
                throw new SmsException("短信验证码出错:" + CloopenSmsApiErrorCode.getMessage((String) result.get("statusCode")));
            }
        } catch (SmsException smsException) {
            smsException.printStackTrace();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy