sys_admin.go 35 KB

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