shicf
9 小时以前 b640cc3489981365bf7edcc46dc9701c10f04eeb
凌凯短信
已添加1个文件
124 ■■■■■ 文件已修改
src/main/java/com/product/text/message/util/LinKaiSMSUtil.java 124 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/text/message/util/LinKaiSMSUtil.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,124 @@
package com.product.text.message.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.product.core.config.Global;
import com.product.core.exception.BaseException;
import com.product.core.message.ISMSService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.util.config.ErrorCode;
/**
 * å®žçŽ°çŸ­ä¿¡å‘é€
 * æœ‰å˜æ›´ï¼Œå¦å†™ä¸€ä¸ªæ–°ç±»ï¼Œå®žçŽ°æŽ¥å£ï¼Œå¹¶åŽ»æŽ‰è€ç±»çš„å®žçŽ°ç±»ï¼Œå³å¯
 * @version åˆ›å»ºæ—¶é—´ï¼š2017å¹´3月27日 ä¸‹åˆ2:36:08
 *
 */
@Service
public class LinKaiSMSUtil implements  ISMSService  {
    private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
    private static String CorpID="CDJS003057";//账户名
    private static String Pwd="zm0513@";//密码
    private static String Signature="【成都龙欣软件有限公司】";//签名  å¾·æ¶¦ç¾Žå®¶  ä¼ä¸šç®¡ç†å¹³å° æˆéƒ½é¾™æ¬£ç§‘技
    static Map<String,String> error=new HashMap<>();
    static{
        error.put("-1","账号未注册");
        error.put("-2","其他错误:");
        error.put("-3","帐号或密码错误");
        error.put("-5","余额不足,请充值");
        error.put("-6","定时发送时间不是有效的时间格式");
        error.put("-7","提交信息末尾未加签名,请添加中文的企业签名【】");
        error.put("-8","发送内容需在1到300字之间");
        error.put("-9","发送号码为空");
        error.put("-10","定时时间不能小于系统当前时间");
        error.put("-11","屏蔽手机号码");
        error.put("-100","IP黑名单");
        error.put("-101","调用接口频率过快(大于30s调用一次)");
        error.put("-102","账号黑名单");
        error.put("-103","IP未导白");
    }
    /**林凱-發送短信接口*/
    /**
     *
     */
    @Override
    public int sendSMS(String mobile,String content,String send_time) throws BaseException{
        URL url = null;
        content+=Signature;
        try {
            String send_content=URLEncoder.encode(content.replaceAll("<br/>", " "), "GBK");//发送内容
            url = new URL("https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID="+CorpID+"&Pwd="+Pwd+"&Mobile="+mobile+"&Content="+send_content+"&Cell=&SendTime="+send_time);
    //        url = new URL("https://sdk2.028lk.com/sdk2/SelSum.aspx?CorpID="+CorpID+"&Pwd="+Pwd); // æŸ¥è¯¢çŸ­ä¿¡å‰©ä½™æ¡æ•°
            BufferedReader in = null;
            int inputLine = 0;
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            inputLine = new Integer(in.readLine()).intValue();
            SpringMVCContextHolder.getSystemLogger().info("短信发送:"+content+",返回结果:"+inputLine);
            if(inputLine<0) {
                throw new BaseException(""+inputLine,""+error.get(""+inputLine));
            }
            return inputLine;
        }catch(Exception e) {
            e.printStackTrace();
            throw new BaseException(ErrorCode.MESSAGE_SMS_SEND_FAIL);
        }
    }
//    /**林凱-發送短信接口*/
//    @Override
//    public int sendSMS(String Mobile,String Content,String send_time)throws BaseException {
//        URL url = null;
//        Content+=Signature;
//        String send_content= null;//发送内容
//        try {
//            send_content = URLEncoder.encode(Content.replaceAll("<br/>", " "), "GBK");
//            url = new URL("https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID="+CorpID+"&Pwd="+Pwd+"&Mobile="+Mobile+"&Content="+send_content+"&Cell=&SendTime="+send_time);
//        } catch (UnsupportedEncodingException | MalformedURLException e) {
//            throw new BaseException("public int sendSMS(String Mobile,String Content,String send_time)throws BaseException",e.getMessage());
//        }
//        System.out.println("url:"+url.to);
////        url = new URL("https://sdk2.028lk.com/sdk2/SelSum.aspx?CorpID="+CorpID+"&Pwd="+Pwd); // æŸ¥è¯¢çŸ­ä¿¡å‰©ä½™æ¡æ•°
//        BufferedReader in = null;
//        int inputLine = 0;
//        try {
//            System.out.println("开始发送短信手机号码为 ï¼š"+Mobile);
//            in = new BufferedReader(new InputStreamReader(url.openStream()));
//            inputLine = new Integer(in.readLine()).intValue();
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("网络异常,发送短信失败!");
//            throw new BaseException("网络异常,发送短信失败!-2",e.getMessage());
//        }
//        System.out.println("结束发送短信返回值:  "+inputLine);
//        return inputLine;
//    }
    public String SendSMS(String phoneNum) {
        int mobile_code = (int)((Math.random()*9+1)*100000);
        String content = new String("您的验证码是:" + mobile_code + ",请不要把验证码泄露给其他人。");
        int result;
        try {
            result = sendSMS(phoneNum, content, "");
            if (result > 0) {
                return mobile_code + "";
            }
        } catch (Exception e) {
            return "-2";
        }
        return result + "";
    }
    public static void main(String[] args) {
        LinKaiSMSUtil smsUtil = new LinKaiSMSUtil();
        System.out.println(smsUtil.SendSMS("18030850875"));
    }
}