feat: 用户管理数据导入教师和管理员区分

This commit is contained in:
TsMask
2024-06-24 17:09:44 +08:00
parent 045b939c32
commit 6ac9d8aee5
7 changed files with 75 additions and 79 deletions

View File

@@ -270,7 +270,7 @@ INSERT INTO `sys_dict_data` VALUES (1536, 1536, 'dictType.noData', '没有可访
INSERT INTO `sys_dict_data` VALUES (1537, 1537, 'dictType.errNameExists', '操作字典【{name}】失败,字典名称已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1538, 1538, 'dictType.errTypeExists', '操作字典【{name}】失败,字典类型已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1600, 1600, 'dept.root', 'XXXX大学', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1601, 1601, 'dept.root.item1', '网络工程学院', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1601, 1601, 'dept.root.item1', '未指定', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1602, 1602, 'dept.noData', '没有可访问班级数据!', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1603, 1603, 'dept.errParentDelFlag', '上级班级【{name}】已删除,不允许新增', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1604, 1604, 'dept.errParentStatus', '上级班级【{name}】停用,不允许新增', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
@@ -397,14 +397,14 @@ INSERT INTO `sys_dict_data` VALUES (1849, 1849, 'user.errPhoneFormat', '操作
INSERT INTO `sys_dict_data` VALUES (1850, 1850, 'user.errPhoneExists', '操作用户【{name}】失败,手机号码已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1851, 1851, 'user.errNameExists', '操作用户【{name}】失败,登录账号已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1852, 1852, 'user.import.mustItem', '表格中必填列表项,{text}', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1853, 1853, 'user.import.phoneExist', '用户编号:{id} 手机号码 {phone} 已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1854, 1854, 'user.import.phoneFormat', '用户编号:{id} 手机号码 {phone} 格式错误', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1855, 1855, 'user.import.emailExist', '用户编号:{id} 用户邮箱:{email} 已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1856, 1856, 'user.import.emailFormat', '用户编号:{id} 用户邮箱:{email} 格式错误', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1857, 1857, 'user.import.success', '用户编号:{id} 登录名称:{name} 导入成功', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1858, 1858, 'user.import.fail', '用户编号:{id} 登录名称:{name} 导入失败', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1859, 1859, 'user.import.successUpdate', '用户编号:{id} 登录名称:{name} 更新成功', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1860, 1860, 'user.import.failUpdate', '用户编号:{id} 登录名称:{name} 更新失败', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1853, 1853, 'user.import.phoneExist', '号:{id} 手机号码 {phone} 已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1854, 1854, 'user.import.phoneFormat', '号:{id} 手机号码 {phone} 格式错误', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1855, 1855, 'user.import.emailExist', '号:{id} 用户邮箱:{email} 已存在', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1856, 1856, 'user.import.emailFormat', '号:{id} 用户邮箱:{email} 格式错误', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1857, 1857, 'user.import.success', '号:{id} 登录名称:{name} 导入成功', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1858, 1858, 'user.import.fail', '号:{id} 登录名称:{name} 导入失败', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1859, 1859, 'user.import.successUpdate', '号:{id} 登录名称:{name} 更新成功', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1860, 1860, 'user.import.failUpdate', '号:{id} 登录名称:{name} 更新失败', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1861, 1861, 'user.import.failTip', '很抱歉,导入失败!共 {num} 条数据格式不正确,错误如下:', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1862, 1862, 'user.import.successTip', '恭喜您,数据已全部导入成功!共 {num} 条,数据如下:', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (1900, 1900, 'app.common.err403', '无权访问 {method} {requestURI}', 'i18n_zh', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);

View File

@@ -270,7 +270,7 @@ INSERT INTO `sys_dict_data` VALUES (3536, 3536, 'dictType.noData', 'There is no
INSERT INTO `sys_dict_data` VALUES (3537, 3537, 'dictType.errNameExists', 'Failed to manipulate dictionary [{name}], dictionary name already exists!', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3538, 3538, 'dictType.errTypeExists', 'Failed to manipulate dictionary [{name}], dictionary type already exists!', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3600, 3600, 'dept.root', 'XXXX University', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3601, 3601, 'dept.root.item1', 'Network Engineering Institute', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3601, 3601, 'dept.root.item1', 'Unspecified', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3602, 3602, 'dept.noData', 'There is no accessible class data!', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3603, 3603, 'dept.errParentDelFlag', 'The parent class [{name}] has been deleted and is not allowed to be added.', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3604, 3604, 'dept.errParentStatus', 'Parent class [{name}] is deactivated, additions are not allowed!', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
@@ -397,14 +397,14 @@ INSERT INTO `sys_dict_data` VALUES (3849, 3849, 'user.errPhoneFormat', 'Failed t
INSERT INTO `sys_dict_data` VALUES (3850, 3850, 'user.errPhoneExists', 'Failed to operate user [{name}], cell phone number already exists.', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3851, 3851, 'user.errNameExists', 'Failed to operate user [{name}], login account already exists.', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3852, 3852, 'user.import.mustItem', 'Required list item in form, {text}', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3853, 3853, 'user.import.phoneExist', 'User ID: {id} cell phone number {phone} Existing', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3854, 3854, 'user.import.phoneFormat', 'User ID: {id} cell phone number {phone} Wrong format', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3855, 3855, 'user.import.emailExist', 'User ID: {id} User Email: {email} Existing', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3856, 3856, 'user.import.emailFormat', 'User ID: {id} Email: {email} Wrong Format', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3857, 3857, 'user.import.success', 'User ID:{id} Login name:{name} Imported successfully!', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3858, 3858, 'user.import.fail', 'User ID: {id} Login name: {name} Import failed', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3859, 3859, 'user.import.successUpdate', 'User ID: {id} Login name: {name} Update success', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3860, 3860, 'user.import.failUpdate', 'User ID: {id} Login Name: {name} Update Failed', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3853, 3853, 'user.import.phoneExist', 'Serial No.{id} cell phone number {phone} Existing', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3854, 3854, 'user.import.phoneFormat', 'Serial No.{id} cell phone number {phone} Wrong format', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3855, 3855, 'user.import.emailExist', 'Serial No.{id} User Email: {email} Existing', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3856, 3856, 'user.import.emailFormat', 'Serial No.{id} Email: {email} Wrong Format', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3857, 3857, 'user.import.success', 'Serial No.{id} Login name:{name} Imported successfully!', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3858, 3858, 'user.import.fail', 'Serial No.{id} Login name: {name} Import failed', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3859, 3859, 'user.import.successUpdate', 'Serial No.{id} Login name: {name} Update success', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3860, 3860, 'user.import.failUpdate', 'Serial No.{id} Login Name: {name} Update Failed', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3861, 3861, 'user.import.failTip', 'Sorry, the import failed! A total of {num} entries were not formatted correctly, the error is below:', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3862, 3862, 'user.import.successTip', 'Congratulations, the data has been imported successfully! There are {num} entries with the following data:', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);
INSERT INTO `sys_dict_data` VALUES (3900, 3900, 'app.common.err403', 'Unauthorized access {method} {requestURI}', 'i18n_en', NULL, NULL, '1', 'supervisor', 1700000000000, NULL, 0, NULL);

View File

@@ -573,11 +573,23 @@ func (s *SysUserController) Export(c *gin.Context) {
//
// GET /importTemplate
func (s *SysUserController) Template(c *gin.Context) {
fileName := fmt.Sprintf("user_import_template_%d.xlsx", time.Now().UnixMilli())
// 多语言处理
language := ctx.AcceptLanguage(c)
asserPath := fmt.Sprintf("assets/template/excel/user_import_template_%s.xlsx", language)
// 登录用户
loginUser, err := ctx.LoginUser(c)
if err != nil {
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
return
}
// 根据角色指定导入模板
fileKey := "user"
roles := loginUser.User.Roles
if len(roles) == 1 && roles[0].RoleKey == "teacher" {
fileKey = "student"
}
fileName := fmt.Sprintf("%s_import_template_%d.xlsx", fileKey, time.Now().UnixMilli())
asserPath := fmt.Sprintf("assets/template/excel/%s_import_template_%s.xlsx", fileKey, language)
// 从 embed.FS 中读取默认配置文件内容
assetsDir := config.GetAssetsDirFS()
@@ -601,6 +613,12 @@ func (s *SysUserController) Template(c *gin.Context) {
// POST /importData
func (s *SysUserController) ImportData(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 登录用户
loginUser, err := ctx.LoginUser(c)
if err != nil {
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
return
}
// 允许进行更新
updateSupport := c.PostForm("updateSupport")
// 上传的文件
@@ -624,6 +642,12 @@ func (s *SysUserController) ImportData(c *gin.Context) {
return
}
// 根据角色指定导入模板
roleKey := ""
roles := loginUser.User.Roles
if len(roles) == 1 && roles[0].RoleKey == "teacher" {
roleKey = "student"
}
// 获取操作人名称
operName := ctx.LoginUserToUserName(c)
isUpdateSupport := parse.Boolean(updateSupport)
@@ -661,77 +685,49 @@ func (s *SysUserController) ImportData(c *gin.Context) {
sysUserSex := "0"
for _, v := range dictSysUserSex {
label := i18n.TKey(language, v.DictLabel)
if row["F"] == label {
if row["D"] == label {
sysUserSex = v.DictValue
break
}
}
// 用户状态
sysUserStatus := common.STATUS_NO
if row["G"] == "正常" || row["G"] == "Normal" {
if row["E"] == "正常" || row["G"] == "Normal" {
sysUserStatus = common.STATUS_YES
}
// 用户角色 拿编号
sysUserRole := ""
if v, ok := row["H"]; ok && v != "" {
sysUserRole = strings.SplitN(v, "-", 2)[0]
if sysUserRole == "1" {
sysUserRole = ""
sysUserRole := "" // 用户角色
sysUserPost := "" // 用户岗位
sysUserDept := "101" // 用户部门 101未指定
if roleKey == "student" {
sysUserRole = "4"
sysUserPost = "3"
sysUserDept = loginUser.DeptID
}
if v, ok := row["F"]; ok && v != "" {
if v == "学生" || v == "Student" {
sysUserRole = "4"
sysUserPost = "3"
} else if v == "教师" || v == "Teacher" {
sysUserRole = "3"
sysUserPost = "2"
}
}
if v, ok := row["G"]; ok && v != "" && v != "100" {
sysUserDept = v
}
// 构建用户实体信息
newSysUser := model.SysUser{
UserType: "sys",
Password: initPassword,
DeptID: row["I"],
UserName: row["B"],
NickName: row["C"],
PhoneNumber: row["E"],
Email: row["D"],
Status: sysUserStatus,
Sex: sysUserSex,
RoleIDs: []string{sysUserRole},
}
// 检查手机号码格式并判断是否唯一
if newSysUser.PhoneNumber != "" {
if regular.ValidMobile(newSysUser.PhoneNumber) {
uniquePhone := s.sysUserService.CheckUniquePhone(newSysUser.PhoneNumber, "")
if !uniquePhone {
// 用户编号:%s 手机号码 %s 已存在
msg := i18n.TTemplate(language, "user.import.phoneExist", map[string]any{"id": row["A"], "phone": newSysUser.PhoneNumber})
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
} else {
// 用户编号:%s 手机号码 %s 格式错误
msg := i18n.TTemplate(language, "user.import.phoneFormat", map[string]any{"id": row["A"], "phone": newSysUser.PhoneNumber})
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
}
// 检查邮箱格式并判断是否唯一
if newSysUser.Email != "" {
if regular.ValidEmail(newSysUser.Email) {
uniqueEmail := s.sysUserService.CheckUniqueEmail(newSysUser.Email, "")
if !uniqueEmail {
// 用户编号:%s 用户邮箱 %s 已存在
msg := i18n.TTemplate(language, "user.import.emailExist", map[string]any{"id": row["A"], "email": newSysUser.Email})
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
} else {
// 用户编号:%s 用户邮箱 %s 格式错误
msg := i18n.TTemplate(language, "user.import.emailFormat", map[string]any{"id": row["A"], "email": newSysUser.Email})
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
UserType: "sys",
Password: initPassword,
DeptID: sysUserDept,
UserName: row["B"],
NickName: row["C"],
Status: sysUserStatus,
Sex: sysUserSex,
RoleIDs: []string{sysUserRole},
PostIDs: []string{sysUserPost},
}
// 验证是否存在这个用户