package com.chinacreator.videoalliance.smc.action; import com.alibaba.fastjson.JSON; import com.chinacreator.common.exception.BusinessException; import com.chinacreator.common.pipe.DataOutPipe; import com.chinacreator.common.util.DESUtil; import com.chinacreator.common.util.MD5; import com.chinacreator.videoalliance.common.annotation.DataOut; import com.chinacreator.videoalliance.common.dao.DictionaryDao; import com.chinacreator.videoalliance.common.util.ConfigUtil; import com.chinacreator.videoalliance.common.util.DataSource; import com.chinacreator.videoalliance.order.bean.ChannelOrgBean; import com.chinacreator.videoalliance.order.dao.ChannelDao; import com.chinacreator.videoalliance.order.util.JsonUtil; import com.chinacreator.videoalliance.order.util.URLUtil; import com.chinacreator.videoalliance.smc.bean.TlVerifiCationCodeBean; import com.chinacreator.videoalliance.smc.dao.SendSmsCodeDao; import com.chinacreator.videoalliance.smc.dao.SmsContentDao; import com.chinacreator.videoalliance.smc.dao.SmsWhiteDao; import com.chinacreator.videoalliance.smc.util.SHAUtil; import com.chinacreator.videoalliance.smc.util.SendSmsUtil; import com.chinacreator.videoalliance.smc.util.SendVoiceCodeUtil; import com.frameworkset.common.poolman.SQLExecutor; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.log4j.Logger; import org.frameworkset.util.annotations.ResponseBody; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.sql.SQLException; import java.util.Calendar; import java.util.Date; import java.util.Map; @Controller public class SendSmsCodeAction { @Autowired private SmsContentDao smsContentDao; @Autowired private SmsWhiteDao smsWhiteDao; @Autowired private ChannelDao channelDao; @Autowired private DictionaryDao dictionaryDao; private static Logger logger = Logger.getLogger("SmcError"); private static Logger log = Logger.getLogger("sendSms"); @RequestMapping({"/sendSmsCode.do"}) @ResponseBody @DataOut(callback = "sendSmsCode") public DataOutPipe doNet(HttpServletRequest request, HttpServletResponse response) throws Exception { long startTime = System.currentTimeMillis(); String cpid = request.getParameter("cpid"); String userid = request.getParameter("userid"); String spid = request.getParameter("spid"); String channel = request.getParameter("channel"); String flag = request.getParameter("flag"); String entype = request.getParameter("entype"); String isvoice = request.getParameter("isvoice");//1:使用语音验证码 String phone = ""; DataOutPipe dop = new DataOutPipe(); try { if ("1".equals(entype)) {//渠道方接入 ChannelOrgBean channelOrgBean = channelDao.findByChannelId(channel); if (channelOrgBean == null || channelOrgBean.getStatus().equals("1")) { throw new BusinessException("9001", "渠道参数错误"); } System.out.println(userid + " " + channelOrgBean.getPassword()); phone = DESUtil.decode(userid, channelOrgBean.getPassword()); } else { //entype为空或为1时 phone = ConfigUtil.decrypt(userid, cpid); } } catch (Exception e) { e.printStackTrace(); logger.error((Object) ("cpid=" + cpid + "userid=" + userid + "spid=" + spid + "channel=" + channel + "Error" + e.getMessage())); throw new BusinessException("8009", "手机号解密失败", new String[0]); } if (this.smsWhiteDao.isBlackUserCanSend(phone, cpid)) { throw new BusinessException("8007", "黑名单用户,短信发送失败", new String[0]); } this.concurrentCheck(phone, cpid);//发短信 SendSmsCodeDao sscd = new SendSmsCodeDao(); long timeDifference; if ("1".equals(isvoice)){ cpid=cpid+"isvoice"; timeDifference = Long.parseLong(sscd.checkValidatTime(cpid, phone)); }else { timeDifference = Long.parseLong(sscd.checkValidatTime(cpid, phone)); } long vercode = (long) (Math.random() * 1000000.0); if (vercode < 100000L) { vercode += 100000L; } //如果是湖北订购页面及领取会员页面时间超过5秒就可以再发短信 if (timeDifference > 60L || (("chinacreator".equals(channel) || "hubeijoin".equals(channel)) && timeDifference > 5L)) { if (!this.smsWhiteDao.isWhiteUserCanSend(phone, cpid)) { this.totalCheck(phone); } try { String content = this.getSmsContent(cpid, spid, String.valueOf(vercode), channel); dop.add("vercode", (Object) vercode); dop.add("smsContent", (Object) content); logger.info("userid:" + phone + ", vercode:" + vercode + ", smscontent:" + content); //查询短信验证码是否走模板 String isTemplate = this.dictionaryDao.getValue("isTemplate");// // final String sendStatus = SmsUtil.sendSms(phone, content); String sendStatus = ""; if (StringUtils.isNotEmpty(phone) && phone.length() > 3) { if ("1".equals(isvoice)){ SendVoiceCodeUtil.SendVoiceCode(phone,vercode); }else { String substring = phone.substring(0, 3); if ("167".equals(substring) || "171".equals(substring) || "170".equals(substring)) { //虚商短信发送 sendStatus = sendSmsCode(phone, "20200722001", vercode); } else { //发送短信 if("0".equals(isTemplate)){ sendStatus = SendSmsUtil.send(phone, vercode); }else{ sendStatus = SendSmsUtil.send(phone, content); } } } } else { throw new BusinessException("8009", "手机号不存在", new String[0]); } TlVerifiCationCodeBean tvcc = new TlVerifiCationCodeBean(); logger.info("sendStatus:" + sendStatus); tvcc.setCpid(cpid); tvcc.setUserid(phone); tvcc.setSendStatus(sendStatus); tvcc.setVercode(String.valueOf(vercode)); tvcc.setChannel(""); /* Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*/ String date=getCurrentTime(); tvcc.setMakeTime(date); tvcc.setRemark("成功"); sscd.insertTlVerifiCationCode(tvcc); } catch (Exception e2) { e2.printStackTrace(); logger.error((Object) ("cpid=" + cpid + "phone=" + phone + "spid=" + spid + "channel=" + channel + "Error" + e2.getMessage())); } }else { dop.setResultCode("8001"); dop.setErrorInfo("一分钟内重复请求"); } dop.add("remaintime", (Object) timeDifference); if ("0".equals(flag)) { try { int i = sscd.checkTransfer(phone); if (i > 0) { dop.add("validflag", (Object) "0"); } else { i = sscd.checkArea(phone); if (i > 0) { dop.add("validflag", (Object) "0"); } else { dop.add("validflag", (Object) "1"); } } } catch (Exception e2) { e2.printStackTrace(); logger.error((Object) ("cpid=" + cpid + "phone=" + phone + "spid=" + spid + "channel=" + channel + "Error" + e2.getMessage())); dop.add("validflag", (Object) "1"); } } log.info("手机号:"+phone+"sendSmsCode.do响应时间==="+(System.currentTimeMillis()-startTime)/1000); return dop; } /** * 获取当前时间 * @return * @throws SQLException */ public String getCurrentTime() throws SQLException { String sql = "select to_char(sysdate, 'yyyy-MM-dd HH24:mi:ss') from dual"; return SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql); } public void concurrentCheck(String userid, String cpid) throws Exception { String url = this.dictionaryDao.getValue("smscheckurl"); // http://testapi.mige.tv/redis-service/incr String timestamp = String.valueOf(System.currentTimeMillis() / 1000L); String pwd = this.dictionaryDao.getValue("smscheckpwd");//fgdfwhh2 String channel = this.dictionaryDao.getValue("smscheckcchannel");// String key = "controlsendsms" + userid + channel + cpid; //controlsendsms15581368293inactcurchangshi String result = ""; try { key = DESUtil.encode(key, pwd); String sign = MD5.MD5Encode("channel=" + channel + "&key=" + key + "×tamp=" + timestamp + pwd); url = url + "?key=" + URLEncoder.encode(key, "utf-8") + "&channel=" + channel + "&sign=" + sign + "×tamp=" + timestamp; result = URLUtil.get(url); } catch (Exception e) { e.printStackTrace(); } if (StringUtils.isNotEmpty(result)) { Map map = (Map) JsonUtil.jsonToMap(result); if (map.get("count") != null && NumberUtils.toLong((String) map.get("count")) > 1L) { logger.error((Object) (userid + "用户发送短信次数超过总量上限")); throw new BusinessException("8008", "并发请求,请稍后再试", new String[0]); } } } public void totalCheck(String userid) throws Exception { String url = this.dictionaryDao.getValue("smscheckurl"); String timestamp = String.valueOf(System.currentTimeMillis() / 1000L); String pwd = this.dictionaryDao.getValue("smscheckpwd"); String channel = this.dictionaryDao.getValue("smschecktchannel"); String key = "totalControlsendsms" + userid + channel; String result = ""; try { key = DESUtil.encode(key, pwd); String sign = MD5.MD5Encode("channel=" + channel + "&key=" + key + "×tamp=" + timestamp + pwd); url = url + "?key=" + URLEncoder.encode(key, "utf-8") + "&channel=" + channel + "&sign=" + sign + "×tamp=" + timestamp; result = URLUtil.get(url); } catch (Exception e) { e.printStackTrace(); } if (StringUtils.isNotEmpty(result)) { Map map = (Map) JsonUtil.jsonToMap(result); if (map.get("count") != null && NumberUtils.toLong((String) map.get("count")) > NumberUtils.toLong(this.dictionaryDao.getValue("smschecktotalnum"))) { logger.error((Object) (userid + "用户发送短信次数超过总量上限")); throw new BusinessException("8008", "用户发送短信次数超过总量上限", new String[0]); } } } /** * 发送虚拟短信内容 * * @param userid * @param contentid * @return */ private String sendSmsCode(String userid, String contentid, Long code) { try { String smsid = "106550077614"; String key = "wo6bslq2"; String timestamp = System.currentTimeMillis() / 1000 + ""; String usermob = DESUtil.encode(userid, key); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); String vars = "{\"code\":" + code + "}"; String sign = SHAUtil.shaEncode(smsid + usermob + timestamp + vars + contentid + key).toLowerCase(); usermob = URLEncoder.encode(usermob, "utf-8"); contentid = URLEncoder.encode(contentid, "utf-8"); vars = URLEncoder.encode(vars, "utf-8"); String url = "http://172.16.1.81:809/xsbusi/sms/send?userid=" + usermob + "&smsid=" + smsid + "×tamp=" + timestamp + "&sign=" + sign + "&contentid=" + contentid + "&vars=" + vars; String resp = URLUtil.get(url); logger.info("虚商验证码路径:" + url); logger.info("调用虚商验证码后返回的参数:" + resp); Map map = JSON.parseObject(resp, Map.class); String resultcode = map.get("resultcode") + ""; if ("0".equals(resultcode)) { logger.info("虚商验证码发送成功"); return resultcode; } else { logger.info("虚商验证码发送失败"); return resultcode; } } catch (Exception e) { logger.info("虚商验证码发送失败"); e.printStackTrace(); } logger.info("虚商验证码发送失败"); return null; } public String getSmsContent(String cpid, String spid, String vcode, String channel) throws SQLException { String content = ""; content = this.smsContentDao.getContent(cpid, (spid == null) ? "" : spid, (channel == null) ? "" : channel); content = content.replace("${randomCode}", vcode); return content; } public static void main(String[] args) throws Exception { /* String url = "http://114.255.201.238:8092/videoif/sendSmsCode.do?cpid=tencent&userid=M50FpIQ0x6CWRybWXqmEzg%3D%3D&apptype=2"; System.out.println(URLEncoder.encode(DESUtil.encode("18574414678", "tenc1234"), "utf-8")); */ System.out.println(DESUtil.encode("17673136322","tenc1234")); System.out.println(URLEncoder.encode(DESUtil.encode("17673136322","tenc1234"),"utf-8")); System.out.println(DESUtil.decode("rpeKTUz4z6DGY9IIJQd8QA==","jkl32q12")); /* SendSmsCodeAction s=new SendSmsCodeAction(); s.sendSmsCode("16714680036","20200722001",1L);*/ } }