Yansen 2 anos atrás
pai
commit
e4b4a6f996

+ 54
- 10
src/main/java/com/example/civilizedcity/controller/CommController.java Ver arquivo

8
 import io.swagger.annotations.ApiOperation;
8
 import io.swagger.annotations.ApiOperation;
9
 import io.swagger.annotations.ApiParam;
9
 import io.swagger.annotations.ApiParam;
10
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.beans.factory.annotation.Value;
11
 import org.springframework.web.bind.annotation.*;
12
 import org.springframework.web.bind.annotation.*;
12
 import org.springframework.web.multipart.MultipartFile;
13
 import org.springframework.web.multipart.MultipartFile;
13
 
14
 
15
+import java.io.File;
14
 import java.io.IOException;
16
 import java.io.IOException;
17
+import java.time.LocalDateTime;
18
+import java.time.format.DateTimeFormatter;
15
 import java.util.HashMap;
19
 import java.util.HashMap;
16
 import java.util.Map;
20
 import java.util.Map;
17
 
21
 
23
     @Autowired
27
     @Autowired
24
     OSSUtils ossUtils;
28
     OSSUtils ossUtils;
25
 
29
 
30
+    @Value("${yz.upload.path}")
31
+    String uploadPath;
32
+    @Value("${yz.upload.prefix}")
33
+    String uploadPrefix;
34
+
26
     @GetMapping("/ma/captcha")
35
     @GetMapping("/ma/captcha")
27
     @ApiOperation(value="获取验证码", notes = "获取验证码", httpMethod = "GET", response = ResponseBean.class)
36
     @ApiOperation(value="获取验证码", notes = "获取验证码", httpMethod = "GET", response = ResponseBean.class)
28
     public ResponseBean getCaptcha(@ApiParam("手机号码") @RequestParam("phone") String phone) throws Exception {
37
     public ResponseBean getCaptcha(@ApiParam("手机号码") @RequestParam("phone") String phone) throws Exception {
39
     @PostMapping("/{client}/file")
48
     @PostMapping("/{client}/file")
40
     public ResponseBean uploadImage(@ApiParam("客户端") @PathVariable String client,
49
     public ResponseBean uploadImage(@ApiParam("客户端") @PathVariable String client,
41
                                     @ApiParam("文件") @RequestParam("file") MultipartFile multipartFile,
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 Ver arquivo

4
 import com.baomidou.mybatisplus.core.metadata.IPage;
4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6
 import com.example.civilizedcity.common.BaseController;
6
 import com.example.civilizedcity.common.BaseController;
7
+import com.example.civilizedcity.common.Constants;
7
 import com.example.civilizedcity.common.ResponseBean;
8
 import com.example.civilizedcity.common.ResponseBean;
9
+
10
+import java.time.LocalDateTime;
8
 import java.util.List;
11
 import java.util.List;
12
+
13
+import com.example.civilizedcity.common.StringUtils;
9
 import io.swagger.annotations.Api;
14
 import io.swagger.annotations.Api;
10
 import io.swagger.annotations.ApiOperation;
15
 import io.swagger.annotations.ApiOperation;
11
 import io.swagger.annotations.ApiParam;
16
 import io.swagger.annotations.ApiParam;
14
 import com.example.civilizedcity.entity.TaNotice;
19
 import com.example.civilizedcity.entity.TaNotice;
15
 import com.example.civilizedcity.service.TaNoticeService;
20
 import com.example.civilizedcity.service.TaNoticeService;
16
 
21
 
17
- /**
22
+/**
18
  * 通知公告;(ta_notice)表控制层
23
  * 通知公告;(ta_notice)表控制层
24
+ *
19
  * @author : http://njyunzhi.com
25
  * @author : http://njyunzhi.com
20
  * @date : 2022-12-12
26
  * @date : 2022-12-12
21
  */
27
  */
23
 @RestController
29
 @RestController
24
 @RequestMapping("/")
30
 @RequestMapping("/")
25
 public class TaNoticeController extends BaseController {
31
 public class TaNoticeController extends BaseController {
26
-    
32
+
27
     @Autowired
33
     @Autowired
28
     private TaNoticeService taNoticeService;
34
     private TaNoticeService taNoticeService;
29
-    
30
-    /** 
31
-     * 通过ID查询单条数据 
35
+
36
+    /**
37
+     * 通过ID查询单条数据
32
      *
38
      *
33
-     * @param noticeId 主键
39
+     * @param id 主键
34
      * @return 实例对象
40
      * @return 实例对象
35
      */
41
      */
36
     @ApiOperation("通过ID查询单条数据")
42
     @ApiOperation("通过ID查询单条数据")
38
     public ResponseBean queryById(@ApiParam("对象ID") @PathVariable String id) throws Exception {
44
     public ResponseBean queryById(@ApiParam("对象ID") @PathVariable String id) throws Exception {
39
         return ResponseBean.success(taNoticeService.getById(id));
45
         return ResponseBean.success(taNoticeService.getById(id));
40
     }
46
     }
41
-    
42
-    /** 
47
+
48
+    /**
43
      * 分页查询
49
      * 分页查询
44
      *
50
      *
45
-     * @param pageNum 当前页码
51
+     * @param pageNum  当前页码
46
      * @param pageSize 每页条数
52
      * @param pageSize 每页条数
47
      * @return 查询结果
53
      * @return 查询结果
48
      */
54
      */
49
     @ApiOperation("分页查询")
55
     @ApiOperation("分页查询")
50
     @GetMapping("/taNotice")
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
         IPage<TaNotice> pg = new Page<>(pageNum, pageSize);
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
         return ResponseBean.success(result);
69
         return ResponseBean.success(result);
60
     }
70
     }
61
-    
62
-    /** 
71
+
72
+    /**
63
      * 新增数据
73
      * 新增数据
64
      *
74
      *
65
      * @param taNotice 实例对象
75
      * @param taNotice 实例对象
68
     @ApiOperation("新增数据")
78
     @ApiOperation("新增数据")
69
     @PostMapping("/taNotice")
79
     @PostMapping("/taNotice")
70
     public ResponseBean add(@ApiParam("对象实体") @RequestBody TaNotice taNotice) throws Exception {
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
         taNoticeService.save(taNotice);
89
         taNoticeService.save(taNotice);
72
         return ResponseBean.success(taNotice);
90
         return ResponseBean.success(taNotice);
73
     }
91
     }
74
-    
75
-    /** 
92
+
93
+    /**
76
      * 更新数据
94
      * 更新数据
77
      *
95
      *
78
      * @param taNotice 实例对象
96
      * @param taNotice 实例对象
81
     @ApiOperation("更新数据")
99
     @ApiOperation("更新数据")
82
     @PutMapping("/taNotice/{id}")
100
     @PutMapping("/taNotice/{id}")
83
     public ResponseBean edit(@ApiParam("对象实体") @RequestBody TaNotice taNotice,
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
         taNoticeService.updateById(taNotice);
109
         taNoticeService.updateById(taNotice);
86
         return ResponseBean.success(taNotice);
110
         return ResponseBean.success(taNotice);
87
     }
111
     }
88
-    
89
-    /** 
112
+
113
+    /**
90
      * 通过主键删除数据
114
      * 通过主键删除数据
91
      *
115
      *
92
-     * @param noticeId 主键
116
+     * @param id 主键
93
      * @return 是否成功
117
      * @return 是否成功
94
      */
118
      */
95
     @ApiOperation("通过主键删除数据")
119
     @ApiOperation("通过主键删除数据")
96
     @DeleteMapping("/taNotice/{id}")
120
     @DeleteMapping("/taNotice/{id}")
97
-    public ResponseBean deleteById(@ApiParam("对象ID") @PathVariable String id){
121
+    public ResponseBean deleteById(@ApiParam("对象ID") @PathVariable String id) {
98
         taNoticeService.removeLogicById(id);
122
         taNoticeService.removeLogicById(id);
99
         return ResponseBean.success("success");
123
         return ResponseBean.success("success");
100
     }
124
     }

+ 1
- 1
src/main/java/com/example/civilizedcity/entity/TaNotice.java Ver arquivo

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

+ 3
- 0
src/main/resources/application.yml Ver arquivo

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