修改--修改部分文件上传方式, 修改更新个人信息接口
This commit is contained in:
parent
b9c268abaf
commit
2ebfeef737
@ -7,6 +7,7 @@ import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.common.utils.*;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
import com.ruoyi.common.utils.ip.IpUtils;
|
||||
import com.ruoyi.common.utils.uuid.IdUtils;
|
||||
import com.ruoyi.common.utils.uuid.UUID;
|
||||
@ -332,38 +333,61 @@ public class AppSystemController extends BaseController {
|
||||
public AjaxResult uploadAvatar(@RequestParam("user_name") String userName,
|
||||
@RequestParam("avatarImage") MultipartFile avatarImage)
|
||||
{
|
||||
//权限验证
|
||||
// SysUser user = SecurityUtils.getLoginUser().getUser();
|
||||
|
||||
// String fileName = FileUploadUtils.upload(RuoYiConfig.getProfile(), file);
|
||||
//获取本地图像保存路径
|
||||
String localPath = RuoYiConfig.getProfile();
|
||||
// String localPath = RuoYiConfig.getProfile();
|
||||
//头像图片
|
||||
// MultipartFile avatarImage = uploadAvatarDto.getAvatarImage();
|
||||
if (avatarImage.isEmpty()) {
|
||||
return error("文件为空");
|
||||
}
|
||||
// 验证文件类型(可选,这里只允许jpg,jpeg和png格式)
|
||||
String fileType = avatarImage.getContentType().split("/")[1].toLowerCase();
|
||||
if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) {
|
||||
return error("只支持jpg和png格式的图片");
|
||||
//------------------------------------------------新版文件上传-------------------------------------------------
|
||||
String fileName = "";
|
||||
try{
|
||||
fileName = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/avatar", avatarImage);
|
||||
}
|
||||
catch (Exception e){
|
||||
return error("头像文件上传失败");
|
||||
}
|
||||
LoginUser loginUser = getLoginUser();
|
||||
boolean flag = sysUserService.updateUserAvatar(loginUser.getUsername(), fileName);
|
||||
if (flag){
|
||||
AjaxResult ajax = success();
|
||||
ajax.put("上传头像地址", fileName);
|
||||
return ajax;// 可以返回文件的相对路径或URL给前端
|
||||
}
|
||||
else{
|
||||
return error("修改头像失败");
|
||||
}
|
||||
String filePath = localPath + "/avatar";
|
||||
try {
|
||||
// 保存文件到服务器
|
||||
avatarImage.transferTo(new File(filePath, userName+"_avatar.jpg"));
|
||||
boolean flag = sysUserService.updateUserAvatar(userName,filePath+"/"+userName+"_avatar.jpg");
|
||||
if (flag){
|
||||
AjaxResult ajax = success();
|
||||
ajax.put("上传头像地址", filePath+"/"+userName+"_avatar.jpg");
|
||||
return ajax;// 可以返回文件的相对路径或URL给前端
|
||||
}
|
||||
else{
|
||||
return error("修改头像失败");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return error("文件上传失败");
|
||||
}
|
||||
//-----------------------------------------------新版文件上传end-----------------------------------------------
|
||||
|
||||
|
||||
// //--------------------------------------------旧版文件上传--------------------------------------------------
|
||||
// // 验证文件类型(可选,这里只允许jpg,jpeg和png格式)
|
||||
// String fileType = avatarImage.getContentType().split("/")[1].toLowerCase();
|
||||
// if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) {
|
||||
// return error("只支持jpg和png格式的图片");
|
||||
// }
|
||||
// String filePath = localPath + "/avatar";
|
||||
// try {
|
||||
// // 保存文件到服务器
|
||||
// avatarImage.transferTo(new File(filePath, userName+"_avatar.jpg"));
|
||||
// boolean flag = sysUserService.updateUserAvatar(userName,filePath+"/"+userName+"_avatar.jpg");
|
||||
// if (flag){
|
||||
// AjaxResult ajax = success();
|
||||
// ajax.put("上传头像地址", filePath+"/"+userName+"_avatar.jpg");
|
||||
// return ajax;// 可以返回文件的相对路径或URL给前端
|
||||
// }
|
||||
// else{
|
||||
// return error("修改头像失败");
|
||||
// }
|
||||
//
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// return error("文件上传失败");
|
||||
// }
|
||||
// //--------------------------------------------旧版文件上传失败--------------------------------------------------------------
|
||||
}
|
||||
//测试局部方法
|
||||
@GetMapping("/test")
|
||||
@ -407,9 +431,9 @@ public class AppSystemController extends BaseController {
|
||||
String userName = realNameAuthenticationDto.getUser_name();
|
||||
|
||||
String username = SecurityUtils.getUsername();
|
||||
// if (!username.equals(userName)){
|
||||
// return error("token和用户名不符");
|
||||
// }
|
||||
if (!username.equals(userName)){
|
||||
return error("token和用户名不符");
|
||||
}
|
||||
|
||||
// 正式时这里会有登录认证,所以不存在未注册用户
|
||||
//-----------------------先判断是否实名认证过-------没必要--------------------
|
||||
@ -419,6 +443,19 @@ public class AppSystemController extends BaseController {
|
||||
if (cardFront.isEmpty() || cardBack.isEmpty()) {
|
||||
return error("文件为空");
|
||||
}
|
||||
// //---------------------------------------------------新版文件上传--------------------------------------------------
|
||||
// String cardFrontPath = "";
|
||||
// String cardBackPath = "";
|
||||
// try{
|
||||
// cardFrontPath = FileUploadUtils.upload(RuoYiConfig.getIdCardPath()+"/avatar", cardFront);
|
||||
// cardBackPath = FileUploadUtils.upload(RuoYiConfig.getIdCardPath()+"/avatar", cardBack);
|
||||
// }
|
||||
// catch (Exception e){
|
||||
// return error("身份证图片文件上传失败");
|
||||
// }
|
||||
// //---------------------------------------------------新版文件上传end--------------------------------------------------
|
||||
|
||||
//--------------------------------------------------------旧版文件上传-------------------------------------------------------------
|
||||
// 验证文件类型(可选,这里只允许jpg,jpeg和png格式)
|
||||
String fileType1 = cardFront.getContentType().split("/")[1].toLowerCase();
|
||||
if (!"jpg".equals(fileType1) && !"jpeg".equals(fileType1) && !"png".equals(fileType1)) {
|
||||
@ -435,7 +472,7 @@ public class AppSystemController extends BaseController {
|
||||
// String filePath = localPath + "/id_card";
|
||||
|
||||
String localPathId = RuoYiConfig.getIdCardPath();
|
||||
String filePath = localPathId + "/id_card";
|
||||
String filePath = localPathId;
|
||||
|
||||
|
||||
try {
|
||||
@ -452,6 +489,7 @@ public class AppSystemController extends BaseController {
|
||||
e.printStackTrace();
|
||||
return error("身份证人像照上传失败");
|
||||
}
|
||||
//-------------------------------------------------------------旧版文件上传end-------------------------------------------------------------
|
||||
|
||||
//验证上传身份证
|
||||
OcrIdCardBAI ocrIdCardBAI= new OcrIdCardBAI();
|
||||
@ -510,8 +548,23 @@ public class AppSystemController extends BaseController {
|
||||
|
||||
}
|
||||
|
||||
// return error("实名认证失败,请再次重试");
|
||||
}
|
||||
|
||||
// 测试文件上传
|
||||
@PostMapping("/upload")
|
||||
public AjaxResult upload(@RequestParam("file") MultipartFile file) {
|
||||
try {
|
||||
// 保存文件到配置的路径
|
||||
String fileName = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/work", file);
|
||||
// 返回访问URL(如:http://域名/profile/文件名)
|
||||
String url = fileName;
|
||||
return AjaxResult.success("上传成功", url);
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 获取用户个人信息
|
||||
@GetMapping("/getUserInfo/{phone}")
|
||||
public AjaxResult getUserInfo(@PathVariable String phone)
|
||||
@ -571,13 +624,12 @@ public class AppSystemController extends BaseController {
|
||||
return ajax;
|
||||
}
|
||||
|
||||
// 更新个人信息
|
||||
// 设置--更新个人信息
|
||||
@Transactional
|
||||
@PostMapping("/updateNickSign")
|
||||
public AjaxResult updateUserInfo(@RequestParam("updateNickSignDto") String updateNickSignDt, @RequestParam("avatar") MultipartFile avatarImage)
|
||||
{
|
||||
UpdateNickSignDto updateNickSignDto = GsonUtils.fromJson(updateNickSignDt, UpdateNickSignDto.class);
|
||||
String userName = updateNickSignDto.getUsername();
|
||||
String nickName = updateNickSignDto.getNickname();
|
||||
String signature = updateNickSignDto.getSignature();
|
||||
|
||||
@ -585,55 +637,97 @@ public class AppSystemController extends BaseController {
|
||||
|
||||
System.out.println("头像图片是否为空: "+avatarImage.isEmpty());
|
||||
|
||||
// if(!username.equals(userName)){
|
||||
// return error("请不要操作不是自己的账号");
|
||||
// }
|
||||
// 和以前一样就不用修改了
|
||||
// 新的昵称不为空
|
||||
if(!StringUtils.isEmpty(nickName)){
|
||||
SysUser sysUser = sysUserService.selectUserByUserName(username);
|
||||
if (!nickName.equals(sysUser.getNickName())){
|
||||
boolean f1 = sysUserService.updateNickName(username,nickName);
|
||||
if(!f1){
|
||||
return error("更新昵称失败");
|
||||
}
|
||||
}
|
||||
|
||||
// System.out.println(username+nickName+signature);
|
||||
boolean f1 = sysUserService.updateNickName(username,nickName);
|
||||
boolean f2 = userInfoService.updateSignature(username, signature);
|
||||
|
||||
if(f1==false || f2==false){
|
||||
return error("更新昵称和介绍失败");
|
||||
}
|
||||
// 新的签名不为空
|
||||
if(!StringUtils.isEmpty(signature)){
|
||||
UserInfo userInfo = userInfoService.selectUserInfoByUserId(getUserId());
|
||||
if(!signature.equals(userInfo.getSignature())){
|
||||
boolean f2 = userInfoService.updateSignature(username, signature);
|
||||
if(!f2){
|
||||
return error("更新签名失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String localPath = RuoYiConfig.getProfile();
|
||||
|
||||
|
||||
|
||||
//头像图片---可以为空
|
||||
if (!avatarImage.isEmpty()) {
|
||||
// String mimeType = avatarImage.getContentType();
|
||||
// System.out.println(mimeType);
|
||||
// 验证文件类型(可选,这里只允许jpg,jpeg和png格式)
|
||||
String fileType = avatarImage.getContentType().split("/")[1].toLowerCase();
|
||||
// System.out.println(avatarImage.getContentType());
|
||||
System.out.println(fileType);
|
||||
if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) {
|
||||
return error("只支持jpg、jpeg和png格式的图片");
|
||||
}
|
||||
String filePath = localPath + "/avatar";
|
||||
if (!avatarImage.isEmpty()){
|
||||
//-----------------------------------------新版文件上传------------------------------------------------------
|
||||
String avatarFilePath = "";
|
||||
try {
|
||||
// 保存文件到服务器
|
||||
avatarImage.transferTo(new File(filePath, username+"_avatar.jpg"));
|
||||
boolean flag = sysUserService.updateUserAvatar(username,filePath+"/"+username+"_avatar.jpg");
|
||||
if (flag){
|
||||
AjaxResult ajax = success();
|
||||
ajax.put("上传头像地址", filePath+"/"+username+"_avatar.jpg");
|
||||
return ajax;// 可以返回文件的相对路径或URL给前端
|
||||
}
|
||||
else{
|
||||
return error("修改头像失败");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return error("头像文件上传失败");
|
||||
// 保存文件到配置的路径
|
||||
avatarFilePath = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/avatar", avatarImage);
|
||||
} catch (Exception e) {
|
||||
return error("更新头像文件失败");
|
||||
}
|
||||
|
||||
boolean flag = sysUserService.updateUserAvatar(username,avatarFilePath);
|
||||
if (flag){
|
||||
AjaxResult ajax = success();
|
||||
ajax.put("上传头像地址", avatarFilePath);
|
||||
return ajax;// 可以返回文件的相对路径或URL给前端
|
||||
}
|
||||
else{
|
||||
return error("修改头像失败");
|
||||
}
|
||||
//-----------------------------------------新版文件上传---------------------------------------------------
|
||||
}
|
||||
else{
|
||||
return success("更新昵称和介绍成功");
|
||||
}
|
||||
|
||||
return success("修改信息成功");
|
||||
|
||||
|
||||
|
||||
|
||||
// //--------------------------------------旧版文件上传--------------------------------------------------------
|
||||
//
|
||||
// String localPath = RuoYiConfig.getProfile();
|
||||
//
|
||||
//
|
||||
//
|
||||
// //头像图片---可以为空
|
||||
// if (!avatarImage.isEmpty()) {
|
||||
// // String mimeType = avatarImage.getContentType();
|
||||
//// System.out.println(mimeType);
|
||||
// // 验证文件类型(可选,这里只允许jpg,jpeg和png格式)
|
||||
// String fileType = avatarImage.getContentType().split("/")[1].toLowerCase();
|
||||
//// System.out.println(avatarImage.getContentType());
|
||||
// System.out.println(fileType);
|
||||
// if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) {
|
||||
// return error("只支持jpg、jpeg和png格式的图片");
|
||||
// }
|
||||
// String filePath = localPath + "/avatar";
|
||||
// try {
|
||||
// // 保存文件到服务器
|
||||
// avatarImage.transferTo(new File(filePath, username+"_avatar.jpg"));
|
||||
// boolean flag = sysUserService.updateUserAvatar(username,filePath+"/"+username+"_avatar.jpg");
|
||||
// if (flag){
|
||||
// AjaxResult ajax = success();
|
||||
// ajax.put("上传头像地址", filePath+"/"+username+"_avatar.jpg");
|
||||
// return ajax;// 可以返回文件的相对路径或URL给前端
|
||||
// }
|
||||
// else{
|
||||
// return error("修改头像失败");
|
||||
// }
|
||||
//
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// return error("头像文件上传失败");
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// return success("修改信息成功");
|
||||
// //-------------------------------------------------旧版文件上传end----------------------------------------------------------------
|
||||
|
||||
}
|
||||
@PostMapping("/createWork")
|
||||
|
||||
@ -18,7 +18,7 @@ ruoyi:
|
||||
|
||||
|
||||
|
||||
# idCardPath: /home/idCard
|
||||
# idCardPath: /home/IdCard
|
||||
# profile: /home/uploadPath
|
||||
# 获取ip地址开关
|
||||
addressEnabled: false
|
||||
|
||||
@ -113,6 +113,7 @@ public class FileUploadUtils
|
||||
String fileName = extractFilename(file);
|
||||
|
||||
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
||||
|
||||
file.transferTo(Paths.get(absPath));
|
||||
return getPathFileName(baseDir, fileName);
|
||||
}
|
||||
@ -144,7 +145,13 @@ public class FileUploadUtils
|
||||
{
|
||||
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
|
||||
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
|
||||
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
|
||||
|
||||
// currentDir若是为空,就会有 // 两根斜杠
|
||||
if (StringUtils.isNotBlank(currentDir))
|
||||
{
|
||||
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
|
||||
}
|
||||
return Constants.RESOURCE_PREFIX + "/" + fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user