SMSController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.yihu.wlyy.web.common.sms;
  2. import io.swagger.annotations.Api;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  11. import com.yihu.wlyy.entity.patient.Patient;
  12. import com.yihu.wlyy.repository.doctor.DoctorDao;
  13. import com.yihu.wlyy.repository.patient.PatientDao;
  14. import com.yihu.wlyy.service.common.SMSService;
  15. import com.yihu.wlyy.util.NetworkUtil;
  16. import com.yihu.wlyy.web.BaseController;
  17. @Controller
  18. @RequestMapping(value = "/common")
  19. @Api(description = "短信")
  20. public class SMSController extends BaseController {
  21. @Autowired
  22. public SMSService smsService;
  23. @Autowired
  24. private PatientDao patientDao;
  25. @Autowired
  26. private DoctorDao doctorDao;
  27. /**
  28. * 发送短信验证码接口
  29. * @param mobile 手机号
  30. * @param type 消息类型:1微信端注册,2微信端找回密码,3医生端找回密码,4患者登录,5医生登录 .6患者签约验证
  31. * @return
  32. */
  33. @RequestMapping(value = "captcha", method = RequestMethod.POST)
  34. @ResponseBody
  35. public String send(String mobile, int type, @RequestParam(required = false) String captchaToken) {
  36. try {
  37. if (type != 1 && type != 2 && type != 3 && type != 4 && type != 5&& type != 6) {
  38. return error(-1, "无效的请求!");
  39. }
  40. if (StringUtils.isEmpty(mobile)) {
  41. return error(-1, "手机号码不允许为空!");
  42. }
  43. if (type == 4) {
  44. // 登录时校验用户是否存在
  45. Patient patient = patientDao.findByMobile(mobile);
  46. if (patient == null) {
  47. return error(-1, "该手机号未注册!");
  48. }
  49. } else if (type == 5) {
  50. // 登录时校验用户是否存在
  51. Doctor doctor = doctorDao.findByMobile(mobile);
  52. if (doctor == null) {
  53. return error(-1, "该手机号未注册!");
  54. }
  55. }
  56. if (StringUtils.isNotEmpty(captchaToken)) {
  57. String ct = request.getSession().getAttribute("captchaToken").toString();
  58. if (!StringUtils.equalsIgnoreCase(captchaToken, ct)) {
  59. return error(-1, "图形验证码错误!");
  60. }
  61. }
  62. // 获取ip地址
  63. String ip = NetworkUtil.getIpAddress(request);
  64. if (StringUtils.isEmpty(ip)) {
  65. return error(-1, "无效的ip请求!");
  66. }
  67. String res = smsService.send(mobile, ip, type);
  68. if (StringUtils.equals(res, "ok")) {
  69. return write(200, "验证码短信已发送!");
  70. } else {
  71. return error(-1, res);
  72. }
  73. } catch (Exception e) {
  74. error(e);
  75. return error(-1, "短信发送失败!");
  76. }
  77. }
  78. /**
  79. * 验证手机验证码
  80. * @param type 消息类型:1微信端注册,2微信端找回密码,3医生端找回密码
  81. * @param mobile
  82. * @param captcha
  83. * @return
  84. */
  85. @RequestMapping(value = "check_captcha", method = RequestMethod.POST)
  86. @ResponseBody
  87. public String checkCaptcha(int type, String mobile, String captcha) {
  88. try {
  89. // 对验证码进行校验
  90. int res = smsService.check(mobile, type, captcha);
  91. switch (res) {
  92. case -2:
  93. return error(-1, "验证码已过期!");
  94. case -1:
  95. return error(-1, "验证码错误!");
  96. case 0:
  97. return error(-1, "验证码无效!");
  98. }
  99. return success("有效验证码!");
  100. } catch (Exception e) {
  101. error(e);
  102. return error(-1, "验证码检查失败!");
  103. }
  104. }
  105. }