12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310 |
- 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.CheckPwdV2(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
- }
- }
- 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 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)
- if !utils.CheckPwdV2(pwd) {
- br.Msg = "密码格式错误,请重新输入"
- return
- }
- 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 = "获取成功"
- }
|