TencentMsgSender腾讯发送手机短信验证码

package com.hengtong.boxcode.utils.tool;

import java.io.IOException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.hengtong.boxcode.utils.exception.BizException;

public class TencentMsgSender {

	private static Integer appId = 1400206210;  //假的id,只参考长度和类型
	private static String appKey = "48c88b5cf2887802d8ee29f9d4855841";//假的id,只参考长度和类型
	private static Integer templateId_checkcode = 386325;//假的id,只参考长度和类型
	private static String sign_checkcode = "耐克官网";

	/**
	 * 生成随机4位校验码
	 * 
	 * @return
	 */
	public static String generator() {
		String verifyCode = String.valueOf(new Random().nextInt(8999) + 1000);
		return verifyCode;
	}

	/**
	 * 发送checkcode信息
	 */
	public static Integer sendCheckCodeMsg(String mobile, String code) {
		try {
			String[] params = { code };// 数组具体的元素个数和模板中变量个数必须一致,例如事例中templateId:5678对应一个变量,参数数组中元素个数也必须是一个
			SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
			SmsSingleSenderResult result;
			result = ssender.sendWithParam("86", mobile, templateId_checkcode, params, sign_checkcode, "", "");
			if (!"OK".equals(result.errMsg)) {
				throw new BizException(result.errMsg);
			} else {
				return result.result;
			}

		} catch (org.json.JSONException e) {
			e.printStackTrace();
			throw new BizException(e.getMessage());
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
			throw new BizException(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new BizException(e.getMessage());
		}
	}

	/**
	 * 手机号码校验
	 */
	public static boolean isRightPhoneNumber(String phone) {
		String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
		if (phone.length() != 11) {
			return false;
		} else {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(phone);
			return m.matches();
		}
	}
	
	public static void main(String[] args) {
		sendCheckCodeMsg("13588889999","wweq");
		System.out.println("succ");
	}
}

 

你可能感兴趣的