LoginController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package com.yihu.wlyy.web.common.account;
  2. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  3. import com.yihu.wlyy.entity.patient.Patient;
  4. import com.yihu.wlyy.entity.security.Token;
  5. import com.yihu.wlyy.repository.security.TokenDao;
  6. import com.yihu.wlyy.service.common.SMSService;
  7. import com.yihu.wlyy.service.common.account.DoctorService;
  8. import com.yihu.wlyy.service.common.account.PatientService;
  9. import com.yihu.wlyy.service.common.account.RoleService;
  10. import com.yihu.wlyy.service.common.account.TokenService;
  11. import com.yihu.wlyy.util.DateUtil;
  12. import com.yihu.wlyy.util.RSAUtils;
  13. import com.yihu.wlyy.util.SystemData;
  14. import com.yihu.wlyy.web.BaseController;
  15. import io.swagger.annotations.Api;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.json.JSONObject;
  18. import org.patchca.color.SingleColorFactory;
  19. import org.patchca.filter.predefined.*;
  20. import org.patchca.service.ConfigurableCaptchaService;
  21. import org.patchca.utils.encoder.EncoderHelper;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Controller;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.bind.annotation.ResponseBody;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import javax.servlet.http.HttpSession;
  30. import java.awt.*;
  31. import java.io.IOException;
  32. import java.net.URLDecoder;
  33. import java.net.URLEncoder;
  34. import java.util.*;
  35. import java.util.List;
  36. //import io.swagger.annotations.Api;
  37. /**
  38. * @author calvin
  39. */
  40. @Controller
  41. @RequestMapping(value = "/login")
  42. @Api(description = "患者、医生及管理员登录")
  43. public class LoginController extends BaseController {
  44. @Autowired
  45. private DoctorService doctorService;
  46. @Autowired
  47. private PatientService patientService;
  48. @Autowired
  49. private TokenService tokenService;
  50. @Autowired
  51. private SMSService smsService;
  52. @Autowired
  53. public TokenDao tokenDao;
  54. @Autowired
  55. private RoleService roleService;
  56. /**
  57. * 公钥生成并返回接口
  58. *
  59. * @return
  60. */
  61. @RequestMapping(value = "public_key")
  62. @ResponseBody
  63. public String publicKey() {
  64. try {
  65. String modulus = RSAUtils.getInstance(smsService).getModulus();
  66. String exponent = RSAUtils.getInstance(smsService).getExponent();
  67. if (StringUtils.isEmpty(modulus) || StringUtils.isEmpty(exponent)) {
  68. return error(-1, "公钥获取失败!");
  69. } else {
  70. JSONObject json = new JSONObject();
  71. json.put("modulus", modulus);
  72. json.put("exponent", exponent);
  73. return write(200, "公钥获取成功!", "data", json);
  74. }
  75. } catch (Exception e) {
  76. return error(-1, "公钥获取失败!");
  77. }
  78. }
  79. // @RequestMapping(value = "testPush")
  80. // @ResponseBody
  81. // public String testPush(String receiver, String type, String title, String msg, String data) throws JSONException, InterruptedException {
  82. // PushMsgTask.getInstance().put(receiver, type, title, msg, data);
  83. // return "OK";
  84. // }
  85. //
  86. // @RequestMapping(value = "test_public_key")
  87. // @ResponseBody
  88. // public String testPublicKey(String str) {
  89. // try {
  90. // String modulus = RSAUtils.getInstance(smsService).getModulus();
  91. // String exponent = RSAUtils.getInstance(smsService).getExponent();
  92. //
  93. // String temp = RSAUtils.getInstance(smsService).decryptStringByJs(str);
  94. // System.out.println(temp);
  95. // temp = URLDecoder.decode(temp, "UTF-8");
  96. // System.out.println(temp);
  97. //
  98. // if (StringUtils.isEmpty(modulus) || StringUtils.isEmpty(exponent)) {
  99. // return error(-1, "公钥获取失败!");
  100. // } else {
  101. // JSONObject json = new JSONObject();
  102. // json.put("modulus", modulus);
  103. // json.put("exponent", exponent);
  104. // return write(200, "公钥获取成功!", "data", json);
  105. // }
  106. // } catch (Exception e) {
  107. // return error(-1, "公钥获取失败!");
  108. // }
  109. // }
  110. /**
  111. * 医生登录接口
  112. *
  113. * @param mobile 手机号
  114. * @param captcha 短信验证码
  115. * @return
  116. */
  117. @RequestMapping(value = "doctor")
  118. @ResponseBody
  119. public String doctor(String mobile, String captcha) {
  120. try {
  121. if (StringUtils.isEmpty(getIMEI())) {
  122. return error(-1, "获取手机IMEI码失败!");
  123. }
  124. // 对验证码进行校验
  125. int res = smsService.check(mobile, 5, captcha);
  126. switch (res) {
  127. case -2:
  128. return error(-1, "验证码已过期!");
  129. case -1:
  130. return error(-1, "请输入正确的验证码!");
  131. case 0:
  132. return error(-1, "验证码无效!");
  133. }
  134. Doctor doctor = doctorService.findDoctorByMobile(mobile);
  135. if (doctor == null) {
  136. return error(-1, "该手机号未注册,请确认!");
  137. } else {
  138. // 用户校验通过,生成token
  139. Token token = tokenService.newToken(doctor.getCode(), getIMEI(), 2);
  140. Map<String, Object> map = new HashMap<>();
  141. map.put("id", doctor.getId());
  142. map.put("uid", doctor.getCode());
  143. map.put("token", token.getToken());
  144. map.put("name", doctor.getName());
  145. map.put("hospital", doctor.getHospital());
  146. map.put("photo", doctor.getPhoto());
  147. // 设置医生类型:1专科医生,2全科医生,3健康管理师
  148. map.put("doctorType", doctor.getLevel());
  149. //获取医生角色和区域权限
  150. List<Map<String, String>> roleMap = roleService.getUserRoleAndArea(doctor.getCode());
  151. map.put("userRole", roleMap);
  152. if("10".equals(doctor.getLevel())&&roleMap.size()==0){
  153. return error(-1, "改用户没有管理员权限");
  154. }
  155. return write(200, "登录成功", "data", map);
  156. }
  157. } catch (Exception e) {
  158. error(e);
  159. return error(-1, "系统异常,登录失败");
  160. }
  161. }
  162. /**
  163. * 患者登录接口
  164. *
  165. * @param mobile 手机号
  166. * @param captcha 短信验证码
  167. * @return
  168. */
  169. @RequestMapping(value = "patient")
  170. @ResponseBody
  171. public String patient(String mobile, String captcha) {
  172. try {
  173. if (StringUtils.isEmpty(getIMEI())) {
  174. return error(-1, "获取手机IMEI码失败!");
  175. }
  176. // 对验证码进行校验
  177. int res = smsService.check(mobile, 4, captcha);
  178. switch (res) {
  179. case -2:
  180. return error(-1, "验证码已过期!");
  181. case -1:
  182. return error(-1, "请输入正确的验证码!");
  183. case 0:
  184. return error(-1, "验证码无效!");
  185. }
  186. // password = RSAUtils.getInstance(doctorService).decryptString(password);
  187. // password = URLDecoder.decode(password, "UTF-8");
  188. // password = StringUtils.reverse(password);
  189. // idcard = RSAUtils.getInstance(doctorService).decryptString(idcard);
  190. // idcard = URLDecoder.decode(idcard, "UTF-8");
  191. // idcard = StringUtils.reverse(idcard);
  192. Patient patient = patientService.findByMobile(mobile);
  193. if (patient == null) {
  194. return error(-1, "该手机号暂未注册帐号,请确认后重新输入!");
  195. } else if (patient.getStatus() == 0) {
  196. return error(-1, "该手机号已被禁止使用!");
  197. } else if (patient.getStatus() == 2) {
  198. return error(-1, "该帐号正在审核中,请确认审核通过后再登录,“如有疑问,拨打400-6677-400转2人工客服”");
  199. } else {
  200. // 用户校验通过,生成token
  201. Token token = tokenService.newToken(patient.getCode(), getIMEI(), 1);
  202. Map<Object, Object> map = new HashMap<Object, Object>();
  203. map.put("id", patient.getId());
  204. map.put("uid", patient.getCode());
  205. map.put("name", URLEncoder.encode(patient.getName(), "UTF-8"));
  206. map.put("token", token.getToken());
  207. map.put("photo", patient.getPhoto());
  208. return write(200, "登录成功", "data", map);
  209. }
  210. } catch (Exception e) {
  211. error(e);
  212. return error(-1, "系统异常,登录失败");
  213. }
  214. }
  215. /**
  216. * 忘记密码
  217. *
  218. * @param type 1患者端,2医生端
  219. * @param idcard 患者身份证号
  220. * @param mobile 手机号
  221. * @param captcha 手机验证码
  222. * @param newpwd 新密码
  223. * @return
  224. */
  225. @RequestMapping(value = "forgetpwd")
  226. @ResponseBody
  227. public String forgetpwd(int type, @RequestParam(required = false) String idcard, String mobile, String captchaToken, String captcha, String newpwd) {
  228. try {
  229. String ct = request.getSession().getAttribute("captchaToken").toString();
  230. if (StringUtils.isEmpty(captchaToken)) {
  231. return error(-1, "图形验证码不允许为空!");
  232. }
  233. if (!StringUtils.equalsIgnoreCase(captchaToken, ct)) {
  234. return error(-1, "图形验证码错误!");
  235. }
  236. idcard = RSAUtils.getInstance(doctorService).decryptString(idcard);
  237. idcard = URLDecoder.decode(idcard, "UTF-8");
  238. idcard = StringUtils.reverse(idcard);
  239. newpwd = RSAUtils.getInstance(doctorService).decryptString(newpwd);
  240. newpwd = URLDecoder.decode(newpwd, "UTF-8");
  241. newpwd = StringUtils.reverse(newpwd);
  242. // 对验证码进行校验
  243. int res = smsService.check(mobile, type, captcha);
  244. switch (res) {
  245. case -2:
  246. return error(-1, "验证码已过期!");
  247. case -1:
  248. return error(-1, "验证码错误!");
  249. case 0:
  250. return error(-1, "验证码无效!");
  251. }
  252. if (type == 2) {
  253. // 医生端
  254. Doctor doctor = doctorService.findDoctorByMobile(mobile);
  255. if (doctor == null) {
  256. return error(-1, "操作失败:此用户未注册");
  257. } else {
  258. doctor.setPassword(newpwd);
  259. doctorService.updateDoctorPwd(doctor);
  260. return success("操作成功!");
  261. }
  262. } else {
  263. // 患者端
  264. Patient patient = patientService.findByIdcard(idcard);
  265. if (patient == null) {
  266. return error(-1, "操作失败:此用户未注册");
  267. } else {
  268. patient.setPassword(newpwd);
  269. patientService.updatePatientPwd(patient);
  270. return success("操作成功!");
  271. }
  272. }
  273. } catch (Exception e) {
  274. error(e);
  275. return error(-1, "系统异常,操作失败");
  276. }
  277. }
  278. /**
  279. * 退出登录
  280. *
  281. * @param type 1患者,2医生,3微信公众号
  282. * @return
  283. */
  284. @RequestMapping(value = "exit")
  285. @ResponseBody
  286. public String logout(int type) {
  287. try {
  288. tokenService.delToken(type, getUID());
  289. return success("已成功退出!");
  290. } catch (Exception e) {
  291. error(e);
  292. return invalidUserException(e, -1, "操作失败!");
  293. }
  294. }
  295. @RequestMapping(value = "/third/login")
  296. @ResponseBody
  297. public String thirdLogin(
  298. @RequestParam(value = "id") int id,
  299. @RequestParam(value = "uid") String uid,
  300. @RequestParam(value = "imei") String imei,
  301. @RequestParam(value = "token") String tokenStr,
  302. @RequestParam(value = "platform") int platform) {
  303. try {
  304. if (StringUtils.isEmpty(tokenStr) || StringUtils.isEmpty(imei) || StringUtils.isEmpty(uid)) {
  305. // response.getWriter().write(error(SystemConf.NOT_LOGIN, "请登录后再操作!"));
  306. return error(-1, "系统异常,操作失败");
  307. }
  308. Token token = SystemData.doctorTokens.get(uid);
  309. if (token == null) {
  310. token = tokenDao.findByToken(tokenStr);
  311. if (token != null) {
  312. // 加入缓存
  313. SystemData.doctorTokens.put(uid, token);
  314. }
  315. }
  316. if (token == null || token.getPlatform() != 2) {
  317. // 未登录
  318. return error(-1, "系统异常,操作失败");
  319. } else {
  320. if (token.getTimeout().getTime() < new Date().getTime()) {
  321. // 登录超时
  322. return error(-1, "系统异常,操作失败");
  323. } else if (!StringUtils.equals(uid, token.getUser()) || !StringUtils.equals(imei, token.getImei())) {
  324. // 别处登录
  325. return error(-1, "系统异常,操作失败");
  326. } else {
  327. // 一天只更新一次
  328. if (DateUtil.getDays(token.getCzrq(), DateUtil.getNowDateShort()) != 0) {
  329. // 今天未更新,则更新缓存
  330. token.setCzrq(new Date());
  331. // 更新内存
  332. SystemData.doctorTokens.put(uid, token);
  333. // 更新数据库
  334. tokenDao.save(token);
  335. }
  336. }
  337. }
  338. return success("登陆成功");
  339. } catch (Exception ex) {
  340. error(ex);
  341. return error(-1, "系统异常,操作失败");
  342. }
  343. }
  344. // public String getLoginInfo()
  345. // {
  346. // try{
  347. // Token token = SystemData.doctorTokens.get(tokenStr);
  348. // return "";
  349. // }
  350. // catch(Exception ex)
  351. // {
  352. // error(ex);
  353. // return error(-1, "系统异常,操作失败");
  354. // }
  355. // }
  356. /**
  357. * 验证码地址
  358. *
  359. * @param request
  360. * @param response
  361. * @throws IOException
  362. */
  363. @RequestMapping("pcrimg")
  364. public void crimg(HttpServletRequest request, HttpServletResponse response) throws IOException {
  365. ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
  366. cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
  367. Random random = new Random();
  368. switch (random.nextInt(5)) {
  369. case 0:
  370. cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
  371. break;
  372. case 1:
  373. cs.setFilterFactory(new MarbleRippleFilterFactory());
  374. break;
  375. case 2:
  376. cs.setFilterFactory(new DoubleRippleFilterFactory());
  377. break;
  378. case 3:
  379. cs.setFilterFactory(new WobbleRippleFilterFactory());
  380. break;
  381. case 4:
  382. cs.setFilterFactory(new DiffuseRippleFilterFactory());
  383. break;
  384. }
  385. HttpSession session = request.getSession(false);
  386. if (session == null) {
  387. session = request.getSession();
  388. }
  389. setResponseHeaders(response);
  390. String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
  391. session.setAttribute("captchaToken", token);
  392. }
  393. protected void setResponseHeaders(HttpServletResponse response) {
  394. response.setContentType("image/png");
  395. response.setHeader("Cache-Control", "no-cache, no-store");
  396. response.setHeader("Pragma", "no-cache");
  397. long time = System.currentTimeMillis();
  398. response.setDateHeader("Last-Modified", time);
  399. response.setDateHeader("Date", time);
  400. response.setDateHeader("Expires", time);
  401. }
  402. // @RequestMapping(value = "test_sms")
  403. // @ResponseBody
  404. // public String testSms() {
  405. // try {
  406. // JSONObject params = new JSONObject();
  407. // params.put("SpCode", SystemConf.SMS_SP_CODE);
  408. // params.put("LoginName", SystemConf.SMS_LOGIN_NAME);
  409. // params.put("Password", SystemConf.SMS_PASSWORD);
  410. // params.put("MessageContent", "您的找回密码验证码为:123456");
  411. // params.put("UserNumber", "18559687019");
  412. // params.put("SerialNumber", "");
  413. // params.put("ScheduleTime", "");
  414. // params.put("f", 1);
  415. // String result = HttpClientUtil.post(SystemConf.SMS_URL, SMSService.buildSmsParams("您的找回密码验证码为:123456", "18559687019"), "GBK");
  416. // JSONObject json = SMSService.toJson(result);
  417. // System.out.println(json.toString());
  418. // System.out.println(json.getInt("result"));
  419. // if (json.getInt("result") != 0) {
  420. // return error(-1, "短信发送失败!");
  421. // }
  422. // return success("短信发送成功!");
  423. // } catch (Exception e) {
  424. // error(e);
  425. // return error(-1, "短信发送失败!");
  426. // }
  427. // }
  428. }