2021.11.20
项目参考:
1、后端:https://www.bilibili.com/video/BV1y7411R73P
2、前端:https://www.bilibili.com/video/BV1fw411d7R5?p=31
都快答辩了,我系统的注册还没做。因为这个原来是没用JS框架的,其实注册的接口已经写好了,而且还用了阿里云的短信服务。由于1、短信要钱,我比较抠 2、作为给学校用的系统,学生的信息是很容易拿到的,所以我直接做成批量导入感觉也挺好玩的,之前没有做过。
效果:
ok。废话少说,流程如下:
一、使用的antd的组件
import {Upload, message} from 'antd';
import {InboxOutlined} from '@ant-design/icons';
const {Dragger} = Upload;
export default function BatchImport() {
const handelFileChange = (info) => {
const {status} = info.file;
if (status === 'done') {
message.success('批量导入成功!');
} else if (status === 'error') {
message.error('批量导入失败,请检查数据格式!');
}
}
return <Dragger name='file' multiple
action='/association/batchImportUser'
onChange={handelFileChange}
onDrop={(e) => {
console.log('Dropped files', e.dataTransfer.files);
}}>
<p><InboxOutlined/></p>
<p className="ant-upload-text">点击右上角按钮或上传文件批量导入</p>
<p className="ant-upload-hint"
style={{
marginTop: 10,
}}
>可选择学生名单进行社团成员批量导入,请确保文件数据格式符合右侧规则说明!</p>
</Dragger>
}
其中action就是我们要发送请求的接口,name及表单提交的key,具体含义看接口:
二、接口
controller层:主要读取了excel中的文件,封装成list传下去了
这里用了一个读取excel的包jxl:
maven如下:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
@ResponseBody
@ApiOperation("批量导入社团成员")
@PostMapping("/batchImportUser")
public AppResponse<String> batchImportUser(@RequestBody MultipartFile file) {
try {
// 读excel文件流
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
// 获取工作簿sheet
Sheet sheet = workbook.getSheet(0);
// 遍历获取内容
List<UserAddReqVo> users = new ArrayList<>();
for (int i = 0; i < sheet.getRows(); i++) {
UserAddReqVo user = new UserAddReqVo();
user.setUserId(sheet.getCell(0, i).getContents());
user.setName(sheet.getCell(1, i).getContents());
user.setRoleName(sheet.getCell(2, i).getContents());
user.setAssociationName(sheet.getCell(3, i).getContents());
user.setPassword(sheet.getCell(4, i).getContents());
user.setGender(sheet.getCell(5, i).getContents());
users.add(user);
}
userService.batchImportUser(users);
AppResponse<String> resp = AppResponse.ok(null);
resp.setMsg("批量导入成功!");
return resp;
} catch (Exception e) {
e.printStackTrace();
AppResponse<String> fail = AppResponse.fail(null);
fail.setMsg("批量导入失败!原因:" + e.getMessage());
return fail;
}
}
service层 :主要做一些校验,和默认值的set
@Override
@Transactional
public void batchImportUser(List<UserAddReqVo> users) {
HashMap<String, Integer> nameIdmap = associationService.getNameIdMap();
for (Map.Entry<String, Integer> entry : nameIdmap.entrySet()) {
log.debug("nameIdmap ! Key = {}, Value = {}", entry.getKey(), entry.getValue());
}
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
for (UserAddReqVo addUserVO : users) {
// 社团可选。默认-1,没有社团
if (addUserVO.getAssociationName() == null) {
addUserVO.setAssociationId(-1);
} else {
Integer associationId = nameIdmap.get(addUserVO.getAssociationName());
addUserVO.setAssociationId(associationId);
}
// 角色可选。默认普通学生
if (addUserVO.getRoleName() == null) {
addUserVO.setRoleId(4);
} else {
Integer roleId = RoleTypeEnum.getCodeByName(addUserVO.getRoleName());
addUserVO.setRoleId(roleId);
}
// 密码默认123456。并进行加密
if (addUserVO.getPassword() == null) {
addUserVO.setPassword(encoder.encode("123456"));
} else {
addUserVO.setPassword(encoder.encode(addUserVO.getPassword()));
}
// 性别格式化。出错则为 未知
if (addUserVO.getGender() != null) {
addUserVO.setGender(GenderEnum.getGender(addUserVO.getGender()));
}
}
// 批量insert User
userMapper.batchImportUser(users);
// 批量insert User_Role 。初始化角色信息
userRoleMapper.initUserRole(users);
}
最后就DAO层啦: Mybatis框架
<!-- void batchImportUser(List<UserAddReqVo> users);-->
<insert id="batchImportUser" parameterType="com.jmu.uacs.vo.request.UserAddReqVo">
INSERT INTO `user`(user_id, `name`, `password`, gender, state)
VALUES
<foreach item="user" collection="users" separator="," >
(#{user.userId},#{user.name},#{user.password},#{user.gender},'OPEN')
</foreach>
</insert>
<!-- void initUserRole(List<UserAddReqVo> users);-->
<insert id="initUserRole" parameterType="com.jmu.uacs.vo.request.UserAddReqVo">
INSERT INTO user_role(user_id,role_id,association_id,department_id) VALUES
<foreach item="user" collection="users" separator=",">
(#{user.userId},#{user.roleId},#{user.associationId},-1)
</foreach>
</insert>