sys_admin.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. package controllers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services"
  10. "eta/eta_api/services/eta_forum"
  11. etaTrialService "eta/eta_api/services/eta_trial"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. )
  19. // 系统用户
  20. type SysAdminController struct {
  21. BaseAuthController
  22. }
  23. // @Title 根据分组或者部门获取系统用户列表
  24. // @Description 根据分组或者部门获取系统用户列表接口
  25. // @Param PageSize query int true "每页数据条数"
  26. // @Param CurrentIndex query int true "当前页页码,从1开始"
  27. // @Param DepartmentId query int true "部门ID"
  28. // @Param GroupId query int true "分组ID"
  29. // @Param TeamId query int true "小组ID"
  30. // @Param RoleId query int true "角色ID"
  31. // @Param KeyWord query string true "搜索关键词"
  32. // @Success 200 {object} system.SysuserListResp
  33. // @router /sysuser/list [get]
  34. func (this *SysAdminController) ListSysuser() {
  35. br := new(models.BaseResponse).Init()
  36. defer func() {
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. sysUser := this.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. return
  45. }
  46. pageSize, _ := this.GetInt("PageSize")
  47. currentIndex, _ := this.GetInt("CurrentIndex")
  48. departmentId, _ := this.GetInt("DepartmentId")
  49. groupId, _ := this.GetInt("GroupId")
  50. teamId, _ := this.GetInt("TeamId")
  51. roleId, _ := this.GetInt("RoleId")
  52. keyWord := this.GetString("KeyWord")
  53. var startSize int
  54. if pageSize <= 0 {
  55. pageSize = utils.PageSize20
  56. }
  57. if currentIndex <= 0 {
  58. currentIndex = 1
  59. }
  60. startSize = utils.StartIndex(currentIndex, pageSize)
  61. var condition string
  62. var pars []interface{}
  63. if departmentId > 0 {
  64. condition += ` AND department_id=? `
  65. pars = append(pars, departmentId)
  66. }
  67. if roleId > 0 {
  68. condition += ` AND role_id=? `
  69. pars = append(pars, roleId)
  70. }
  71. if keyWord != "" {
  72. condition += ` AND (real_name LIKE ? OR admin_name LIKE ? OR mobile LIKE ? ) `
  73. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  74. }
  75. var total int
  76. var err error
  77. if teamId <= 0 {
  78. if groupId > 0 {
  79. ids, err := system.GetGroupIdByParentId(groupId)
  80. if err != nil {
  81. br.Msg = "获取失败"
  82. br.ErrMsg = "获取失败,Err:" + err.Error()
  83. return
  84. }
  85. sGroupId := strconv.Itoa(groupId)
  86. ids = append(ids, &sGroupId)
  87. var idList []string
  88. for _, id := range ids {
  89. idList = append(idList, *id)
  90. }
  91. sid := strings.Join(idList, ",")
  92. condition += ` AND group_id IN (` + sid + `) `
  93. }
  94. total, err = system.GetSysuserListCount(condition, pars)
  95. if err != nil {
  96. br.Msg = "获取失败"
  97. br.ErrMsg = "获取失败,Err:" + err.Error()
  98. return
  99. }
  100. } else {
  101. nCondition := condition + ` AND group_id=? `
  102. nPars := append(pars, teamId)
  103. total, err = system.GetSysuserListCount(nCondition, nPars)
  104. if err != nil {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = "获取失败,Err:" + err.Error()
  107. return
  108. }
  109. }
  110. var list []*system.AdminItem
  111. var roleIdList []int
  112. adminIdArr := make([]int, 0)
  113. if teamId <= 0 {
  114. //if groupId > 0 {
  115. // ids, err := system.GetGroupIdByParentId(groupId, startSize, pageSize)
  116. // if err != nil {
  117. // br.Msg = "获取失败"
  118. // br.ErrMsg = "获取失败,Err:" + err.Error()
  119. // return
  120. // }
  121. // sGroupId := strconv.Itoa(groupId)
  122. // ids = append(ids, &sGroupId)
  123. // var idList []string
  124. // for _, id := range ids {
  125. // idList = append(idList, *id)
  126. // }
  127. // sid := strings.Join(idList,",")
  128. //
  129. // condition += ` AND group_id IN (` + sid + `) `
  130. //}
  131. list, err = system.GetSysuserList(condition, pars, startSize, pageSize)
  132. if err != nil {
  133. br.Msg = "获取失败"
  134. br.ErrMsg = "获取失败,Err:" + err.Error()
  135. return
  136. }
  137. lenList := len(list)
  138. for i := 0; i < lenList; i++ {
  139. item := list[i]
  140. //adminIdArr = append(adminIdArr, strconv.Itoa(item.AdminId))
  141. adminIdArr = append(adminIdArr, item.AdminId)
  142. roleIdList = append(roleIdList, item.RoleId)
  143. var secondName *string
  144. if item.GroupId != 0 {
  145. pid, err := company.GetParentIdFromGroup(item.GroupId)
  146. if err != nil {
  147. br.Msg = "获取失败"
  148. br.ErrMsg = "查询父级id异常" + err.Error()
  149. return
  150. }
  151. if pid != nil && *pid != 0 {
  152. secondName, err = company.GetGroupNamesById(*pid)
  153. if err != nil {
  154. br.Msg = "获取失败"
  155. br.ErrMsg = "查询二级组名异常" + err.Error()
  156. return
  157. }
  158. }
  159. if *pid > 0 {
  160. list[i].TeamId = list[i].GroupId
  161. list[i].GroupId = *pid
  162. }
  163. }
  164. if item.DepartmentName != "" {
  165. if item.GroupName == "" {
  166. list[i].DepartmentGroup = item.DepartmentName
  167. } else if secondName != nil {
  168. list[i].DepartmentGroup = item.DepartmentName + "/" + *secondName + "/" + item.GroupName
  169. } else {
  170. list[i].DepartmentGroup = item.DepartmentName + "/" + item.GroupName
  171. }
  172. }
  173. labelVal := 0
  174. fmt.Println("item.RoleTypeCode:", item.RoleTypeCode)
  175. fmt.Println("item.Authority:", item.Authority)
  176. if item.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  177. labelVal = 1
  178. } else if item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  179. item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  180. labelVal = 2
  181. } else {
  182. if item.Authority == 1 {
  183. labelVal = 3
  184. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  185. labelVal = 4
  186. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  187. labelVal = 5
  188. } else if item.Authority == 4 {
  189. labelVal = 4
  190. }
  191. }
  192. list[i].LabelVal = labelVal
  193. }
  194. } else {
  195. condition += ` AND group_id=? `
  196. pars = append(pars, teamId)
  197. list, err = system.GetSysuserList(condition, pars, startSize, pageSize)
  198. if err != nil {
  199. br.Msg = "获取失败"
  200. br.ErrMsg = "获取失败,Err:" + err.Error()
  201. return
  202. }
  203. groupItem, err := system.GetSysGroupById(groupId)
  204. if err != nil {
  205. br.Msg = "获取失败"
  206. br.ErrMsg = "获取失败,Err:" + err.Error()
  207. return
  208. }
  209. lenList := len(list)
  210. for i := 0; i < lenList; i++ {
  211. item := list[i]
  212. //adminIdArr = append(adminIdArr, strconv.Itoa(item.AdminId))
  213. adminIdArr = append(adminIdArr, item.AdminId)
  214. roleIdList = append(roleIdList, item.RoleId)
  215. if item.DepartmentName != "" {
  216. if item.GroupName == "" {
  217. list[i].DepartmentGroup = item.DepartmentName
  218. } else if item.GroupName != "" {
  219. list[i].DepartmentGroup = item.DepartmentName + "/" + groupItem.GroupName + "/" + item.GroupName
  220. } else {
  221. list[i].DepartmentGroup = item.DepartmentName + "/" + item.GroupName
  222. }
  223. }
  224. labelVal := 0
  225. if item.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  226. labelVal = 1
  227. } else if item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  228. item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  229. labelVal = 2
  230. } else {
  231. if item.Authority == 1 {
  232. labelVal = 3
  233. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  234. labelVal = 4
  235. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  236. labelVal = 5
  237. } else if item.Authority == 4 {
  238. labelVal = 4
  239. }
  240. }
  241. list[i].LabelVal = labelVal
  242. }
  243. }
  244. //if teamId > 0 {
  245. // var groupIdSlice []string
  246. // var groupIds string
  247. // for _, item := range list {
  248. // groupIdSlice = append(groupIdSlice, strconv.Itoa(groupId))
  249. // }
  250. // groupIds = strings.Join(groupIdSlice, ",")
  251. // fmt.Println("groupIds:", groupIds)
  252. //}
  253. if teamId > 0 {
  254. for _, item := range list {
  255. item.TeamId = teamId
  256. item.GroupId = groupId
  257. }
  258. }
  259. // 研究方向分组
  260. if len(adminIdArr) > 0 && utils.BusinessCode == utils.BusinessCodeRelease {
  261. //adminIds := strings.Join(adminIdArr, ",")
  262. //researchGroupList, e := system.GetAdminResearchGroupListByAdminId(adminIds)
  263. researchGroupList, e := models.GetAdminVarietyTagRelationListByAdminId(adminIdArr)
  264. if e != nil {
  265. br.Msg = "获取失败"
  266. br.ErrMsg = "获取研究方向分组列表失败, Err:" + e.Error()
  267. return
  268. }
  269. researchGroupAdminIdName := make(map[int]string, 0)
  270. for _, g := range researchGroupList {
  271. researchGroupAdminIdName[g.AdminId] = g.GroupNameStr
  272. }
  273. for _, v := range list {
  274. v.ResearchGroupName = researchGroupAdminIdName[v.AdminId]
  275. }
  276. }
  277. roles, err := system.GetSysRoleByIdList(roleIdList)
  278. if err != nil {
  279. br.ErrMsg = "获取角色信息失败,Err:" + err.Error()
  280. } else {
  281. roleMap := make(map[int]string)
  282. for _, v := range roles {
  283. roleMap[v.RoleId] = v.RoleName
  284. }
  285. for _, v := range list {
  286. if mv, ok := roleMap[v.RoleId]; ok {
  287. v.RoleName = mv
  288. }
  289. }
  290. }
  291. fmt.Println("teamId:", teamId)
  292. page := paging.GetPaging(currentIndex, pageSize, total)
  293. resp := new(system.SysuserListResp)
  294. resp.List = list
  295. resp.Paging = page
  296. br.Ret = 200
  297. br.Success = true
  298. br.Msg = "获取成功"
  299. br.Data = resp
  300. }
  301. // Add
  302. // @Title 新增系统用户
  303. // @Description 新增系统用户接口
  304. // @Param request body system.SysuserAddReq true "type json string"
  305. // @Success 200 新增成功
  306. // @router /sysuser/add [post]
  307. func (this *SysAdminController) Add() {
  308. br := new(models.BaseResponse).Init()
  309. defer func() {
  310. this.Data["json"] = br
  311. this.ServeJSON()
  312. }()
  313. var req system.SysuserAddReq
  314. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  315. if err != nil {
  316. br.Msg = "参数解析异常!"
  317. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  318. return
  319. }
  320. if req.RoleId <= 0 {
  321. br.Msg = "请选择角色"
  322. br.ErrMsg = "角色ID小于等于0"
  323. return
  324. }
  325. count, err := system.GetSysAdminCount(req.AdminName)
  326. if err != nil {
  327. br.Msg = "获取数据失败"
  328. br.ErrMsg = "获取数据失败"
  329. return
  330. }
  331. if count > 0 {
  332. br.Msg = "账号已存在,请重新输入"
  333. br.IsSendEmail = false
  334. return
  335. }
  336. // 手机号和邮箱必填一个
  337. req.Mobile = strings.TrimSpace(req.Mobile)
  338. req.Email = strings.TrimSpace(req.Email)
  339. if req.Mobile == "" && req.Email == "" {
  340. br.Msg = "至少输入一个手机号或邮箱"
  341. return
  342. }
  343. if req.Mobile != "" {
  344. if req.TelAreaCode == "86" {
  345. if !utils.ValidateMobileFormatat(req.Mobile) {
  346. br.Msg = "手机号格式有误, 请检查"
  347. return
  348. }
  349. }
  350. mobileCount, err := system.GetSysAdminCountByMobile(req.Mobile, 0)
  351. if err != nil {
  352. br.Msg = "判断手机号是否存在失败"
  353. br.ErrMsg = "判断手机号是否存在失败,Err:" + err.Error()
  354. return
  355. }
  356. if mobileCount > 0 {
  357. br.Msg = "手机号已存在,请重新填写"
  358. br.IsSendEmail = false
  359. return
  360. }
  361. }
  362. if req.Email != "" {
  363. if !utils.ValidateEmailFormatat(req.Email) {
  364. br.Msg = "邮箱格式有误, 请检查"
  365. return
  366. }
  367. emailUser, e := system.GetSysUserByEmail(req.Email)
  368. if e != nil && e.Error() != utils.ErrNoRow() {
  369. br.Msg = "操作失败"
  370. br.ErrMsg = "邮箱获取用户失败"
  371. return
  372. }
  373. if emailUser != nil && emailUser.AdminId > 0 {
  374. br.Msg = "邮箱已存在, 请重新填写"
  375. return
  376. }
  377. }
  378. var roleName, departmentName, groupName, teamName string
  379. roleItem, err := system.GetSysRoleById(req.RoleId)
  380. if err != nil {
  381. if err.Error() == utils.ErrNoRow() {
  382. br.Msg = "所选角色不存在"
  383. return
  384. }
  385. br.Msg = "获取数据失败"
  386. br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
  387. return
  388. }
  389. if roleItem != nil {
  390. roleName = roleItem.RoleName
  391. }
  392. if req.DepartmentId > 0 {
  393. departmentItem, err := system.GetSysDepartmentById(req.DepartmentId)
  394. if err != nil {
  395. if err.Error() == utils.ErrNoRow() {
  396. br.Msg = "所选部门不存在"
  397. return
  398. }
  399. br.Msg = "获取数据失败"
  400. br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
  401. return
  402. }
  403. if departmentItem != nil {
  404. departmentName = departmentItem.DepartmentName
  405. }
  406. }
  407. if req.GroupId > 0 {
  408. groupItem, err := system.GetSysGroupById(req.GroupId)
  409. if err != nil {
  410. if err.Error() == utils.ErrNoRow() {
  411. br.Msg = "所选分组不存在"
  412. return
  413. }
  414. br.Msg = "获取数据失败"
  415. br.ErrMsg = "获取分组数据失败,Err:" + err.Error()
  416. return
  417. }
  418. if groupItem != nil {
  419. groupName = groupItem.GroupName
  420. }
  421. }
  422. if req.TeamId > 0 {
  423. teamItem, err := system.GetSysGroupById(req.TeamId)
  424. if err != nil {
  425. if err.Error() == utils.ErrNoRow() {
  426. br.Msg = "所选小组不存在"
  427. return
  428. }
  429. br.Msg = "获取数据失败"
  430. br.ErrMsg = "获取小组数据失败,Err:" + err.Error()
  431. return
  432. }
  433. if teamItem != nil {
  434. teamName = teamItem.GroupName
  435. }
  436. }
  437. var originPass, pass string
  438. if req.Password != "" {
  439. pwdByte, e := base64.StdEncoding.DecodeString(req.Password)
  440. if e != nil {
  441. br.Msg = "解析数据失败"
  442. br.ErrMsg = "解析数据失败,Err:" + e.Error()
  443. return
  444. }
  445. originPass = string(pwdByte)
  446. }
  447. // 系统用户-密码必填且需要校验密码格式
  448. if req.IsLdap == 0 {
  449. if originPass == "" {
  450. br.Msg = "请输入密码"
  451. return
  452. }
  453. if !utils.CheckPwdV2(originPass) {
  454. br.Msg = "密码格式错误,请重新输入"
  455. return
  456. }
  457. pass = utils.MD5(originPass)
  458. }
  459. // 域用户-密码非必填(实际登录用不到这个密码)
  460. if req.IsLdap == 1 {
  461. if originPass != "" {
  462. pass = utils.MD5(originPass)
  463. } else {
  464. // 未填写的话给个初始密码即可
  465. pass = utils.MD5(utils.LdapInitPassword)
  466. }
  467. }
  468. // 员工工号
  469. req.EmployeeId = strings.TrimSpace(req.EmployeeId)
  470. admin := new(system.Admin)
  471. admin.AdminName = req.AdminName
  472. admin.RealName = req.RealName
  473. admin.Password = pass
  474. admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  475. admin.Enabled = 1
  476. admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  477. admin.CreatedTime = time.Now()
  478. admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  479. admin.Mobile = req.Mobile
  480. admin.RoleType = 0
  481. admin.RoleId = req.RoleId
  482. admin.RoleName = roleName
  483. admin.RoleTypeCode = roleItem.RoleTypeCode
  484. admin.DepartmentId = req.DepartmentId
  485. admin.DepartmentName = departmentName
  486. admin.AdminAvatar = req.AdminAvatar
  487. if req.TeamId <= 0 {
  488. admin.GroupId = req.GroupId
  489. admin.GroupName = groupName
  490. } else {
  491. admin.GroupId = req.TeamId
  492. admin.GroupName = teamName
  493. }
  494. if strings.Contains(admin.RoleTypeCode, "researcher") {
  495. admin.Role = "researcher"
  496. } else if strings.Contains(admin.RoleTypeCode, "seller") {
  497. admin.Role = "sales"
  498. } else {
  499. admin.Role = "admin"
  500. }
  501. admin.EmployeeId = req.EmployeeId
  502. admin.Email = req.Email
  503. var authority int
  504. if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  505. authority = 3
  506. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  507. authority = 4
  508. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  509. authority = 2
  510. } else {
  511. if strings.Contains(roleItem.RoleType, "销售主管") {
  512. authority = 2
  513. } else if strings.Contains(roleItem.RoleType, "部门经理") {
  514. authority = 1
  515. } else {
  516. authority = 0
  517. }
  518. }
  519. admin.Authority = authority
  520. admin.Position = req.Position
  521. admin.Province = req.Province
  522. admin.ProvinceCode = req.ProvinceCode
  523. admin.City = req.City
  524. admin.CityCode = req.CityCode
  525. admin.TelAreaCode = req.TelAreaCode
  526. admin.IsLdap = req.IsLdap
  527. err = system.AddAdmin(admin)
  528. if err != nil {
  529. br.Msg = "新增失败"
  530. br.ErrMsg = "新增失败,Err:" + err.Error()
  531. return
  532. }
  533. // 同步用户缓存
  534. if utils.BusinessCode == utils.BusinessCodeRelease {
  535. var syncData system.SyncAdminData
  536. syncData.Source = utils.SOURCE_ETA_FLAG
  537. syncData.AdminName = admin.AdminName
  538. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  539. e := services.UpdateResearcherTagGroup(admin.AdminId, req.ResearchGroupIds)
  540. if e != nil {
  541. br.Msg = "新增失败"
  542. br.ErrMsg = "新增用户研究方向分组失败, Err:" + e.Error()
  543. return
  544. }
  545. }
  546. // 清楚系统用户列表缓存key
  547. utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  548. utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  549. //thirdGroupId := 0 //角色:2=研究员,3=机构销售
  550. if admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
  551. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
  552. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
  553. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
  554. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  555. //thirdGroupId = 2
  556. } else if admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER ||
  557. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP ||
  558. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  559. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  560. //thirdGroupId = 3
  561. }
  562. //if thirdGroupId > 0 {
  563. // go roadshow.CreateOaUser(admin.Mobile, admin.RealName, thirdGroupId)
  564. //}
  565. // 新增弘则联系人
  566. //go services.AddHzCompanyUser(admin.Mobile, admin.RealName, this.SysUser.AdminId, this.SysUser.RealName)
  567. go eta_forum.AdminSave(admin.AdminId)
  568. br.Ret = 200
  569. br.Success = true
  570. br.Msg = "新增成功"
  571. }
  572. // Edit
  573. // @Title 编辑系统用户
  574. // @Description 编辑系统用户接口
  575. // @Param request body system.SysuserEditReq true "type json string"
  576. // @Success 200 编辑成功
  577. // @router /sysuser/edit [post]
  578. func (this *SysAdminController) Edit() {
  579. br := new(models.BaseResponse).Init()
  580. defer func() {
  581. this.Data["json"] = br
  582. this.ServeJSON()
  583. }()
  584. var req system.SysuserEditReq
  585. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  586. if err != nil {
  587. br.Msg = "参数解析异常!"
  588. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  589. return
  590. }
  591. if req.RoleId <= 0 {
  592. br.Msg = "请选择角色"
  593. br.ErrMsg = "角色ID小于等于0"
  594. return
  595. }
  596. //根据用户id获取用户信息
  597. adminInfo, err := system.GetSysAdminById(req.AdminId)
  598. if err != nil {
  599. if err.Error() == utils.ErrNoRow() {
  600. br.Msg = "用户已被删除, 请刷新页面"
  601. return
  602. }
  603. br.Msg = "获取数据失败"
  604. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
  605. return
  606. }
  607. // 根据用户登录用户名获取用户信息
  608. item, err := system.GetSysAdminByName(req.AdminName)
  609. if err != nil && err.Error() != utils.ErrNoRow() {
  610. br.Msg = "获取数据失败"
  611. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminName:" + req.AdminName
  612. return
  613. }
  614. if item != nil && item.AdminId != req.AdminId {
  615. br.Msg = "账号名称已存在,请重新输入"
  616. return
  617. }
  618. // 手机号和邮箱必填一个
  619. req.Mobile = strings.TrimSpace(req.Mobile)
  620. req.Email = strings.TrimSpace(req.Email)
  621. if req.Mobile == "" && req.Email == "" {
  622. br.Msg = "至少输入一个手机号或邮箱"
  623. return
  624. }
  625. if req.Mobile != "" {
  626. if req.TelAreaCode == "86" {
  627. if !utils.ValidateMobileFormatat(req.Mobile) {
  628. br.Msg = "手机号格式有误, 请检查"
  629. return
  630. }
  631. }
  632. mobileCount, e := system.GetSysAdminCountByMobile(req.Mobile, adminInfo.AdminId)
  633. if e != nil {
  634. br.Msg = "判断手机号是否存在失败"
  635. br.ErrMsg = "判断手机号是否存在失败,Err:" + e.Error()
  636. return
  637. }
  638. if mobileCount > 0 {
  639. br.Msg = "手机号已存在,请重新填写"
  640. br.IsSendEmail = false
  641. return
  642. }
  643. }
  644. if req.Email != "" {
  645. if !utils.ValidateEmailFormatat(req.Email) {
  646. br.Msg = "邮箱格式有误, 请检查"
  647. return
  648. }
  649. emailUser, e := system.GetSysUserByEmail(req.Email)
  650. if e != nil && e.Error() != utils.ErrNoRow() {
  651. br.Msg = "操作失败"
  652. br.ErrMsg = "邮箱获取用户信息失败, Err: " + e.Error()
  653. return
  654. }
  655. if emailUser != nil && emailUser.AdminId != adminInfo.AdminId {
  656. br.Msg = "邮箱已存在, 请检查"
  657. return
  658. }
  659. }
  660. // 角色
  661. var roleName string
  662. roleItem, err := system.GetSysRoleById(req.RoleId)
  663. if err != nil {
  664. if err.Error() == utils.ErrNoRow() {
  665. br.Msg = "角色不存在,请重新选择"
  666. br.ErrMsg = "角色不存在"
  667. return
  668. }
  669. br.Msg = "获取数据失败"
  670. br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
  671. return
  672. }
  673. if roleItem != nil {
  674. roleName = roleItem.RoleName
  675. }
  676. // 员工工号
  677. req.EmployeeId = strings.TrimSpace(req.EmployeeId)
  678. if req.EmployeeId != "" && utils.BusinessCode == utils.BusinessCodeRelease {
  679. // 去重
  680. countOB := new(system.Admin)
  681. countCond := ` AND employee_id = ? AND admin_id <> ?`
  682. countPars := make([]interface{}, 0)
  683. countPars = append(countPars, req.EmployeeId, req.AdminId)
  684. countEmployee, e := countOB.GetCountByCondition(countCond, countPars)
  685. if e != nil {
  686. br.Msg = "工号去重查询失败"
  687. br.ErrMsg = "工号去重查询失败, Err: " + e.Error()
  688. return
  689. }
  690. if countEmployee > 0 {
  691. br.Msg = "工号已存在"
  692. return
  693. }
  694. }
  695. // 管理权限
  696. var authority int
  697. if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  698. authority = 3
  699. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  700. authority = 4
  701. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  702. authority = 2
  703. } else {
  704. if strings.Contains(roleItem.RoleType, "销售主管") {
  705. authority = 2
  706. } else if strings.Contains(roleItem.RoleType, "部门经理") {
  707. authority = 1
  708. } else {
  709. authority = 0
  710. }
  711. }
  712. if adminInfo.RoleName == "admin" && adminInfo.RealName == "admin" {
  713. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  714. adminInfo.Mobile = req.Mobile
  715. adminInfo.Position = req.Position
  716. adminInfo.Province = req.Province
  717. adminInfo.ProvinceCode = req.ProvinceCode
  718. adminInfo.City = req.City
  719. adminInfo.CityCode = req.CityCode
  720. adminInfo.EmployeeId = req.EmployeeId
  721. adminInfo.Email = req.Email
  722. adminInfo.TelAreaCode = req.TelAreaCode
  723. adminInfo.IsLdap = req.IsLdap
  724. cols := []string{
  725. "LastUpdatedTime", "Mobile", "Position", "Province", "ProvinceCode",
  726. "City", "CityCode", "EmployeeId", "Email", "TelAreaCode", "IsLdap",
  727. }
  728. if e := adminInfo.Update(cols); e != nil {
  729. br.Msg = "编辑失败"
  730. br.ErrMsg = "更新用户信息失败, Err:" + e.Error()
  731. return
  732. }
  733. } else {
  734. adminInfo.AdminName = req.AdminName
  735. adminInfo.RealName = req.RealName
  736. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  737. adminInfo.Mobile = req.Mobile
  738. adminInfo.RoleId = req.RoleId
  739. adminInfo.RoleName = roleName
  740. adminInfo.Enabled = req.Enabled
  741. adminInfo.Authority = authority
  742. adminInfo.Position = req.Position
  743. adminInfo.RoleTypeCode = roleItem.RoleTypeCode
  744. adminInfo.Province = req.Province
  745. adminInfo.ProvinceCode = req.ProvinceCode
  746. adminInfo.City = req.City
  747. adminInfo.CityCode = req.CityCode
  748. adminInfo.EmployeeId = req.EmployeeId
  749. adminInfo.Email = req.Email
  750. adminInfo.TelAreaCode = req.TelAreaCode
  751. adminInfo.IsLdap = req.IsLdap
  752. cols := []string{
  753. "AdminName", "RealName", "LastUpdatedTime", "Mobile", "RoleId", "RoleName", "Enabled", "Authority",
  754. "Position", "RoleTypeCode", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "Email", "TelAreaCode", "IsLdap",
  755. }
  756. if e := adminInfo.Update(cols); e != nil {
  757. br.Msg = "编辑失败"
  758. br.ErrMsg = "更新用户信息失败, Err:" + e.Error()
  759. return
  760. }
  761. }
  762. // 同步用户缓存
  763. if utils.BusinessCode == utils.BusinessCodeRelease {
  764. var syncData system.SyncAdminData
  765. syncData.Source = utils.SOURCE_ETA_FLAG
  766. syncData.AdminName = adminInfo.AdminName
  767. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  768. // 研究方向分组
  769. if e := services.UpdateResearcherTagGroup(req.AdminId, req.ResearchGroupIds); e != nil {
  770. br.Msg = "编辑失败"
  771. br.ErrMsg = "更新用户研究方向失败, Err:" + e.Error()
  772. return
  773. }
  774. }
  775. // 用户登出
  776. logOutSystemUser(adminInfo.AdminId)
  777. // 清楚系统用户列表缓存key
  778. utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  779. utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  780. // 试用客户更新对应信息
  781. if utils.BusinessCode == utils.BusinessCodeSandbox && adminInfo.DepartmentName == "ETA试用客户" {
  782. go func() {
  783. var r etaTrialService.EtaTrialUserEditReq
  784. r.RealName = req.RealName
  785. r.Position = req.Position
  786. r.Mobile = adminInfo.Mobile
  787. r.Enabled = req.Enabled
  788. _, _ = etaTrialService.EditEtaTrialUser(r)
  789. }()
  790. }
  791. go eta_forum.AdminSave(adminInfo.AdminId)
  792. br.Ret = 200
  793. br.Success = true
  794. br.IsAddLog = true
  795. br.Msg = "编辑成功"
  796. }
  797. // @Title 开启/禁用系统用户
  798. // @Description 开启/禁用系统用户接口
  799. // @Param request body system.SysuserEditReq true "type json string"
  800. // @Success 200 操作成功
  801. // @router /sysuser/editEnabled [post]
  802. func (this *SysAdminController) EditEnabled() {
  803. br := new(models.BaseResponse).Init()
  804. defer func() {
  805. this.Data["json"] = br
  806. this.ServeJSON()
  807. }()
  808. var req system.SysuserEditEnabledReq
  809. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  810. if err != nil {
  811. br.Msg = "参数解析异常!"
  812. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  813. return
  814. }
  815. adminItem, err := system.GetSysAdminById(req.AdminId)
  816. if err != nil {
  817. br.Msg = "获取数据失败"
  818. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  819. return
  820. }
  821. if adminItem.RoleName == "admin" && adminItem.RealName == "admin" {
  822. br.Msg = "禁止对admin使用<禁用>功能"
  823. return
  824. }
  825. if req.Enabled == 0 {
  826. //禁用
  827. err = system.DisableAdmin(req.AdminId)
  828. if err != nil {
  829. br.Msg = "修改失败"
  830. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  831. return
  832. }
  833. // 更新试用平台客户
  834. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  835. go func() {
  836. var r etaTrialService.EtaTrialUserReq
  837. r.Mobile = adminItem.Mobile
  838. _, _ = etaTrialService.DisableEtaTrialUser(r)
  839. }()
  840. }
  841. } else {
  842. admin := new(system.Admin)
  843. admin.Enabled = req.Enabled
  844. admin.AdminId = req.AdminId
  845. err = system.EditAdminEnabled(admin)
  846. if err != nil {
  847. br.Msg = "修改失败"
  848. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  849. return
  850. }
  851. }
  852. // 同步用户缓存
  853. if utils.BusinessCode == utils.BusinessCodeRelease {
  854. var syncData system.SyncAdminData
  855. syncData.Source = utils.SOURCE_ETA_FLAG
  856. syncData.AdminName = adminItem.AdminName
  857. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  858. // 联动弘则研究公司下的联系人启禁用
  859. go services.SwitchHzUserEnabledByMobile(req.Enabled, adminItem.Mobile)
  860. }
  861. //用户被禁用的情况下,需要将他对应的token给过期
  862. if adminItem.Enabled == 1 && req.Enabled == 0 {
  863. logOutSystemUser(adminItem.AdminId)
  864. }
  865. if err != nil {
  866. br.Msg = "操作失败"
  867. br.ErrMsg = "操作失败,Err:" + err.Error()
  868. return
  869. }
  870. go eta_forum.AdminSave(adminItem.AdminId)
  871. br.Ret = 200
  872. br.Success = true
  873. br.IsAddLog = true
  874. br.Msg = "操作成功"
  875. }
  876. // @Title 删除系统用户
  877. // @Description 删除系统用户接口
  878. // @Param request body system.SysuserDeleteReq true "type json string"
  879. // @Success 200 删除成功
  880. // @router /sysuser/delete [post]
  881. func (this *SysAdminController) Delete() {
  882. br := new(models.BaseResponse).Init()
  883. defer func() {
  884. this.Data["json"] = br
  885. this.ServeJSON()
  886. }()
  887. var req system.SysuserDeleteReq
  888. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  889. if err != nil {
  890. br.Msg = "参数解析异常!"
  891. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  892. return
  893. }
  894. if req.AdminId <= 0 {
  895. br.Msg = "参数错误"
  896. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  897. return
  898. }
  899. mobile := ""
  900. adminInfo, _ := system.GetSysUserById(req.AdminId)
  901. if adminInfo != nil {
  902. mobile = adminInfo.Mobile
  903. }
  904. err = system.DeleteSysuser(req.AdminId)
  905. if err != nil {
  906. br.Msg = "删除失败"
  907. br.ErrMsg = "删除失败,Err:" + err.Error()
  908. return
  909. }
  910. // 同步用户缓存
  911. if utils.BusinessCode == utils.BusinessCodeRelease {
  912. var syncData system.SyncAdminData
  913. syncData.Source = utils.SOURCE_ETA_FLAG
  914. syncData.AdminName = adminInfo.AdminName
  915. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  916. // 删除弘则研究公司下的相同手机号联系人
  917. go services.DeleteHzUserByMobile(mobile)
  918. }
  919. // 删除手工数据关联用户
  920. {
  921. go data_manage.DeleteManualUser(req.AdminId)
  922. }
  923. // 清楚系统用户列表缓存key
  924. utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  925. utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  926. // 删除试用平台客户
  927. if utils.BusinessCode == utils.BusinessCodeSandbox && adminInfo.DepartmentName == "ETA试用客户" {
  928. go func() {
  929. var r etaTrialService.EtaTrialUserReq
  930. r.Mobile = mobile
  931. _, _ = etaTrialService.RemoveEtaTrialUser(r)
  932. }()
  933. }
  934. go eta_forum.AdminDelete(adminInfo.AdminName)
  935. br.Ret = 200
  936. br.Success = true
  937. br.IsAddLog = true
  938. br.Msg = "删除成功"
  939. }
  940. /*
  941. func init() {
  942. fmt.Println("start")
  943. pwdStr:=`11111`
  944. //compile := `(\d+.*[^a-zA-Z]+){6,12}|([^a-zA-Z]+.*\d+){6,12}`
  945. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  946. ///^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/
  947. ///^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/
  948. reg := regexp.MustCompile(compile)
  949. flag := reg.MatchString(pwdStr)
  950. fmt.Println(flag)
  951. fmt.Println("end")
  952. }*/
  953. // SetConfig
  954. // @Title 用户信息配置接口
  955. // @Description 用户信息配置接口
  956. // @Success 200 {object} system.SetAdminConfigReq
  957. // @router /sysuser/config/set [post]
  958. func (this *SysAdminController) SetConfig() {
  959. br := new(models.BaseResponse).Init()
  960. defer func() {
  961. this.Data["json"] = br
  962. this.ServeJSON()
  963. }()
  964. sysUser := this.SysUser
  965. if sysUser == nil {
  966. br.Msg = "请登录"
  967. br.ErrMsg = "请登录,SysUser Is Empty"
  968. br.Ret = 408
  969. return
  970. }
  971. var req system.SetAdminConfigReq
  972. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  973. if err != nil {
  974. br.Msg = "参数解析异常!"
  975. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  976. return
  977. }
  978. item, err := system.GetConfigDetailByCode(this.SysUser.AdminId, req.ConfigCode)
  979. if err != nil {
  980. if err.Error() != utils.ErrNoRow() {
  981. br.Msg = "设置失败"
  982. br.ErrMsg = "设置失败,Err:" + err.Error()
  983. return
  984. }
  985. // 获取默认配置
  986. defaultConfig, err := system.GetDefaultConfigDetailByCode(req.ConfigCode)
  987. if err != nil {
  988. br.Msg = "设置失败"
  989. br.ErrMsg = "获取默认配置失败,Err:" + err.Error()
  990. return
  991. }
  992. adminConfigItem := &system.AdminConfig{
  993. //ConfigId: 0,
  994. AdminId: this.SysUser.AdminId,
  995. ConfigCode: req.ConfigCode,
  996. ConfigValue: req.ConfigValue,
  997. Remark: defaultConfig.Remark,
  998. CreateTime: time.Now(),
  999. }
  1000. err = adminConfigItem.Create()
  1001. if err != nil {
  1002. br.Msg = "设置失败"
  1003. br.ErrMsg = "设置失败,Err:" + err.Error()
  1004. return
  1005. }
  1006. } else {
  1007. // 存在配置,那么就修改
  1008. item.ConfigValue = req.ConfigValue
  1009. err = item.Update([]string{"ConfigValue"})
  1010. if err != nil {
  1011. br.Msg = "设置失败"
  1012. br.ErrMsg = "设置失败,Err:" + err.Error()
  1013. return
  1014. }
  1015. }
  1016. br.Ret = 200
  1017. br.Success = true
  1018. br.Msg = "设置成功"
  1019. }
  1020. // logOutSystemUser 用户登出
  1021. func logOutSystemUser(adminId int) {
  1022. // 退出管理后台
  1023. _ = system.ExpiredSysSessionByAdminId(adminId)
  1024. // 退出随手办公小程序
  1025. _ = system.UnBindAdminRecordByUserId(adminId)
  1026. }
  1027. // Move
  1028. // @Title 移动分组
  1029. // @Description 移动分组
  1030. // @Param request body system.SysUserMoveReq true "type json string"
  1031. // @Success 200 编辑成功
  1032. // @router /sysuser/move [post]
  1033. func (this *SysAdminController) Move() {
  1034. br := new(models.BaseResponse).Init()
  1035. defer func() {
  1036. this.Data["json"] = br
  1037. this.ServeJSON()
  1038. }()
  1039. var req system.SysUserMoveReq
  1040. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1041. if err != nil {
  1042. br.Msg = "参数解析异常!"
  1043. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1044. return
  1045. }
  1046. if req.AdminId <= 0 {
  1047. br.Msg = "参数有误"
  1048. br.ErrMsg = "参数有误, AdminId"
  1049. return
  1050. }
  1051. if req.DepartmentId <= 0 {
  1052. br.Msg = "请选择部门"
  1053. return
  1054. }
  1055. adminInfo, err := system.GetSysAdminById(req.AdminId)
  1056. if err != nil && err.Error() != utils.ErrNoRow() {
  1057. br.Msg = "获取数据失败"
  1058. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
  1059. return
  1060. }
  1061. var departmentName, groupName, teamName string
  1062. if req.DepartmentId > 0 {
  1063. departmentItem, err := system.GetSysDepartmentById(req.DepartmentId)
  1064. if err != nil {
  1065. br.Msg = "获取数据失败"
  1066. br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
  1067. return
  1068. }
  1069. if departmentItem != nil {
  1070. departmentName = departmentItem.DepartmentName
  1071. }
  1072. }
  1073. if req.GroupId > 0 {
  1074. groupItem, err := system.GetSysGroupById(req.GroupId)
  1075. if err != nil {
  1076. br.Msg = "获取数据失败"
  1077. br.ErrMsg = "获取分组数据失败,Err:" + err.Error()
  1078. return
  1079. }
  1080. if groupItem != nil {
  1081. groupName = groupItem.GroupName
  1082. }
  1083. }
  1084. if req.TeamId > 0 {
  1085. teamItem, err := system.GetSysGroupById(req.TeamId)
  1086. if err != nil {
  1087. br.Msg = "获取数据失败"
  1088. br.ErrMsg = "获取小组数据失败,Err:" + err.Error()
  1089. return
  1090. }
  1091. if teamItem != nil {
  1092. teamName = teamItem.GroupName
  1093. }
  1094. }
  1095. adminInfo.DepartmentId = req.DepartmentId
  1096. adminInfo.DepartmentName = departmentName
  1097. if req.TeamId <= 0 {
  1098. adminInfo.GroupId = req.GroupId
  1099. adminInfo.GroupName = groupName
  1100. } else {
  1101. adminInfo.GroupId = req.TeamId
  1102. adminInfo.GroupName = teamName
  1103. }
  1104. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  1105. if e := adminInfo.Update([]string{"DepartmentId", "DepartmentName", "GroupId", "GroupName", "LastUpdatedTime"}); e != nil {
  1106. br.Msg = "操作失败"
  1107. br.ErrMsg = "更新系统用户分组失败, Err: " + e.Error()
  1108. return
  1109. }
  1110. // 修改客户关联的分组
  1111. {
  1112. if req.TeamId > 0 {
  1113. if req.TeamId != adminInfo.GroupId {
  1114. err = company.ModifyCompanyGroupId(req.AdminId, req.TeamId)
  1115. if err != nil {
  1116. br.Msg = "编辑失败"
  1117. br.ErrMsg = "ModifyCompanyGroupId,Err:" + err.Error()
  1118. return
  1119. }
  1120. }
  1121. } else {
  1122. if req.GroupId != adminInfo.GroupId {
  1123. err = company.ModifyCompanyGroupId(req.AdminId, req.GroupId)
  1124. if err != nil {
  1125. br.Msg = "编辑失败"
  1126. br.ErrMsg = "ModifyCompanyGroupId,Err:" + err.Error()
  1127. return
  1128. }
  1129. }
  1130. }
  1131. if req.DepartmentId != adminInfo.DepartmentId {
  1132. err = company.ModifyCompanyDepartmentId(req.AdminId, req.DepartmentId)
  1133. if err != nil {
  1134. br.Msg = "编辑失败"
  1135. br.ErrMsg = "ModifyCompanyDepartmentId,Err:" + err.Error()
  1136. return
  1137. }
  1138. }
  1139. }
  1140. // 同步用户缓存
  1141. if utils.BusinessCode == utils.BusinessCodeRelease {
  1142. var syncData system.SyncAdminData
  1143. syncData.Source = utils.SOURCE_ETA_FLAG
  1144. syncData.AdminName = adminInfo.AdminName
  1145. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  1146. }
  1147. // 清除系统用户列表缓存key
  1148. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  1149. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  1150. br.Ret = 200
  1151. br.Success = true
  1152. br.IsAddLog = true
  1153. br.Msg = "操作成功"
  1154. }
  1155. // ResetPass
  1156. // @Title 重置密码
  1157. // @Description 重置密码
  1158. // @Param request body system.SysUserResetPassReq true "type json string"
  1159. // @Success 200 编辑成功
  1160. // @router /sysuser/reset_pass [post]
  1161. func (this *SysAdminController) ResetPass() {
  1162. br := new(models.BaseResponse).Init()
  1163. defer func() {
  1164. this.Data["json"] = br
  1165. this.ServeJSON()
  1166. }()
  1167. var req system.SysUserResetPassReq
  1168. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1169. if err != nil {
  1170. br.Msg = "参数解析异常!"
  1171. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1172. return
  1173. }
  1174. if req.AdminId <= 0 {
  1175. br.Msg = "参数有误"
  1176. br.ErrMsg = "参数有误, AdminId"
  1177. return
  1178. }
  1179. req.Password = strings.TrimSpace(req.Password)
  1180. if req.Password == "" {
  1181. br.Msg = "新密码不能为空"
  1182. return
  1183. }
  1184. req.RePassword = strings.TrimSpace(req.RePassword)
  1185. if req.RePassword == "" {
  1186. br.Msg = "确认密码不能为空"
  1187. return
  1188. }
  1189. if req.Password != req.RePassword {
  1190. br.Msg = "两次输入密码不一致,请重新填写"
  1191. return
  1192. }
  1193. adminInfo, err := system.GetSysAdminById(req.AdminId)
  1194. if err != nil {
  1195. if err.Error() == utils.ErrNoRow() {
  1196. br.Msg = "用户已被删除, 请刷新页面"
  1197. return
  1198. }
  1199. br.Msg = "获取数据失败"
  1200. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
  1201. return
  1202. }
  1203. b, err := base64.StdEncoding.DecodeString(req.Password)
  1204. if err != nil {
  1205. br.Msg = "解析数据失败"
  1206. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  1207. return
  1208. }
  1209. pwd := string(b)
  1210. //pwd = strings.ToLower(pwd)
  1211. if !utils.CheckPwdV2(pwd) {
  1212. br.Msg = "密码格式错误,请重新输入"
  1213. return
  1214. }
  1215. pwd = utils.MD5(pwd)
  1216. adminInfo.Password = pwd
  1217. adminInfo.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  1218. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  1219. adminInfo.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  1220. if e := adminInfo.Update([]string{"Password", "LastUpdatedPasswordTime", "LastUpdatedTime", "LastLoginTime"}); e != nil {
  1221. br.Msg = "操作失败"
  1222. br.ErrMsg = "更新系统用户分组失败, Err: " + e.Error()
  1223. return
  1224. }
  1225. // 同步用户缓存
  1226. if utils.BusinessCode == utils.BusinessCodeRelease {
  1227. var syncData system.SyncAdminData
  1228. syncData.Source = utils.SOURCE_ETA_FLAG
  1229. syncData.AdminName = adminInfo.AdminName
  1230. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  1231. }
  1232. // 清除系统用户列表缓存key及异常登录缓存
  1233. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  1234. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  1235. abnormalKey := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, adminInfo.AdminName)
  1236. errPassKey := fmt.Sprint(utils.CACHE_LOGIN_ERR_PASS, adminInfo.AdminName)
  1237. _ = utils.Rc.Delete(abnormalKey)
  1238. _ = utils.Rc.Delete(errPassKey)
  1239. br.Ret = 200
  1240. br.Success = true
  1241. br.IsAddLog = true
  1242. br.Msg = "重置密码成功"
  1243. }
  1244. // Add
  1245. // @Title 用户详情信息
  1246. // @Description 用户详情信息
  1247. // @Param request body system.SysuserAddReq true "type json string"
  1248. // @Success 200 新增成功
  1249. // @router /sysuser/detail [get]
  1250. func (this *SysAdminController) Detail() {
  1251. br := new(models.BaseResponse).Init()
  1252. defer func() {
  1253. this.Data["json"] = br
  1254. this.ServeJSON()
  1255. }()
  1256. br.Data = this.SysUser
  1257. br.Ret = 200
  1258. br.Success = true
  1259. br.Msg = "获取成功"
  1260. }