PublicController.java 8.9 KB

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