package com.xcong.farmer.cms.modules.system.controller;
|
|
import cn.hutool.core.util.IdUtil;
|
import com.xcong.farmer.cms.common.contants.AppContants;
|
import com.xcong.farmer.cms.common.response.Result;
|
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
|
import com.xcong.farmer.cms.modules.system.service.ICommonService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import javax.validation.Valid;
|
import java.io.File;
|
import java.io.IOException;
|
|
import java.util.Map;
|
|
|
@RestController
|
@RequestMapping(value = "/api/common")
|
@Slf4j
|
@Api(value = "登录", tags = "登录")
|
public class AdminCommonController {
|
|
@Resource
|
private ICommonService iCommonService;
|
|
@ApiOperation(value = "登陆接口", notes = "登陆接口")
|
@PostMapping("/login")
|
public Result login(@RequestBody @Valid AdminLoginDto adminLoginDto) {
|
return iCommonService.login(adminLoginDto);
|
}
|
|
@ApiOperation(value = "获取验证码", notes = "获取验证码")
|
@GetMapping("/captcha")
|
public Result captcha() throws IOException {
|
return iCommonService.captchaCreator();
|
}
|
|
/**
|
* 用户退出登录
|
* @return
|
*/
|
@ApiOperation(value="用户退出登录", notes="用户退出登录")
|
@PostMapping(value = "/Logout")
|
public Result memberLogout() {
|
return iCommonService.memberLogout();
|
}
|
|
private Long maxSize = 1024*1024*100L;
|
|
|
|
// /**
|
// * 文件上传方法
|
// */
|
// @ApiOperation(value = "文件上传", notes = "文件上传")
|
// @PostMapping(value = "/doUpload")
|
// public Result doFileUpload(@RequestBody @Validated AdminBase64UploadDto uploadDto){
|
// // 文件保存目录路径
|
// String savePath = AppContants.PICTURE_PATH;
|
// // 文件保存目录URL
|
// String saveUrl = resourceUrl;
|
// // 保存和访问路径检查
|
// if (StrUtil.isEmpty(saveUrl) || StrUtil.isEmpty(savePath)) {
|
// return Result.fail("文件上传失败");
|
// }
|
// // 检查目录
|
// File uploadDir = new File(savePath);
|
// if (!uploadDir.isDirectory()) {
|
// uploadDir.mkdir();
|
// }
|
// log.info("uploadDto:" + uploadDto.getBase64Str());
|
// BASE64Decoder decoder = new BASE64Decoder();
|
// byte[] bytes = new byte[0];
|
// try {
|
// bytes = decoder.decodeBuffer(uploadDto.getBase64Str());
|
// } catch (IOException e) {
|
// return Result.fail("上传文件失败");
|
// }
|
//
|
// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
// String ymd = sdf.format(new Date());
|
// savePath += ymd + "/";
|
// saveUrl += ymd + "/";
|
// File dirFile = new File(savePath);
|
// if (!dirFile.exists()) {
|
// dirFile.mkdirs();
|
// }
|
// if (bytes.length > maxSize) {
|
// return Result.fail("上传文件大小超过限制");
|
// }
|
// String newFileName = IdUtil.simpleUUID() + IdUtil.simpleUUID() + ".png";
|
// File uploadedFile = new File(savePath, newFileName);
|
// try {
|
// FileCopyUtils.copy(bytes, uploadedFile);
|
// } catch (Exception e) {
|
// return Result.fail("上传文件失败");
|
// }
|
// log.info("saveUrl:" + saveUrl);
|
// String visitPath = saveUrl + newFileName;
|
// log.info("上传一个文件:" + newFileName);
|
// log.info("访问路径:" + visitPath);
|
//
|
// return Result.ok("上传成功",visitPath);
|
// }
|
|
@Value("${static.resource.url}")
|
private String resourceUrl;
|
|
@Value("${static.resource.path}")
|
private String resourcePath;
|
|
@ApiOperation(value = "文件上传", notes = "文件上传")
|
@PostMapping("/uploadFile")
|
public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
|
|
// 文件保存目录路径
|
String savePath = resourcePath;
|
// 文件保存目录URL
|
String saveUrl = resourceUrl;
|
// 检查目录
|
File uploadDir = new File(savePath);
|
if (!uploadDir.isDirectory()) {
|
uploadDir.mkdir();
|
}
|
//获得文件的后缀
|
String filename = IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
File filepath = new File(savePath + filename);
|
try {
|
//存文件
|
file.transferTo(filepath);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
String visitPath = (saveUrl + filename);
|
return Result.ok("上传成功",visitPath);
|
}
|
|
|
|
}
|