| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- package com.goafanti.common.controller;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.UUID;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.aliyuncs.DefaultAcsClient;
- import com.aliyuncs.IAcsClient;
- import com.aliyuncs.exceptions.ClientException;
- import com.aliyuncs.exceptions.ServerException;
- import com.aliyuncs.profile.DefaultProfile;
- import com.aliyuncs.profile.IClientProfile;
- import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
- import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
- import com.goafanti.common.bo.Result;
- import com.goafanti.common.constant.ErrorConstants;
- import com.goafanti.common.utils.LoggerUtils;
- import com.goafanti.common.utils.PictureUtils;
- import com.goafanti.common.utils.TimeUtils;
- import com.goafanti.common.utils.VerifyCodeUtils;
- import com.goafanti.core.shiro.token.TokenManager;
- import com.goafanti.user.bo.UserDownLoadBo;
- import com.goafanti.user.service.UserService;
- @Controller
- @Scope(value = "prototype")
- @RequestMapping("/open")
- public class PublicController extends BaseController {
- @Value(value = "${accessKey}")
- private String accessKey = null;
- @Value(value = "${accessSecret}")
- private String accessSecret = null;
- @Value(value = "${upload.path}")
- private String uploadPath = null;
- @Value(value = "${upload.private.path}")
- private String uploadPrivatePath = null;
- @Value(value = "${static.host}")
- private String staticHost = null;
-
- @Resource
- private UserService userService;
- /**
- * 获取验证码
- *
- * @param response
- */
- @RequestMapping(value = "/getVCode", method = RequestMethod.GET)
- public void getVCode(HttpServletResponse response, HttpServletRequest request) {
- try {
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- response.setContentType("image/jpg");
- // 生成随机字串
- String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
- // 存入Shiro会话session
- TokenManager.setVal2Session(VerifyCodeUtils.V_CODE, verifyCode.toLowerCase());
- // 生成图片
- int w = 146, h = 33;
- VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
- } catch (Exception e) {
- LoggerUtils.fmtError(getClass(), e, "获取验证码异常:%s", e.getMessage());
- }
- }
- /**
- * 手机验证码
- * @param mobile
- * @param sign 是否用与找回密码
- * @param type
- * @return
- */
- @RequestMapping(value = "/getMCode", method = RequestMethod.GET)
- @ResponseBody
- public Result getMcode(String mobile ,boolean sign,Integer type) {
- Result res = new Result();
- //用于找回密码
- if(sign){
- if(null == userService.selectByMobieAndType(mobile, type)){
- res.getError().add(buildError(ErrorConstants.NON_REGISTER, "该用户未注册!"));
- return res;
- }
- }
-
-
- if (TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == null
- || TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE_TIME) == "") {
- sendMessage(mobile, res);
- } else if (TimeUtils.checkOverTime("getMCode")) {
- TokenManager.getSession().removeAttribute(VerifyCodeUtils.M_CODE);
- TokenManager.getSession().removeAttribute(VerifyCodeUtils.M_CODE_TIME);
- sendMessage(mobile, res);
- } else {
- res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码发送频繁"));
- }
-
- //resetCode
- if(res.getError().isEmpty() && sign){
- String resetCode = UUID.randomUUID().toString();
- TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE, resetCode);
- TokenManager.setVal2Session(VerifyCodeUtils.RESET_CODE_TIME, System.currentTimeMillis());
- res.setData(resetCode);
- }
- return res;
- }
- /**
- *
- * @param mobile
- * @param res
- * @return
- */
- @SuppressWarnings("unused")
- private Result sendMessage(String mobile, Result res) {
- // 生成随机字串
- String mobileVerifyCode = VerifyCodeUtils.generateMobileVerifyCode(6);
- // 存入Shiro会话session
- TokenManager.setVal2Session(VerifyCodeUtils.M_CODE, mobileVerifyCode);
- TokenManager.setVal2Session(VerifyCodeUtils.M_CODE_TIME, System.currentTimeMillis());
- IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret);
- try {
- DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
- } catch (ClientException e1) {
- res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "获取手机验证码异常"));
- }
- IAcsClient client = new DefaultAcsClient(profile);
- SingleSendSmsRequest request = new SingleSendSmsRequest();
- try {
- request.setSignName("阿凡提信息科技");// {\"code\":\""+mobileVerifyCode+"\"}
- request.setTemplateCode("SMS_37845022");
- request.setParamString("{\"code\":\"" + mobileVerifyCode + "\",\"product\":\"阿凡提信息科技\"}");
- request.setRecNum(mobile);
- SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
- } catch (ServerException e) {
- // e.printStackTrace();
- res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "获取手机验证码异常"));
- LoggerUtils.fmtError(getClass(), e, "服务器端手机验证码异常:%s", e.getMessage());
- } catch (ClientException e) {
- // e.printStackTrace();
- res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "获取手机验证码异常"));
- LoggerUtils.fmtError(getClass(), e, "客户端手机验证码异常:%s", e.getMessage());
- }
- return res;
- }
-
- /**
- * 下载图片
- * @param response
- * @param path
- * @param request
- */
- @RequestMapping(value = "/downLoadPicture", method = RequestMethod.GET)
- public Result downLoadPicture(HttpServletResponse response, String path, HttpServletRequest request){
- Result res = new Result();
- if(path == "" || path == null){
- res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "下载文件不存在!"));
- return res;
- }
- String filename =Long.toString(System.nanoTime())+".jpg";
- InputStream in = null;
- OutputStream out = null;
- byte[] buffer = new byte[8 * 1024];
- String fileSaveRootPath = "";
- //下载权限判断
- /*if(null == TokenManager.getAdminToken()){
- UserDownLoadBo u = userService.selectUserDownLoadBoByUserId(TokenManager.getUserId());
- if(!path.equals(u.getDegreeDiplomaUrl())&&!path.equals(u.getDiplomaUrl())&&
- !path.equals(u.getOppositeIdUrl())&&!path.equals(u.getPositiveIdUrl())&&
- !path.equals(u.getProfessionalCertificateUrl())&&!path.equals(u.getQualificationCertificateUrl())){
- res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "权限不足,无法下载!"));
- return res;
- }
- }*/
- try {
- fileSaveRootPath = uploadPrivatePath + path;
- File file = new File(fileSaveRootPath);
- in = new FileInputStream(file);
- out = response.getOutputStream();
- // 设置文件MIME类型
- response.setContentType("application/octet-stream");
- response.setHeader("Content-Disposition", "attachment; filename=" + filename);
- for (;;) {
- int bytes = in.read(buffer);
- if (bytes == -1) {
- break;
- }
- out.write(buffer, 0, bytes);
- }
- } catch (IOException e) {
- LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
- } finally {
- try {
- in.close();
- } catch (IOException e) {
- LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
- }
- try {
- out.close();
- } catch (IOException e) {
- LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
- }
- }
- return res;
- }
-
- /**
- * 删除图片文件(public图片)
- * @param path
- * @return
- */
- @RequestMapping(value = "/deleteUploadPicture", method = RequestMethod.POST)
- public boolean deleteUploadPicture(String path){
- boolean flag = false;
- path = uploadPath+path;
- File file = new File(path);
- // 判断目录或文件是否存在
- if (!file.exists()||!file.isFile()) { // 不存在返回 false
- return flag;
- } else {
- return file.delete();
- }
- }
-
- /**
- * 获取并输出private图片
- */
- @RequestMapping(value = "/writeImage" , method = RequestMethod.GET)
- public void writeImage (HttpServletResponse response, HttpServletRequest request, String path){
- try {
- String realPath = uploadPrivatePath + path;
- PictureUtils.outputImage(response, realPath);
- } catch (IOException e) {
- LoggerUtils.fmtError(getClass(), e, "获取图片异常:%s", e.getMessage());
- }
-
-
- }
- }
|