✨ Feat: [后台]:完成头像修改接口
This commit is contained in:
parent
3ad499d283
commit
caa9c65466
@ -0,0 +1,33 @@
|
|||||||
|
package com.fanxb.bookmark.business.user.constant;
|
||||||
|
|
||||||
|
import com.fanxb.bookmark.common.constant.Constant;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类功能简述:
|
||||||
|
* 类功能详述:
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/9/9 14:30
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class FileConstant {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户头像目录
|
||||||
|
*/
|
||||||
|
public static String iconPath = Paths.get("static", "public", "icon").toString();
|
||||||
|
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void init() {
|
||||||
|
File file = Paths.get(Constant.fileSavePath, iconPath).toFile();
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -71,7 +71,7 @@ public class UserController {
|
|||||||
* @param file 头像文件
|
* @param file 头像文件
|
||||||
*/
|
*/
|
||||||
@PostMapping("/icon")
|
@PostMapping("/icon")
|
||||||
public Result pushIcon(@RequestParam("file") MultipartFile file) {
|
public Result pushIcon(@RequestParam("file") MultipartFile file) throws Exception {
|
||||||
return Result.success(userService.updateIcon(file));
|
return Result.success(userService.updateIcon(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package com.fanxb.bookmark.business.user.dao;
|
|||||||
|
|
||||||
import com.fanxb.bookmark.common.entity.User;
|
import com.fanxb.bookmark.common.entity.User;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Update;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,10 +58,21 @@ public interface UserDao {
|
|||||||
/**
|
/**
|
||||||
* Description: 根据用户id查询用户信息
|
* Description: 根据用户id查询用户信息
|
||||||
*
|
*
|
||||||
* @author fanxb
|
|
||||||
* @date 2019/7/30 16:08
|
|
||||||
* @param userId userId
|
* @param userId userId
|
||||||
* @return com.fanxb.bookmark.common.entity.User
|
* @return com.fanxb.bookmark.common.entity.User
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/7/30 16:08
|
||||||
*/
|
*/
|
||||||
User selectByUserId(int userId);
|
User selectByUserId(int userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 更新用户icon
|
||||||
|
*
|
||||||
|
* @param userId userId
|
||||||
|
* @param icon icon
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/9/9 13:57
|
||||||
|
*/
|
||||||
|
@Update("update user set icon=#{icon} where userId=#{userId}")
|
||||||
|
void updateUserIcon(@Param("userId") int userId, @Param("icon") String icon);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.fanxb.bookmark.business.user.service;
|
package com.fanxb.bookmark.business.user.service;
|
||||||
|
|
||||||
|
import com.fanxb.bookmark.business.user.constant.FileConstant;
|
||||||
import com.fanxb.bookmark.business.user.dao.UserDao;
|
import com.fanxb.bookmark.business.user.dao.UserDao;
|
||||||
import com.fanxb.bookmark.business.user.entity.LoginBody;
|
import com.fanxb.bookmark.business.user.entity.LoginBody;
|
||||||
import com.fanxb.bookmark.business.user.entity.LoginRes;
|
import com.fanxb.bookmark.business.user.entity.LoginRes;
|
||||||
import com.fanxb.bookmark.business.user.entity.RegisterBody;
|
import com.fanxb.bookmark.business.user.entity.RegisterBody;
|
||||||
import com.fanxb.bookmark.common.constant.Constant;
|
import com.fanxb.bookmark.common.constant.Constant;
|
||||||
|
import com.fanxb.bookmark.common.constant.NumberConstant;
|
||||||
import com.fanxb.bookmark.common.entity.MailInfo;
|
import com.fanxb.bookmark.common.entity.MailInfo;
|
||||||
import com.fanxb.bookmark.common.entity.User;
|
import com.fanxb.bookmark.common.entity.User;
|
||||||
import com.fanxb.bookmark.common.exception.FormDataException;
|
import com.fanxb.bookmark.common.exception.FormDataException;
|
||||||
@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -36,6 +39,11 @@ public class UserService {
|
|||||||
*/
|
*/
|
||||||
private static final long LONG_EXPIRE_TIME = 30L * TimeUtil.DAY_MS;
|
private static final long LONG_EXPIRE_TIME = 30L * TimeUtil.DAY_MS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 头像文件大小限制 单位:KB
|
||||||
|
*/
|
||||||
|
private static final int ICON_SIZE = 200;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserDao userDao;
|
private UserDao userDao;
|
||||||
|
|
||||||
@ -159,10 +167,19 @@ public class UserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改用户头像
|
* 修改用户头像
|
||||||
|
*
|
||||||
* @param file file
|
* @param file file
|
||||||
* @return 修改后的路径
|
* @return 访问路径
|
||||||
*/
|
*/
|
||||||
public String updateIcon(MultipartFile file){
|
public String updateIcon(MultipartFile file) throws Exception {
|
||||||
return "asdf";
|
if (file.getSize() / NumberConstant.K_SIZE > ICON_SIZE) {
|
||||||
|
throw new FormDataException("文件大小超过限制");
|
||||||
|
}
|
||||||
|
int userId = UserContextHolder.get().getUserId();
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
String path = Paths.get(FileConstant.iconPath, userId + fileName.substring(fileName.lastIndexOf("."))).toString();
|
||||||
|
file.transferTo(Paths.get(Constant.fileSavePath, path));
|
||||||
|
userDao.updateUserIcon(userId, path);
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,4 +54,15 @@ public class Constant {
|
|||||||
public void setJwtSecret(String jwtSecret) {
|
public void setJwtSecret(String jwtSecret) {
|
||||||
Constant.jwtSecret = jwtSecret;
|
Constant.jwtSecret = jwtSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件存储基路径
|
||||||
|
*/
|
||||||
|
public static String fileSavePath = "./";
|
||||||
|
|
||||||
|
@Value("${fileSavePath}")
|
||||||
|
public void setFileSavePath(String path) {
|
||||||
|
fileSavePath = path;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.fanxb.bookmark.common.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类功能简述:
|
||||||
|
* 类功能详述:
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2019/9/9 11:41
|
||||||
|
*/
|
||||||
|
public class NumberConstant {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2^10
|
||||||
|
*/
|
||||||
|
public static final int K_SIZE = 1024;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user