Browse Source

南岸纠错提交

wrh 2 years ago
parent
commit
e380c52a9a

+ 28 - 2
nngkxxdp/src/main/java/com/example/nngkxxdp/controller/RectificationController.java

@@ -7,7 +7,10 @@ import com.example.nngkxxdp.util.ConstStr;
 import com.example.nngkxxdp.util.SendUtil;
 import lombok.AllArgsConstructor;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
+import javax.servlet.http.HttpSession;
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -30,6 +33,29 @@ public class RectificationController {
         }
         return rectificationService.info(id);
     }
+
+    @PostMapping("/front/submit")
+    public Map<String,Object> submit(RectificationDO rectificationDO, MultipartFile file, HttpSession session) throws IOException {
+        String [] o={"captcha","errorUrl","equipment","mail","phone", "submitter", "questionDescription", "questionType", "uniqueCode"};
+        if (!Blank.checkObjectParamNotNull(rectificationDO,o)){
+            return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
+        }
+        if (Blank.isEmpty(file)) {
+            return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
+        }
+        if (!rectificationDO.getUniqueCode().equals(ConstStr.UNIQUE_CODE)) {
+            return SendUtil.send(false,ConstStr.REQUEST_WRONGPARAMS);
+        }
+        String captcha= (String) session.getAttribute("captcha");
+        if (Blank.isNotEmpty(captcha)){
+            if (!captcha.equalsIgnoreCase(rectificationDO.getCaptcha())){
+                return SendUtil.send(false,"验证码错误");
+            }
+        } else {
+            return SendUtil.send(false,"验证码为空");
+        }
+        return rectificationService.add(rectificationDO, file);
+    }
     
     @GetMapping("/list")
     public Map<String,Object> list() {
@@ -56,9 +82,9 @@ public class RectificationController {
     }
     
     @PostMapping("/add")
-    public Map<String,Object> add(@RequestBody RectificationDO rectificationDO) {
+    public Map<String,Object> add(@RequestBody RectificationDO rectificationDO) throws IOException {
         // TODO 校验参数
-        return rectificationService.add(rectificationDO);
+        return rectificationService.add(rectificationDO, null);
     }
     
     @PostMapping("/update")

+ 10 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/entity/dos/RectificationDO.java

@@ -84,5 +84,15 @@ public class RectificationDO implements Serializable {
     * 是否删除【0:未删除 1:删除】
     */
     private Integer isdel;
+
+    /**
+     * 验证码
+     */
+    private String captcha;
+
+    /**
+     * 唯一码
+     */
+    private String uniqueCode;
     
 }

+ 5 - 2
nngkxxdp/src/main/java/com/example/nngkxxdp/service/RectificationService.java

@@ -1,7 +1,9 @@
 package com.example.nngkxxdp.service;
 
 import com.example.nngkxxdp.entity.dos.RectificationDO;
-import com.example.nngkxxdp.entity.vos.RectificationVO;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -55,9 +57,10 @@ public interface RectificationService {
      * @author zwq
      * @date 2023/01/30 10:44
      * @param rectificationDO 实例对象
+     * @param file 文件对象
      * @return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
-    Map<String, Object> add(RectificationDO rectificationDO);
+    Map<String, Object> add(RectificationDO rectificationDO, MultipartFile file) throws IOException;
 
     /**
      * description: 修改

+ 75 - 23
nngkxxdp/src/main/java/com/example/nngkxxdp/service/impl/RectificationServiceImpl.java

@@ -1,16 +1,24 @@
 package com.example.nngkxxdp.service.impl;
 
+import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.lang.UUID;
+import com.example.nngkxxdp.dao.FileDao;
 import com.example.nngkxxdp.dao.RectificationDao;
+import com.example.nngkxxdp.entity.SFile;
 import com.example.nngkxxdp.service.RectificationService;
+import com.example.nngkxxdp.util.Blank;
 import com.example.nngkxxdp.util.ConstStr;
 import com.example.nngkxxdp.util.SendUtil;
-import org.apache.ibatis.annotations.Param;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import com.example.nngkxxdp.entity.dos.RectificationDO;
 import com.example.nngkxxdp.entity.vos.RectificationVO;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.IOException;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -22,31 +30,38 @@ import java.util.Map;
  * @Date 2023/01/30 10:44
  */
 @Service("rectificationService")
+@Transactional(rollbackFor = Exception.class)
 public class RectificationServiceImpl implements RectificationService {
 
     @Resource
     private RectificationDao rectificationDao;
-    
+
+    @Value("${file.location}")
+    private String location;
+
+    @Resource
+    private FileDao fileDao;
+
     /**
      * description: 详情
      *
+     * @param id 主键
+     *           return java.util.Map<java.lang.String,java.lang.Object> 数据
      * @author zwq
      * @date 2023/01/30 10:44
-     * @param id 主键
-     * return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
     @Override
     public Map<String, Object> info(String id) {
         RectificationVO rectificationVO = rectificationDao.info(id);
         return SendUtil.send(true, ConstStr.RESULT_SUCCESS, rectificationVO);
     }
-    
+
     /**
      * description: 列表
      *
+     * @return java.util.Map<java.lang.String, java.lang.Object> 数据
      * @author zwq
      * @date 2023/01/30 10:44
-     * @return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
     @Override
     public Map<String, Object> list() {
@@ -55,18 +70,19 @@ public class RectificationServiceImpl implements RectificationService {
 
     /**
      * description: 分页查询
-     * @author zwq
-     * @date 2023/1/30 11:26
+     *
      * @param startRows 分页参数,起始
-     * @param limit 分页参数,查几个
-     * @param type 类型
-     * @param url 网站地址
-     * @param status 状态
+     * @param limit     分页参数,查几个
+     * @param type      类型
+     * @param url       网站地址
+     * @param status    状态
      * @param submitter 提交人
-     * @param mail 邮箱
-     * @param phone 电话
+     * @param mail      邮箱
+     * @param phone     电话
      * @param equipment 设备来源
-     * @return java.util.Map<java.lang.String,java.lang.Object>
+     * @return java.util.Map<java.lang.String, java.lang.Object>
+     * @author zwq
+     * @date 2023/1/30 11:26
      */
     @Override
     public Map<String, Object> page(Integer startRows, Integer limit, Integer type, String url, Integer status,
@@ -86,16 +102,52 @@ public class RectificationServiceImpl implements RectificationService {
     /**
      * description: 新增
      *
+     * @param rectificationDO 实例对象
+     * @param file            文件对象
+     * @return java.util.Map<java.lang.String, java.lang.Object> 数据
      * @author zwq
      * @date 2023/01/30 10:44
-     * @param rectificationDO 实例对象
-     * @return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
     @Override
-    public Map<String, Object> add(RectificationDO rectificationDO) {
-        // TODO 校验
+    public Map<String, Object> add(RectificationDO rectificationDO, MultipartFile file) throws IOException {
+        if (!Blank.isPhone(rectificationDO.getPhone())) {
+            return SendUtil.send(false, "请输入正确的手机号");
+        }
+        if (!Blank.isEmail(rectificationDO.getMail())) {
+            return SendUtil.send(false, "请输入正确的邮箱");
+        }
+        if (Blank.isNotEmpty(file)) {
+            File pathFile = new File(location);
+            if (!pathFile.isDirectory()) {
+                if (!pathFile.mkdirs()) {
+                    return SendUtil.send(false, ConstStr.ADD_FAILED, "");
+                }
+            }
+            //获取上传过来的文件名
+            File temporaryFile = new File(location + file.getOriginalFilename());
+            file.transferTo(temporaryFile);
+            String type = FileTypeUtil.getType(temporaryFile);
+            if (!("jpg".equals(type) || "png".equals(type) || "bmp".equals(type) || "jpeg".equals(type))) {
+                return SendUtil.send(false, ConstStr.ADD_FAILED, "上传的格式必须是jpg或png或bmp或jpeg");
+            }
+            String newName = (new Date()).getTime() + ((int) (Math.random() * 9000) + 1000) + "." + type;
+            String filePath = location + newName;
+            File newFile = new File(filePath);
+            if (!temporaryFile.renameTo(newFile)) {
+                return SendUtil.send(false, ConstStr.ADD_FAILED, "");
+            }
+            // 增加数据到s_file表
+            SFile sFile = new SFile();
+            sFile.setPath(newName);
+            sFile.setSuffix(type);
+            if (fileDao.addFile(sFile) <= 0) {
+                return SendUtil.send(false, ConstStr.ADD_FAILED, "");
+            }
+            rectificationDO.setPicture(newName);
+        }
         rectificationDO.setId(UUID.randomUUID().toString());
         rectificationDO.setCreateTime(new Date());
+        rectificationDO.setStatus(0);
         rectificationDO.setIsdel(0);
         if (rectificationDao.add(rectificationDO) <= 0) {
             return SendUtil.send(false, ConstStr.RESULT_FAILED);
@@ -106,10 +158,10 @@ public class RectificationServiceImpl implements RectificationService {
     /**
      * description: 修改
      *
+     * @param rectificationDO 实例对象
+     * @return java.util.Map<java.lang.String, java.lang.Object> 数据
      * @author zwq
      * @date 2023/01/30 10:44
-     * @param rectificationDO 实例对象
-     * @return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
     @Override
     public Map<String, Object> update(RectificationDO rectificationDO) {
@@ -125,10 +177,10 @@ public class RectificationServiceImpl implements RectificationService {
     /**
      * description: 通过主键删除数据
      *
+     * @param id 主键
+     * @return java.util.Map<java.lang.String, java.lang.Object> 数据
      * @author zwq
      * @date 2023/01/30 10:44
-     * @param id 主键
-     * @return java.util.Map<java.lang.String,java.lang.Object> 数据
      */
     @Override
     public Map<String, Object> delete(String id) {

+ 18 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/util/Blank.java

@@ -228,6 +228,24 @@ public class Blank {
 
         return true;
     }
+
+    /**
+     * 验证是否为邮箱
+     * @param email
+     * @return
+     */
+    public static Boolean isEmail(String email) {
+        if (isEmpty(email)) {
+            return false;
+        }
+
+        String pattern = "^([a-zA-Z\\d][\\w-]{2,})@(\\w{2,})\\.([a-z]{2,})(\\.[a-z]{2,})?$";
+        if (!Pattern.matches(pattern, email)) {
+            return false;
+        }
+
+        return true;
+    }
     
     public static void main(String[] args) {
     	Map<String, String> params = new HashMap<>();

+ 5 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/util/ConstStr.java

@@ -66,6 +66,11 @@ public class ConstStr {
     public final static String CRYPTO_KEY = "hauxbfrducydtmxt";
 
     /**
+     * 纠错提交的唯一码
+     */
+    public final static String UNIQUE_CODE = "nNwLRLlNmjeSZavZZDxmKlumQtvJ3vcL";
+
+    /**
      * 业务返回常量
      */
     public final static String DATA_NOT_FOUND = "data_not_found";