PublicController.java 11 KB

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