PublicController.java 11 KB

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