1、文章删除功能开发

2、邮件发送功能优化
This commit is contained in:
x ronger 2019-11-21 00:46:39 +08:00
parent 6b5b42c419
commit 31ae93112a
22 changed files with 1307 additions and 49 deletions

View File

@ -28,6 +28,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
</dependency> </dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@ -177,6 +183,7 @@
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
</plugins> </plugins>
<finalName>vertical-console</finalName>
</build> </build>
</project> </project>

View File

@ -1,11 +1,14 @@
package com.rymcu.vertical.core.result; package com.rymcu.vertical.core.result;
import lombok.Getter;
@Getter
public enum GlobalResultMessage { public enum GlobalResultMessage {
SUCCESS("操作成功!"), SUCCESS("操作成功!"),
FAIL("操作失败!"), FAIL("操作失败!"),
SEND_FAIL("发送失败,请稍后再试!"), SEND_FAIL("发送失败,请稍后再试!"),
SEND_SUCCESS("发送成功"); SEND_SUCCESS("验证码已发送至邮箱");
private String message; private String message;

View File

@ -27,6 +27,8 @@ public class ArticleDTO {
private String articlePreviewContent; private String articlePreviewContent;
/** 文章内容 */ /** 文章内容 */
private String articleContent; private String articleContent;
/** 文章内容html */
private String articleContentHtml;
/** 评论总数 */ /** 评论总数 */
private Integer commentCount; private Integer commentCount;
/** 过去时长 */ /** 过去时长 */

View File

@ -40,6 +40,9 @@ public class RestAuthTokenInterceptor implements HandlerInterceptor {
//从header中得到token //从header中得到token
String authHeader = request.getHeader(JwtConstants.AUTHORIZATION); String authHeader = request.getHeader(JwtConstants.AUTHORIZATION);
if(StringUtils.isBlank(authHeader)){
authHeader = request.getHeader(JwtConstants.UPLOAD_TOKEN);
}
if (StringUtils.isBlank(authHeader)) { if (StringUtils.isBlank(authHeader)) {
throw new MallApiException(ErrorCode.UNAUTHORIZED); throw new MallApiException(ErrorCode.UNAUTHORIZED);
} }

View File

@ -8,6 +8,7 @@ public class JwtConstants {
public static final String JWT_SECRET = "JYJ5Qv2WF4lA6jPl5GKuAG"; public static final String JWT_SECRET = "JYJ5Qv2WF4lA6jPl5GKuAG";
public static final String AUTHORIZATION = "Authorization"; 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_USER_NAME = "CURRENT_TOKEN_USER_NAME";
public static final String CURRENT_TOKEN_CLAIMS = "CURRENT_TOKEN_CLAIMS"; public static final String CURRENT_TOKEN_CLAIMS = "CURRENT_TOKEN_CLAIMS";
public static final long TOKEN_EXPIRES_HOUR = 2 * 60; public static final long TOKEN_EXPIRES_HOUR = 2 * 60;

View File

@ -3,6 +3,7 @@ package com.rymcu.vertical.service;
import com.rymcu.vertical.core.service.Service; import com.rymcu.vertical.core.service.Service;
import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.ArticleDTO;
import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.Article;
import com.rymcu.vertical.web.api.exception.MallApiException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
@ -11,7 +12,7 @@ import java.util.Map;
public interface ArticleService extends Service<Article> { public interface ArticleService extends Service<Article> {
List<ArticleDTO> articles(String searchText, String tag); List<ArticleDTO> 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);
} }

View File

@ -8,7 +8,9 @@ import com.rymcu.vertical.entity.ArticleContent;
import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.mapper.ArticleMapper;
import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.service.ArticleService;
import com.rymcu.vertical.util.Html2TextUtil; import com.rymcu.vertical.util.Html2TextUtil;
import com.rymcu.vertical.util.UserUtils;
import com.rymcu.vertical.util.Utils; import com.rymcu.vertical.util.Utils;
import com.rymcu.vertical.web.api.exception.MallApiException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -39,13 +41,13 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
@Override @Override
@Transactional @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(); Map map = new HashMap();
Article article; Article article;
if(idArticle == null || idArticle == 0){ if(idArticle == null || idArticle == 0){
article = new Article(); article = new Article();
article.setArticleTitle(articleTitle); article.setArticleTitle(articleTitle);
article.setArticleAuthorId(5); article.setArticleAuthorId(UserUtils.getWxCurrentUser().getIdUser());
article.setArticleTags(articleTags); article.setArticleTags(articleTags);
article.setCreatedTime(new Date()); article.setCreatedTime(new Date());
article.setUpdatedTime(article.getCreatedTime()); article.setUpdatedTime(article.getCreatedTime());
@ -55,6 +57,10 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
articleMapper.insertArticleContent(article.getIdArticle(),articleContent,articleContentHtml); articleMapper.insertArticleContent(article.getIdArticle(),articleContent,articleContentHtml);
} else { } else {
article = articleMapper.selectByPrimaryKey(idArticle); article = articleMapper.selectByPrimaryKey(idArticle);
if(UserUtils.getWxCurrentUser().getIdUser() != article.getIdArticle()){
map.put("message","非法用户!");
return map;
}
article.setArticleTitle(articleTitle); article.setArticleTitle(articleTitle);
article.setArticleTags(articleTags); article.setArticleTags(articleTags);
if(StringUtils.isNotBlank(articleContentHtml)){ if(StringUtils.isNotBlank(articleContentHtml)){
@ -74,9 +80,9 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
} }
@Override @Override
public ArticleDTO findArticleDTOById(Integer id) { public ArticleDTO findArticleDTOById(Integer id, int type) {
ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id); ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id);
articleDTO = genArticle(articleDTO,1); articleDTO = genArticle(articleDTO,type);
return articleDTO; return articleDTO;
} }
@ -87,6 +93,9 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
if(type == 1){ if(type == 1){
ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle());
article.setArticleContent(articleContent.getArticleContentHtml()); article.setArticleContent(articleContent.getArticleContentHtml());
} else if(type == 2){
ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle());
article.setArticleContent(articleContent.getArticleContent());
} else { } else {
if(StringUtils.isBlank(article.getArticlePreviewContent())){ if(StringUtils.isBlank(article.getArticlePreviewContent())){
ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle());

View File

@ -3,11 +3,13 @@ package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.service.redis.RedisService; import com.rymcu.vertical.core.service.redis.RedisService;
import com.rymcu.vertical.service.JavaMailService; import com.rymcu.vertical.service.JavaMailService;
import com.rymcu.vertical.util.Utils; import com.rymcu.vertical.util.Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Properties;
@Service @Service
public class JavaMailServiceImpl implements JavaMailService { public class JavaMailServiceImpl implements JavaMailService {
@ -17,17 +19,40 @@ public class JavaMailServiceImpl implements JavaMailService {
@Resource @Resource
private RedisService redisService; 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 @Override
public Integer sendEmailCode(String email) { public Integer sendEmailCode(String email) {
return sendCode(email,0); return sendCode(email,0);
} }
private Integer sendCode(String to, Integer type) { 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(); Integer code = Utils.genCode();
redisService.set(to,code,5*60); redisService.set(to,code,5*60);
System.out.println(code);
if(type == 0) { if(type == 0) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("service@rymcu.com"); simpleMailMessage.setFrom(USERNAME);
simpleMailMessage.setTo(to); simpleMailMessage.setTo(to);
simpleMailMessage.setSubject("新用户注册邮箱验证"); simpleMailMessage.setSubject("新用户注册邮箱验证");
simpleMailMessage.setText("【RYMCU】您的校验码是 " + code + ",有效时间 5 分钟,请不要泄露验证码给其他人。如非本人操作,请忽略!"); simpleMailMessage.setText("【RYMCU】您的校验码是 " + code + ",有效时间 5 分钟,请不要泄露验证码给其他人。如非本人操作,请忽略!");

View File

@ -66,6 +66,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
Role role = roleMapper.selectRoleByInputCode("user"); Role role = roleMapper.selectRoleByInputCode("user");
userMapper.insertUserRole(user.getIdUser(), role.getIdRole()); userMapper.insertUserRole(user.getIdUser(), role.getIdRole());
map.put("message","注册成功!"); map.put("message","注册成功!");
map.put("flag",1);
redisService.delete(email); redisService.delete(email);
} }
} }

View File

@ -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<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
/**
* 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);
}
}
}

View File

@ -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();
}
}
}
/**
* 判断文件是否为图片<br>
* <br>
*
* @param filename
* 文件名<br>
* 判断具体文件类型<br>
* @return 检查后的结果<br>
* @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<br>
* <br>
*
* @param filename
* 文件名<br>
* 判断具体文件类型<br>
* @return 检查后的结果<br>
* @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;
}
}

View File

@ -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);
}
}

View File

@ -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];
}
}

View File

@ -0,0 +1,107 @@
/**
* Copyright &copy; 2012-2016 <a href="http://www.zkjkgc.com">zkjkgc</a> 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> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> 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=&lt=&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.");
}
}

View File

@ -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;
}
}

View File

@ -5,8 +5,8 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException; import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session; import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
import org.springframework.core.env.Environment;
import java.text.ParseException;
import java.time.*; import java.time.*;
import java.util.Date; import java.util.Date;
@ -14,6 +14,9 @@ public class Utils {
public static final String HASH_ALGORITHM = "SHA-1"; public static final String HASH_ALGORITHM = "SHA-1";
public static final int HASH_INTERATIONS = 1024; public static final int HASH_INTERATIONS = 1024;
public static final int SALT_SIZE = 8; public static final int SALT_SIZE = 8;
private static Environment env = SpringContextHolder.getBean(Environment.class);
/** /**
* 生成安全的密码生成随机的16位salt并经过1024次 sha-1 hash * 生成安全的密码生成随机的16位salt并经过1024次 sha-1 hash
*/ */
@ -62,6 +65,15 @@ public class Utils {
return code; return code;
} }
/**
* 获取配置文件内属性
* @param key 键值
* @return 属性值
* */
public static String getProperty(String key){
return env.getProperty(key);
}
public static String getTimeAgo(Date date) { public static String getTimeAgo(Date date) {
String timeAgo; String timeAgo;
@ -88,7 +100,7 @@ public class Utils {
timeAgo = hours+" 小时前 "; timeAgo = hours+" 小时前 ";
}else { }else {
int minutes = (int) ((to - from)/(1000 * 60)); int minutes = (int) ((to - from)/(1000 * 60));
if(minutes > 0){ if(minutes == 0){
timeAgo = " 刚刚 "; timeAgo = " 刚刚 ";
}else { }else {
timeAgo = minutes+" 分钟前 "; timeAgo = minutes+" 分钟前 ";

View File

@ -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<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> 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<Object, Object> getHashMap() {
return new HashMap<Object, Object>();
}
/**
* SET转换MAP
*
* @param str
* @return
*/
public static Map<Object, Object> SetToMap(Set<Object> setobj) {
Map<Object, Object> map = getHashMap();
for (Iterator iterator = setobj.iterator(); iterator.hasNext();) {
Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) 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);
/**
* 私有IPA类 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);
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式
* 如果转换前的下划线大写方式命名的字符串为空则返回空字符串</br>
* 例如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();
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式
* 如果转换前的下划线大写方式命名的字符串为空则返回空字符串</br>
* 例如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);
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式(首字母写)
* 如果转换前的下划线大写方式命名的字符串为空则返回空字符串</br>
* 例如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();
}
}

View File

@ -3,10 +3,8 @@ package com.rymcu.vertical.web.api.article;
import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.service.ArticleService;
import org.springframework.web.bind.annotation.PostMapping; import com.rymcu.vertical.web.api.exception.MallApiException;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -22,9 +20,23 @@ public class ArticleController {
@PostMapping("/post") @PostMapping("/post")
public GlobalResult postArticle(@RequestParam(name = "idArticle",defaultValue = "0") Integer idArticle,@RequestParam(name = "articleTitle",defaultValue = "") String articleTitle, 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 = "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); Map map = articleService.postArticle(idArticle,articleTitle,articleContent,articleContentHtml,articleTags,request);
return GlobalResultGenerator.genSuccessResult(map); 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();
}
} }

View File

@ -34,14 +34,14 @@ public class CommonApiController {
@PostMapping("/get-email-code") @PostMapping("/get-email-code")
public GlobalResult getEmailCode(@RequestParam("email") String email) throws ServiceException { public GlobalResult getEmailCode(@RequestParam("email") String email) throws ServiceException {
Map map = new HashMap(); Map map = new HashMap();
map.put("message",GlobalResultMessage.SEND_SUCCESS); map.put("message",GlobalResultMessage.SEND_SUCCESS.getMessage());
User user = userService.findByAccount(email); User user = userService.findByAccount(email);
if (user != null) { if (user != null) {
map.put("message","该邮箱已被注册!"); map.put("message","该邮箱已被注册!");
} else { } else {
Integer result = javaMailService.sendEmailCode(email); Integer result = javaMailService.sendEmailCode(email);
if(result == 0){ if(result == 0){
map.put("message",GlobalResultMessage.SEND_FAIL); map.put("message",GlobalResultMessage.SEND_FAIL.getMessage());
} }
} }
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(map);
@ -81,9 +81,17 @@ public class CommonApiController {
@GetMapping("/articles/{id}") @GetMapping("/article/{id}")
public GlobalResult detail(@PathVariable Integer 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 map = new HashMap<>();
map.put("article", articleDTO); map.put("article", articleDTO);
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(map);

View File

@ -3,31 +3,133 @@ package com.rymcu.vertical.web.api.common;
import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.jwt.def.JwtConstants; 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.ErrorCode;
import com.rymcu.vertical.web.api.exception.MallApiException; import com.rymcu.vertical.web.api.exception.MallApiException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
@RestController @RestController
@RequestMapping("/api/v1/upload") @RequestMapping("/api/v1/upload")
public class UploadController { 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<len;i++){
MultipartFile multipartFile = multipartFiles[i];
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);
}
@GetMapping("/token") @GetMapping("/token")
public GlobalResult uploadToken(HttpServletRequest request, @RequestParam("q") String q) throws MallApiException { public GlobalResult uploadToken(HttpServletRequest request) throws MallApiException {
/*String authHeader = request.getHeader(JwtConstants.AUTHORIZATION); String authHeader = request.getHeader(JwtConstants.AUTHORIZATION);
if(StringUtils.isBlank(authHeader)){ if(StringUtils.isBlank(authHeader)){
throw new MallApiException(ErrorCode.UNAUTHORIZED); throw new MallApiException(ErrorCode.UNAUTHORIZED);
}*/ }
Map map = new HashMap(); Map map = new HashMap();
map.put("uploadToken","authHeader.hashCode()"); map.put("uploadToken",authHeader);
map.put("uploadURL","authHeader.hashCode()"); map.put("uploadURL", UPLOAD_URL);
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(map);
} }
} }

View File

@ -66,6 +66,6 @@
select art.*,su.nickname,su.avatar_url from article art left join sys_user su on art.article_author_id = su.id where art.id = #{id} select art.*,su.nickname,su.avatar_url from article art left join sys_user su on art.article_author_id = su.id where art.id = #{id}
</select> </select>
<select id="selectArticleContent" resultMap="ArticleContentResultMap"> <select id="selectArticleContent" resultMap="ArticleContentResultMap">
select article_content_html from article_content where id_article = #{idArticle} select article_content,article_content_html from article_content where id_article = #{idArticle}
</select> </select>
</mapper> </mapper>

View File

@ -27,11 +27,14 @@ spring:
resources: resources:
add-mappings: true add-mappings: true
mail: mail:
host: smtp.ym.163.com #SMTP服务器地址 host: smtp.ym.163.com
port: 465
username: service@rymcu.com username: service@rymcu.com
password: Aa12345678 password: Aa12345678
properties: servlet:
form: service@rymcu.com multipart:
max-file-size: 10MB
max-request-size: 20MB
wx: wx:
miniapp: miniapp:
configs: configs:
@ -43,10 +46,16 @@ wx:
env: dev env: dev
logging: logging:
file: file:
path: f:/logs/vertical path: /logs/vertical
level:
root: info
server: server:
port: 8099 port: 8099
compression: compression:
enabled: true enabled: true
mime-types: application/json,application/xml,text/html,text/xml,text/plain mime-types: application/json,application/xml,text/html,text/xml,text/plain
min-response-size: 1024 min-response-size: 1024
max-http-header-size: 1024
version: 1.0
resource:
file-path: https://rymcu.com/vertical