PublicController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package com.goafanti.common.controller;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.UUID;
  8. import javax.annotation.Resource;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.context.annotation.Scope;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import com.goafanti.admin.service.AftFileService;
  20. import com.goafanti.common.bo.Result;
  21. import com.goafanti.common.constant.ErrorConstants;
  22. import com.goafanti.common.enums.MobileCodeCheckOverTimeSign;
  23. import com.goafanti.common.enums.UserType;
  24. import com.goafanti.common.model.User;
  25. import com.goafanti.common.service.IndustryCategoryService;
  26. import com.goafanti.common.utils.LoggerUtils;
  27. import com.goafanti.common.utils.MobileMessageUtils;
  28. import com.goafanti.common.utils.PasswordUtil;
  29. import com.goafanti.common.utils.PictureUtils;
  30. import com.goafanti.common.utils.TimeUtils;
  31. import com.goafanti.common.utils.VerifyCodeUtils;
  32. import com.goafanti.core.shiro.token.TokenManager;
  33. import com.goafanti.dataGlossory.service.DistrictGlossoryService;
  34. import com.goafanti.user.service.UserService;
  35. @Controller
  36. @Scope(value = "prototype")
  37. @RequestMapping("/open")
  38. public class PublicController extends BaseController {
  39. @Value(value = "${accessKey}")
  40. private String accessKey = null;
  41. @Value(value = "${accessSecret}")
  42. private String accessSecret = null;
  43. @Value(value = "${upload.path}")
  44. private String uploadPath = null;
  45. @Value(value = "${upload.private.path}")
  46. private String uploadPrivatePath = null;
  47. @Value(value = "${static.host}")
  48. private String staticHost = null;
  49. @Value(value = "${mobileCodeTemplate}")
  50. private String mobileCodeTemplate = null;
  51. @Resource
  52. private UserService userService;
  53. @Resource
  54. private AftFileService aftFileService;
  55. @Autowired
  56. private IndustryCategoryService industryCategoryService;
  57. @Autowired
  58. private DistrictGlossoryService districtGlossoryService;
  59. @Value(value = "${patentTemplate}")
  60. private String patentTemplate = null;
  61. @Resource(name = "passwordUtil")
  62. private PasswordUtil passwordUtil;
  63. /**
  64. * 获取验证码
  65. *
  66. * @param response
  67. */
  68. @RequestMapping(value = "/getVCode", method = RequestMethod.GET)
  69. public void getVCode(HttpServletResponse response, HttpServletRequest request) {
  70. try {
  71. response.setHeader("Pragma", "No-cache");
  72. response.setHeader("Cache-Control", "no-cache");
  73. response.setDateHeader("Expires", 0);
  74. response.setContentType("image/jpg");
  75. // 生成随机字串
  76. String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
  77. // 存入Shiro会话session
  78. TokenManager.setVal2Session(VerifyCodeUtils.V_CODE, verifyCode.toLowerCase());
  79. // 生成图片
  80. int w = 146, h = 33;
  81. VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
  82. } catch (Exception e) {
  83. LoggerUtils.fmtError(getClass(), e, "获取验证码异常:%s", e.getMessage());
  84. }
  85. }
  86. /**
  87. * 手机验证码
  88. *
  89. * @param mobile
  90. * @param sign
  91. * 是否用与找回密码
  92. * @param type
  93. * @return
  94. */
  95. @RequestMapping(value = "/getMCode", method = RequestMethod.GET)
  96. @ResponseBody
  97. public Result getMcode(String mobile, boolean sign, Integer type, String verificationCode) {
  98. Result res = new Result();
  99. // 用于找回密码
  100. if (sign) {
  101. if (null == userService.selectByMobieAndType(mobile.trim(), type)) {
  102. res.getError().add(buildError(ErrorConstants.NON_REGISTER, "", "该用户未注册!"));
  103. return res;
  104. }
  105. }
  106. if (!sign) {
  107. if (StringUtils.isBlank(verificationCode)) {
  108. res.getError().add(buildError(ErrorConstants.VCODE_EMPTY_ERROR, "", "图像验证码不能为空!"));
  109. return res;
  110. }
  111. if (!VerifyCodeUtils.verifyCode(verificationCode)) {
  112. res.getError().add(buildError(ErrorConstants.VCODE_ERROR, "", "图形验证码输入错误!"));
  113. return res;
  114. }
  115. }
  116. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == null
  117. || TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == "") {
  118. sendMessage(mobile, res);
  119. } else if (TimeUtils.checkOverTime("getMCode")) {
  120. VerifyCodeUtils.clearMobileCode();
  121. VerifyCodeUtils.clearMobileCodeTime();
  122. sendMessage(mobile, res);
  123. } else {
  124. res.getError().add(buildError(ErrorConstants.MCODE_FREQUENCY_ERROR, "", "手机验证码发送频繁!"));
  125. }
  126. // resetCode
  127. if (res.getError().isEmpty() && sign) {
  128. String resetCode = UUID.randomUUID().toString();
  129. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE, resetCode);
  130. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE_TIME, System.currentTimeMillis());
  131. res.setData(resetCode);
  132. }
  133. return res;
  134. }
  135. /**
  136. *
  137. * @param mobile
  138. * @param res
  139. * @return
  140. */
  141. private Result sendMessage(String mobile, Result res) {
  142. String mobileVerifyCode = VerifyCodeUtils.generateMobileVerifyCode(6); // 生成随机字串
  143. TokenManager.setVal2Session(VerifyCodeUtils.M_CODE, mobileVerifyCode);// 存入Shiro会话session
  144. TokenManager.setVal2Session(VerifyCodeUtils.M_CODE_TIME, System.currentTimeMillis());
  145. String paramString = "{\"code\":\"" + mobileVerifyCode + "\",\"product\":\"阿凡提信息科技\"}";
  146. String ret = MobileMessageUtils.sendMessage(mobileCodeTemplate, paramString, mobile, accessKey, accessSecret);
  147. if (StringUtils.isNotBlank(ret)) {
  148. res.getError().add(buildError(ErrorConstants.M_CODE_ERROR));
  149. }
  150. return res;
  151. }
  152. /**
  153. * 下载图片
  154. *
  155. * @param response
  156. * @param path
  157. * @param request
  158. */
  159. @RequestMapping(value = "/downLoadPicture", method = RequestMethod.GET)
  160. public Result downLoadPicture(HttpServletResponse response, String path, HttpServletRequest request) {
  161. Result res = new Result();
  162. if (StringUtils.isBlank(path)) {
  163. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  164. return res;
  165. }
  166. String filename = Long.toString(System.nanoTime()) + ".jpg";
  167. InputStream in = null;
  168. OutputStream out = null;
  169. byte[] buffer = new byte[8 * 1024];
  170. String fileSaveRootPath = "";
  171. try {
  172. fileSaveRootPath = uploadPrivatePath + path;
  173. File file = new File(fileSaveRootPath);
  174. in = new FileInputStream(file);
  175. out = response.getOutputStream();
  176. // 设置文件MIME类型
  177. response.setContentType("application/octet-stream");
  178. response.setHeader("Content-Disposition", "attachment; filename=" + filename);
  179. for (;;) {
  180. int bytes = in.read(buffer);
  181. if (bytes == -1) {
  182. break;
  183. }
  184. out.write(buffer, 0, bytes);
  185. }
  186. } catch (IOException e) {
  187. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  188. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  189. } finally {
  190. try {
  191. if (null != in) {
  192. in.close();
  193. }
  194. } catch (IOException e) {
  195. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  196. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  197. }
  198. try {
  199. if (null != out) {
  200. out.close();
  201. }
  202. } catch (IOException e) {
  203. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  204. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  205. }
  206. }
  207. return res;
  208. }
  209. /**
  210. * 删除图片文件(public图片)
  211. *
  212. * @param path
  213. * @return
  214. */
  215. @RequestMapping(value = "/deleteUploadPicture", method = RequestMethod.POST)
  216. public boolean deleteUploadPicture(String path) {
  217. boolean flag = false;
  218. path = uploadPath + path;
  219. File file = new File(path);
  220. // 判断目录或文件是否存在
  221. if (!file.exists() || !file.isFile()) { // 不存在返回 false
  222. return flag;
  223. } else {
  224. return file.delete();
  225. }
  226. }
  227. /**
  228. * 获取并输出private图片
  229. */
  230. @RequestMapping(value = "/writeImage", method = RequestMethod.GET)
  231. public void writeImage(HttpServletResponse response, HttpServletRequest request, String path) {
  232. try {
  233. String realPath = uploadPrivatePath + path;
  234. PictureUtils.outputImage(response, realPath);
  235. } catch (IOException e) {
  236. LoggerUtils.fmtError(getClass(), e, "获取图片异常:%s", e.getMessage());
  237. }
  238. }
  239. /**
  240. * 下载文件
  241. *
  242. * @param res
  243. * @param fileName
  244. * @param response
  245. * @param path
  246. * @param request
  247. * @return
  248. */
  249. @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
  250. public Result downloadFile(String fileName, HttpServletResponse response, String path) {
  251. Result res = new Result();
  252. return handleDownloadFile(response, fileName, path, res);
  253. }
  254. /**
  255. * 下载模版文件
  256. *
  257. * @param response
  258. * @return
  259. */
  260. @RequestMapping(value = "/downloadTemplateFile", method = RequestMethod.GET)
  261. public Result downloadTemplateFile(HttpServletResponse response, String sign) {
  262. Result res = new Result();
  263. String fileName = "";
  264. String path = aftFileService.selectAftFileBySign(sign).getFilePath();
  265. String suffix = path.substring(path.lastIndexOf("."));
  266. if (sign.equals("patent_prory_statement")) {
  267. fileName = "专利代理委托书模版" + suffix;
  268. } else {
  269. fileName = System.nanoTime() + " ";
  270. }
  271. if (!StringUtils.isBlank(path)) {
  272. return handleDownloadFile(response, fileName, path, res);
  273. } else {
  274. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "下载文件不存在!"));
  275. return res;
  276. }
  277. }
  278. private Result handleDownloadFile(HttpServletResponse response, String fileName, String path, Result res) {
  279. String fileSaveRootPath = uploadPrivatePath + path;
  280. InputStream in = null;
  281. OutputStream out = null;
  282. byte[] buffer = new byte[8 * 1024];
  283. try {
  284. File file = new File(fileSaveRootPath);
  285. in = new FileInputStream(file);
  286. out = response.getOutputStream();
  287. response.setContentType("multipart/form-data");
  288. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  289. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  290. for (;;) {
  291. int bytes = in.read(buffer);
  292. if (bytes == -1) {
  293. break;
  294. }
  295. out.write(buffer, 0, bytes);
  296. }
  297. } catch (IOException e) {
  298. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  299. } finally {
  300. try {
  301. in.close();
  302. } catch (IOException e) {
  303. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  304. }
  305. try {
  306. out.close();
  307. } catch (IOException e) {
  308. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  309. }
  310. }
  311. return res;
  312. }
  313. @RequestMapping(value = "/findIndustryCategory", method = RequestMethod.GET)
  314. @ResponseBody
  315. public Result findIndustryCategory(String id, String noCache) {
  316. Integer pid = 0;
  317. if (StringUtils.isNumeric(id)) {
  318. try {
  319. pid = Integer.parseInt(id);
  320. } catch (Exception e) {
  321. pid = 0;
  322. }
  323. }
  324. if (StringUtils.isNotBlank(noCache)) {
  325. industryCategoryService.clear(pid);
  326. }
  327. return new Result().data(industryCategoryService.list(pid));
  328. }
  329. @RequestMapping(value = "/findDistrict", method = RequestMethod.GET)
  330. @ResponseBody
  331. public Result findDistrictGlossory(String id, String noCache) {
  332. Integer pid = 0;
  333. if (StringUtils.isNumeric(id)) {
  334. try {
  335. pid = Integer.parseInt(id);
  336. } catch (Exception e) {
  337. pid = 0;
  338. }
  339. }
  340. if (StringUtils.isNotBlank(noCache)) {
  341. districtGlossoryService.clear(pid);
  342. }
  343. return new Result().data(districtGlossoryService.list(pid));
  344. }
  345. /**
  346. *
  347. * @param mobileCode
  348. * @return
  349. */
  350. @RequestMapping(value = "/checkMCode", method = RequestMethod.POST)
  351. @ResponseBody
  352. public Result checkMCode(String mobileCode) {
  353. Result res = new Result();
  354. if (StringUtils.isBlank(mobileCode)) {
  355. res.getError().add(buildError(ErrorConstants.MCODE_EMPTY_ERROR, "", "手机验证码"));
  356. return res;
  357. }
  358. if (TimeUtils.checkOverTime(MobileCodeCheckOverTimeSign.REGISTER.getCode())) {
  359. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "", "手机验证码超时失效!"));
  360. VerifyCodeUtils.clearMobileCode();
  361. VerifyCodeUtils.clearMobileCodeTime();
  362. return res;
  363. }
  364. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode)) {
  365. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "", "手机验证码错误"));
  366. return res;
  367. }
  368. return res;
  369. }
  370. /**
  371. * 重置密码
  372. *
  373. * @param resetCode
  374. * @param mobile
  375. * @param type
  376. * @param newPwd
  377. * @return
  378. */
  379. @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
  380. @ResponseBody
  381. public Result resetPwd(String resetCode, String mobile, Integer type, String newPwd) {
  382. Result res = new Result();
  383. if (TimeUtils.checkOverTime(MobileCodeCheckOverTimeSign.RESETCODE.getCode())) {
  384. res.getError().add(buildError(ErrorConstants.RESET_CODE_OVERTIME, "", "页面超时失效,请重新获取验证码!"));
  385. cleanCodeSession();
  386. return res;
  387. }
  388. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.RESET_CODE).equals(resetCode)) {
  389. res.getError().add(buildError(ErrorConstants.RESET_CODE_ERROR, "", "页面失效,请重新获取验证码!"));
  390. cleanCodeSession();
  391. return res;
  392. }
  393. if (StringUtils.isBlank(newPwd)) {
  394. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新密码"));
  395. return res;
  396. }
  397. if (StringUtils.isBlank(mobile)) {
  398. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "mobile"));
  399. return res;
  400. }
  401. if (!UserType.ORGANIZATION.getCode().equals(type) && !UserType.PERSONAL.getCode().equals(type)) {
  402. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "type"));
  403. return res;
  404. }
  405. User user = userService.selectByMobieAndType(mobile, type);
  406. if (null == user) {
  407. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "mobile"));
  408. return res;
  409. }
  410. user.setPassword(newPwd);
  411. user.setPassword(passwordUtil.getEncryptPwd(user));
  412. userService.updateByPrimaryKey(user);
  413. VerifyCodeUtils.clearResetCode();
  414. VerifyCodeUtils.clearResetCodeTime();
  415. return res;
  416. }
  417. private void cleanCodeSession() {
  418. VerifyCodeUtils.clearResetCode();
  419. VerifyCodeUtils.clearResetCodeTime();
  420. VerifyCodeUtils.clearMobileCode();
  421. VerifyCodeUtils.clearMobileCodeTime();
  422. }
  423. }