阿里大鱼短信平台错误解决

在项目中使用了阿里大鱼短信验证码功能,在调用接口当中遇到如下错误:

{"error_response":{"code":15,"msg":"Remote service error","sub_code":"isv.BUSINESS_LIMIT_CONTROL","sub_msg":"触发业务流控","request_id":"147587plah8ic"}}

查找相关文档找到了错误的原因:
这个原因是:
短信验证码,使用同一个签名,对同一个手机号码发送短信验证码,允许每分钟1条,累计每小时7条。 短信通知,使用同一签名、同一模板,对同一手机号发送短信通知,允许每天50条(自然日)。

解决方案:
短信通讯实体模型

/**
 * 短信通讯实体模型
 * @author 黄宝康
 * 2017年9月7日 上午9:39:41
 */
public class SmsDataModel {
    private String smsCode;// 验证码
    private long createTime; // 创建时间

    public String getSmsCode() {
        return smsCode;
    }
    public long getCreateTime() {
        return createTime;
    }
    public SmsDataModel(String smsCode, long createTime) {
        super();
        this.smsCode = smsCode;
        this.createTime = createTime;
    }
    public SmsDataModel() {
        super();
    }
}

数据暂存对象

import java.util.HashMap;
import java.util.Map;
/**
 * 数据暂存对象
 * @author 黄宝康
 *
 */
public class DataCache {


    private static Map map = new HashMap();

    private DataCache() {
    }

    public synchronized static void addData(String key, SmsDataModel value) {
        map.put(key, value);
    }

    public synchronized static SmsDataModel getData(String key) {
        return map.get(key);
    }
}

调用

/**
     * 请求发送验证码到指定手机
     * 
     * @param tel
     *            , 接收短信验证码的手机号
     * @return
     */
    @Override
    public boolean sendSmsCode(String tel) {
        // 获取随机4位数,作为短信验证码
        String randomStr = RandomUtil.getNumber(4);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("登录验证");
        req.setSmsParamString("{\"code\":\"" + randomStr + "\",\"product\":\"" + APP_NAME + "\"}");
        req.setRecNum(tel);
        req.setSmsTemplateCode("SMS_360XXXXX");
        SmsDataModel model = DataCache.getData(tel);
        long currentTime;
        if(model==null){
            // 缓存中没有
            logger.info("缓存中没有该手机号码对应的key");
            currentTime = System.currentTimeMillis();
            DataCache.addData(tel, new SmsDataModel(randomStr, currentTime));
            return AlidayuUtil.send(req);
        }else{
            // 缓存中有
            logger.info("先从缓存中取,取到的验证码为:"+model.getSmsCode()+"============");
            long lastTime = model.getCreateTime();
            currentTime = System.currentTimeMillis();
            if((currentTime - lastTime) / 60000 >1){
                DataCache.addData(tel, new SmsDataModel(randomStr, currentTime));
                logger.info("验证码:"+randomStr+"已加入缓存");
                return AlidayuUtil.send(req);
            }
        }
        return true;
    }

阿里大鱼调用封装类

import net.sf.json.JSONObject;

import com.jfinal.kit.PropKit;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class AlidayuUtil {

    public static final String URL = PropKit.use("config.properties").get("url");
    public static final String APPKEY = PropKit.use("config.properties").get("appkey");
    public static final String SECRET = PropKit.use("config.properties").get("secret");

    public static boolean send(AlibabaAliqinFcSmsNumSendRequest req) {
        TaobaoClient client = new DefaultTaobaoClient(AlidayuUtil.URL, AlidayuUtil.APPKEY, AlidayuUtil.SECRET);
        AlibabaAliqinFcSmsNumSendResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            return false;
        }
        String body = rsp.getBody();
        JSONObject jo = JSONObject.fromObject(body);
        Object response = jo.get("alibaba_aliqin_fc_sms_num_send_response");
        if (response != null) {
            jo = JSONObject.fromObject(response);
            Object result = jo.get("result");
            if (result != null) {
                jo = JSONObject.fromObject(result);
                Object success = jo.getBoolean("success");
                if (success != null) {
                    boolean bl = (Boolean) success;
                    return bl;
                }
            }else{
                System.out.println(rsp.getBody());
            }
        }
        return false;
    }
}

专业墙纸贴纸厨房用具装饰出售,本人网店经营

博客对你有用记得访问下哦,增加下访问量,如有需要可以下单购买哦^_^。https://item.taobao.com/item.htm?id=569617707364

你可能感兴趣的:(异常处理,工具类,缓存)