SMSService.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.yihu.wlyy.service.common;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import com.yihu.wlyy.entity.patient.Patient;
  6. import com.yihu.wlyy.repository.patient.PatientDao;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.json.JSONObject;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.data.domain.Page;
  12. import org.springframework.data.domain.PageRequest;
  13. import org.springframework.data.domain.Sort;
  14. import org.springframework.data.domain.Sort.Direction;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import com.yihu.wlyy.entity.message.SMS;
  18. import com.yihu.wlyy.repository.message.SMSDao;
  19. import com.yihu.wlyy.service.BaseService;
  20. import com.yihu.wlyy.util.DateUtil;
  21. import com.yihu.wlyy.util.HttpClientUtil;
  22. import com.yihu.wlyy.util.SystemConf;
  23. @Component
  24. @Transactional(rollbackFor = Exception.class)
  25. public class SMSService extends BaseService {
  26. @Autowired
  27. public SMSDao smsDao;
  28. @Autowired
  29. PatientDao patientDao;
  30. /**
  31. * 发送短信验证码接口
  32. *
  33. * @param mobile 手机号
  34. * @param type 消息类型:1微信端注册,2微信端找回密码,3医生端找回密码,4患者登录,5医生登录,7用户变更手机号验证 8用户新手机号验证 9绑定手机号
  35. * 10 家庭成员添加验证
  36. * @return
  37. * @throws Exception
  38. */
  39. public String send(String mobile, String ip, int type, String code) throws Exception {
  40. // 生成6位随机验证码
  41. String captcha = String.valueOf(Math.random()).substring(2, 8);
  42. // 默认为123456
  43. // captcha = "123456";
  44. // 同一ip一天不允许超过10条短信
  45. String today = DateUtil.getStringDateShort();
  46. Date begin = DateUtil.strToDate(today + " 00:00:00", DateUtil.YYYY_MM_DD_HH_MM_SS);
  47. Date end = DateUtil.strToDate(today + " 23:59:59", DateUtil.YYYY_MM_DD_HH_MM_SS);
  48. // int count = smsDao.countByIp(ip, begin, end);
  49. // if (count >= SystemConf.MAX_SMS_IP) {
  50. // return "当前IP:[" + ip + "]今天最多发送" + SystemConf.MAX_SMS_IP + "条短信!";
  51. // }
  52. // 一天的短信数不允许超过10条
  53. int count = smsDao.countByMobile(mobile, begin, end);
  54. if (count >= SystemConf.MAX_SMS_MOBILE) {
  55. return "当前手机号:[" + mobile + "]今天最多发送" + SystemConf.MAX_SMS_MOBILE + "条短信!";
  56. }
  57. // 两分钟之内不允许重复发送
  58. PageRequest pageRequest = new PageRequest(0, 1, new Sort(Direction.DESC, "id"));
  59. Page<SMS> page = smsDao.findByMobileType(mobile, type, pageRequest);
  60. if (page != null) {
  61. for (SMS sms : page) {
  62. if (sms == null) {
  63. continue;
  64. }
  65. // 计算间隔时间
  66. Date temp = DateUtil.getNextMin(sms.getCzrq(), SystemConf.SMS_INTERVAL);
  67. long leftTime = (temp.getTime() - System.currentTimeMillis()) / 1000;
  68. if (leftTime > 0) {
  69. return "发送短信验证码间隔时间为:" + SystemConf.SMS_INTERVAL + "分钟,还剩" + leftTime + "秒";
  70. }
  71. }
  72. }
  73. // 保存验证码
  74. SMS sms = new SMS();
  75. // 1微信端注册,2微信端找回密码,3医生端找回密码,4患者登录,5医生登录
  76. if (type == 1) {
  77. // 患者注册
  78. sms.setContent("您的注册验证码为:" + captcha);
  79. } else if (type == 2 || type == 3) {
  80. // 找回密码
  81. sms.setContent("您找回密码验证码为:" + captcha);
  82. } else if (type == 4 || type == 5) {
  83. // 登录
  84. sms.setContent("您的登录验证码为:" + captcha);
  85. } else if (type == 7) {
  86. // 手机号变更验证
  87. sms.setContent("您更换手机号验证码为:" + captcha);
  88. } else if (type == 8) {
  89. // 新手机号绑定验证
  90. sms.setContent("您新手机号验证码为:" + captcha);
  91. } else if (type == 9) {
  92. // 新手机号绑定验证
  93. sms.setContent("您绑定手机号验证码为:" + captcha);
  94. } else if (type == 10) {
  95. Patient patient = patientDao.findByCode(code);
  96. if (patient == null) {
  97. throw new Exception("短信发送失败!");
  98. }
  99. // 新手机号绑定验证
  100. sms.setContent(patient.getName() + "欲添加您为家人,添加成功后,对方可登录您的账号,为您处理各类健康服务。如同意添加,可告知其验证码(" + captcha + ")。");
  101. } else {
  102. // 其他验证码
  103. sms.setContent("验证码:" + captcha);
  104. }
  105. sms.setCaptcha(captcha);
  106. Date date = new Date();
  107. // 延后5分钟
  108. sms.setDeadline(DateUtil.getNextMin(date, type == 10 ? 30 : 5));
  109. sms.setCzrq(date);
  110. sms.setMobile(mobile);
  111. sms.setIp(ip);
  112. sms.setType(type);
  113. sms.setStatus(1);
  114. // 调用总部发送信息的接口
  115. String result = HttpClientUtil.post(SystemConf.getInstance().getSmsUrl(), buildSmsParams(sms.getContent(), mobile), "GBK");
  116. JSONObject json = toJson(result);
  117. if (json == null) {
  118. // 发送失败
  119. throw new Exception("短信发送失败!");
  120. } else if (json.getInt("result") != 0) {
  121. return json.getString("description");
  122. } else {
  123. //发送成功,保存到数据库
  124. smsDao.save(sms);
  125. }
  126. return "ok";
  127. }
  128. /**
  129. * 发送短信
  130. *
  131. * @param mobile
  132. * @param content
  133. * @return
  134. */
  135. public static JSONObject sendMsg(String mobile, String content) {
  136. // 调用总部发送信息的接口
  137. String result = HttpClientUtil.post(SystemConf.getInstance().getSmsUrl(), buildSmsParams(content, mobile), "GBK");
  138. JSONObject json = toJson(result);
  139. return json;
  140. }
  141. /**
  142. * 验证码校验
  143. *
  144. * @param mobile 手机号
  145. * @param type type 消息类型:1微信端注册,2微信端找回密码,3医生端找回密码,4患者登录,5医生登录 7用户变更手机号 8用户新手机号验证 9用户绑定手机号
  146. * @param captcha 验证码
  147. * @return -1验证码过期,-1验证码错误,0验证码无效,1验证通过
  148. */
  149. public int check(String mobile, int type, String captcha) {
  150. // 根据手机号和验证码查询对应的短信信息
  151. // 排序
  152. Sort sort = new Sort(Direction.DESC, "id");
  153. // 分页信息
  154. PageRequest pageRequest = new PageRequest(0, 1, sort);
  155. Page<SMS> page = smsDao.findByCaptcha(mobile, captcha, type, pageRequest);
  156. SMS sms = null;
  157. for (SMS temp : page) {
  158. if (temp != null) {
  159. sms = temp;
  160. break;
  161. }
  162. }
  163. // 验证码校验
  164. if (sms == null) {
  165. // 验证码错误
  166. return -1;
  167. } else if (type != sms.getType()) {
  168. // 验证码无效,也视为错误
  169. return -1;
  170. } else if (sms.getDeadline().before(new Date())) {
  171. // 验证码过期
  172. return -2;
  173. }
  174. return 1;
  175. }
  176. public static List<NameValuePair> buildSmsParams(String content, String mobile) {
  177. List<NameValuePair> params = new ArrayList<NameValuePair>();
  178. params.add(new BasicNameValuePair("SpCode", SystemConf.getInstance().getSmsCode()));
  179. params.add(new BasicNameValuePair("LoginName", SystemConf.getInstance().getSmsName()));
  180. params.add(new BasicNameValuePair("Password", SystemConf.getInstance().getSmsPassword()));
  181. params.add(new BasicNameValuePair("MessageContent", content));
  182. params.add(new BasicNameValuePair("UserNumber", mobile));
  183. params.add(new BasicNameValuePair("SerialNumber", String.valueOf(System.currentTimeMillis())));
  184. params.add(new BasicNameValuePair("ScheduleTime", ""));
  185. params.add(new BasicNameValuePair("f", "1"));
  186. return params;
  187. }
  188. public static JSONObject toJson(String result) {
  189. JSONObject json = new JSONObject();
  190. try {
  191. String[] temps = result.split("&");
  192. for (String temp : temps) {
  193. if (temp.split("=").length != 2) {
  194. continue;
  195. }
  196. String key = temp.split("=")[0];
  197. String value = temp.split("=")[1];
  198. json.put(key, value);
  199. }
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. return json;
  204. }
  205. public static void main(String[] args) {
  206. // JSONObject params = new JSONObject();
  207. // params.put("SpCode", SystemConf.SMS_SP_CODE);
  208. // params.put("LoginName", SystemConf.SMS_LOGIN_NAME);
  209. // params.put("Password", SystemConf.SMS_PASSWORD);
  210. // params.put("MessageContent", "您的找回密码验证码为:123456");
  211. // params.put("UserNumber", "18559687019");
  212. // params.put("SerialNumber", "");
  213. // params.put("ScheduleTime", "");
  214. // params.put("f", 1);
  215. //
  216. // // String result = HttpUtil.sendPost(SystemConf.SMS_URL, params.toString(), "GBK");
  217. // String result = HttpClientUtil.post(SystemConf.SMS_URL, buildSmsParams("您的找回密码验证码为:123456", "18559687019"), "GBK");
  218. // JSONObject json = toJson(result);
  219. // System.out.println(json.toString());
  220. // System.out.println(json.getInt("result"));
  221. // if (json.getInt("result") != 0) {
  222. // System.out.println("短信发送失败!");
  223. // }
  224. System.out.println(toJson("result=0&description=发送短信成功&taskid=22624861237&faillist=&task_id=22624861237"));
  225. }
  226. }