feat: 用户管理数据导入教师和管理员区分
This commit is contained in:
BIN
src/assets/template/excel/student_import_template_en.xlsx
Normal file
BIN
src/assets/template/excel/student_import_template_en.xlsx
Normal file
Binary file not shown.
BIN
src/assets/template/excel/student_import_template_zh.xlsx
Normal file
BIN
src/assets/template/excel/student_import_template_zh.xlsx
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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},
|
||||
}
|
||||
|
||||
// 验证是否存在这个用户
|
||||
|
||||
Reference in New Issue
Block a user