Yansen 2 лет назад
Родитель
Сommit
e4b4a6f996

+ 54
- 10
src/main/java/com/example/civilizedcity/controller/CommController.java Просмотреть файл

@@ -8,10 +8,14 @@ import io.swagger.annotations.Api;
8 8
 import io.swagger.annotations.ApiOperation;
9 9
 import io.swagger.annotations.ApiParam;
10 10
 import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.beans.factory.annotation.Value;
11 12
 import org.springframework.web.bind.annotation.*;
12 13
 import org.springframework.web.multipart.MultipartFile;
13 14
 
15
+import java.io.File;
14 16
 import java.io.IOException;
17
+import java.time.LocalDateTime;
18
+import java.time.format.DateTimeFormatter;
15 19
 import java.util.HashMap;
16 20
 import java.util.Map;
17 21
 
@@ -23,6 +27,11 @@ public class CommController extends BaseController {
23 27
     @Autowired
24 28
     OSSUtils ossUtils;
25 29
 
30
+    @Value("${yz.upload.path}")
31
+    String uploadPath;
32
+    @Value("${yz.upload.prefix}")
33
+    String uploadPrefix;
34
+
26 35
     @GetMapping("/ma/captcha")
27 36
     @ApiOperation(value="获取验证码", notes = "获取验证码", httpMethod = "GET", response = ResponseBean.class)
28 37
     public ResponseBean getCaptcha(@ApiParam("手机号码") @RequestParam("phone") String phone) throws Exception {
@@ -39,16 +48,51 @@ public class CommController extends BaseController {
39 48
     @PostMapping("/{client}/file")
40 49
     public ResponseBean uploadImage(@ApiParam("客户端") @PathVariable String client,
41 50
                                     @ApiParam("文件") @RequestParam("file") MultipartFile multipartFile,
42
-                                    @ApiParam("文件类型") @RequestParam(value = "fileType", defaultValue = "image") String fileType) {
43
-        try {
44
-            String url = ossUtils.putObject(multipartFile);
45
-
46
-            Map<String, Object> resp = new HashMap<>();
47
-            resp.put("url", url);
48
-            resp.put("fileType", fileType);
49
-            return ResponseBean.success(resp);
50
-        } catch (IOException e) {
51
-            return ResponseBean.error("上传图片失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
51
+                                    @ApiParam("文件类型") @RequestParam(value = "fileType", defaultValue = "image") String fileType) throws Exception {
52
+//        try {
53
+//            String url = ossUtils.putObject(multipartFile);
54
+//
55
+//            Map<String, Object> resp = new HashMap<>();
56
+//            resp.put("url", url);
57
+//            resp.put("fileType", fileType);
58
+//            return ResponseBean.success(resp);
59
+//        } catch (IOException e) {
60
+//            return ResponseBean.error("上传图片失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
61
+//        }
62
+
63
+
64
+        //获取文件名
65
+        String fileName = multipartFile.getOriginalFilename();
66
+        String newFileName = String.format("%s/%d-%s", getMonth(), System.currentTimeMillis(), fileName);
67
+
68
+        // 上传目录以日期分类
69
+        File tmp = new File(String.format("%s/%s", getUploadPath(), getMonth()));
70
+        if (!tmp.exists()) {
71
+            tmp.mkdir();
72
+        }
73
+
74
+        //
75
+        File upFile = new File(String.format("%s/%s", getUploadPath(), newFileName));
76
+        multipartFile.transferTo(upFile);
77
+
78
+        String url = String.format("%s/%s", uploadPrefix, newFileName);
79
+        return ResponseBean.success(new HashMap<String, Object>(){{
80
+            put("url", url);
81
+        }});
82
+    }
83
+
84
+    private String getUploadPath() {
85
+        File tmp = new File(uploadPath);
86
+        if (!tmp.exists()) {
87
+            tmp.mkdir();
52 88
         }
89
+
90
+        return uploadPath;
91
+    }
92
+
93
+    private String getMonth() {
94
+        LocalDateTime now = LocalDateTime.now();
95
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
96
+        return now.format(formatter);
53 97
     }
54 98
 }

+ 49
- 25
src/main/java/com/example/civilizedcity/controller/TaNoticeController.java Просмотреть файл

@@ -4,8 +4,13 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6 6
 import com.example.civilizedcity.common.BaseController;
7
+import com.example.civilizedcity.common.Constants;
7 8
 import com.example.civilizedcity.common.ResponseBean;
9
+
10
+import java.time.LocalDateTime;
8 11
 import java.util.List;
12
+
13
+import com.example.civilizedcity.common.StringUtils;
9 14
 import io.swagger.annotations.Api;
10 15
 import io.swagger.annotations.ApiOperation;
11 16
 import io.swagger.annotations.ApiParam;
@@ -14,8 +19,9 @@ import org.springframework.web.bind.annotation.*;
14 19
 import com.example.civilizedcity.entity.TaNotice;
15 20
 import com.example.civilizedcity.service.TaNoticeService;
16 21
 
17
- /**
22
+/**
18 23
  * 通知公告;(ta_notice)表控制层
24
+ *
19 25
  * @author : http://njyunzhi.com
20 26
  * @date : 2022-12-12
21 27
  */
@@ -23,14 +29,14 @@ import com.example.civilizedcity.service.TaNoticeService;
23 29
 @RestController
24 30
 @RequestMapping("/")
25 31
 public class TaNoticeController extends BaseController {
26
-    
32
+
27 33
     @Autowired
28 34
     private TaNoticeService taNoticeService;
29
-    
30
-    /** 
31
-     * 通过ID查询单条数据 
35
+
36
+    /**
37
+     * 通过ID查询单条数据
32 38
      *
33
-     * @param noticeId 主键
39
+     * @param id 主键
34 40
      * @return 实例对象
35 41
      */
36 42
     @ApiOperation("通过ID查询单条数据")
@@ -38,28 +44,32 @@ public class TaNoticeController extends BaseController {
38 44
     public ResponseBean queryById(@ApiParam("对象ID") @PathVariable String id) throws Exception {
39 45
         return ResponseBean.success(taNoticeService.getById(id));
40 46
     }
41
-    
42
-    /** 
47
+
48
+    /**
43 49
      * 分页查询
44 50
      *
45
-     * @param pageNum 当前页码
51
+     * @param pageNum  当前页码
46 52
      * @param pageSize 每页条数
47 53
      * @return 查询结果
48 54
      */
49 55
     @ApiOperation("分页查询")
50 56
     @GetMapping("/taNotice")
51
-    public ResponseBean list(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
52
-                            @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception {
53
-        
57
+    public ResponseBean list(@ApiParam("页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
58
+                             @ApiParam("单页数据量") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
59
+                             @ApiParam("发布状态") @RequestParam(value = "status", required = false) Integer status) throws Exception {
60
+
54 61
         IPage<TaNotice> pg = new Page<>(pageNum, pageSize);
55
-        // QueryWrapper<TaNotice> queryWrapper = new QueryWrapper<>();
56
-        // queryWrapper.orderByDesc("create_date");
57
-        IPage<TaNotice> result = taNoticeService.page(pg);
58
-        
62
+        QueryWrapper<TaNotice> queryWrapper = new QueryWrapper<>();
63
+        queryWrapper.eq(null != status, "status", status);
64
+        queryWrapper.gt("status", Constants.STATUS_DELETE);
65
+        queryWrapper.orderByDesc("weight");
66
+        queryWrapper.orderByDesc("create_date");
67
+        IPage<TaNotice> result = taNoticeService.page(pg, queryWrapper);
68
+
59 69
         return ResponseBean.success(result);
60 70
     }
61
-    
62
-    /** 
71
+
72
+    /**
63 73
      * 新增数据
64 74
      *
65 75
      * @param taNotice 实例对象
@@ -68,11 +78,19 @@ public class TaNoticeController extends BaseController {
68 78
     @ApiOperation("新增数据")
69 79
     @PostMapping("/taNotice")
70 80
     public ResponseBean add(@ApiParam("对象实体") @RequestBody TaNotice taNotice) throws Exception {
81
+        taNotice.setNoticeId(null);
82
+
83
+        if (StringUtils.isEmpty(taNotice.getTitle())) {
84
+            return ResponseBean.error("公告标题不能为空");
85
+        }
86
+
87
+        taNotice.setCreateDate(LocalDateTime.now());
88
+
71 89
         taNoticeService.save(taNotice);
72 90
         return ResponseBean.success(taNotice);
73 91
     }
74
-    
75
-    /** 
92
+
93
+    /**
76 94
      * 更新数据
77 95
      *
78 96
      * @param taNotice 实例对象
@@ -81,20 +99,26 @@ public class TaNoticeController extends BaseController {
81 99
     @ApiOperation("更新数据")
82 100
     @PutMapping("/taNotice/{id}")
83 101
     public ResponseBean edit(@ApiParam("对象实体") @RequestBody TaNotice taNotice,
84
-                            @ApiParam("对象ID") @PathVariable String id ) throws Exception {
102
+                             @ApiParam("对象ID") @PathVariable String id) throws Exception {
103
+        taNotice.setNoticeId(id);
104
+
105
+        if (StringUtils.isEmpty(taNotice.getTitle())) {
106
+            return ResponseBean.error("公告标题不能为空");
107
+        }
108
+
85 109
         taNoticeService.updateById(taNotice);
86 110
         return ResponseBean.success(taNotice);
87 111
     }
88
-    
89
-    /** 
112
+
113
+    /**
90 114
      * 通过主键删除数据
91 115
      *
92
-     * @param noticeId 主键
116
+     * @param id 主键
93 117
      * @return 是否成功
94 118
      */
95 119
     @ApiOperation("通过主键删除数据")
96 120
     @DeleteMapping("/taNotice/{id}")
97
-    public ResponseBean deleteById(@ApiParam("对象ID") @PathVariable String id){
121
+    public ResponseBean deleteById(@ApiParam("对象ID") @PathVariable String id) {
98 122
         taNoticeService.removeLogicById(id);
99 123
         return ResponseBean.success("success");
100 124
     }

+ 1
- 1
src/main/java/com/example/civilizedcity/entity/TaNotice.java Просмотреть файл

@@ -25,7 +25,7 @@ import lombok.experimental.Accessors;
25 25
 public class TaNotice implements Serializable,Cloneable{
26 26
     /** 通知ID */
27 27
     @ApiModelProperty(name = "通知ID",notes = "")
28
-    @TableId(value = "notice_id", type = IdType.INPUT)
28
+    @TableId(value = "notice_id", type = IdType.ASSIGN_UUID)
29 29
     private String noticeId ;
30 30
     /** 标题 */
31 31
     @ApiModelProperty(name = "标题",notes = "")

+ 3
- 0
src/main/resources/application.yml Просмотреть файл

@@ -20,6 +20,9 @@ spring:
20 20
 
21 21
 ###
22 22
 yz:
23
+  upload:
24
+    path: E:\work\public-upload
25
+    prefix: http://127.0.0.1:8000
23 26
   filter:
24 27
     annList:
25 28
       - "/swagger-ui/**"