sys_admin.go 35 KB

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