PublicController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. in.close();
  192. } catch (IOException e) {
  193. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  194. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  195. }
  196. try {
  197. out.close();
  198. } catch (IOException e) {
  199. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  200. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT));
  201. }
  202. }
  203. return res;
  204. }
  205. /**
  206. * 删除图片文件(public图片)
  207. *
  208. * @param path
  209. * @return
  210. */
  211. @RequestMapping(value = "/deleteUploadPicture", method = RequestMethod.POST)
  212. public boolean deleteUploadPicture(String path) {
  213. boolean flag = false;
  214. path = uploadPath + path;
  215. File file = new File(path);
  216. // 判断目录或文件是否存在
  217. if (!file.exists() || !file.isFile()) { // 不存在返回 false
  218. return flag;
  219. } else {
  220. return file.delete();
  221. }
  222. }
  223. /**
  224. * 获取并输出private图片
  225. */
  226. @RequestMapping(value = "/writeImage", method = RequestMethod.GET)
  227. public void writeImage(HttpServletResponse response, HttpServletRequest request, String path) {
  228. try {
  229. String realPath = uploadPrivatePath + path;
  230. PictureUtils.outputImage(response, realPath);
  231. } catch (IOException e) {
  232. LoggerUtils.fmtError(getClass(), e, "获取图片异常:%s", e.getMessage());
  233. }
  234. }
  235. /**
  236. * 下载文件
  237. *
  238. * @param res
  239. * @param fileName
  240. * @param response
  241. * @param path
  242. * @param request
  243. * @return
  244. */
  245. @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
  246. public Result downloadFile(String fileName, HttpServletResponse response, String path) {
  247. Result res = new Result();
  248. return handleDownloadFile(response, fileName, path, res);
  249. }
  250. /**
  251. * 下载模版文件
  252. *
  253. * @param response
  254. * @return
  255. */
  256. @RequestMapping(value = "/downloadTemplateFile", method = RequestMethod.GET)
  257. public Result downloadTemplateFile(HttpServletResponse response, String sign) {
  258. Result res = new Result();
  259. String fileName = "";
  260. String path = aftFileService.selectAftFileBySign(sign).getFilePath();
  261. String suffix = path.substring(path.lastIndexOf("."));
  262. if (sign.equals("patent_prory_statement")) {
  263. fileName = "专利代理委托书模版" + suffix;
  264. } else {
  265. fileName = System.nanoTime() + " ";
  266. }
  267. if (!StringUtils.isBlank(path)) {
  268. return handleDownloadFile(response, fileName, path, res);
  269. } else {
  270. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "下载文件不存在!"));
  271. return res;
  272. }
  273. }
  274. private Result handleDownloadFile(HttpServletResponse response, String fileName, String path, Result res) {
  275. String fileSaveRootPath = uploadPrivatePath + path;
  276. InputStream in = null;
  277. OutputStream out = null;
  278. byte[] buffer = new byte[8 * 1024];
  279. try {
  280. File file = new File(fileSaveRootPath);
  281. in = new FileInputStream(file);
  282. out = response.getOutputStream();
  283. response.setContentType("multipart/form-data");
  284. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  285. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  286. for (;;) {
  287. int bytes = in.read(buffer);
  288. if (bytes == -1) {
  289. break;
  290. }
  291. out.write(buffer, 0, bytes);
  292. }
  293. } catch (IOException e) {
  294. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  295. } finally {
  296. try {
  297. in.close();
  298. } catch (IOException e) {
  299. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  300. }
  301. try {
  302. out.close();
  303. } catch (IOException e) {
  304. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  305. }
  306. }
  307. return res;
  308. }
  309. @RequestMapping(value = "/findIndustryCategory", method = RequestMethod.GET)
  310. @ResponseBody
  311. public Result findIndustryCategory(String id, String noCache) {
  312. Integer pid = 0;
  313. if (StringUtils.isNumeric(id)) {
  314. try {
  315. pid = Integer.parseInt(id);
  316. } catch (Exception e) {
  317. pid = 0;
  318. }
  319. }
  320. if (StringUtils.isNotBlank(noCache)) {
  321. industryCategoryService.clear(pid);
  322. }
  323. return new Result().data(industryCategoryService.list(pid));
  324. }
  325. @RequestMapping(value = "/findDistrict", method = RequestMethod.GET)
  326. @ResponseBody
  327. public Result findDistrictGlossory(String id, String noCache) {
  328. Integer pid = 0;
  329. if (StringUtils.isNumeric(id)) {
  330. try {
  331. pid = Integer.parseInt(id);
  332. } catch (Exception e) {
  333. pid = 0;
  334. }
  335. }
  336. if (StringUtils.isNotBlank(noCache)) {
  337. districtGlossoryService.clear(pid);
  338. }
  339. return new Result().data(districtGlossoryService.list(pid));
  340. }
  341. /**
  342. *
  343. * @param mobileCode
  344. * @return
  345. */
  346. @RequestMapping(value = "/checkMCode", method = RequestMethod.POST)
  347. @ResponseBody
  348. public Result checkMCode(String mobileCode) {
  349. Result res = new Result();
  350. if (StringUtils.isBlank(mobileCode)) {
  351. res.getError().add(buildError(ErrorConstants.MCODE_EMPTY_ERROR, "", "手机验证码"));
  352. return res;
  353. }
  354. if (TimeUtils.checkOverTime(MobileCodeCheckOverTimeSign.REGISTER.getCode())) {
  355. res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "", "手机验证码超时失效!"));
  356. VerifyCodeUtils.clearMobileCode();
  357. VerifyCodeUtils.clearMobileCodeTime();
  358. return res;
  359. }
  360. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode)) {
  361. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "", "手机验证码错误"));
  362. return res;
  363. }
  364. return res;
  365. }
  366. /**
  367. * 重置密码
  368. *
  369. * @param resetCode
  370. * @param mobile
  371. * @param type
  372. * @param newPwd
  373. * @return
  374. */
  375. @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
  376. @ResponseBody
  377. public Result resetPwd(String resetCode, String mobile, Integer type, String newPwd) {
  378. Result res = new Result();
  379. if (TimeUtils.checkOverTime(MobileCodeCheckOverTimeSign.RESETCODE.getCode())) {
  380. res.getError().add(buildError(ErrorConstants.RESET_CODE_OVERTIME, "", "页面超时失效,请重新获取验证码!"));
  381. cleanCodeSession();
  382. return res;
  383. }
  384. if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.RESET_CODE).equals(resetCode)) {
  385. res.getError().add(buildError(ErrorConstants.RESET_CODE_ERROR, "", "页面失效,请重新获取验证码!"));
  386. cleanCodeSession();
  387. return res;
  388. }
  389. if (StringUtils.isBlank(newPwd)) {
  390. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新密码"));
  391. return res;
  392. }
  393. if (StringUtils.isBlank(mobile)) {
  394. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "mobile"));
  395. return res;
  396. }
  397. if (!UserType.ORGANIZATION.getCode().equals(type) && !UserType.PERSONAL.getCode().equals(type)) {
  398. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "type"));
  399. return res;
  400. }
  401. User user = userService.selectByMobieAndType(mobile, type);
  402. if (null == user) {
  403. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "mobile"));
  404. return res;
  405. }
  406. user.setPassword(newPwd);
  407. user.setPassword(passwordUtil.getEncryptPwd(user));
  408. userService.updateByPrimaryKey(user);
  409. VerifyCodeUtils.clearResetCode();
  410. VerifyCodeUtils.clearResetCodeTime();
  411. return res;
  412. }
  413. private void cleanCodeSession() {
  414. VerifyCodeUtils.clearResetCode();
  415. VerifyCodeUtils.clearResetCodeTime();
  416. VerifyCodeUtils.clearMobileCode();
  417. VerifyCodeUtils.clearMobileCodeTime();
  418. }
  419. }