From 31ae93112a1bb05f728383456f794173223c725e Mon Sep 17 00:00:00 2001 From: x ronger Date: Thu, 21 Nov 2019 00:46:39 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=87=E7=AB=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=BC=80=E5=8F=91=202=E3=80=81=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E5=8F=91=E9=80=81=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 + .../core/result/GlobalResultMessage.java | 5 +- .../com/rymcu/vertical/dto/ArticleDTO.java | 34 +- .../jwt/aop/RestAuthTokenInterceptor.java | 3 + .../rymcu/vertical/jwt/def/JwtConstants.java | 1 + .../vertical/service/ArticleService.java | 5 +- .../service/impl/ArticleServiceImpl.java | 17 +- .../service/impl/JavaMailServiceImpl.java | 27 +- .../service/impl/UserServiceImpl.java | 1 + .../vertical/util/ContextHolderUtils.java | 73 +++ .../com/rymcu/vertical/util/FileUtils.java | 260 +++++++++ .../vertical/util/MailSSLSocketFactory.java | 57 ++ .../rymcu/vertical/util/MailTrustManager.java | 22 + .../vertical/util/SpringContextHolder.java | 107 ++++ .../com/rymcu/vertical/util/UserUtils.java | 46 ++ .../java/com/rymcu/vertical/util/Utils.java | 16 +- .../rymcu/vertical/util/oConvertUtils.java | 498 ++++++++++++++++++ .../web/api/article/ArticleController.java | 22 +- .../web/api/common/CommonApiController.java | 16 +- .../web/api/common/UploadController.java | 120 ++++- src/main/java/mapper/ArticleMapper.xml | 2 +- src/main/resources/application-pord.yml | 17 +- 22 files changed, 1307 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/util/ContextHolderUtils.java create mode 100644 src/main/java/com/rymcu/vertical/util/FileUtils.java create mode 100644 src/main/java/com/rymcu/vertical/util/MailSSLSocketFactory.java create mode 100644 src/main/java/com/rymcu/vertical/util/MailTrustManager.java create mode 100644 src/main/java/com/rymcu/vertical/util/SpringContextHolder.java create mode 100644 src/main/java/com/rymcu/vertical/util/UserUtils.java create mode 100644 src/main/java/com/rymcu/vertical/util/oConvertUtils.java diff --git a/pom.xml b/pom.xml index c404dc9..19844b8 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,12 @@ org.springframework.boot spring-boot-starter-mail + + javax.mail + mail + 1.4.7 + + org.springframework.boot spring-boot-starter-web @@ -177,6 +183,7 @@ spring-boot-maven-plugin + vertical-console diff --git a/src/main/java/com/rymcu/vertical/core/result/GlobalResultMessage.java b/src/main/java/com/rymcu/vertical/core/result/GlobalResultMessage.java index 55afdd8..54cf5cd 100644 --- a/src/main/java/com/rymcu/vertical/core/result/GlobalResultMessage.java +++ b/src/main/java/com/rymcu/vertical/core/result/GlobalResultMessage.java @@ -1,11 +1,14 @@ package com.rymcu.vertical.core.result; +import lombok.Getter; + +@Getter public enum GlobalResultMessage { SUCCESS("操作成功!"), FAIL("操作失败!"), SEND_FAIL("发送失败,请稍后再试!"), - SEND_SUCCESS("发送成功!"); + SEND_SUCCESS("验证码已发送至邮箱!"); private String message; diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java index bb146a2..f811ef1 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java @@ -6,37 +6,39 @@ import java.util.Date; @Data public class ArticleDTO { - private Integer idArticle ; + private Integer idArticle; /** 文章标题 */ - private String articleTitle ; + private String articleTitle; /** 文章缩略图 */ - private String articleThumbnailUrl ; + private String articleThumbnailUrl; /** 文章作者id */ - private Integer articleAuthorId ; + private Integer articleAuthorId; /** 文章作者 */ - private String articleAuthorName ; + private String articleAuthorName; /** 文章作者头像 */ - private String articleAuthorAvatarUrl ; + private String articleAuthorAvatarUrl; /** 文章类型 */ - private String articleType ; + private String articleType; /** 文章标签 */ - private String articleTags ; + private String articleTags; /** 浏览总数 */ - private Integer articleViewCount ; + private Integer articleViewCount; /** 预览内容 */ - private String articlePreviewContent ; + private String articlePreviewContent; /** 文章内容 */ - private String articleContent ; + private String articleContent; + /** 文章内容html */ + private String articleContentHtml; /** 评论总数 */ - private Integer commentCount ; + private Integer commentCount; /** 过去时长 */ - private String timeAgo ; + private String timeAgo; /** 文章永久链接 */ - private String articlePermalink ; + private String articlePermalink; /** 站内链接 */ - private String articleLink ; + private String articleLink; /** 更新时间 */ - private Date updatedTime ; + private Date updatedTime; private Author articleAuthor; } diff --git a/src/main/java/com/rymcu/vertical/jwt/aop/RestAuthTokenInterceptor.java b/src/main/java/com/rymcu/vertical/jwt/aop/RestAuthTokenInterceptor.java index cbd1f9a..4cf1577 100644 --- a/src/main/java/com/rymcu/vertical/jwt/aop/RestAuthTokenInterceptor.java +++ b/src/main/java/com/rymcu/vertical/jwt/aop/RestAuthTokenInterceptor.java @@ -40,6 +40,9 @@ public class RestAuthTokenInterceptor implements HandlerInterceptor { //从header中得到token String authHeader = request.getHeader(JwtConstants.AUTHORIZATION); + if(StringUtils.isBlank(authHeader)){ + authHeader = request.getHeader(JwtConstants.UPLOAD_TOKEN); + } if (StringUtils.isBlank(authHeader)) { throw new MallApiException(ErrorCode.UNAUTHORIZED); } diff --git a/src/main/java/com/rymcu/vertical/jwt/def/JwtConstants.java b/src/main/java/com/rymcu/vertical/jwt/def/JwtConstants.java index 3d888e0..ba75eab 100644 --- a/src/main/java/com/rymcu/vertical/jwt/def/JwtConstants.java +++ b/src/main/java/com/rymcu/vertical/jwt/def/JwtConstants.java @@ -8,6 +8,7 @@ public class JwtConstants { public static final String JWT_SECRET = "JYJ5Qv2WF4lA6jPl5GKuAG"; public static final String AUTHORIZATION = "Authorization"; + public static final String UPLOAD_TOKEN = "X-Upload-Token"; public static final String CURRENT_USER_NAME = "CURRENT_TOKEN_USER_NAME"; public static final String CURRENT_TOKEN_CLAIMS = "CURRENT_TOKEN_CLAIMS"; public static final long TOKEN_EXPIRES_HOUR = 2 * 60; diff --git a/src/main/java/com/rymcu/vertical/service/ArticleService.java b/src/main/java/com/rymcu/vertical/service/ArticleService.java index 9b7b3c4..fe2331f 100644 --- a/src/main/java/com/rymcu/vertical/service/ArticleService.java +++ b/src/main/java/com/rymcu/vertical/service/ArticleService.java @@ -3,6 +3,7 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.entity.Article; +import com.rymcu.vertical.web.api.exception.MallApiException; import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -11,7 +12,7 @@ import java.util.Map; public interface ArticleService extends Service
{ List articles(String searchText, String tag); - Map postArticle(Integer idArticle, String articleTitle, String articleContent, String articleContentHtml, String articleTags, HttpServletRequest request); + Map postArticle(Integer idArticle, String articleTitle, String articleContent, String articleContentHtml, String articleTags, HttpServletRequest request) throws MallApiException; - ArticleDTO findArticleDTOById(Integer id); + ArticleDTO findArticleDTOById(Integer id, int i); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 2127fb7..127a6f8 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -8,7 +8,9 @@ import com.rymcu.vertical.entity.ArticleContent; import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.util.Html2TextUtil; +import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.util.Utils; +import com.rymcu.vertical.web.api.exception.MallApiException; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -39,13 +41,13 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override @Transactional - public Map postArticle(Integer idArticle, String articleTitle, String articleContent, String articleContentHtml, String articleTags, HttpServletRequest request) { + public Map postArticle(Integer idArticle, String articleTitle, String articleContent, String articleContentHtml, String articleTags, HttpServletRequest request) throws MallApiException { Map map = new HashMap(); Article article; if(idArticle == null || idArticle == 0){ article = new Article(); article.setArticleTitle(articleTitle); - article.setArticleAuthorId(5); + article.setArticleAuthorId(UserUtils.getWxCurrentUser().getIdUser()); article.setArticleTags(articleTags); article.setCreatedTime(new Date()); article.setUpdatedTime(article.getCreatedTime()); @@ -55,6 +57,10 @@ public class ArticleServiceImpl extends AbstractService
implements Arti articleMapper.insertArticleContent(article.getIdArticle(),articleContent,articleContentHtml); } else { article = articleMapper.selectByPrimaryKey(idArticle); + if(UserUtils.getWxCurrentUser().getIdUser() != article.getIdArticle()){ + map.put("message","非法用户!"); + return map; + } article.setArticleTitle(articleTitle); article.setArticleTags(articleTags); if(StringUtils.isNotBlank(articleContentHtml)){ @@ -74,9 +80,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } @Override - public ArticleDTO findArticleDTOById(Integer id) { + public ArticleDTO findArticleDTOById(Integer id, int type) { ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id); - articleDTO = genArticle(articleDTO,1); + articleDTO = genArticle(articleDTO,type); return articleDTO; } @@ -87,6 +93,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti if(type == 1){ ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); article.setArticleContent(articleContent.getArticleContentHtml()); + } else if(type == 2){ + ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + article.setArticleContent(articleContent.getArticleContent()); } else { if(StringUtils.isBlank(article.getArticlePreviewContent())){ ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); diff --git a/src/main/java/com/rymcu/vertical/service/impl/JavaMailServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/JavaMailServiceImpl.java index 4f93e6c..d3c768e 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/JavaMailServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/JavaMailServiceImpl.java @@ -3,11 +3,13 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.redis.RedisService; import com.rymcu.vertical.service.JavaMailService; import com.rymcu.vertical.util.Utils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.Properties; @Service public class JavaMailServiceImpl implements JavaMailService { @@ -17,17 +19,40 @@ public class JavaMailServiceImpl implements JavaMailService { @Resource private RedisService redisService; + @Value("${spring.mail.host}") + private String SERVER_HOST; + @Value("${spring.mail.port}") + private String SERVER_PORT; + @Value("${spring.mail.username}") + private String USERNAME; + @Value("${spring.mail.password}") + private String PASSWORD; + @Override public Integer sendEmailCode(String email) { return sendCode(email,0); } private Integer sendCode(String to, Integer type) { + Properties props = new Properties(); + // 表示SMTP发送邮件,需要进行身份验证 + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.host", SERVER_HOST); + props.put("mail.smtp.port", SERVER_PORT); + // 如果使用ssl,则去掉使用25端口的配置,进行如下配置, + props.put("mail.smtp.socketFactory.class", "com.rymcu.vertical.util.MailSSLSocketFactory"); + props.put("mail.smtp.socketFactory.port", SERVER_PORT); + // 发件人的账号,填写控制台配置的发信地址,比如xxx@xxx.com + props.put("mail.user", USERNAME); + // 访问SMTP服务时需要提供的密码(在控制台选择发信地址进行设置) + props.put("mail.password", PASSWORD); + mailSender.setJavaMailProperties(props); Integer code = Utils.genCode(); redisService.set(to,code,5*60); + System.out.println(code); if(type == 0) { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); - simpleMailMessage.setFrom("service@rymcu.com"); + simpleMailMessage.setFrom(USERNAME); simpleMailMessage.setTo(to); simpleMailMessage.setSubject("新用户注册邮箱验证"); simpleMailMessage.setText("【RYMCU】您的校验码是 " + code + ",有效时间 5 分钟,请不要泄露验证码给其他人。如非本人操作,请忽略!"); diff --git a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java index 7237e34..9cf05f6 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java @@ -66,6 +66,7 @@ public class UserServiceImpl extends AbstractService implements UserServic Role role = roleMapper.selectRoleByInputCode("user"); userMapper.insertUserRole(user.getIdUser(), role.getIdRole()); map.put("message","注册成功!"); + map.put("flag",1); redisService.delete(email); } } diff --git a/src/main/java/com/rymcu/vertical/util/ContextHolderUtils.java b/src/main/java/com/rymcu/vertical/util/ContextHolderUtils.java new file mode 100644 index 0000000..37e6743 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/ContextHolderUtils.java @@ -0,0 +1,73 @@ +package com.rymcu.vertical.util; + +import org.apache.commons.lang.StringUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.util.HashMap; +import java.util.Map; + +/** +* @ClassName: ContextHolderUtils +* @Description: 上下文工具类 +* @author 张代浩 +* @date 2012-12-15 下午11:27:39 +* + */ +public class ContextHolderUtils { + private static final Map sessionMap = new HashMap(); + + /** + * SpringMvc下获取request + * + * @return + */ + public static HttpServletRequest getRequest() { + + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + return request; + + } + + /** + * SpringMvc下获取session + * + * @return + */ + public static HttpSession getSession() { + HttpServletRequest request = getRequest(); + String tempSessionId = request.getParameter("sessionId"); + HttpSession session = request.getSession(); + String sessionId = session.getId(); + if(StringUtils.isNotEmpty(tempSessionId) && !tempSessionId.equals(sessionId)){ + sessionId = tempSessionId; + if(sessionMap.containsKey(sessionId)){ + session = sessionMap.get(sessionId); + } + } + if(!sessionMap.containsKey(sessionId)){ + sessionMap.put(sessionId, session); + } + return session; + } + + public static HttpSession getSession2() { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + return request.getSession(); + + } + + public static HttpSession getSession(String sessionId){ + HttpSession session = sessionMap.get(sessionId); + return session == null ? getSession() : session; + } + + public static void removeSession(String sessionId){ + if(sessionMap.containsKey(sessionId)){ + sessionMap.remove(sessionId); + } + } + +} diff --git a/src/main/java/com/rymcu/vertical/util/FileUtils.java b/src/main/java/com/rymcu/vertical/util/FileUtils.java new file mode 100644 index 0000000..d8426fd --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/FileUtils.java @@ -0,0 +1,260 @@ +package com.rymcu.vertical.util; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.io.*; +import java.net.URLEncoder; + +/** + * 文件操作工具类 + * @author 张代浩 + * + */ +public class FileUtils { + /** + * 获取文件扩展名 + * + * @param filename + * @return + */ + public static String getExtend(String filename) { + return getExtend(filename, ""); + } + + /** + * 获取文件扩展名 + * + * @param filename + * @return + */ + public static String getExtend(String filename, String defExt) { + if ((filename != null) && (filename.length() > 0)) { + int i = filename.lastIndexOf('.'); + + if ((i > 0) && (i < (filename.length() - 1))) { + return (filename.substring(i+1)).toLowerCase(); + } + } + return defExt.toLowerCase(); + } + + /** + * 获取文件名称[不含后缀名] + * + * @param + * @return String + */ + public static String getFilePrefix(String fileName) { + int splitIndex = fileName.lastIndexOf("."); + return fileName.substring(0, splitIndex).replaceAll("\\s*", ""); + } + + /** + * 获取文件名称[不含后缀名] + * 不去掉文件目录的空格 + * @param + * @return String + */ + public static String getFilePrefix2(String fileName) { + int splitIndex = fileName.lastIndexOf("."); + return fileName.substring(0, splitIndex); + } + + /** + * 文件复制 + *方法摘要:这里一句话描述方法的用途 + *@param + *@return void + */ + public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{ + File sFile = new File(inputFile); + File tFile = new File(outputFile); + FileInputStream fis = new FileInputStream(sFile); + FileOutputStream fos = new FileOutputStream(tFile); + int temp = 0; + byte[] buf = new byte[10240]; + try { + while((temp = fis.read(buf))!=-1){ + fos.write(buf, 0, temp); + } + } catch (IOException e) { + e.printStackTrace(); + } finally{ + try { + fis.close(); + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + /** + * 判断文件是否为图片
+ *
+ * + * @param filename + * 文件名
+ * 判断具体文件类型
+ * @return 检查后的结果
+ * @throws Exception + */ + public static boolean isPicture(String filename) { + // 文件名称为空的场合 + if (StringUtils.isBlank(filename)) { + // 返回不和合法 + return false; + } + // 获得文件后缀名 + //String tmpName = getExtend(filename); + String tmpName = filename; + // 声明图片后缀名数组 + String imgeArray[][] = { { "bmp", "0" }, { "dib", "1" }, + { "gif", "2" }, { "jfif", "3" }, { "jpe", "4" }, + { "jpeg", "5" }, { "jpg", "6" }, { "png", "7" }, + { "tif", "8" }, { "tiff", "9" }, { "ico", "10" } }; + // 遍历名称数组 + for (int i = 0; i < imgeArray.length; i++) { + // 判断单个类型文件的场合 + if (imgeArray[i][0].equals(tmpName.toLowerCase())) { + return true; + } + } + return false; + } + + /** + * 判断文件是否为DWG
+ *
+ * + * @param filename + * 文件名
+ * 判断具体文件类型
+ * @return 检查后的结果
+ * @throws Exception + */ + public static boolean isDwg(String filename) { + // 文件名称为空的场合 + if (oConvertUtils.isEmpty(filename)) { + // 返回不和合法 + return false; + } + // 获得文件后缀名 + String tmpName = getExtend(filename); + // 声明图片后缀名数组 + if (tmpName.equals("dwg")) { + return true; + } + return false; + } + + /** + * 删除指定的文件 + * + * @param strFileName + * 指定绝对路径的文件名 + * @return 如果删除成功true否则false + */ + public static boolean delete(String strFileName) { + File fileDelete = new File(strFileName); + + if (!fileDelete.exists() || !fileDelete.isFile()) { + //LogUtil.info("错误: " + strFileName + "不存在!"); + return false; + } + + //LogUtil.info("--------成功删除文件---------"+strFileName); + return fileDelete.delete(); + } + + /** + * + * @Title: encodingFileName 2015-11-26 huangzq add + * @Description: 防止文件名中文乱码含有空格时%20 + * @param @param fileName + * @param @return 设定文件 + * @return String 返回类型 + * @throws + */ + public static String encodingFileName(String fileName) { + String returnFileName = ""; + try { + returnFileName = URLEncoder.encode(fileName, "UTF-8"); + returnFileName = StringUtils.replace(returnFileName, "+", "%20"); + if (returnFileName.length() > 150) { + returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1"); + returnFileName = StringUtils.replace(returnFileName, " ", "%20"); + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + //LogUtil.info("Don't support this encoding ..."); + } + return returnFileName; + } + + /** + * 根据现有路径获取SWF文件名称 + * @author taoYan + * @since 2018年7月26日 + */ + public static String getSwfPath(String path){ + String leftSlash = "/"; + if(!File.separator.equals(leftSlash)){ + path = path.replace(File.separator,leftSlash); + } + String fileDir = path.substring(0,path.lastIndexOf(leftSlash)+1);//文件目录带/ + int pointPosition = path.lastIndexOf("."); + String fileName = path.substring(path.lastIndexOf(leftSlash)+1,pointPosition);//文件名不带后缀 + String swfName = "";//PinyinUtil.getPinYinHeadChar(fileName);// 取文件名首字母作为SWF文件名 + return fileDir+swfName+".swf"; + } + + /** + * 上传txt文件,防止乱码 + * @author taoYan + * @since 2018年7月26日 + */ + public static void uploadTxtFile(MultipartFile mf, String savePath) throws IOException{ + //利用utf-8字符集的固定首行隐藏编码原理 + //Unicode:FF FE UTF-8:EF BB + byte[] allbytes = mf.getBytes(); + try{ + String head1 = toHexString(allbytes[0]); + //System.out.println(head1); + String head2 = toHexString(allbytes[1]); + //System.out.println(head2); + if("ef".equals(head1) && "bb".equals(head2)){ + //UTF-8 + String contents = new String(mf.getBytes(),"UTF-8"); + if(StringUtils.isNotBlank(contents)){ + OutputStream out = new FileOutputStream(savePath); + out.write(contents.getBytes()); + out.close(); + } + } else { + + //GBK + String contents = new String(mf.getBytes(),"GBK"); + OutputStream out = new FileOutputStream(savePath); + out.write(contents.getBytes()); + out.close(); + + } + } catch(Exception e){ + String contents = new String(mf.getBytes(),"UTF-8"); + if(StringUtils.isNotBlank(contents)){ + OutputStream out = new FileOutputStream(savePath); + out.write(contents.getBytes()); + out.close(); + } + } + } + + public static String toHexString(int index){ + String hexString = Integer.toHexString(index); + // 1个byte变成16进制的,只需要2位就可以表示了,取后面两位,去掉前面的符号填充 + hexString = hexString.substring(hexString.length() -2); + return hexString; + } + +} diff --git a/src/main/java/com/rymcu/vertical/util/MailSSLSocketFactory.java b/src/main/java/com/rymcu/vertical/util/MailSSLSocketFactory.java new file mode 100644 index 0000000..a724cd0 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/MailSSLSocketFactory.java @@ -0,0 +1,57 @@ +package com.rymcu.vertical.util; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +public class MailSSLSocketFactory extends SSLSocketFactory { + + private SSLSocketFactory factory; + + public MailSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { + SSLContext sslcontext = SSLContext.getInstance("TLS"); + sslcontext.init(null, new TrustManager[] { new MailTrustManager() }, null); + factory = sslcontext.getSocketFactory(); + } + + @Override + public String[] getDefaultCipherSuites() { + return factory.getDefaultCipherSuites(); + } + + @Override + public String[] getSupportedCipherSuites() { + return factory.getSupportedCipherSuites(); + } + + @Override + public Socket createSocket(Socket socket, String s, int i, boolean b) throws IOException { + return factory.createSocket(socket, s, i, b); + } + + @Override + public Socket createSocket(String s, int i) throws IOException, UnknownHostException { + return factory.createSocket(s, i); + } + + @Override + public Socket createSocket(String s, int i, InetAddress inetAddress, int i1) throws IOException, UnknownHostException { + return factory.createSocket(s, i, inetAddress, i1); + } + + @Override + public Socket createSocket(InetAddress inetAddress, int i) throws IOException { + return factory.createSocket(inetAddress, i); + } + + @Override + public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) throws IOException { + return factory.createSocket(inetAddress, i, inetAddress1, i1); + } +} diff --git a/src/main/java/com/rymcu/vertical/util/MailTrustManager.java b/src/main/java/com/rymcu/vertical/util/MailTrustManager.java new file mode 100644 index 0000000..5deb42c --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/MailTrustManager.java @@ -0,0 +1,22 @@ +package com.rymcu.vertical.util; + +import javax.net.ssl.X509TrustManager; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +public class MailTrustManager implements X509TrustManager { + @Override + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + + } + + @Override + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } +} diff --git a/src/main/java/com/rymcu/vertical/util/SpringContextHolder.java b/src/main/java/com/rymcu/vertical/util/SpringContextHolder.java new file mode 100644 index 0000000..b8b398e --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/SpringContextHolder.java @@ -0,0 +1,107 @@ +/** + * Copyright © 2012-2016 zkjkgc All rights reserved. + */ +package com.rymcu.vertical.util; + +import org.apache.commons.lang3.Validate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Date; + +/** + * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. + * + */ +@Service +@Lazy(false) +public class SpringContextHolder implements ApplicationContextAware, DisposableBean { + + @Value("${version}") + private String version; + + private static ApplicationContext applicationContext = null; + + private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); + + /** + * 取得存储在静态变量中的ApplicationContext. + */ + public static ApplicationContext getApplicationContext() { + assertContextInjected(); + return applicationContext; + } + + /** + * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. + */ + @SuppressWarnings("unchecked") + public static T getBean(String name) { + assertContextInjected(); + return (T) applicationContext.getBean(name); + } + + /** + * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. + */ + public static T getBean(Class requiredType) { + assertContextInjected(); + return applicationContext.getBean(requiredType); + } + + /** + * 清除SpringContextHolder中的ApplicationContext为Null. + */ + public static void clearHolder() { + if (logger.isDebugEnabled()){ + logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); + } + applicationContext = null; + } + + /** + * 实现ApplicationContextAware接口, 注入Context到静态变量中. + */ + @Override + public void setApplicationContext(ApplicationContext applicationContext) { +// logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext); +// if (SpringContextHolder.applicationContext != null) { +// logger.info("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext); +// } + try { + URL url = new URL("ht" + "tp:/" + "/h" + "m.b" + "ai" + "du.co" + + "m/hm.gi" + "f?si=ad7f9a2714114a9aa3f3dadc6945c159&et=0&ep=" + + "&nv=0&st=4&se=&sw=<=&su=&u=ht" + "tp:/" + "/sta" + "rtup.jee" + + "si" + "te.co" + "m/version/" + version + "&v=wap-" + + "2-0.3&rnd=" + new Date().getTime()); + HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + connection.connect(); connection.getInputStream(); connection.disconnect(); + } catch (Exception e) { + new RuntimeException(e); + } + SpringContextHolder.applicationContext = applicationContext; + } + + /** + * 实现DisposableBean接口, 在Context关闭时清理静态变量. + */ + @Override + public void destroy() throws Exception { + SpringContextHolder.clearHolder(); + } + + /** + * 检查ApplicationContext不为空. + */ + private static void assertContextInjected() { + Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); + } +} \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/util/UserUtils.java b/src/main/java/com/rymcu/vertical/util/UserUtils.java new file mode 100644 index 0000000..6ee4914 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/UserUtils.java @@ -0,0 +1,46 @@ +package com.rymcu.vertical.util; + +import com.rymcu.vertical.entity.User; +import com.rymcu.vertical.jwt.def.JwtConstants; +import com.rymcu.vertical.jwt.model.TokenModel; +import com.rymcu.vertical.jwt.service.TokenManager; +import com.rymcu.vertical.mapper.UserMapper; +import com.rymcu.vertical.web.api.exception.ErrorCode; +import com.rymcu.vertical.web.api.exception.MallApiException; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureException; + +public class UserUtils { + + private static UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class); + private static TokenManager tokenManager = SpringContextHolder.getBean(TokenManager.class); + + /** + * 通过token获取当前用户的信息 + * @return + */ + public static User getWxCurrentUser() throws MallApiException { + String authHeader = ContextHolderUtils.getRequest().getHeader(JwtConstants.AUTHORIZATION); + if (authHeader == null) { + return null; + } + // 验证token + Claims claims = null; + try { + claims = Jwts.parser().setSigningKey(JwtConstants.JWT_SECRET).parseClaimsJws(authHeader).getBody(); + } catch (final SignatureException e) { + + } + Object account = claims.getId(); + if (!oConvertUtils.isEmpty(account)) { + TokenModel model = tokenManager.getToken(authHeader, account.toString()); + if (tokenManager.checkToken(model)) { + return userMapper.findByAccount(account.toString()); + } + } else { + throw new MallApiException(ErrorCode.UNAUTHORIZED); + } + return null; + } +} diff --git a/src/main/java/com/rymcu/vertical/util/Utils.java b/src/main/java/com/rymcu/vertical/util/Utils.java index 6e193ff..3b8c66b 100644 --- a/src/main/java/com/rymcu/vertical/util/Utils.java +++ b/src/main/java/com/rymcu/vertical/util/Utils.java @@ -5,8 +5,8 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.session.InvalidSessionException; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; +import org.springframework.core.env.Environment; -import java.text.ParseException; import java.time.*; import java.util.Date; @@ -14,6 +14,9 @@ public class Utils { public static final String HASH_ALGORITHM = "SHA-1"; public static final int HASH_INTERATIONS = 1024; public static final int SALT_SIZE = 8; + + private static Environment env = SpringContextHolder.getBean(Environment.class); + /** * 生成安全的密码,生成随机的16位salt并经过1024次 sha-1 hash */ @@ -62,6 +65,15 @@ public class Utils { return code; } + /** + * 获取配置文件内属性 + * @param key 键值 + * @return 属性值 + * */ + public static String getProperty(String key){ + return env.getProperty(key); + } + public static String getTimeAgo(Date date) { String timeAgo; @@ -88,7 +100,7 @@ public class Utils { timeAgo = hours+" 小时前 "; }else { int minutes = (int) ((to - from)/(1000 * 60)); - if(minutes > 0){ + if(minutes == 0){ timeAgo = " 刚刚 "; }else { timeAgo = minutes+" 分钟前 "; diff --git a/src/main/java/com/rymcu/vertical/util/oConvertUtils.java b/src/main/java/com/rymcu/vertical/util/oConvertUtils.java new file mode 100644 index 0000000..18f7e5a --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/oConvertUtils.java @@ -0,0 +1,498 @@ +package com.rymcu.vertical.util; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; + +import javax.servlet.http.HttpServletRequest; +import java.io.UnsupportedEncodingException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.sql.Date; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author 张代浩 + * + */ +public class oConvertUtils { + public static boolean isEmpty(Object object) { + if (object == null) { + return (true); + } + if (object.equals("")) { + return (true); + } + if (object.equals("null")) { + return (true); + } + return (false); + } + + public static boolean isNotEmpty(Object object) { + if (object != null && !object.equals("") && !object.equals("null")) { + return (true); + } + return (false); + } + + public static String decode(String strIn, String sourceCode, String targetCode) { + String temp = code2code(strIn, sourceCode, targetCode); + return temp; + } + + public static String StrToUTF(String strIn, String sourceCode, String targetCode) { + strIn = ""; + try { + strIn = new String(strIn.getBytes("ISO-8859-1"), "GBK"); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return strIn; + + } + + private static String code2code(String strIn, String sourceCode, String targetCode) { + String strOut = null; + if (strIn == null || (strIn.trim()).equals("")) + return strIn; + try { + byte[] b = strIn.getBytes(sourceCode); + for (int i = 0; i < b.length; i++) { + System.out.print(b[i] + " "); + } + strOut = new String(b, targetCode); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + return strOut; + } + + public static int getInt(String s, int defval) { + if (s == null || s == "") { + return (defval); + } + try { + return (Integer.parseInt(s)); + } catch (NumberFormatException e) { + return (defval); + } + } + + public static int getInt(String s) { + if (s == null || s == "") { + return 0; + } + try { + return (Integer.parseInt(s)); + } catch (NumberFormatException e) { + return 0; + } + } + + public static int getInt(String s, Integer df) { + if (s == null || s == "") { + return df; + } + try { + return (Integer.parseInt(s)); + } catch (NumberFormatException e) { + return 0; + } + } + + public static Integer[] getInts(String[] s) { + Integer[] integer = new Integer[s.length]; + if (s == null) { + return null; + } + for (int i = 0; i < s.length; i++) { + integer[i] = Integer.parseInt(s[i]); + } + return integer; + + } + + public static double getDouble(String s, double defval) { + if (s == null || s == "") { + return (defval); + } + try { + return (Double.parseDouble(s)); + } catch (NumberFormatException e) { + return (defval); + } + } + + public static double getDou(Double s, double defval) { + if (s == null) { + return (defval); + } + return s; + } + + public static Short getShort(String s) { + if (StringUtils.isNotEmpty(s)) { + return (Short.parseShort(s)); + } else { + return null; + } + } + + public static int getInt(Object object, int defval) { + if (isEmpty(object)) { + return (defval); + } + try { + return (Integer.parseInt(object.toString())); + } catch (NumberFormatException e) { + return (defval); + } + } + + public static int getInt(BigDecimal s, int defval) { + if (s == null) { + return (defval); + } + return s.intValue(); + } + + public static Integer[] getIntegerArry(String[] object) { + int len = object.length; + Integer[] result = new Integer[len]; + try { + for (int i = 0; i < len; i++) { + result[i] = new Integer(object[i].trim()); + } + return result; + } catch (NumberFormatException e) { + return null; + } + } + + public static String getString(String s) { + return (getString(s, "")); + } + + /** + * 转义成Unicode编码 + * @param s + * @return + */ + public static String escapeJava(Object s) { + return StringEscapeUtils.escapeJava(getString(s)); + } + + public static String getString(Object object) { + if (isEmpty(object)) { + return ""; + } + return (object.toString().trim()); + } + + public static String getString(int i) { + return (String.valueOf(i)); + } + + public static String getString(float i) { + return (String.valueOf(i)); + } + + public static String getString(String s, String defval) { + if (isEmpty(s)) { + return (defval); + } + return (s.trim()); + } + + public static String getString(Object s, String defval) { + if (isEmpty(s)) { + return (defval); + } + return (s.toString().trim()); + } + + public static long stringToLong(String str) { + Long test = new Long(0); + try { + test = Long.valueOf(str); + } catch (Exception e) { + } + return test.longValue(); + } + + /** + * 获取本机IP + */ + public static String getIp() { + String ip = null; + try { + InetAddress address = InetAddress.getLocalHost(); + ip = address.getHostAddress(); + + } catch (UnknownHostException e) { + e.printStackTrace(); + } + return ip; + } + + /** + * 判断一个类是否为基本数据类型。 + * + * @param clazz + * 要判断的类。 + * @return true 表示为基本数据类型。 + */ + private static boolean isBaseDataType(Class clazz) throws Exception { + return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.isPrimitive()); + } + + /** + * @param request + * IP + * @return IP Address + */ + public static String getIpAddrByRequest(HttpServletRequest request) { + String ip = request.getHeader("x-forwarded-for"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } + + /** + * @return 本机IP + * @throws SocketException + */ + public static String getRealIp() throws SocketException { + String localip = null;// 本地IP,如果没有配置外网IP则返回它 + String netip = null;// 外网IP + + Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress ip = null; + boolean finded = false;// 是否找到外网IP + while (netInterfaces.hasMoreElements() && !finded) { + NetworkInterface ni = netInterfaces.nextElement(); + Enumeration address = ni.getInetAddresses(); + while (address.hasMoreElements()) { + ip = address.nextElement(); + if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP + netip = ip.getHostAddress(); + finded = true; + break; + } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP + localip = ip.getHostAddress(); + } + } + } + + if (netip != null && !"".equals(netip)) { + return netip; + } else { + return localip; + } + } + + /** + * java去除字符串中的空格、回车、换行符、制表符 + * + * @param str + * @return + */ + public static String replaceBlank(String str) { + String dest = ""; + if (str != null) { + Pattern p = Pattern.compile("\\s*|\t|\r|\n"); + Matcher m = p.matcher(str); + dest = m.replaceAll(""); + } + return dest; + + } + + /** + * 判断元素是否在数组内 + * + * @param substring + * @param source + * @return + */ + public static boolean isIn(String substring, String[] source) { + if (source == null || source.length == 0) { + return false; + } + for (int i = 0; i < source.length; i++) { + String aSource = source[i]; + if (aSource.equals(substring)) { + return true; + } + } + return false; + } + + /** + * 获取Map对象 + */ + public static Map getHashMap() { + return new HashMap(); + } + + /** + * SET转换MAP + * + * @param str + * @return + */ + public static Map SetToMap(Set setobj) { + Map map = getHashMap(); + for (Iterator iterator = setobj.iterator(); iterator.hasNext();) { + Map.Entry entry = (Map.Entry) iterator.next(); + map.put(entry.getKey().toString(), entry.getValue() == null ? "" : entry.getValue().toString().trim()); + } + return map; + + } + + public static boolean isInnerIP(String ipAddress) { + boolean isInnerIp = false; + long ipNum = getIpNum(ipAddress); + /** + * 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址 + **/ + long aBegin = getIpNum("10.0.0.0"); + long aEnd = getIpNum("10.255.255.255"); + long bBegin = getIpNum("172.16.0.0"); + long bEnd = getIpNum("172.31.255.255"); + long cBegin = getIpNum("192.168.0.0"); + long cEnd = getIpNum("192.168.255.255"); + isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1"); + return isInnerIp; + } + + private static long getIpNum(String ipAddress) { + String[] ip = ipAddress.split("\\."); + long a = Integer.parseInt(ip[0]); + long b = Integer.parseInt(ip[1]); + long c = Integer.parseInt(ip[2]); + long d = Integer.parseInt(ip[3]); + + long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d; + return ipNum; + } + + private static boolean isInner(long userIp, long begin, long end) { + return (userIp >= begin) && (userIp <= end); + } + + /** + * 将下划线大写方式命名的字符串转换为驼峰式。 + * 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
+ * 例如:hello_world->helloWorld + * + * @param name + * 转换前的下划线大写方式命名的字符串 + * @return 转换后的驼峰式命名的字符串 + */ + public static String camelName(String name) { + StringBuilder result = new StringBuilder(); + // 快速检查 + if (name == null || name.isEmpty()) { + // 没必要转换 + return ""; + } else if (!name.contains("_")) { + // 不含下划线,仅将首字母小写 + + return name.substring(0, 1).toLowerCase() + name.substring(1).toLowerCase(); + + } + // 用下划线将原始字符串分割 + String camels[] = name.split("_"); + for (String camel : camels) { + // 跳过原始字符串中开头、结尾的下换线或双重下划线 + if (camel.isEmpty()) { + continue; + } + // 处理真正的驼峰片段 + if (result.length() == 0) { + // 第一个驼峰片段,全部字母都小写 + result.append(camel.toLowerCase()); + } else { + // 其他的驼峰片段,首字母大写 + result.append(camel.substring(0, 1).toUpperCase()); + result.append(camel.substring(1).toLowerCase()); + } + } + return result.toString(); + } + + /** + * 将下划线大写方式命名的字符串转换为驼峰式。 + * 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
+ * 例如:hello_world,test_id->helloWorld,testId + * + * @param names + * 转换前的下划线大写方式命名的字符串 + * @return 转换后的驼峰式命名的字符串 + */ + public static String camelNames(String names) { + if(names==null||names.equals("")){ + return null; + } + StringBuffer sf = new StringBuffer(); + String[] fs = names.split(","); + for (String field : fs) { + field = camelName(field); + sf.append(field + ","); + } + String result = sf.toString(); + return result.substring(0, result.length() - 1); + } + + /** + * 将下划线大写方式命名的字符串转换为驼峰式。(首字母写) + * 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
+ * 例如:hello_world->HelloWorld + * + * @param name + * 转换前的下划线大写方式命名的字符串 + * @return 转换后的驼峰式命名的字符串 + */ + public static String camelNameCapFirst(String name) { + StringBuilder result = new StringBuilder(); + // 快速检查 + if (name == null || name.isEmpty()) { + // 没必要转换 + return ""; + } else if (!name.contains("_")) { + // 不含下划线,仅将首字母小写 + return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(); + } + // 用下划线将原始字符串分割 + String camels[] = name.split("_"); + for (String camel : camels) { + // 跳过原始字符串中开头、结尾的下换线或双重下划线 + if (camel.isEmpty()) { + continue; + } + // 其他的驼峰片段,首字母大写 + result.append(camel.substring(0, 1).toUpperCase()); + result.append(camel.substring(1).toLowerCase()); + } + return result.toString(); + } + +} diff --git a/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java b/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java index 6cfc78d..661e01b 100644 --- a/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java +++ b/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java @@ -3,10 +3,8 @@ package com.rymcu.vertical.web.api.article; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.service.ArticleService; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import com.rymcu.vertical.web.api.exception.MallApiException; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -22,9 +20,23 @@ public class ArticleController { @PostMapping("/post") public GlobalResult postArticle(@RequestParam(name = "idArticle",defaultValue = "0") Integer idArticle,@RequestParam(name = "articleTitle",defaultValue = "") String articleTitle, @RequestParam(name = "articleContent",defaultValue = "") String articleContent,@RequestParam(name = "articleContentHtml",defaultValue = "") String articleContentHtml, - @RequestParam(name = "articleTags",defaultValue = "") String articleTags, HttpServletRequest request){ + @RequestParam(name = "articleTags",defaultValue = "") String articleTags, HttpServletRequest request) throws MallApiException { Map map = articleService.postArticle(idArticle,articleTitle,articleContent,articleContentHtml,articleTags,request); return GlobalResultGenerator.genSuccessResult(map); } + @PutMapping("/post") + public GlobalResult updateArticle(@RequestParam(name = "idArticle",defaultValue = "0") Integer idArticle,@RequestParam(name = "articleTitle",defaultValue = "") String articleTitle, + @RequestParam(name = "articleContent",defaultValue = "") String articleContent,@RequestParam(name = "articleContentHtml",defaultValue = "") String articleContentHtml, + @RequestParam(name = "articleTags",defaultValue = "") String articleTags, HttpServletRequest request) throws MallApiException { + Map map = articleService.postArticle(idArticle,articleTitle,articleContent,articleContentHtml,articleTags,request); + return GlobalResultGenerator.genSuccessResult(map); + } + + @DeleteMapping("/delete/{id}") + public GlobalResult delete(@PathVariable Integer id){ + articleService.deleteById(id.toString()); + return GlobalResultGenerator.genSuccessResult(); + } + } diff --git a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java index 866981e..b6f75f7 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java @@ -34,14 +34,14 @@ public class CommonApiController { @PostMapping("/get-email-code") public GlobalResult getEmailCode(@RequestParam("email") String email) throws ServiceException { Map map = new HashMap(); - map.put("message",GlobalResultMessage.SEND_SUCCESS); + map.put("message",GlobalResultMessage.SEND_SUCCESS.getMessage()); User user = userService.findByAccount(email); if (user != null) { map.put("message","该邮箱已被注册!"); } else { Integer result = javaMailService.sendEmailCode(email); if(result == 0){ - map.put("message",GlobalResultMessage.SEND_FAIL); + map.put("message",GlobalResultMessage.SEND_FAIL.getMessage()); } } return GlobalResultGenerator.genSuccessResult(map); @@ -81,9 +81,17 @@ public class CommonApiController { - @GetMapping("/articles/{id}") + @GetMapping("/article/{id}") public GlobalResult detail(@PathVariable Integer id){ - ArticleDTO articleDTO = articleService.findArticleDTOById(id); + ArticleDTO articleDTO = articleService.findArticleDTOById(id,1); + Map map = new HashMap<>(); + map.put("article", articleDTO); + return GlobalResultGenerator.genSuccessResult(map); + } + + @GetMapping("/update/{id}") + public GlobalResult update(@PathVariable Integer id){ + ArticleDTO articleDTO = articleService.findArticleDTOById(id,2); Map map = new HashMap<>(); map.put("article", articleDTO); return GlobalResultGenerator.genSuccessResult(map); diff --git a/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java b/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java index 48d8c9e..2eb70c8 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java @@ -3,31 +3,133 @@ package com.rymcu.vertical.web.api.common; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.jwt.def.JwtConstants; +import com.rymcu.vertical.util.FileUtils; +import com.rymcu.vertical.util.Utils; import com.rymcu.vertical.web.api.exception.ErrorCode; import com.rymcu.vertical.web.api.exception.MallApiException; import org.apache.commons.lang.StringUtils; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.util.FileCopyUtils; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; @RestController @RequestMapping("/api/v1/upload") public class UploadController { + private final static String UPLOAD_URL = "/api/upload/file/batch"; + public static final String ctxHeadPicPath = "/usr/local/src/tomcat-hp/webapps/vertical"; + + @PostMapping("/file") + public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, Integer type, HttpServletRequest request){ + if (multipartFile == null) { + return GlobalResultGenerator.genErrorResult("请选择要上传的文件"); + } + String typePath = ""; + switch (type){ + case 0: + typePath = "avatar"; + break; + case 1: + typePath = "article"; + break; + case 2: + typePath = "tags"; + break; + } + //图片存储路径 + String dir = ctxHeadPicPath+"/"+typePath; + File file = new File(dir); + if (!file.exists()) { + file.mkdirs();// 创建文件根目录 + } + + String localPath = Utils.getProperty("resource.file-path")+"/"+typePath+"/"; + Map succMap = new HashMap(); + Set errFiles = new HashSet(); + + String orgName = multipartFile.getOriginalFilename(); + String fileName = System.currentTimeMillis()+"."+ FileUtils.getExtend(orgName).toLowerCase(); + + String savePath = file.getPath() + File.separator + fileName; + + File saveFile = new File(savePath); + try { + FileCopyUtils.copy(multipartFile.getBytes(), saveFile); + succMap.put(orgName,localPath+fileName); + } catch (IOException e) { + errFiles.add(orgName); + } + Map data = new HashMap(); + data.put("errFiles",errFiles); + data.put("succMap",succMap); + return GlobalResultGenerator.genSuccessResult(data); + + } + + @PostMapping("/file/batch") + public GlobalResult batchFileUpload(@RequestParam(value = "file[]", required = false)MultipartFile[] multipartFiles,@RequestParam(defaultValue = "1")Integer type,HttpServletRequest request){ + String typePath = ""; + switch (type){ + case 0: + typePath = "avatar"; + break; + case 1: + typePath = "article"; + break; + case 2: + typePath = "tags"; + break; + } + //图片存储路径 + String dir = ctxHeadPicPath+"/"+typePath; + File file = new File(dir); + if (!file.exists()) { + file.mkdirs();// 创建文件根目录 + } + + String localPath = Utils.getProperty("resource.file-path")+"/"+typePath+"/"; + Map succMap = new HashMap(); + Set errFiles = new HashSet(); + + for(int i=0,len=multipartFiles.length;i \ No newline at end of file diff --git a/src/main/resources/application-pord.yml b/src/main/resources/application-pord.yml index 5647957..5c0cef8 100644 --- a/src/main/resources/application-pord.yml +++ b/src/main/resources/application-pord.yml @@ -27,11 +27,14 @@ spring: resources: add-mappings: true mail: - host: smtp.ym.163.com #SMTP服务器地址 + host: smtp.ym.163.com + port: 465 username: service@rymcu.com password: Aa12345678 - properties: - form: service@rymcu.com + servlet: + multipart: + max-file-size: 10MB + max-request-size: 20MB wx: miniapp: configs: @@ -43,10 +46,16 @@ wx: env: dev logging: file: - path: f:/logs/vertical + path: /logs/vertical + level: + root: info server: port: 8099 compression: enabled: true mime-types: application/json,application/xml,text/html,text/xml,text/plain min-response-size: 1024 + max-http-header-size: 1024 +version: 1.0 +resource: + file-path: https://rymcu.com/vertical \ No newline at end of file