b69b122d4c90923c93879e9e0fe0bc7f42f7309e.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package com.chinacreator.videoalliance.smc.action;
  2. import com.alibaba.fastjson.JSON;
  3. import com.chinacreator.common.exception.BusinessException;
  4. import com.chinacreator.common.pipe.DataOutPipe;
  5. import com.chinacreator.common.util.DESUtil;
  6. import com.chinacreator.common.util.MD5;
  7. import com.chinacreator.videoalliance.common.annotation.DataOut;
  8. import com.chinacreator.videoalliance.common.dao.DictionaryDao;
  9. import com.chinacreator.videoalliance.common.util.ConfigUtil;
  10. import com.chinacreator.videoalliance.common.util.DataSource;
  11. import com.chinacreator.videoalliance.order.bean.ChannelOrgBean;
  12. import com.chinacreator.videoalliance.order.dao.ChannelDao;
  13. import com.chinacreator.videoalliance.order.util.JsonUtil;
  14. import com.chinacreator.videoalliance.order.util.URLUtil;
  15. import com.chinacreator.videoalliance.smc.bean.TlVerifiCationCodeBean;
  16. import com.chinacreator.videoalliance.smc.dao.SendSmsCodeDao;
  17. import com.chinacreator.videoalliance.smc.dao.SmsContentDao;
  18. import com.chinacreator.videoalliance.smc.dao.SmsWhiteDao;
  19. import com.chinacreator.videoalliance.smc.util.SHAUtil;
  20. import com.chinacreator.videoalliance.smc.util.SendSmsUtil;
  21. import com.chinacreator.videoalliance.smc.util.SendVoiceCodeUtil;
  22. import com.frameworkset.common.poolman.SQLExecutor;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.commons.lang.math.NumberUtils;
  25. import org.apache.log4j.Logger;
  26. import org.frameworkset.util.annotations.ResponseBody;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Controller;
  29. import org.springframework.web.bind.annotation.RequestMapping;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import java.net.URLEncoder;
  33. import java.sql.SQLException;
  34. import java.util.Calendar;
  35. import java.util.Date;
  36. import java.util.Map;
  37. @Controller
  38. public class SendSmsCodeAction {
  39. @Autowired
  40. private SmsContentDao smsContentDao;
  41. @Autowired
  42. private SmsWhiteDao smsWhiteDao;
  43. @Autowired
  44. private ChannelDao channelDao;
  45. @Autowired
  46. private DictionaryDao dictionaryDao;
  47. private static Logger logger = Logger.getLogger("SmcError");
  48. private static Logger log = Logger.getLogger("sendSms");
  49. @RequestMapping({"/sendSmsCode.do"})
  50. @ResponseBody
  51. @DataOut(callback = "sendSmsCode")
  52. public DataOutPipe doNet(HttpServletRequest request, HttpServletResponse response) throws Exception {
  53. long startTime = System.currentTimeMillis();
  54. String cpid = request.getParameter("cpid");
  55. String userid = request.getParameter("userid");
  56. String spid = request.getParameter("spid");
  57. String channel = request.getParameter("channel");
  58. String flag = request.getParameter("flag");
  59. String entype = request.getParameter("entype");
  60. String isvoice = request.getParameter("isvoice");//1:使用语音验证码
  61. String phone = "";
  62. DataOutPipe dop = new DataOutPipe();
  63. try {
  64. if ("1".equals(entype)) {//渠道方接入
  65. ChannelOrgBean channelOrgBean = channelDao.findByChannelId(channel);
  66. if (channelOrgBean == null || channelOrgBean.getStatus().equals("1")) {
  67. throw new BusinessException("9001", "渠道参数错误");
  68. }
  69. System.out.println(userid + " " + channelOrgBean.getPassword());
  70. phone = DESUtil.decode(userid, channelOrgBean.getPassword());
  71. } else { //entype为空或为1时
  72. phone = ConfigUtil.decrypt(userid, cpid);
  73. }
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. logger.error((Object) ("cpid=" + cpid + "userid=" + userid + "spid=" + spid + "channel=" + channel + "Error" + e.getMessage()));
  77. throw new BusinessException("8009", "手机号解密失败", new String[0]);
  78. }
  79. if (this.smsWhiteDao.isBlackUserCanSend(phone, cpid)) {
  80. throw new BusinessException("8007", "黑名单用户,短信发送失败", new String[0]);
  81. }
  82. this.concurrentCheck(phone, cpid);//发短信
  83. SendSmsCodeDao sscd = new SendSmsCodeDao();
  84. long timeDifference;
  85. if ("1".equals(isvoice)){
  86. cpid=cpid+"isvoice";
  87. timeDifference = Long.parseLong(sscd.checkValidatTime(cpid, phone));
  88. }else {
  89. timeDifference = Long.parseLong(sscd.checkValidatTime(cpid, phone));
  90. }
  91. long vercode = (long) (Math.random() * 1000000.0);
  92. if (vercode < 100000L) {
  93. vercode += 100000L;
  94. }
  95. //如果是湖北订购页面及领取会员页面时间超过5秒就可以再发短信
  96. if (timeDifference > 60L || (("chinacreator".equals(channel) || "hubeijoin".equals(channel)) && timeDifference > 5L)) {
  97. if (!this.smsWhiteDao.isWhiteUserCanSend(phone, cpid)) {
  98. this.totalCheck(phone);
  99. }
  100. try {
  101. String content = this.getSmsContent(cpid, spid, String.valueOf(vercode), channel);
  102. dop.add("vercode", (Object) vercode);
  103. dop.add("smsContent", (Object) content);
  104. logger.info("userid:" + phone + ", vercode:" + vercode + ", smscontent:" + content);
  105. //查询短信验证码是否走模板
  106. String isTemplate = this.dictionaryDao.getValue("isTemplate");//
  107. // final String sendStatus = SmsUtil.sendSms(phone, content);
  108. String sendStatus = "";
  109. if (StringUtils.isNotEmpty(phone) && phone.length() > 3) {
  110. if ("1".equals(isvoice)){
  111. SendVoiceCodeUtil.SendVoiceCode(phone,vercode);
  112. }else {
  113. String substring = phone.substring(0, 3);
  114. if ("167".equals(substring) || "171".equals(substring) || "170".equals(substring)) {
  115. //虚商短信发送
  116. sendStatus = sendSmsCode(phone, "20200722001", vercode);
  117. } else {
  118. //发送短信
  119. if("0".equals(isTemplate)){
  120. sendStatus = SendSmsUtil.send(phone, vercode);
  121. }else{
  122. sendStatus = SendSmsUtil.send(phone, content);
  123. }
  124. }
  125. }
  126. } else {
  127. throw new BusinessException("8009", "手机号不存在", new String[0]);
  128. }
  129. TlVerifiCationCodeBean tvcc = new TlVerifiCationCodeBean();
  130. logger.info("sendStatus:" + sendStatus);
  131. tvcc.setCpid(cpid);
  132. tvcc.setUserid(phone);
  133. tvcc.setSendStatus(sendStatus);
  134. tvcc.setVercode(String.valueOf(vercode));
  135. tvcc.setChannel("");
  136. /* Date date = new Date();
  137. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*/
  138. String date=getCurrentTime();
  139. tvcc.setMakeTime(date);
  140. tvcc.setRemark("成功");
  141. sscd.insertTlVerifiCationCode(tvcc);
  142. } catch (Exception e2) {
  143. e2.printStackTrace();
  144. logger.error((Object) ("cpid=" + cpid + "phone=" + phone + "spid=" + spid + "channel=" + channel + "Error" + e2.getMessage()));
  145. }
  146. }else {
  147. dop.setResultCode("8001");
  148. dop.setErrorInfo("一分钟内重复请求");
  149. }
  150. dop.add("remaintime", (Object) timeDifference);
  151. if ("0".equals(flag)) {
  152. try {
  153. int i = sscd.checkTransfer(phone);
  154. if (i > 0) {
  155. dop.add("validflag", (Object) "0");
  156. } else {
  157. i = sscd.checkArea(phone);
  158. if (i > 0) {
  159. dop.add("validflag", (Object) "0");
  160. } else {
  161. dop.add("validflag", (Object) "1");
  162. }
  163. }
  164. } catch (Exception e2) {
  165. e2.printStackTrace();
  166. logger.error((Object) ("cpid=" + cpid + "phone=" + phone + "spid=" + spid + "channel=" + channel + "Error" + e2.getMessage()));
  167. dop.add("validflag", (Object) "1");
  168. }
  169. }
  170. log.info("手机号:"+phone+"sendSmsCode.do响应时间==="+(System.currentTimeMillis()-startTime)/1000);
  171. return dop;
  172. }
  173. /**
  174. * 获取当前时间
  175. * @return
  176. * @throws SQLException
  177. */
  178. public String getCurrentTime() throws SQLException {
  179. String sql = "select to_char(sysdate, 'yyyy-MM-dd HH24:mi:ss') from dual";
  180. return SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql);
  181. }
  182. public void concurrentCheck(String userid, String cpid) throws Exception {
  183. String url = this.dictionaryDao.getValue("smscheckurl"); // http://testapi.mige.tv/redis-service/incr
  184. String timestamp = String.valueOf(System.currentTimeMillis() / 1000L);
  185. String pwd = this.dictionaryDao.getValue("smscheckpwd");//fgdfwhh2
  186. String channel = this.dictionaryDao.getValue("smscheckcchannel");//
  187. String key = "controlsendsms" + userid + channel + cpid; //controlsendsms15581368293inactcurchangshi
  188. String result = "";
  189. try {
  190. key = DESUtil.encode(key, pwd);
  191. String sign = MD5.MD5Encode("channel=" + channel + "&key=" + key + "&timestamp=" + timestamp + pwd);
  192. url = url + "?key=" + URLEncoder.encode(key, "utf-8") + "&channel=" + channel + "&sign=" + sign + "&timestamp=" + timestamp;
  193. result = URLUtil.get(url);
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. }
  197. if (StringUtils.isNotEmpty(result)) {
  198. Map<?, ?> map = (Map<?, ?>) JsonUtil.jsonToMap(result);
  199. if (map.get("count") != null && NumberUtils.toLong((String) map.get("count")) > 1L) {
  200. logger.error((Object) (userid + "用户发送短信次数超过总量上限"));
  201. throw new BusinessException("8008", "并发请求,请稍后再试", new String[0]);
  202. }
  203. }
  204. }
  205. public void totalCheck(String userid) throws Exception {
  206. String url = this.dictionaryDao.getValue("smscheckurl");
  207. String timestamp = String.valueOf(System.currentTimeMillis() / 1000L);
  208. String pwd = this.dictionaryDao.getValue("smscheckpwd");
  209. String channel = this.dictionaryDao.getValue("smschecktchannel");
  210. String key = "totalControlsendsms" + userid + channel;
  211. String result = "";
  212. try {
  213. key = DESUtil.encode(key, pwd);
  214. String sign = MD5.MD5Encode("channel=" + channel + "&key=" + key + "&timestamp=" + timestamp + pwd);
  215. url = url + "?key=" + URLEncoder.encode(key, "utf-8") + "&channel=" + channel + "&sign=" + sign + "&timestamp=" + timestamp;
  216. result = URLUtil.get(url);
  217. } catch (Exception e) {
  218. e.printStackTrace();
  219. }
  220. if (StringUtils.isNotEmpty(result)) {
  221. Map<?, ?> map = (Map<?, ?>) JsonUtil.jsonToMap(result);
  222. if (map.get("count") != null && NumberUtils.toLong((String) map.get("count")) > NumberUtils.toLong(this.dictionaryDao.getValue("smschecktotalnum"))) {
  223. logger.error((Object) (userid + "用户发送短信次数超过总量上限"));
  224. throw new BusinessException("8008", "用户发送短信次数超过总量上限", new String[0]);
  225. }
  226. }
  227. }
  228. /**
  229. * 发送虚拟短信内容
  230. *
  231. * @param userid
  232. * @param contentid
  233. * @return
  234. */
  235. private String sendSmsCode(String userid, String contentid, Long code) {
  236. try {
  237. String smsid = "106550077614";
  238. String key = "wo6bslq2";
  239. String timestamp = System.currentTimeMillis() / 1000 + "";
  240. String usermob = DESUtil.encode(userid, key);
  241. Calendar calendar = Calendar.getInstance();
  242. calendar.setTime(new Date());
  243. String vars = "{\"code\":" + code + "}";
  244. String sign = SHAUtil.shaEncode(smsid + usermob + timestamp + vars + contentid + key).toLowerCase();
  245. usermob = URLEncoder.encode(usermob, "utf-8");
  246. contentid = URLEncoder.encode(contentid, "utf-8");
  247. vars = URLEncoder.encode(vars, "utf-8");
  248. String url = "http://172.16.1.81:809/xsbusi/sms/send?userid=" + usermob + "&smsid=" + smsid + "&timestamp="
  249. + timestamp + "&sign=" + sign + "&contentid=" + contentid + "&vars=" + vars;
  250. String resp = URLUtil.get(url);
  251. logger.info("虚商验证码路径:" + url);
  252. logger.info("调用虚商验证码后返回的参数:" + resp);
  253. Map map = JSON.parseObject(resp, Map.class);
  254. String resultcode = map.get("resultcode") + "";
  255. if ("0".equals(resultcode)) {
  256. logger.info("虚商验证码发送成功");
  257. return resultcode;
  258. } else {
  259. logger.info("虚商验证码发送失败");
  260. return resultcode;
  261. }
  262. } catch (Exception e) {
  263. logger.info("虚商验证码发送失败");
  264. e.printStackTrace();
  265. }
  266. logger.info("虚商验证码发送失败");
  267. return null;
  268. }
  269. public String getSmsContent(String cpid, String spid, String vcode, String channel) throws SQLException {
  270. String content = "";
  271. content = this.smsContentDao.getContent(cpid, (spid == null) ? "" : spid, (channel == null) ? "" : channel);
  272. content = content.replace("${randomCode}", vcode);
  273. return content;
  274. }
  275. public static void main(String[] args) throws Exception {
  276. /* String url = "http://114.255.201.238:8092/videoif/sendSmsCode.do?cpid=tencent&userid=M50FpIQ0x6CWRybWXqmEzg%3D%3D&apptype=2";
  277. System.out.println(URLEncoder.encode(DESUtil.encode("18574414678", "tenc1234"), "utf-8"));
  278. */ System.out.println(DESUtil.encode("17673136322","tenc1234"));
  279. System.out.println(URLEncoder.encode(DESUtil.encode("17673136322","tenc1234"),"utf-8"));
  280. System.out.println(DESUtil.decode("rpeKTUz4z6DGY9IIJQd8QA==","jkl32q12"));
  281. /* SendSmsCodeAction s=new SendSmsCodeAction();
  282. s.sendSmsCode("16714680036","20200722001",1L);*/
  283. }
  284. }