PublicController.java 12 KB

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