PublicController.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.springframework.beans.factory.annotation.Value;
  12. import org.springframework.context.annotation.Scope;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. import com.aliyuncs.DefaultAcsClient;
  18. import com.aliyuncs.IAcsClient;
  19. import com.aliyuncs.exceptions.ClientException;
  20. import com.aliyuncs.exceptions.ServerException;
  21. import com.aliyuncs.profile.DefaultProfile;
  22. import com.aliyuncs.profile.IClientProfile;
  23. import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
  24. import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
  25. import com.goafanti.common.bo.Result;
  26. import com.goafanti.common.constant.ErrorConstants;
  27. import com.goafanti.common.utils.LoggerUtils;
  28. import com.goafanti.common.utils.PictureUtils;
  29. import com.goafanti.common.utils.TimeUtils;
  30. import com.goafanti.common.utils.VerifyCodeUtils;
  31. import com.goafanti.core.shiro.token.TokenManager;
  32. import com.goafanti.user.bo.UserDownLoadBo;
  33. import com.goafanti.user.service.UserService;
  34. @Controller
  35. @Scope(value = "prototype")
  36. @RequestMapping("/open")
  37. public class PublicController extends BaseController {
  38. @Value(value = "${accessKey}")
  39. private String accessKey = null;
  40. @Value(value = "${accessSecret}")
  41. private String accessSecret = null;
  42. @Value(value = "${upload.path}")
  43. private String uploadPath = null;
  44. @Value(value = "${upload.private.path}")
  45. private String uploadPrivatePath = null;
  46. @Value(value = "${static.host}")
  47. private String staticHost = null;
  48. @Resource
  49. private UserService userService;
  50. /**
  51. * 获取验证码
  52. *
  53. * @param response
  54. */
  55. @RequestMapping(value = "/getVCode", method = RequestMethod.GET)
  56. public void getVCode(HttpServletResponse response, HttpServletRequest request) {
  57. try {
  58. response.setHeader("Pragma", "No-cache");
  59. response.setHeader("Cache-Control", "no-cache");
  60. response.setDateHeader("Expires", 0);
  61. response.setContentType("image/jpg");
  62. // 生成随机字串
  63. String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
  64. // 存入Shiro会话session
  65. TokenManager.setVal2Session(VerifyCodeUtils.V_CODE, verifyCode.toLowerCase());
  66. // 生成图片
  67. int w = 146, h = 33;
  68. VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
  69. } catch (Exception e) {
  70. LoggerUtils.fmtError(getClass(), e, "获取验证码异常:%s", e.getMessage());
  71. }
  72. }
  73. /**
  74. * 手机验证码
  75. * @param mobile
  76. * @param sign 是否用与找回密码
  77. * @param type
  78. * @return
  79. */
  80. @RequestMapping(value = "/getMCode", method = RequestMethod.GET)
  81. @ResponseBody
  82. public Result getMcode(String mobile ,boolean sign,Integer type) {
  83. Result res = new Result();
  84. //用于找回密码
  85. if(sign){
  86. if(null == userService.selectByMobieAndType(mobile, type)){
  87. res.getError().add(buildError(ErrorConstants.NON_REGISTER, "该用户未注册!"));
  88. return res;
  89. }
  90. }
  91. if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == null
  92. || TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == "") {
  93. sendMessage(mobile, res);
  94. } else if (TimeUtils.checkOverTime("getMCode")) {
  95. TokenManager.getSession().removeAttribute(VerifyCodeUtils.M_CODE);
  96. TokenManager.getSession().removeAttribute(VerifyCodeUtils.M_CODE_TIME);
  97. sendMessage(mobile, res);
  98. } else {
  99. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码发送频繁"));
  100. }
  101. //resetCode
  102. if(res.getError().isEmpty() && sign){
  103. String resetCode = UUID.randomUUID().toString();
  104. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE, resetCode);
  105. TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE_TIME, System.currentTimeMillis());
  106. res.setData(resetCode);
  107. }
  108. return res;
  109. }
  110. /**
  111. *
  112. * @param mobile
  113. * @param res
  114. * @return
  115. */
  116. @SuppressWarnings("unused")
  117. private Result sendMessage(String mobile, Result res) {
  118. // 生成随机字串
  119. String mobileVerifyCode = VerifyCodeUtils.generateMobileVerifyCode(6);
  120. // 存入Shiro会话session
  121. TokenManager.setVal2Session(VerifyCodeUtils.M_CODE, mobileVerifyCode);
  122. TokenManager.setVal2Session(VerifyCodeUtils.M_CODE_TIME, System.currentTimeMillis());
  123. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret);
  124. try {
  125. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
  126. } catch (ClientException e1) {
  127. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "获取手机验证码异常"));
  128. }
  129. IAcsClient client = new DefaultAcsClient(profile);
  130. SingleSendSmsRequest request = new SingleSendSmsRequest();
  131. try {
  132. request.setSignName("阿凡提信息科技");// {\"code\":\""+mobileVerifyCode+"\"}
  133. request.setTemplateCode("SMS_37845022");
  134. request.setParamString("{\"code\":\"" + mobileVerifyCode + "\",\"product\":\"阿凡提信息科技\"}");
  135. request.setRecNum(mobile);
  136. SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
  137. } catch (ServerException e) {
  138. // e.printStackTrace();
  139. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "获取手机验证码异常"));
  140. LoggerUtils.fmtError(getClass(), e, "服务器端手机验证码异常:%s", e.getMessage());
  141. } catch (ClientException e) {
  142. // e.printStackTrace();
  143. res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "获取手机验证码异常"));
  144. LoggerUtils.fmtError(getClass(), e, "客户端手机验证码异常:%s", e.getMessage());
  145. }
  146. return res;
  147. }
  148. /**
  149. * 下载图片
  150. * @param response
  151. * @param path
  152. * @param request
  153. */
  154. @RequestMapping(value = "/downLoadPicture", method = RequestMethod.GET)
  155. public Result downLoadPicture(HttpServletResponse response, String path, HttpServletRequest request){
  156. Result res = new Result();
  157. if(path == "" || path == null){
  158. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "下载文件不存在!"));
  159. return res;
  160. }
  161. String filename =Long.toString(System.nanoTime())+".jpg";
  162. InputStream in = null;
  163. OutputStream out = null;
  164. byte[] buffer = new byte[8 * 1024];
  165. String fileSaveRootPath = "";
  166. //下载权限判断
  167. /*if(null == TokenManager.getAdminToken()){
  168. UserDownLoadBo u = userService.selectUserDownLoadBoByUserId(TokenManager.getUserId());
  169. if(!path.equals(u.getDegreeDiplomaUrl())&&!path.equals(u.getDiplomaUrl())&&
  170. !path.equals(u.getOppositeIdUrl())&&!path.equals(u.getPositiveIdUrl())&&
  171. !path.equals(u.getProfessionalCertificateUrl())&&!path.equals(u.getQualificationCertificateUrl())){
  172. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "权限不足,无法下载!"));
  173. return res;
  174. }
  175. }*/
  176. try {
  177. fileSaveRootPath = uploadPrivatePath + path;
  178. File file = new File(fileSaveRootPath);
  179. in = new FileInputStream(file);
  180. out = response.getOutputStream();
  181. // 设置文件MIME类型
  182. response.setContentType("application/octet-stream");
  183. response.setHeader("Content-Disposition", "attachment; filename=" + filename);
  184. for (;;) {
  185. int bytes = in.read(buffer);
  186. if (bytes == -1) {
  187. break;
  188. }
  189. out.write(buffer, 0, bytes);
  190. }
  191. } catch (IOException e) {
  192. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  193. } finally {
  194. try {
  195. in.close();
  196. } catch (IOException e) {
  197. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  198. }
  199. try {
  200. out.close();
  201. } catch (IOException e) {
  202. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  203. }
  204. }
  205. return res;
  206. }
  207. /**
  208. * 删除图片文件(public图片)
  209. * @param path
  210. * @return
  211. */
  212. @RequestMapping(value = "/deleteUploadPicture", method = RequestMethod.POST)
  213. public boolean deleteUploadPicture(String path){
  214. boolean flag = false;
  215. path = uploadPath+path;
  216. File file = new File(path);
  217. // 判断目录或文件是否存在
  218. if (!file.exists()||!file.isFile()) { // 不存在返回 false
  219. return flag;
  220. } else {
  221. return file.delete();
  222. }
  223. }
  224. /**
  225. * 获取并输出private图片
  226. */
  227. @RequestMapping(value = "/writeImage" , method = RequestMethod.GET)
  228. public void writeImage (HttpServletResponse response, HttpServletRequest request, String path){
  229. try {
  230. String realPath = uploadPrivatePath + path;
  231. PictureUtils.outputImage(response, realPath);
  232. } catch (IOException e) {
  233. LoggerUtils.fmtError(getClass(), e, "获取图片异常:%s", e.getMessage());
  234. }
  235. }
  236. }