1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333 |
- package controllers
- import (
- "encoding/base64"
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services"
- "eta/eta_api/services/eta_forum"
- etaTrialService "eta/eta_api/services/eta_trial"
- "eta/eta_api/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- "github.com/rdlucklib/rdluck_tools/paging"
- )
- // 系统用户
- type SysAdminController struct {
- BaseAuthController
- }
- // @Title 根据分组或者部门获取系统用户列表
- // @Description 根据分组或者部门获取系统用户列表接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param DepartmentId query int true "部门ID"
- // @Param GroupId query int true "分组ID"
- // @Param TeamId query int true "小组ID"
- // @Param RoleId query int true "角色ID"
- // @Param KeyWord query string true "搜索关键词"
- // @Success 200 {object} system.SysuserListResp
- // @router /sysuser/list [get]
- func (this *SysAdminController) ListSysuser() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- departmentId, _ := this.GetInt("DepartmentId")
- groupId, _ := this.GetInt("GroupId")
- teamId, _ := this.GetInt("TeamId")
- roleId, _ := this.GetInt("RoleId")
- keyWord := this.GetString("KeyWord")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- if departmentId > 0 {
- condition += ` AND department_id=? `
- pars = append(pars, departmentId)
- }
- if roleId > 0 {
- condition += ` AND role_id=? `
- pars = append(pars, roleId)
- }
- if keyWord != "" {
- condition += ` AND (real_name LIKE ? OR admin_name LIKE ? OR mobile LIKE ? ) `
- pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
- }
- var total int
- var err error
- if teamId <= 0 {
- if groupId > 0 {
- ids, err := system.GetGroupIdByParentId(groupId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- sGroupId := strconv.Itoa(groupId)
- ids = append(ids, &sGroupId)
- var idList []string
- for _, id := range ids {
- idList = append(idList, *id)
- }
- sid := strings.Join(idList, ",")
- condition += ` AND group_id IN (` + sid + `) `
- }
- total, err = system.GetSysuserListCount(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- } else {
- nCondition := condition + ` AND group_id=? `
- nPars := append(pars, teamId)
- total, err = system.GetSysuserListCount(nCondition, nPars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- }
- var list []*system.AdminItem
- adminIdArr := make([]int, 0)
- if teamId <= 0 {
- //if groupId > 0 {
- // ids, err := system.GetGroupIdByParentId(groupId, startSize, pageSize)
- // if err != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "获取失败,Err:" + err.Error()
- // return
- // }
- // sGroupId := strconv.Itoa(groupId)
- // ids = append(ids, &sGroupId)
- // var idList []string
- // for _, id := range ids {
- // idList = append(idList, *id)
- // }
- // sid := strings.Join(idList,",")
- //
- // condition += ` AND group_id IN (` + sid + `) `
- //}
- list, err = system.GetSysuserList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- lenList := len(list)
- for i := 0; i < lenList; i++ {
- item := list[i]
- //adminIdArr = append(adminIdArr, strconv.Itoa(item.AdminId))
- adminIdArr = append(adminIdArr, item.AdminId)
- var secondName *string
- if item.GroupId != 0 {
- pid, err := company.GetParentIdFromGroup(item.GroupId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "查询父级id异常" + err.Error()
- return
- }
- if pid != nil && *pid != 0 {
- secondName, err = company.GetGroupNamesById(*pid)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "查询二级组名异常" + err.Error()
- return
- }
- }
- if *pid > 0 {
- list[i].TeamId = list[i].GroupId
- list[i].GroupId = *pid
- }
- }
- if item.DepartmentName != "" {
- if item.GroupName == "" {
- list[i].DepartmentGroup = item.DepartmentName
- } else if secondName != nil {
- list[i].DepartmentGroup = item.DepartmentName + "/" + *secondName + "/" + item.GroupName
- } else {
- list[i].DepartmentGroup = item.DepartmentName + "/" + item.GroupName
- }
- }
- labelVal := 0
- fmt.Println("item.RoleTypeCode:", item.RoleTypeCode)
- fmt.Println("item.Authority:", item.Authority)
- if item.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- labelVal = 1
- } else if item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
- item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
- labelVal = 2
- } else {
- if item.Authority == 1 {
- labelVal = 3
- } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
- labelVal = 4
- } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
- labelVal = 5
- } else if item.Authority == 4 {
- labelVal = 4
- }
- }
- list[i].LabelVal = labelVal
- }
- } else {
- condition += ` AND group_id=? `
- pars = append(pars, teamId)
- list, err = system.GetSysuserList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- groupItem, err := system.GetSysGroupById(groupId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- lenList := len(list)
- for i := 0; i < lenList; i++ {
- item := list[i]
- //adminIdArr = append(adminIdArr, strconv.Itoa(item.AdminId))
- adminIdArr = append(adminIdArr, item.AdminId)
- if item.DepartmentName != "" {
- if item.GroupName == "" {
- list[i].DepartmentGroup = item.DepartmentName
- } else if item.GroupName != "" {
- list[i].DepartmentGroup = item.DepartmentName + "/" + groupItem.GroupName + "/" + item.GroupName
- } else {
- list[i].DepartmentGroup = item.DepartmentName + "/" + item.GroupName
- }
- }
- labelVal := 0
- if item.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- labelVal = 1
- } else if item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
- item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
- labelVal = 2
- } else {
- if item.Authority == 1 {
- labelVal = 3
- } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
- labelVal = 4
- } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
- labelVal = 5
- } else if item.Authority == 4 {
- labelVal = 4
- }
- }
- list[i].LabelVal = labelVal
- }
- }
- //if teamId > 0 {
- // var groupIdSlice []string
- // var groupIds string
- // for _, item := range list {
- // groupIdSlice = append(groupIdSlice, strconv.Itoa(groupId))
- // }
- // groupIds = strings.Join(groupIdSlice, ",")
- // fmt.Println("groupIds:", groupIds)
- //}
- if teamId > 0 {
- for _, item := range list {
- item.TeamId = teamId
- item.GroupId = groupId
- }
- }
- // 研究方向分组
- if len(adminIdArr) > 0 && utils.BusinessCode == utils.BusinessCodeRelease {
- //adminIds := strings.Join(adminIdArr, ",")
- //researchGroupList, e := system.GetAdminResearchGroupListByAdminId(adminIds)
- researchGroupList, e := models.GetAdminVarietyTagRelationListByAdminId(adminIdArr)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取研究方向分组列表失败, Err:" + e.Error()
- return
- }
- researchGroupAdminIdName := make(map[int]string, 0)
- for _, g := range researchGroupList {
- researchGroupAdminIdName[g.AdminId] = g.GroupNameStr
- }
- for _, v := range list {
- v.ResearchGroupName = researchGroupAdminIdName[v.AdminId]
- }
- }
- fmt.Println("teamId:", teamId)
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := new(system.SysuserListResp)
- resp.List = list
- resp.Paging = page
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Add
- // @Title 新增系统用户
- // @Description 新增系统用户接口
- // @Param request body system.SysuserAddReq true "type json string"
- // @Success 200 新增成功
- // @router /sysuser/add [post]
- func (this *SysAdminController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req system.SysuserAddReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RoleId <= 0 {
- br.Msg = "请选择角色"
- br.ErrMsg = "角色ID小于等于0"
- return
- }
- count, err := system.GetSysAdminCount(req.AdminName)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败"
- return
- }
- if count > 0 {
- br.Msg = "账号已存在,请重新输入"
- br.IsSendEmail = false
- return
- }
- // 手机号和邮箱必填一个
- req.Mobile = strings.TrimSpace(req.Mobile)
- req.Email = strings.TrimSpace(req.Email)
- if req.Mobile == "" && req.Email == "" {
- br.Msg = "至少输入一个手机号或邮箱"
- return
- }
- if req.Mobile != "" {
- if req.TelAreaCode == "86" {
- if !utils.ValidateMobileFormatat(req.Mobile) {
- br.Msg = "手机号格式有误, 请检查"
- return
- }
- }
- mobileCount, err := system.GetSysAdminCountByMobile(req.Mobile, 0)
- if err != nil {
- br.Msg = "判断手机号是否存在失败"
- br.ErrMsg = "判断手机号是否存在失败,Err:" + err.Error()
- return
- }
- if mobileCount > 0 {
- br.Msg = "手机号已存在,请重新填写"
- br.IsSendEmail = false
- return
- }
- }
- if req.Email != "" {
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误, 请检查"
- return
- }
- emailUser, e := system.GetSysUserByEmail(req.Email)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "邮箱获取用户失败"
- return
- }
- if emailUser != nil && emailUser.AdminId > 0 {
- br.Msg = "邮箱已存在, 请重新填写"
- return
- }
- }
- var roleName, departmentName, groupName, teamName string
- roleItem, err := system.GetSysRoleById(req.RoleId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "所选角色不存在"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
- return
- }
- if roleItem != nil {
- roleName = roleItem.RoleName
- }
- if req.DepartmentId > 0 {
- departmentItem, err := system.GetSysDepartmentById(req.DepartmentId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "所选部门不存在"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
- return
- }
- if departmentItem != nil {
- departmentName = departmentItem.DepartmentName
- }
- }
- if req.GroupId > 0 {
- groupItem, err := system.GetSysGroupById(req.GroupId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "所选分组不存在"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取分组数据失败,Err:" + err.Error()
- return
- }
- if groupItem != nil {
- groupName = groupItem.GroupName
- }
- }
- if req.TeamId > 0 {
- teamItem, err := system.GetSysGroupById(req.TeamId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "所选小组不存在"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取小组数据失败,Err:" + err.Error()
- return
- }
- if teamItem != nil {
- teamName = teamItem.GroupName
- }
- }
- var originPass, pass string
- if req.Password != "" {
- pwdByte, e := base64.StdEncoding.DecodeString(req.Password)
- if e != nil {
- br.Msg = "解析数据失败"
- br.ErrMsg = "解析数据失败,Err:" + e.Error()
- return
- }
- originPass = string(pwdByte)
- }
- // 系统用户-密码必填且需要校验密码格式
- if req.IsLdap == 0 {
- if originPass == "" {
- br.Msg = "请输入密码"
- return
- }
- if !utils.CheckPwd(originPass) {
- br.Msg = "密码格式错误,请重新输入"
- return
- }
- pass = utils.MD5(originPass)
- }
- // 域用户-密码非必填(实际登录用不到这个密码)
- if req.IsLdap == 1 {
- if originPass != "" {
- pass = utils.MD5(originPass)
- } else {
- // 未填写的话给个初始密码即可
- pass = utils.MD5(utils.LdapInitPassword)
- }
- }
- // 员工工号
- req.EmployeeId = strings.TrimSpace(req.EmployeeId)
- admin := new(system.Admin)
- admin.AdminName = req.AdminName
- admin.RealName = req.RealName
- admin.Password = pass
- admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
- admin.Enabled = 1
- admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
- admin.CreatedTime = time.Now()
- admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- admin.Mobile = req.Mobile
- admin.RoleType = 0
- admin.RoleId = req.RoleId
- admin.RoleName = roleName
- admin.RoleTypeCode = roleItem.RoleTypeCode
- admin.DepartmentId = req.DepartmentId
- admin.DepartmentName = departmentName
- admin.AdminAvatar = req.AdminAvatar
- if req.TeamId <= 0 {
- admin.GroupId = req.GroupId
- admin.GroupName = groupName
- } else {
- admin.GroupId = req.TeamId
- admin.GroupName = teamName
- }
- if strings.Contains(admin.RoleTypeCode, "researcher") {
- admin.Role = "researcher"
- } else if strings.Contains(admin.RoleTypeCode, "seller") {
- admin.Role = "sales"
- } else {
- admin.Role = "admin"
- }
- admin.EmployeeId = req.EmployeeId
- admin.Email = req.Email
- var authority int
- if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- authority = 3
- } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
- authority = 4
- } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
- authority = 2
- } else {
- if strings.Contains(roleItem.RoleType, "销售主管") {
- authority = 2
- } else if strings.Contains(roleItem.RoleType, "部门经理") {
- authority = 1
- } else {
- authority = 0
- }
- }
- admin.Authority = authority
- admin.Position = req.Position
- admin.Province = req.Province
- admin.ProvinceCode = req.ProvinceCode
- admin.City = req.City
- admin.CityCode = req.CityCode
- admin.TelAreaCode = req.TelAreaCode
- admin.IsLdap = req.IsLdap
- err = system.AddAdmin(admin)
- if err != nil {
- br.Msg = "新增失败"
- br.ErrMsg = "新增失败,Err:" + err.Error()
- return
- }
- // 同步用户缓存
- if utils.BusinessCode == utils.BusinessCodeRelease {
- var syncData system.SyncAdminData
- syncData.Source = utils.SOURCE_ETA_FLAG
- syncData.AdminName = admin.AdminName
- _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
- e := services.UpdateResearcherTagGroup(admin.AdminId, req.ResearchGroupIds)
- if e != nil {
- br.Msg = "新增失败"
- br.ErrMsg = "新增用户研究方向分组失败, Err:" + e.Error()
- return
- }
- }
- // 清楚系统用户列表缓存key
- utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
- utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
- //thirdGroupId := 0 //角色:2=研究员,3=机构销售
- if admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
- //thirdGroupId = 2
- } else if admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
- admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
- //thirdGroupId = 3
- }
- //if thirdGroupId > 0 {
- // go roadshow.CreateOaUser(admin.Mobile, admin.RealName, thirdGroupId)
- //}
- // 新增弘则联系人
- //go services.AddHzCompanyUser(admin.Mobile, admin.RealName, this.SysUser.AdminId, this.SysUser.RealName)
- go eta_forum.AdminSave(admin.AdminId)
- br.Ret = 200
- br.Success = true
- br.Msg = "新增成功"
- }
- // Edit
- // @Title 编辑系统用户
- // @Description 编辑系统用户接口
- // @Param request body system.SysuserEditReq true "type json string"
- // @Success 200 编辑成功
- // @router /sysuser/edit [post]
- func (this *SysAdminController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req system.SysuserEditReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RoleId <= 0 {
- br.Msg = "请选择角色"
- br.ErrMsg = "角色ID小于等于0"
- return
- }
- //根据用户id获取用户信息
- adminInfo, err := system.GetSysAdminById(req.AdminId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "用户已被删除, 请刷新页面"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
- return
- }
- // 根据用户登录用户名获取用户信息
- item, err := system.GetSysAdminByName(req.AdminName)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminName:" + req.AdminName
- return
- }
- if item != nil && item.AdminId != req.AdminId {
- br.Msg = "账号名称已存在,请重新输入"
- return
- }
- // 手机号和邮箱必填一个
- req.Mobile = strings.TrimSpace(req.Mobile)
- req.Email = strings.TrimSpace(req.Email)
- if req.Mobile == "" && req.Email == "" {
- br.Msg = "至少输入一个手机号或邮箱"
- return
- }
- if req.Mobile != "" {
- if req.TelAreaCode == "86" {
- if !utils.ValidateMobileFormatat(req.Mobile) {
- br.Msg = "手机号格式有误, 请检查"
- return
- }
- }
- mobileCount, e := system.GetSysAdminCountByMobile(req.Mobile, adminInfo.AdminId)
- if e != nil {
- br.Msg = "判断手机号是否存在失败"
- br.ErrMsg = "判断手机号是否存在失败,Err:" + e.Error()
- return
- }
- if mobileCount > 0 {
- br.Msg = "手机号已存在,请重新填写"
- br.IsSendEmail = false
- return
- }
- }
- if req.Email != "" {
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "邮箱格式有误, 请检查"
- return
- }
- emailUser, e := system.GetSysUserByEmail(req.Email)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "邮箱获取用户信息失败, Err: " + e.Error()
- return
- }
- if emailUser != nil && emailUser.AdminId != adminInfo.AdminId {
- br.Msg = "邮箱已存在, 请检查"
- return
- }
- }
- // 角色
- var roleName string
- roleItem, err := system.GetSysRoleById(req.RoleId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "角色不存在,请重新选择"
- br.ErrMsg = "角色不存在"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
- return
- }
- if roleItem != nil {
- roleName = roleItem.RoleName
- }
- // 员工工号
- req.EmployeeId = strings.TrimSpace(req.EmployeeId)
- if req.EmployeeId != "" && utils.BusinessCode == utils.BusinessCodeRelease {
- // 去重
- countOB := new(system.Admin)
- countCond := ` AND employee_id = ? AND admin_id <> ?`
- countPars := make([]interface{}, 0)
- countPars = append(countPars, req.EmployeeId, req.AdminId)
- countEmployee, e := countOB.GetCountByCondition(countCond, countPars)
- if e != nil {
- br.Msg = "工号去重查询失败"
- br.ErrMsg = "工号去重查询失败, Err: " + e.Error()
- return
- }
- if countEmployee > 0 {
- br.Msg = "工号已存在"
- return
- }
- }
- // 管理权限
- var authority int
- if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- authority = 3
- } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
- authority = 4
- } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
- authority = 2
- } else {
- if strings.Contains(roleItem.RoleType, "销售主管") {
- authority = 2
- } else if strings.Contains(roleItem.RoleType, "部门经理") {
- authority = 1
- } else {
- authority = 0
- }
- }
- if adminInfo.RoleName == "admin" && adminInfo.RealName == "admin" {
- adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- adminInfo.Mobile = req.Mobile
- adminInfo.Position = req.Position
- adminInfo.Province = req.Province
- adminInfo.ProvinceCode = req.ProvinceCode
- adminInfo.City = req.City
- adminInfo.CityCode = req.CityCode
- adminInfo.EmployeeId = req.EmployeeId
- adminInfo.Email = req.Email
- adminInfo.TelAreaCode = req.TelAreaCode
- adminInfo.IsLdap = req.IsLdap
- cols := []string{
- "LastUpdatedTime", "Mobile", "Position", "Province", "ProvinceCode",
- "City", "CityCode", "EmployeeId", "Email", "TelAreaCode", "IsLdap",
- }
- if e := adminInfo.Update(cols); e != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "更新用户信息失败, Err:" + e.Error()
- return
- }
- } else {
- adminInfo.AdminName = req.AdminName
- adminInfo.RealName = req.RealName
- adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- adminInfo.Mobile = req.Mobile
- adminInfo.RoleId = req.RoleId
- adminInfo.RoleName = roleName
- adminInfo.Enabled = req.Enabled
- adminInfo.Authority = authority
- adminInfo.Position = req.Position
- adminInfo.RoleTypeCode = roleItem.RoleTypeCode
- adminInfo.Province = req.Province
- adminInfo.ProvinceCode = req.ProvinceCode
- adminInfo.City = req.City
- adminInfo.CityCode = req.CityCode
- adminInfo.EmployeeId = req.EmployeeId
- adminInfo.Email = req.Email
- adminInfo.TelAreaCode = req.TelAreaCode
- adminInfo.IsLdap = req.IsLdap
- cols := []string{
- "AdminName", "RealName", "LastUpdatedTime", "Mobile", "RoleId", "RoleName", "Enabled", "Authority",
- "Position", "RoleTypeCode", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "Email", "TelAreaCode", "IsLdap",
- }
- if e := adminInfo.Update(cols); e != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "更新用户信息失败, Err:" + e.Error()
- return
- }
- }
- // 同步用户缓存
- if utils.BusinessCode == utils.BusinessCodeRelease {
- var syncData system.SyncAdminData
- syncData.Source = utils.SOURCE_ETA_FLAG
- syncData.AdminName = adminInfo.AdminName
- _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
- // 研究方向分组
- if e := services.UpdateResearcherTagGroup(req.AdminId, req.ResearchGroupIds); e != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "更新用户研究方向失败, Err:" + e.Error()
- return
- }
- }
- // 用户登出
- logOutSystemUser(adminInfo.AdminId)
- // 清楚系统用户列表缓存key
- utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
- utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
- // 试用客户更新对应信息
- if utils.BusinessCode == utils.BusinessCodeSandbox && adminInfo.DepartmentName == "ETA试用客户" {
- go func() {
- var r etaTrialService.EtaTrialUserEditReq
- r.RealName = req.RealName
- r.Position = req.Position
- r.Mobile = adminInfo.Mobile
- r.Enabled = req.Enabled
- _, _ = etaTrialService.EditEtaTrialUser(r)
- }()
- }
- go eta_forum.AdminSave(adminInfo.AdminId)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "编辑成功"
- }
- // @Title 开启/禁用系统用户
- // @Description 开启/禁用系统用户接口
- // @Param request body system.SysuserEditReq true "type json string"
- // @Success 200 操作成功
- // @router /sysuser/editEnabled [post]
- func (this *SysAdminController) EditEnabled() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req system.SysuserEditEnabledReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- adminItem, err := system.GetSysAdminById(req.AdminId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
- return
- }
- if adminItem.RoleName == "admin" && adminItem.RealName == "admin" {
- br.Msg = "禁止对admin使用<禁用>功能"
- return
- }
- if req.Enabled == 0 {
- //禁用
- err = system.DisableAdmin(req.AdminId)
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
- return
- }
- // 更新试用平台客户
- if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
- go func() {
- var r etaTrialService.EtaTrialUserReq
- r.Mobile = adminItem.Mobile
- _, _ = etaTrialService.DisableEtaTrialUser(r)
- }()
- }
- } else {
- admin := new(system.Admin)
- admin.Enabled = req.Enabled
- admin.AdminId = req.AdminId
- err = system.EditAdminEnabled(admin)
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
- return
- }
- }
- // 同步用户缓存
- if utils.BusinessCode == utils.BusinessCodeRelease {
- var syncData system.SyncAdminData
- syncData.Source = utils.SOURCE_ETA_FLAG
- syncData.AdminName = adminItem.AdminName
- _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
- // 联动弘则研究公司下的联系人启禁用
- go services.SwitchHzUserEnabledByMobile(req.Enabled, adminItem.Mobile)
- }
- //用户被禁用的情况下,需要将他对应的token给过期
- if adminItem.Enabled == 1 && req.Enabled == 0 {
- logOutSystemUser(adminItem.AdminId)
- }
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- go eta_forum.AdminSave(adminItem.AdminId)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "操作成功"
- }
- // @Title 删除系统用户
- // @Description 删除系统用户接口
- // @Param request body system.SysuserDeleteReq true "type json string"
- // @Success 200 删除成功
- // @router /sysuser/delete [post]
- func (this *SysAdminController) Delete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req system.SysuserDeleteReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.AdminId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,GroupId 小于等于0 "
- return
- }
- mobile := ""
- adminInfo, _ := system.GetSysUserById(req.AdminId)
- if adminInfo != nil {
- mobile = adminInfo.Mobile
- }
- err = system.DeleteSysuser(req.AdminId)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + err.Error()
- return
- }
- // 同步用户缓存
- if utils.BusinessCode == utils.BusinessCodeRelease {
- var syncData system.SyncAdminData
- syncData.Source = utils.SOURCE_ETA_FLAG
- syncData.AdminName = adminInfo.AdminName
- _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
- // 删除弘则研究公司下的相同手机号联系人
- go services.DeleteHzUserByMobile(mobile)
- }
- // 删除手工数据关联用户
- {
- go data_manage.DeleteManualUser(req.AdminId)
- }
- // 清楚系统用户列表缓存key
- utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
- utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
- // 删除试用平台客户
- if utils.BusinessCode == utils.BusinessCodeSandbox && adminInfo.DepartmentName == "ETA试用客户" {
- go func() {
- var r etaTrialService.EtaTrialUserReq
- r.Mobile = mobile
- _, _ = etaTrialService.RemoveEtaTrialUser(r)
- }()
- }
- go eta_forum.AdminDelete(adminInfo.AdminName)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "删除成功"
- }
- /*
- func init() {
- fmt.Println("start")
- pwdStr:=`11111`
- //compile := `(\d+.*[^a-zA-Z]+){6,12}|([^a-zA-Z]+.*\d+){6,12}`
- compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
- ///^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/
- ///^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/
- reg := regexp.MustCompile(compile)
- flag := reg.MatchString(pwdStr)
- fmt.Println(flag)
- fmt.Println("end")
- }*/
- // SetConfig
- // @Title 用户信息配置接口
- // @Description 用户信息配置接口
- // @Success 200 {object} system.SetAdminConfigReq
- // @router /sysuser/config/set [post]
- func (this *SysAdminController) SetConfig() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req system.SetAdminConfigReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- item, err := system.GetConfigDetailByCode(this.SysUser.AdminId, req.ConfigCode)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- br.Msg = "设置失败"
- br.ErrMsg = "设置失败,Err:" + err.Error()
- return
- }
- // 获取默认配置
- defaultConfig, err := system.GetDefaultConfigDetailByCode(req.ConfigCode)
- if err != nil {
- br.Msg = "设置失败"
- br.ErrMsg = "获取默认配置失败,Err:" + err.Error()
- return
- }
- adminConfigItem := &system.AdminConfig{
- //ConfigId: 0,
- AdminId: this.SysUser.AdminId,
- ConfigCode: req.ConfigCode,
- ConfigValue: req.ConfigValue,
- Remark: defaultConfig.Remark,
- CreateTime: time.Now(),
- }
- err = adminConfigItem.Create()
- if err != nil {
- br.Msg = "设置失败"
- br.ErrMsg = "设置失败,Err:" + err.Error()
- return
- }
- } else {
- // 存在配置,那么就修改
- item.ConfigValue = req.ConfigValue
- err = item.Update([]string{"ConfigValue"})
- if err != nil {
- br.Msg = "设置失败"
- br.ErrMsg = "设置失败,Err:" + err.Error()
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "设置成功"
- }
- // logOutSystemUser 用户登出
- func logOutSystemUser(adminId int) {
- // 退出管理后台
- _ = system.ExpiredSysSessionByAdminId(adminId)
- // 退出随手办公小程序
- _ = system.UnBindAdminRecordByUserId(adminId)
- }
- // Move
- // @Title 移动分组
- // @Description 移动分组
- // @Param request body system.SysUserMoveReq true "type json string"
- // @Success 200 编辑成功
- // @router /sysuser/move [post]
- func (this *SysAdminController) Move() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req system.SysUserMoveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.AdminId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = "参数有误, AdminId"
- return
- }
- if req.DepartmentId <= 0 {
- br.Msg = "请选择部门"
- return
- }
- adminInfo, err := system.GetSysAdminById(req.AdminId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
- return
- }
- var departmentName, groupName, teamName string
- if req.DepartmentId > 0 {
- departmentItem, err := system.GetSysDepartmentById(req.DepartmentId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
- return
- }
- if departmentItem != nil {
- departmentName = departmentItem.DepartmentName
- }
- }
- if req.GroupId > 0 {
- groupItem, err := system.GetSysGroupById(req.GroupId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取分组数据失败,Err:" + err.Error()
- return
- }
- if groupItem != nil {
- groupName = groupItem.GroupName
- }
- }
- if req.TeamId > 0 {
- teamItem, err := system.GetSysGroupById(req.TeamId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取小组数据失败,Err:" + err.Error()
- return
- }
- if teamItem != nil {
- teamName = teamItem.GroupName
- }
- }
- adminInfo.DepartmentId = req.DepartmentId
- adminInfo.DepartmentName = departmentName
- if req.TeamId <= 0 {
- adminInfo.GroupId = req.GroupId
- adminInfo.GroupName = groupName
- } else {
- adminInfo.GroupId = req.TeamId
- adminInfo.GroupName = teamName
- }
- adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- if e := adminInfo.Update([]string{"DepartmentId", "DepartmentName", "GroupId", "GroupName", "LastUpdatedTime"}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新系统用户分组失败, Err: " + e.Error()
- return
- }
- // 修改客户关联的分组
- {
- if req.TeamId > 0 {
- if req.TeamId != adminInfo.GroupId {
- err = company.ModifyCompanyGroupId(req.AdminId, req.TeamId)
- if err != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "ModifyCompanyGroupId,Err:" + err.Error()
- return
- }
- }
- } else {
- if req.GroupId != adminInfo.GroupId {
- err = company.ModifyCompanyGroupId(req.AdminId, req.GroupId)
- if err != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "ModifyCompanyGroupId,Err:" + err.Error()
- return
- }
- }
- }
- if req.DepartmentId != adminInfo.DepartmentId {
- err = company.ModifyCompanyDepartmentId(req.AdminId, req.DepartmentId)
- if err != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "ModifyCompanyDepartmentId,Err:" + err.Error()
- return
- }
- }
- }
- // 同步用户缓存
- if utils.BusinessCode == utils.BusinessCodeRelease {
- var syncData system.SyncAdminData
- syncData.Source = utils.SOURCE_ETA_FLAG
- syncData.AdminName = adminInfo.AdminName
- _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
- }
- // 清除系统用户列表缓存key
- _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
- _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "操作成功"
- }
- // ResetPass
- // @Title 重置密码
- // @Description 重置密码
- // @Param request body system.SysUserResetPassReq true "type json string"
- // @Success 200 编辑成功
- // @router /sysuser/reset_pass [post]
- func (this *SysAdminController) ResetPass() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req system.SysUserResetPassReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.AdminId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = "参数有误, AdminId"
- return
- }
- req.Password = strings.TrimSpace(req.Password)
- if req.Password == "" {
- br.Msg = "新密码不能为空"
- return
- }
- req.RePassword = strings.TrimSpace(req.RePassword)
- if req.RePassword == "" {
- br.Msg = "确认密码不能为空"
- return
- }
- if req.Password != req.RePassword {
- br.Msg = "两次输入密码不一致,请重新填写"
- return
- }
- adminInfo, err := system.GetSysAdminById(req.AdminId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "用户已被删除, 请刷新页面"
- return
- }
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
- return
- }
- b, err := base64.StdEncoding.DecodeString(req.Password)
- if err != nil {
- br.Msg = "解析数据失败"
- br.ErrMsg = "解析数据失败,Err:" + err.Error()
- return
- }
- pwd := string(b)
- //pwd = strings.ToLower(pwd)
- pwd = utils.MD5(pwd)
- adminInfo.Password = pwd
- adminInfo.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
- adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- adminInfo.LastLoginTime = time.Now().Format(utils.FormatDateTime)
- if e := adminInfo.Update([]string{"Password", "LastUpdatedPasswordTime", "LastUpdatedTime", "LastLoginTime"}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新系统用户分组失败, Err: " + e.Error()
- return
- }
- // 同步用户缓存
- if utils.BusinessCode == utils.BusinessCodeRelease {
- var syncData system.SyncAdminData
- syncData.Source = utils.SOURCE_ETA_FLAG
- syncData.AdminName = adminInfo.AdminName
- _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
- }
- // 清除系统用户列表缓存key及异常登录缓存
- _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
- _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
- abnormalKey := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, adminInfo.AdminName)
- errPassKey := fmt.Sprint(utils.CACHE_LOGIN_ERR_PASS, adminInfo.AdminName)
- _ = utils.Rc.Delete(abnormalKey)
- _ = utils.Rc.Delete(errPassKey)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "重置密码成功"
- }
- // Add
- // @Title 用户详情信息
- // @Description 用户详情信息
- // @Param request body system.SysuserAddReq true "type json string"
- // @Success 200 新增成功
- // @router /sysuser/detail [get]
- func (this *SysAdminController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- br.Data = this.SysUser
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
|