SMSService.java 12 KB

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