xiaoyong931011
2022-06-02 d04c117fdebce55aaa97bc0c44181948d1e2150c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.xcong.farmer.cms.modules.system.Controller;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
import com.xcong.farmer.cms.modules.system.dto.AdminBase64UploadDto;
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
import com.xcong.farmer.cms.modules.system.service.ICommonService;
import com.xcong.farmer.cms.utils.SpringContextHolder;
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.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import cn.hutool.core.util.ObjectUtil;
 
 
 
@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 AdminLoginDto adminLoginDto) {
 
        String username = adminLoginDto.getUsername();
        if(StrUtil.isEmpty(username)){
            return Result.fail("请输入用户名");
        }
        String password = adminLoginDto.getPassword();
        if(StrUtil.isEmpty(password)){
            return Result.fail("请输入密码");
        }
        return iCommonService.login(adminLoginDto);
    }
 
    /**
     * 用户退出登录
     * @return
     */
    @ApiOperation(value="用户退出登录", notes="用户退出登录")
    @GetMapping(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;
 
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
 
        // 文件保存目录路径
        String savePath = AppContants.PICTURE_PATH;
        // 文件保存目录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);
    }
 
 
 
}