sys_admin.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. package controllers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/company"
  7. "eta_gn/eta_api/models/data_manage"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services/eta_forum"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. )
  17. type SysAdminController struct {
  18. BaseAuthController
  19. }
  20. // @router /sysuser/list [get]
  21. func (this *SysAdminController) ListSysuser() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. sysUser := this.SysUser
  28. if sysUser == nil {
  29. br.Msg = "请登录"
  30. br.ErrMsg = "请登录,SysUser Is Empty"
  31. return
  32. }
  33. pageSize, _ := this.GetInt("PageSize")
  34. currentIndex, _ := this.GetInt("CurrentIndex")
  35. departmentId, _ := this.GetInt("DepartmentId")
  36. groupId, _ := this.GetInt("GroupId")
  37. teamId, _ := this.GetInt("TeamId")
  38. roleId, _ := this.GetInt("RoleId")
  39. keyWord := this.GetString("KeyWord")
  40. var startSize int
  41. if pageSize <= 0 {
  42. pageSize = utils.PageSize20
  43. }
  44. if currentIndex <= 0 {
  45. currentIndex = 1
  46. }
  47. startSize = utils.StartIndex(currentIndex, pageSize)
  48. var condition string
  49. var pars []interface{}
  50. if departmentId > 0 {
  51. condition += ` AND department_id=? `
  52. pars = append(pars, departmentId)
  53. }
  54. if roleId > 0 {
  55. condition += ` AND role_id=? `
  56. pars = append(pars, roleId)
  57. }
  58. if keyWord != "" {
  59. condition += ` AND (real_name LIKE ? OR admin_name LIKE ? OR mobile LIKE ? ) `
  60. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  61. }
  62. var total int
  63. var err error
  64. if teamId <= 0 {
  65. if groupId > 0 {
  66. ids, err := system.GetGroupIdByParentId(groupId)
  67. if err != nil {
  68. br.Msg = "获取失败"
  69. br.ErrMsg = "获取失败,Err:" + err.Error()
  70. return
  71. }
  72. sGroupId := strconv.Itoa(groupId)
  73. ids = append(ids, &sGroupId)
  74. var idList []string
  75. for _, id := range ids {
  76. idList = append(idList, *id)
  77. }
  78. sid := strings.Join(idList, ",")
  79. condition += ` AND group_id IN (` + sid + `) `
  80. }
  81. total, err = system.GetSysuserListCount(condition, pars)
  82. if err != nil {
  83. br.Msg = "获取失败"
  84. br.ErrMsg = "获取失败,Err:" + err.Error()
  85. return
  86. }
  87. } else {
  88. nCondition := condition + ` AND group_id=? `
  89. nPars := append(pars, teamId)
  90. total, err = system.GetSysuserListCount(nCondition, nPars)
  91. if err != nil {
  92. br.Msg = "获取失败"
  93. br.ErrMsg = "获取失败,Err:" + err.Error()
  94. return
  95. }
  96. }
  97. var list []*system.AdminItem
  98. adminIdArr := make([]int, 0)
  99. if teamId <= 0 {
  100. list, err = system.GetSysuserList(condition, pars, startSize, pageSize)
  101. if err != nil {
  102. br.Msg = "获取失败"
  103. br.ErrMsg = "获取失败,Err:" + err.Error()
  104. return
  105. }
  106. lenList := len(list)
  107. for i := 0; i < lenList; i++ {
  108. item := list[i]
  109. adminIdArr = append(adminIdArr, item.AdminId)
  110. var secondName *string
  111. if item.GroupId != 0 {
  112. pid, err := company.GetParentIdFromGroup(item.GroupId)
  113. if err != nil {
  114. br.Msg = "获取失败"
  115. br.ErrMsg = "查询父级id异常" + err.Error()
  116. return
  117. }
  118. if pid != nil && *pid != 0 {
  119. secondName, err = company.GetGroupNamesById(*pid)
  120. if err != nil {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "查询二级组名异常" + err.Error()
  123. return
  124. }
  125. }
  126. if pid != nil && *pid > 0 {
  127. list[i].TeamId = list[i].GroupId
  128. list[i].GroupId = *pid
  129. }
  130. }
  131. if item.DepartmentName != "" {
  132. if item.GroupName == "" {
  133. list[i].DepartmentGroup = item.DepartmentName
  134. } else if secondName != nil && *secondName != `` {
  135. list[i].DepartmentGroup = item.DepartmentName + "/" + *secondName + "/" + item.GroupName
  136. } else {
  137. list[i].DepartmentGroup = item.DepartmentName + "/" + item.GroupName
  138. }
  139. }
  140. labelVal := 0
  141. fmt.Println("item.RoleTypeCode:", item.RoleTypeCode)
  142. fmt.Println("item.Authority:", item.Authority)
  143. if item.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  144. labelVal = 1
  145. } else if item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  146. item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  147. labelVal = 2
  148. } else {
  149. if item.Authority == 1 {
  150. labelVal = 3
  151. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  152. labelVal = 4
  153. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  154. labelVal = 5
  155. } else if item.Authority == 4 {
  156. labelVal = 4
  157. }
  158. }
  159. list[i].LabelVal = labelVal
  160. }
  161. } else {
  162. condition += ` AND group_id=? `
  163. pars = append(pars, teamId)
  164. list, err = system.GetSysuserList(condition, pars, startSize, pageSize)
  165. if err != nil {
  166. br.Msg = "获取失败"
  167. br.ErrMsg = "获取失败,Err:" + err.Error()
  168. return
  169. }
  170. groupItem, err := system.GetSysGroupById(groupId)
  171. if err != nil {
  172. br.Msg = "获取失败"
  173. br.ErrMsg = "获取失败,Err:" + err.Error()
  174. return
  175. }
  176. lenList := len(list)
  177. for i := 0; i < lenList; i++ {
  178. item := list[i]
  179. adminIdArr = append(adminIdArr, item.AdminId)
  180. if item.DepartmentName != "" {
  181. if item.GroupName == "" {
  182. list[i].DepartmentGroup = item.DepartmentName
  183. } else if item.GroupName != "" {
  184. list[i].DepartmentGroup = item.DepartmentName + "/" + groupItem.GroupName + "/" + item.GroupName
  185. } else {
  186. list[i].DepartmentGroup = item.DepartmentName + "/" + item.GroupName
  187. }
  188. }
  189. labelVal := 0
  190. if item.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  191. labelVal = 1
  192. } else if item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  193. item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  194. labelVal = 2
  195. } else {
  196. if item.Authority == 1 {
  197. labelVal = 3
  198. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  199. labelVal = 4
  200. } else if item.Authority == 2 && item.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  201. labelVal = 5
  202. } else if item.Authority == 4 {
  203. labelVal = 4
  204. }
  205. }
  206. list[i].LabelVal = labelVal
  207. }
  208. }
  209. if teamId > 0 {
  210. for _, item := range list {
  211. item.TeamId = teamId
  212. item.GroupId = groupId
  213. }
  214. }
  215. fmt.Println("teamId:", teamId)
  216. page := paging.GetPaging(currentIndex, pageSize, total)
  217. resp := new(system.SysuserListResp)
  218. resp.List = list
  219. resp.Paging = page
  220. br.Ret = 200
  221. br.Success = true
  222. br.Msg = "获取成功"
  223. br.Data = resp
  224. }
  225. // @router /sysuser/add [post]
  226. func (this *SysAdminController) Add() {
  227. br := new(models.BaseResponse).Init()
  228. defer func() {
  229. this.Data["json"] = br
  230. this.ServeJSON()
  231. }()
  232. var req system.SysuserAddReq
  233. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  234. if err != nil {
  235. br.Msg = "参数解析异常!"
  236. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  237. return
  238. }
  239. if req.RoleId <= 0 {
  240. br.Msg = "请选择角色"
  241. br.ErrMsg = "角色ID小于等于0"
  242. return
  243. }
  244. count, err := system.GetSysAdminCount(req.AdminName)
  245. if err != nil {
  246. br.Msg = "获取数据失败"
  247. br.ErrMsg = "获取数据失败"
  248. return
  249. }
  250. if count > 0 {
  251. br.Msg = "账号已存在,请重新输入"
  252. br.IsSendEmail = false
  253. return
  254. }
  255. req.Mobile = strings.TrimSpace(req.Mobile)
  256. req.Email = strings.TrimSpace(req.Email)
  257. if req.Mobile == "" && req.Email == "" {
  258. br.Msg = "至少输入一个手机号或邮箱"
  259. return
  260. }
  261. if req.Mobile != "" {
  262. if req.TelAreaCode == "86" {
  263. if !utils.ValidateMobileFormatat(req.Mobile) {
  264. br.Msg = "手机号格式有误, 请检查"
  265. return
  266. }
  267. }
  268. mobileCount, err := system.GetSysAdminCountByMobile(req.Mobile, 0)
  269. if err != nil {
  270. br.Msg = "判断手机号是否存在失败"
  271. br.ErrMsg = "判断手机号是否存在失败,Err:" + err.Error()
  272. return
  273. }
  274. if mobileCount > 0 {
  275. br.Msg = "手机号已存在,请重新填写"
  276. br.IsSendEmail = false
  277. return
  278. }
  279. }
  280. if req.Email != "" {
  281. if !utils.ValidateEmailFormatat(req.Email) {
  282. br.Msg = "邮箱格式有误, 请检查"
  283. return
  284. }
  285. emailUser, e := system.GetSysUserByEmail(req.Email)
  286. if e != nil && !utils.IsErrNoRow(e) {
  287. br.Msg = "操作失败"
  288. br.ErrMsg = "邮箱获取用户失败"
  289. return
  290. }
  291. if emailUser != nil && emailUser.AdminId > 0 {
  292. br.Msg = "邮箱已存在, 请重新填写"
  293. return
  294. }
  295. }
  296. var roleName, departmentName, groupName, teamName string
  297. roleItem, err := system.GetSysRoleById(req.RoleId)
  298. if err != nil {
  299. if utils.IsErrNoRow(err) {
  300. br.Msg = "所选角色不存在"
  301. return
  302. }
  303. br.Msg = "获取数据失败"
  304. br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
  305. return
  306. }
  307. if roleItem.RoleId > 0 {
  308. roleName = roleItem.RoleName
  309. }
  310. if req.DepartmentId > 0 {
  311. departmentItem, err := system.GetSysDepartmentById(req.DepartmentId)
  312. if err != nil {
  313. if utils.IsErrNoRow(err) {
  314. br.Msg = "所选部门不存在"
  315. return
  316. }
  317. br.Msg = "获取数据失败"
  318. br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
  319. return
  320. }
  321. if departmentItem.DepartmentId > 0 {
  322. departmentName = departmentItem.DepartmentName
  323. }
  324. }
  325. if req.GroupId > 0 {
  326. groupItem, err := system.GetSysGroupById(req.GroupId)
  327. if err != nil {
  328. if utils.IsErrNoRow(err) {
  329. br.Msg = "所选分组不存在"
  330. return
  331. }
  332. br.Msg = "获取数据失败"
  333. br.ErrMsg = "获取分组数据失败,Err:" + err.Error()
  334. return
  335. }
  336. if groupItem.GroupId > 0 {
  337. groupName = groupItem.GroupName
  338. }
  339. }
  340. if req.TeamId > 0 {
  341. teamItem, err := system.GetSysGroupById(req.TeamId)
  342. if err != nil {
  343. if utils.IsErrNoRow(err) {
  344. br.Msg = "所选小组不存在"
  345. return
  346. }
  347. br.Msg = "获取数据失败"
  348. br.ErrMsg = "获取小组数据失败,Err:" + err.Error()
  349. return
  350. }
  351. if teamItem.GroupId > 0 {
  352. teamName = teamItem.GroupName
  353. }
  354. }
  355. var originPass, pass string
  356. if req.Password != "" {
  357. pwdByte, e := base64.StdEncoding.DecodeString(req.Password)
  358. if e != nil {
  359. br.Msg = "解析数据失败"
  360. br.ErrMsg = "解析数据失败,Err:" + e.Error()
  361. return
  362. }
  363. originPass = string(pwdByte)
  364. }
  365. if req.IsLdap == 0 {
  366. if originPass == "" {
  367. br.Msg = "请输入密码"
  368. return
  369. }
  370. if !utils.CheckPwd(originPass) {
  371. br.Msg = "密码格式错误,请重新输入"
  372. return
  373. }
  374. pass = utils.MD5(originPass)
  375. }
  376. if req.IsLdap == 1 {
  377. if originPass != "" {
  378. pass = utils.MD5(originPass)
  379. } else {
  380. pass = utils.MD5(utils.LdapInitPassword)
  381. }
  382. }
  383. req.EmployeeId = strings.TrimSpace(req.EmployeeId)
  384. admin := new(system.Admin)
  385. admin.AdminName = req.AdminName
  386. admin.RealName = req.RealName
  387. admin.Password = pass
  388. admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  389. admin.Enabled = req.Enabled
  390. admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  391. admin.CreatedTime = time.Now()
  392. admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  393. admin.Mobile = req.Mobile
  394. admin.RoleType = 0
  395. admin.RoleId = req.RoleId
  396. admin.RoleName = roleName
  397. admin.RoleTypeCode = roleItem.RoleTypeCode
  398. admin.DepartmentId = req.DepartmentId
  399. admin.DepartmentName = departmentName
  400. admin.AdminAvatar = req.AdminAvatar
  401. if req.TeamId <= 0 {
  402. admin.GroupId = req.GroupId
  403. admin.GroupName = groupName
  404. } else {
  405. admin.GroupId = req.TeamId
  406. admin.GroupName = teamName
  407. }
  408. if strings.Contains(admin.RoleTypeCode, "researcher") {
  409. admin.Role = "researcher"
  410. } else if strings.Contains(admin.RoleTypeCode, "seller") {
  411. admin.Role = "sales"
  412. } else {
  413. admin.Role = "admin"
  414. }
  415. admin.EmployeeId = req.EmployeeId
  416. admin.Email = req.Email
  417. var authority int
  418. if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  419. authority = 3
  420. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  421. authority = 4
  422. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  423. authority = 2
  424. } else {
  425. if strings.Contains(roleItem.RoleType, "销售主管") {
  426. authority = 2
  427. } else if strings.Contains(roleItem.RoleType, "部门经理") {
  428. authority = 1
  429. } else {
  430. authority = 0
  431. }
  432. }
  433. admin.Authority = authority
  434. admin.Position = req.Position
  435. admin.Province = req.Province
  436. admin.ProvinceCode = req.ProvinceCode
  437. admin.City = req.City
  438. admin.CityCode = req.CityCode
  439. admin.TelAreaCode = req.TelAreaCode
  440. admin.IsLdap = req.IsLdap
  441. err = system.AddAdmin(admin)
  442. if err != nil {
  443. br.Msg = "新增失败"
  444. br.ErrMsg = "新增失败,Err:" + err.Error()
  445. return
  446. }
  447. utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  448. utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  449. if admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
  450. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
  451. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
  452. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
  453. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  454. } else if admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER ||
  455. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP ||
  456. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  457. admin.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  458. }
  459. go eta_forum.AdminSave(admin.AdminId)
  460. br.Ret = 200
  461. br.Success = true
  462. br.Msg = "新增成功"
  463. }
  464. // @router /sysuser/edit [post]
  465. func (this *SysAdminController) Edit() {
  466. br := new(models.BaseResponse).Init()
  467. defer func() {
  468. this.Data["json"] = br
  469. this.ServeJSON()
  470. }()
  471. var req system.SysuserEditReq
  472. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  473. if err != nil {
  474. br.Msg = "参数解析异常!"
  475. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  476. return
  477. }
  478. if req.RoleId <= 0 {
  479. br.Msg = "请选择角色"
  480. br.ErrMsg = "角色ID小于等于0"
  481. return
  482. }
  483. adminInfo, err := system.GetSysAdminById(req.AdminId)
  484. if err != nil {
  485. if utils.IsErrNoRow(err) {
  486. br.Msg = "用户已被删除, 请刷新页面"
  487. return
  488. }
  489. br.Msg = "获取数据失败"
  490. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
  491. return
  492. }
  493. item, err := system.GetSysAdminByName(req.AdminName)
  494. if err != nil {
  495. if !utils.IsErrNoRow(err) {
  496. br.Msg = "获取数据失败"
  497. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminName:" + req.AdminName
  498. return
  499. }
  500. } else {
  501. if item.AdminId != req.AdminId {
  502. br.Msg = "账号名称已存在,请重新输入"
  503. return
  504. }
  505. }
  506. req.Mobile = strings.TrimSpace(req.Mobile)
  507. req.Email = strings.TrimSpace(req.Email)
  508. if req.Mobile == "" && req.Email == "" {
  509. br.Msg = "至少输入一个手机号或邮箱"
  510. return
  511. }
  512. if req.Mobile != "" {
  513. if req.TelAreaCode == "86" {
  514. if !utils.ValidateMobileFormatat(req.Mobile) {
  515. br.Msg = "手机号格式有误, 请检查"
  516. return
  517. }
  518. }
  519. mobileCount, e := system.GetSysAdminCountByMobile(req.Mobile, adminInfo.AdminId)
  520. if e != nil {
  521. br.Msg = "判断手机号是否存在失败"
  522. br.ErrMsg = "判断手机号是否存在失败,Err:" + e.Error()
  523. return
  524. }
  525. if mobileCount > 0 {
  526. br.Msg = "手机号已存在,请重新填写"
  527. br.IsSendEmail = false
  528. return
  529. }
  530. }
  531. if req.Email != "" {
  532. if !utils.ValidateEmailFormatat(req.Email) {
  533. br.Msg = "邮箱格式有误, 请检查"
  534. return
  535. }
  536. emailUser, e := system.GetSysUserByEmail(req.Email)
  537. if e != nil {
  538. if !utils.IsErrNoRow(e) {
  539. br.Msg = "操作失败"
  540. br.ErrMsg = "邮箱获取用户信息失败, Err: " + e.Error()
  541. return
  542. }
  543. } else {
  544. if emailUser.AdminId != adminInfo.AdminId {
  545. br.Msg = "邮箱已存在, 请检查"
  546. return
  547. }
  548. }
  549. }
  550. var roleName string
  551. roleItem, err := system.GetSysRoleById(req.RoleId)
  552. if err != nil {
  553. if utils.IsErrNoRow(err) {
  554. br.Msg = "角色不存在,请重新选择"
  555. br.ErrMsg = "角色不存在"
  556. return
  557. }
  558. br.Msg = "获取数据失败"
  559. br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
  560. return
  561. }
  562. if roleItem.RoleId > 0 {
  563. roleName = roleItem.RoleName
  564. }
  565. req.EmployeeId = strings.TrimSpace(req.EmployeeId)
  566. var authority int
  567. if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  568. authority = 3
  569. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  570. authority = 4
  571. } else if roleItem.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  572. authority = 2
  573. } else {
  574. if strings.Contains(roleItem.RoleType, "销售主管") {
  575. authority = 2
  576. } else if strings.Contains(roleItem.RoleType, "部门经理") {
  577. authority = 1
  578. } else {
  579. authority = 0
  580. }
  581. }
  582. adminInfo.AdminName = req.AdminName
  583. adminInfo.RealName = req.RealName
  584. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  585. adminInfo.Mobile = req.Mobile
  586. adminInfo.RoleId = req.RoleId
  587. adminInfo.RoleName = roleName
  588. adminInfo.Enabled = req.Enabled
  589. adminInfo.Authority = authority
  590. adminInfo.Position = req.Position
  591. adminInfo.RoleTypeCode = roleItem.RoleTypeCode
  592. adminInfo.Province = req.Province
  593. adminInfo.ProvinceCode = req.ProvinceCode
  594. adminInfo.City = req.City
  595. adminInfo.CityCode = req.CityCode
  596. adminInfo.EmployeeId = req.EmployeeId
  597. adminInfo.Email = req.Email
  598. adminInfo.TelAreaCode = req.TelAreaCode
  599. adminInfo.IsLdap = req.IsLdap
  600. cols := []string{
  601. "AdminName", "RealName", "LastUpdatedTime", "Mobile", "RoleId", "RoleName", "Enabled", "Authority",
  602. "Position", "RoleTypeCode", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "Email", "TelAreaCode", "IsLdap",
  603. }
  604. if e := adminInfo.Update(cols); e != nil {
  605. br.Msg = "编辑失败"
  606. br.ErrMsg = "更新用户信息失败, Err:" + e.Error()
  607. return
  608. }
  609. logOutSystemUser(adminInfo.AdminId)
  610. utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  611. utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  612. go eta_forum.AdminSave(adminInfo.AdminId)
  613. br.Ret = 200
  614. br.Success = true
  615. br.IsAddLog = true
  616. br.Msg = "编辑成功"
  617. }
  618. // @router /sysuser/editEnabled [post]
  619. func (this *SysAdminController) EditEnabled() {
  620. br := new(models.BaseResponse).Init()
  621. defer func() {
  622. this.Data["json"] = br
  623. this.ServeJSON()
  624. }()
  625. var req system.SysuserEditEnabledReq
  626. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  627. if err != nil {
  628. br.Msg = "参数解析异常!"
  629. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  630. return
  631. }
  632. adminItem, err := system.GetSysAdminById(req.AdminId)
  633. if err != nil {
  634. br.Msg = "获取数据失败"
  635. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  636. return
  637. }
  638. if req.Enabled == 0 {
  639. err = system.DisableAdmin(req.AdminId)
  640. if err != nil {
  641. br.Msg = "修改失败"
  642. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  643. return
  644. }
  645. } else {
  646. admin := new(system.Admin)
  647. admin.Enabled = req.Enabled
  648. admin.AdminId = req.AdminId
  649. err = system.EditAdminEnabled(admin)
  650. if err != nil {
  651. br.Msg = "修改失败"
  652. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  653. return
  654. }
  655. }
  656. if adminItem.Enabled == 1 && req.Enabled == 0 {
  657. logOutSystemUser(adminItem.AdminId)
  658. }
  659. if err != nil {
  660. br.Msg = "操作失败"
  661. br.ErrMsg = "操作失败,Err:" + err.Error()
  662. return
  663. }
  664. go eta_forum.AdminSave(adminItem.AdminId)
  665. br.Ret = 200
  666. br.Success = true
  667. br.IsAddLog = true
  668. br.Msg = "操作成功"
  669. }
  670. // @router /sysuser/delete [post]
  671. func (this *SysAdminController) Delete() {
  672. br := new(models.BaseResponse).Init()
  673. defer func() {
  674. this.Data["json"] = br
  675. this.ServeJSON()
  676. }()
  677. var req system.SysuserDeleteReq
  678. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  679. if err != nil {
  680. br.Msg = "参数解析异常!"
  681. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  682. return
  683. }
  684. if req.AdminId <= 0 {
  685. br.Msg = "参数错误"
  686. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  687. return
  688. }
  689. adminInfo, _ := system.GetSysUserById(req.AdminId)
  690. err = system.DeleteSysuser(req.AdminId)
  691. if err != nil {
  692. br.Msg = "删除失败"
  693. br.ErrMsg = "删除失败,Err:" + err.Error()
  694. return
  695. }
  696. {
  697. go data_manage.DeleteManualUser(req.AdminId)
  698. }
  699. utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  700. utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  701. go eta_forum.AdminDelete(adminInfo.AdminName)
  702. br.Ret = 200
  703. br.Success = true
  704. br.IsAddLog = true
  705. br.Msg = "删除成功"
  706. }
  707. // @router /sysuser/config/set [post]
  708. func (this *SysAdminController) SetConfig() {
  709. br := new(models.BaseResponse).Init()
  710. defer func() {
  711. this.Data["json"] = br
  712. this.ServeJSON()
  713. }()
  714. sysUser := this.SysUser
  715. if sysUser == nil {
  716. br.Msg = "请登录"
  717. br.ErrMsg = "请登录,SysUser Is Empty"
  718. br.Ret = 408
  719. return
  720. }
  721. var req system.SetAdminConfigReq
  722. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  723. if err != nil {
  724. br.Msg = "参数解析异常!"
  725. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  726. return
  727. }
  728. item, err := system.GetConfigDetailByCode(this.SysUser.AdminId, req.ConfigCode)
  729. if err != nil {
  730. if !utils.IsErrNoRow(err) {
  731. br.Msg = "设置失败"
  732. br.ErrMsg = "设置失败,Err:" + err.Error()
  733. return
  734. }
  735. defaultConfig, err := system.GetDefaultConfigDetailByCode(req.ConfigCode)
  736. if err != nil {
  737. br.Msg = "设置失败"
  738. br.ErrMsg = "获取默认配置失败,Err:" + err.Error()
  739. return
  740. }
  741. adminConfigItem := &system.AdminConfig{
  742. AdminId: this.SysUser.AdminId,
  743. ConfigCode: req.ConfigCode,
  744. ConfigValue: req.ConfigValue,
  745. Remark: defaultConfig.Remark,
  746. CreateTime: time.Now(),
  747. }
  748. err = adminConfigItem.Create()
  749. if err != nil {
  750. br.Msg = "设置失败"
  751. br.ErrMsg = "设置失败,Err:" + err.Error()
  752. return
  753. }
  754. } else {
  755. item.ConfigValue = req.ConfigValue
  756. err = item.Update([]string{"ConfigValue"})
  757. if err != nil {
  758. br.Msg = "设置失败"
  759. br.ErrMsg = "设置失败,Err:" + err.Error()
  760. return
  761. }
  762. }
  763. br.Ret = 200
  764. br.Success = true
  765. br.Msg = "设置成功"
  766. }
  767. func logOutSystemUser(adminId int) {
  768. _ = system.ExpiredSysSessionByAdminId(adminId)
  769. _ = system.UnBindAdminRecordByUserId(adminId)
  770. }
  771. // @router /sysuser/move [post]
  772. func (this *SysAdminController) Move() {
  773. br := new(models.BaseResponse).Init()
  774. defer func() {
  775. this.Data["json"] = br
  776. this.ServeJSON()
  777. }()
  778. var req system.SysUserMoveReq
  779. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  780. if err != nil {
  781. br.Msg = "参数解析异常!"
  782. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  783. return
  784. }
  785. if req.AdminId <= 0 {
  786. br.Msg = "参数有误"
  787. br.ErrMsg = "参数有误, AdminId"
  788. return
  789. }
  790. if req.DepartmentId <= 0 {
  791. br.Msg = "请选择部门"
  792. return
  793. }
  794. adminInfo, err := system.GetSysAdminById(req.AdminId)
  795. if err != nil && !utils.IsErrNoRow(err) {
  796. br.Msg = "获取数据失败"
  797. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
  798. return
  799. }
  800. var departmentName, groupName, teamName string
  801. if req.DepartmentId > 0 {
  802. departmentItem, err := system.GetSysDepartmentById(req.DepartmentId)
  803. if err != nil {
  804. br.Msg = "获取数据失败"
  805. br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
  806. return
  807. }
  808. if departmentItem.DepartmentId > 0 {
  809. departmentName = departmentItem.DepartmentName
  810. }
  811. }
  812. if req.GroupId > 0 {
  813. groupItem, err := system.GetSysGroupById(req.GroupId)
  814. if err != nil {
  815. br.Msg = "获取数据失败"
  816. br.ErrMsg = "获取分组数据失败,Err:" + err.Error()
  817. return
  818. }
  819. if groupItem.GroupId > 0 {
  820. groupName = groupItem.GroupName
  821. }
  822. }
  823. if req.TeamId > 0 {
  824. teamItem, err := system.GetSysGroupById(req.TeamId)
  825. if err != nil {
  826. br.Msg = "获取数据失败"
  827. br.ErrMsg = "获取小组数据失败,Err:" + err.Error()
  828. return
  829. }
  830. if teamItem.GroupId > 0 {
  831. teamName = teamItem.GroupName
  832. }
  833. }
  834. adminInfo.DepartmentId = req.DepartmentId
  835. adminInfo.DepartmentName = departmentName
  836. if req.TeamId <= 0 {
  837. adminInfo.GroupId = req.GroupId
  838. adminInfo.GroupName = groupName
  839. } else {
  840. adminInfo.GroupId = req.TeamId
  841. adminInfo.GroupName = teamName
  842. }
  843. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  844. if e := adminInfo.Update([]string{"DepartmentId", "DepartmentName", "GroupId", "GroupName", "LastUpdatedTime"}); e != nil {
  845. br.Msg = "操作失败"
  846. br.ErrMsg = "更新系统用户分组失败, Err: " + e.Error()
  847. return
  848. }
  849. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  850. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  851. br.Ret = 200
  852. br.Success = true
  853. br.IsAddLog = true
  854. br.Msg = "操作成功"
  855. }
  856. // @router /sysuser/reset_pass [post]
  857. func (this *SysAdminController) ResetPass() {
  858. br := new(models.BaseResponse).Init()
  859. defer func() {
  860. this.Data["json"] = br
  861. this.ServeJSON()
  862. }()
  863. var req system.SysUserResetPassReq
  864. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  865. if err != nil {
  866. br.Msg = "参数解析异常!"
  867. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  868. return
  869. }
  870. if req.AdminId <= 0 {
  871. br.Msg = "参数有误"
  872. br.ErrMsg = "参数有误, AdminId"
  873. return
  874. }
  875. req.Password = strings.TrimSpace(req.Password)
  876. if req.Password == "" {
  877. br.Msg = "新密码不能为空"
  878. return
  879. }
  880. req.RePassword = strings.TrimSpace(req.RePassword)
  881. if req.RePassword == "" {
  882. br.Msg = "确认密码不能为空"
  883. return
  884. }
  885. if req.Password != req.RePassword {
  886. br.Msg = "两次输入密码不一致,请重新填写"
  887. return
  888. }
  889. adminInfo, err := system.GetSysAdminById(req.AdminId)
  890. if err != nil {
  891. if utils.IsErrNoRow(err) {
  892. br.Msg = "用户已被删除, 请刷新页面"
  893. return
  894. }
  895. br.Msg = "获取数据失败"
  896. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";AdminId:" + fmt.Sprint(req.AdminId)
  897. return
  898. }
  899. b, err := base64.StdEncoding.DecodeString(req.Password)
  900. if err != nil {
  901. br.Msg = "解析数据失败"
  902. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  903. return
  904. }
  905. pwd := string(b)
  906. pwd = utils.MD5(pwd)
  907. adminInfo.Password = pwd
  908. adminInfo.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  909. adminInfo.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  910. adminInfo.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  911. if e := adminInfo.Update([]string{"Password", "LastUpdatedPasswordTime", "LastUpdatedTime", "LastLoginTime"}); e != nil {
  912. br.Msg = "操作失败"
  913. br.ErrMsg = "更新系统用户分组失败, Err: " + e.Error()
  914. return
  915. }
  916. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  917. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN_ID)
  918. abnormalKey := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, adminInfo.AdminName)
  919. errPassKey := fmt.Sprint(utils.CACHE_LOGIN_ERR_PASS, adminInfo.AdminName)
  920. _ = utils.Rc.Delete(abnormalKey)
  921. _ = utils.Rc.Delete(errPassKey)
  922. br.Ret = 200
  923. br.Success = true
  924. br.IsAddLog = true
  925. br.Msg = "重置密码成功"
  926. }
  927. // @router /sysuser/detail [get]
  928. func (this *SysAdminController) Detail() {
  929. br := new(models.BaseResponse).Init()
  930. defer func() {
  931. this.Data["json"] = br
  932. this.ServeJSON()
  933. }()
  934. br.Data = this.SysUser
  935. br.Ret = 200
  936. br.Success = true
  937. br.Msg = "获取成功"
  938. }