Jelajahi Sumber

学籍照片上传接口

dzx 1 tahun lalu
induk
melakukan
b0856815fe

+ 14 - 6
src/main/java/com/xjrsoft/module/student/controller/StudentManagerController.java

@@ -45,6 +45,7 @@ import lombok.AllArgsConstructor;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -184,18 +185,25 @@ public class StudentManagerController {
     }
 
 
-    @PostMapping("/add")
+    @PostMapping
     @ApiOperation(value = "新增学生")
     @SaCheckPermission("studentmanager:add")
-    public R add(@Valid AddBaseStudentUserDto dto, @RequestParam("file") MultipartFile file) throws IOException {
-        return R.ok(studentManagerService.add(dto, file));
+    public R add(@Valid @RequestBody AddBaseStudentUserDto dto){
+        return R.ok(studentManagerService.add(dto));
     }
 
-    @PostMapping
+    @PostMapping("upload-image")
+    @ApiOperation(value = "上传学生学籍照片")
+    @SaCheckPermission("studentmanager:add")
+    public R uploadImage(@Valid Long userId, @RequestParam("file") MultipartFile file) throws IOException {
+        return R.ok(studentManagerService.uploadImage(userId, file));
+    }
+
+    @PutMapping
     @ApiOperation(value = "修改学生")
     @SaCheckPermission("studentmanager:edit")
-    public R update(@Valid UpdateBaseStudentUserDto dto, @RequestParam("file") MultipartFile file) throws IOException {
-        return R.ok(studentManagerService.update(dto, file));
+    public R update(@Valid UpdateBaseStudentUserDto dto){
+        return R.ok(studentManagerService.update(dto));
     }
 
     @DeleteMapping

+ 4 - 2
src/main/java/com/xjrsoft/module/student/service/IStudentManagerService.java

@@ -24,7 +24,7 @@ public interface IStudentManagerService extends MPJBaseService<BaseStudentUser>
      * @param baseStudentUser
      * @return
      */
-    Boolean add(AddBaseStudentUserDto baseStudentUser, MultipartFile file) throws IOException;
+    Long add(AddBaseStudentUserDto baseStudentUser);
 
     /**
      * 更新
@@ -32,7 +32,7 @@ public interface IStudentManagerService extends MPJBaseService<BaseStudentUser>
      * @param baseStudentUser
      * @return
      */
-    Boolean update(UpdateBaseStudentUserDto baseStudentUser, MultipartFile file) throws IOException;
+    Boolean update(UpdateBaseStudentUserDto baseStudentUser);
 
     /**
      * 删除
@@ -61,4 +61,6 @@ public interface IStudentManagerService extends MPJBaseService<BaseStudentUser>
 
 
     Page<BaseStudentUserPageVo> getStudentPage(Page<BaseStudentUserPageVo> page, BaseStudentUserPageDto dto);
+
+    Boolean uploadImage(Long userId, MultipartFile file) throws IOException;
 }

+ 12 - 11
src/main/java/com/xjrsoft/module/student/service/impl/StudentManagerServiceImpl.java

@@ -111,15 +111,11 @@ public class StudentManagerServiceImpl extends MPJBaseServiceImpl<BaseStudentUse
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public Boolean add(AddBaseStudentUserDto dto, MultipartFile file) throws IOException {
+    public Long add(AddBaseStudentUserDto dto){
 
         BaseStudentUser baseStudentUser = BeanUtil.toBean(dto, BaseStudentUser.class);
         baseStudentUser.setCode(dto.getUserName());
 
-        //存学籍照片
-        String base64String = Base64.getEncoder().encodeToString(file.getBytes());
-        baseStudentUser.setAvatar(base64String);
-
         // 用户身份证后6位作为默认密码
         baseStudentUser.setPassword(BCrypt.hashpw(propertiesConfig.getDefaultPassword(), BCrypt.gensalt()));
         studentbaseManagerBaseStudentUserMapper.insert(baseStudentUser);
@@ -179,20 +175,16 @@ public class StudentManagerServiceImpl extends MPJBaseServiceImpl<BaseStudentUse
             List<UserRoleRelation> userRoleRelationList = userRoleRelationMapper.selectList(Wrappers.lambdaQuery(UserRoleRelation.class));
             redisUtil.set(GlobalConstant.USER_ROLE_RELATION_CACHE_KEY, userRoleRelationList);
         });
-        return true;
+        return baseStudentUser.getId();
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public Boolean update(UpdateBaseStudentUserDto dto, MultipartFile file) throws IOException {
+    public Boolean update(UpdateBaseStudentUserDto dto){
 
         BaseStudentUser baseStudentUser = BeanUtil.toBean(dto, BaseStudentUser.class);
         baseStudentUser.setCode(dto.getUserName());
 
-        //存学籍照片
-        String base64String = Base64.getEncoder().encodeToString(file.getBytes());
-        baseStudentUser.setAvatar(base64String);
-
         studentbaseManagerBaseStudentUserMapper.updateById(baseStudentUser);
 
         //先删除再新增
@@ -967,6 +959,15 @@ public class StudentManagerServiceImpl extends MPJBaseServiceImpl<BaseStudentUse
         return baseStudentMapper.getStudentPage(page, dto);
     }
 
+    @Override
+    public Boolean uploadImage(Long userId, MultipartFile file) throws IOException {
+        BaseStudentUser studentUser = studentbaseManagerBaseStudentUserMapper.selectById(userId);
+        String base64String = Base64.getEncoder().encodeToString(file.getBytes());
+        studentUser.setAvatar(base64String);
+        studentbaseManagerBaseStudentUserMapper.updateById(studentUser);
+        return true;
+    }
+
     /**
      * 初始化字典信息
      * 身份证类型、