user.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm/models"
  5. "eta/eta_mini_crm/models/request"
  6. "eta/eta_mini_crm/models/response"
  7. "eta/eta_mini_crm/services"
  8. "eta/eta_mini_crm/utils"
  9. "strings"
  10. "time"
  11. "github.com/beego/beego/v2/client/orm"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. )
  14. type UserController struct {
  15. BaseAuthController
  16. }
  17. // Add
  18. // @Title 添加新客户
  19. // @Description 添加新客户
  20. // @Param request body request.UserAddReq true "type json string"
  21. // @Success 200 {object} request.UserAddReq
  22. // @router /add [post]
  23. func (this *UserController) Add() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. var req request.UserAddReq
  30. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. if req.RealName == "" {
  37. br.Msg = "请输入姓名"
  38. return
  39. }
  40. req.Phone = strings.TrimSpace(req.Phone)
  41. req.Email = strings.TrimSpace(req.Email)
  42. if req.Phone == "" && req.Email == "" {
  43. br.Msg = "至少输入一个手机号或邮箱"
  44. return
  45. }
  46. var userByPhone, userByEmail *models.User
  47. if req.Phone != "" {
  48. if req.AreaCode == "86" {
  49. if !utils.ValidateMobileFormatat(req.Phone) {
  50. br.Msg = "手机号格式有误, 请检查"
  51. return
  52. }
  53. }
  54. userByPhone, err = models.GetUserByPhone(req.Phone, req.AreaCode)
  55. if err != nil && err != orm.ErrNoRows {
  56. br.Msg = "添加用户失败"
  57. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  58. return
  59. }
  60. if userByPhone != nil {
  61. switch userByPhone.Status {
  62. case 0:
  63. userByPhone.Status = 2
  64. case 1:
  65. userByPhone.Status = 2
  66. default:
  67. br.Msg = "手机号已存在,请重新输入"
  68. return
  69. }
  70. }
  71. }
  72. if req.Email != "" {
  73. if !utils.ValidateEmailFormatat(req.Email) {
  74. br.Msg = "邮箱格式有误, 请检查"
  75. return
  76. }
  77. userByEmail, err = models.GetUserByEmail(req.Email)
  78. if err != nil && err != orm.ErrNoRows {
  79. br.Msg = "添加用户失败"
  80. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  81. return
  82. }
  83. if userByEmail != nil {
  84. switch userByEmail.Status {
  85. case 0:
  86. userByEmail.Status = 2
  87. case 1:
  88. userByEmail.Status = 2
  89. default:
  90. br.Msg = "邮箱已存在,请重新输入"
  91. return
  92. }
  93. }
  94. }
  95. if req.SellerId <= 0 {
  96. br.Msg = "请选择营业部/销售"
  97. return
  98. }
  99. if req.ValidStartTime == "" || req.ValidEndTime == "" {
  100. br.Msg = "请选择合理的有效期范围"
  101. return
  102. }
  103. validStartTime, err := time.Parse("2006-01-02", req.ValidStartTime)
  104. if err != nil {
  105. br.Msg = "错误的日期格式"
  106. return
  107. }
  108. validEndTime, err := time.Parse("2006-01-02", req.ValidEndTime)
  109. if err != nil {
  110. br.Msg = "错误的日期格式"
  111. return
  112. }
  113. if !validStartTime.Before(validEndTime) {
  114. br.Msg = "请选择合理的有效期范围"
  115. return
  116. }
  117. if req.Company == "" {
  118. br.Msg = "请输入所属公司"
  119. return
  120. }
  121. if userByPhone != nil && userByEmail != nil && userByPhone.UserId != userByEmail.UserId {
  122. br.Msg = "邮箱已存在,请重新输入"
  123. return
  124. }
  125. user := &models.User{}
  126. curTime := time.Now()
  127. if userByPhone == nil && userByEmail == nil {
  128. user.CreateTime = curTime
  129. user.ModifyTime = curTime
  130. }
  131. if userByEmail != nil {
  132. user = userByEmail
  133. }
  134. if userByPhone != nil {
  135. user = userByPhone
  136. }
  137. validStartTime = validStartTime.Local().Add(-time.Hour * 8)
  138. validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24)
  139. if curTime.Before(validEndTime) {
  140. user.Status = 2
  141. } else {
  142. user.Status = 0
  143. }
  144. user.RealName = req.RealName
  145. user.AreaCode = req.AreaCode
  146. user.Phone = req.Phone
  147. user.Email = req.Email
  148. user.SellerId = req.SellerId
  149. user.ValidStartTime = validStartTime
  150. user.ValidEndTime = validEndTime
  151. user.Company = req.Company
  152. user.ModifyTime = curTime
  153. err = models.SaveUser(user, req.ChartPermission)
  154. if err != nil {
  155. br.Msg = "添加客户失败"
  156. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  157. return
  158. }
  159. if user.Status == 2 {
  160. userRecord := &models.UserChangeRecord{}
  161. userRecord.Content = this.SysUser.SysRealName + "新增用户"
  162. userRecord.Insert()
  163. }
  164. br.Msg = "添加成功"
  165. br.Success = true
  166. br.Ret = 200
  167. }
  168. // edit
  169. // @Title 编辑客户
  170. // @Description 编辑客户
  171. // @Param request body request.UserEidtReq true "type json string"
  172. // @Success 200 {object} request.UserAddReq
  173. // @router /edit [post]
  174. func (this *UserController) Edit() {
  175. br := new(models.BaseResponse).Init()
  176. defer func() {
  177. this.Data["json"] = br
  178. this.ServeJSON()
  179. }()
  180. var req request.UserEidtReq
  181. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  182. if err != nil {
  183. br.Msg = "参数解析异常!"
  184. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  185. return
  186. }
  187. if req.RealName == "" {
  188. br.Msg = "请输入姓名"
  189. return
  190. }
  191. req.Phone = strings.TrimSpace(req.Phone)
  192. req.Email = strings.TrimSpace(req.Email)
  193. if req.Phone == "" && req.Email == "" {
  194. br.Msg = "至少输入一个手机号或邮箱"
  195. return
  196. }
  197. if req.Phone != "" {
  198. if req.AreaCode == "86" {
  199. if !utils.ValidateMobileFormatat(req.Phone) {
  200. br.Msg = "手机号格式有误, 请检查"
  201. return
  202. }
  203. }
  204. user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
  205. if err != nil && err != orm.ErrNoRows {
  206. br.Msg = "编辑用户失败"
  207. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  208. return
  209. }
  210. if user != nil && user.UserId != req.UserId {
  211. br.Msg = "手机号已存在,请重新输入"
  212. return
  213. }
  214. }
  215. if req.Email != "" {
  216. if !utils.ValidateEmailFormatat(req.Email) {
  217. br.Msg = "邮箱格式有误, 请检查"
  218. return
  219. }
  220. user, err := models.GetUserByEmail(req.Email)
  221. if err != nil && err != orm.ErrNoRows {
  222. br.Msg = "编辑用户失败"
  223. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  224. return
  225. }
  226. if user != nil && user.UserId != req.UserId {
  227. br.Msg = "邮箱已存在,请重新输入"
  228. return
  229. }
  230. }
  231. if req.SellerId <= 0 {
  232. br.Msg = "请选择营业部/销售"
  233. return
  234. }
  235. if req.ValidStartTime == "" || req.ValidEndTime == "" {
  236. br.Msg = "请选择合理的有效期范围"
  237. return
  238. }
  239. validStartTime, err := time.Parse("2006-01-02", req.ValidStartTime)
  240. if err != nil {
  241. br.Msg = "错误的日期格式"
  242. return
  243. }
  244. validStartTime = validStartTime.In(time.Local).Add(-time.Hour * 8)
  245. validEndTime, err := time.Parse("2006-01-02", req.ValidEndTime)
  246. if err != nil {
  247. br.Msg = "错误的日期格式"
  248. return
  249. }
  250. validEndTime = validEndTime.In(time.Local).Add(-time.Hour*8 + time.Hour*24)
  251. if !validStartTime.Before(validEndTime) {
  252. br.Msg = "请选择合理的有效期范围"
  253. return
  254. }
  255. if req.Company == "" {
  256. br.Msg = "请输入所属公司"
  257. return
  258. }
  259. user := &models.User{}
  260. user.UserId = req.UserId
  261. user.RealName = req.RealName
  262. user.AreaCode = req.AreaCode
  263. user.Phone = req.Phone
  264. user.Email = req.Email
  265. user.SellerId = req.SellerId
  266. user.ValidStartTime = validStartTime
  267. user.ValidEndTime = validEndTime
  268. user.Company = req.Company
  269. user.ModifyTime = time.Now()
  270. curTime := time.Now()
  271. if !req.IsEnabled {
  272. user.Status = 0
  273. } else if curTime.Before(validEndTime) {
  274. user.Status = 2
  275. } else {
  276. user.Status = 1
  277. }
  278. err = models.SaveUser(user, req.ChartPermission)
  279. if err != nil {
  280. br.Msg = "添加客户失败"
  281. br.ErrMsg = "添加客户失败,系统错误,Err:" + err.Error()
  282. return
  283. }
  284. record := &models.UserChangeRecord{}
  285. if user.Status == 2 {
  286. record.Content = this.SysUser.SysRealName + "新增用户"
  287. } else {
  288. record.Content = this.SysUser.SysRealName + "编辑用户"
  289. }
  290. record.Insert()
  291. br.Msg = "编辑成功"
  292. br.Success = true
  293. br.Ret = 200
  294. }
  295. // Check
  296. // @Title 编辑客户
  297. // @Description 编辑客户
  298. // @Param request body request.UserEidtReq true "type json string"
  299. // @Success 200 {object} request.UserAddReq
  300. // @router /check [post]
  301. func (this *UserController) Check() {
  302. br := new(models.BaseResponse).Init()
  303. defer func() {
  304. this.Data["json"] = br
  305. this.ServeJSON()
  306. }()
  307. var req request.UserCheckReq
  308. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  309. if err != nil {
  310. br.Msg = "参数解析异常!"
  311. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  312. return
  313. }
  314. req.Phone = strings.TrimSpace(req.Phone)
  315. req.Email = strings.TrimSpace(req.Email)
  316. if req.Phone == "" && req.Email == "" {
  317. br.Msg = "至少输入一个手机号或邮箱"
  318. return
  319. }
  320. userCheckResp := new(response.UserCheckResp)
  321. if req.Phone != "" {
  322. if req.AreaCode == "86" {
  323. if !utils.ValidateMobileFormatat(req.Phone) {
  324. br.Msg = "手机号格式有误, 请检查"
  325. return
  326. }
  327. }
  328. user, err := models.GetUserByPhone(req.Phone, req.AreaCode)
  329. if err != nil && err != orm.ErrNoRows {
  330. br.Msg = "编辑用户失败"
  331. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  332. return
  333. }
  334. if user != nil {
  335. userCheckResp.UserId = user.UserId
  336. if user.Status == 0 {
  337. br.Ret = 200
  338. br.Success = true
  339. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  340. br.Data = userCheckResp
  341. return
  342. }
  343. if user.Status == 1 {
  344. br.Ret = 200
  345. br.Success = true
  346. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  347. br.Data = userCheckResp
  348. return
  349. }
  350. if user.Status == 2 {
  351. br.Msg = "手机号已存在,请重新输入"
  352. return
  353. }
  354. }
  355. }
  356. if req.Email != "" {
  357. if !utils.ValidateEmailFormatat(req.Email) {
  358. br.Msg = "邮箱格式有误, 请检查"
  359. return
  360. }
  361. user, err := models.GetUserByEmail(req.Email)
  362. if err != nil && err != orm.ErrNoRows {
  363. br.Msg = "编辑用户失败"
  364. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  365. return
  366. }
  367. if user != nil {
  368. userCheckResp.UserId = user.UserId
  369. if user.Status == 0 {
  370. br.Ret = 200
  371. br.Success = true
  372. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  373. br.Data = userCheckResp
  374. return
  375. }
  376. if user.Status == 1 {
  377. br.Ret = 200
  378. br.Success = true
  379. br.Msg = "该用户已被禁用,确认启用并更新用户信息吗?"
  380. br.Data = userCheckResp
  381. return
  382. }
  383. if user.Status == 2 {
  384. br.Msg = "邮箱已存在,请重新输入"
  385. return
  386. }
  387. return
  388. }
  389. }
  390. br.Ret = 200
  391. br.Success = true
  392. br.Msg = "邮箱或手机号合格"
  393. }
  394. // List
  395. // @Title 用户列表
  396. // @Description 用户列表
  397. // @Param PageSize query int true "每页数据条数"
  398. // @Param CurrentIndex query int true "当前页页码,从1开始"
  399. // @Param SellerId query int true "销售id"
  400. // @Param Status query int true "用户状态"
  401. // @Param KeyWord query string true "手机号/邮箱/姓名"
  402. // @Param IsRegistered query string true "是否注册"
  403. // @Param IsSubscribed query string true "是否关注"
  404. // @Param RegisterStartDate query string true "注册开始时间"
  405. // @Param RegisterEndDate query string true "注册结束时间"
  406. // @Param CreateStartDate query string true "创建开始时间"
  407. // @Param CreateEndDate query string true "创建结束时间"
  408. // @Success 200 {object} models.LoginResp
  409. // @router /list [get]
  410. func (this *UserController) List() {
  411. br := new(models.BaseResponse).Init()
  412. defer func() {
  413. this.Data["json"] = br
  414. this.ServeJSON()
  415. }()
  416. pageSize, _ := this.GetInt("PageSize")
  417. currentIndex, _ := this.GetInt("CurrentIndex")
  418. sellerId, _ := this.GetInt("SellerId")
  419. status := this.GetString("Status")
  420. keyWord := this.GetString("KeyWord")
  421. IsRegistered := this.GetString("IsRegisterd")
  422. IsSubscribed := this.GetString("IsSubscribed")
  423. registerStartDate := this.GetString("RegisterStartDate")
  424. registerEndDate := this.GetString("RegisterEndDate")
  425. createStartDate := this.GetString("CreateStartDate")
  426. createEndDate := this.GetString("CreateEndDate")
  427. var condition string
  428. var pars []interface{}
  429. if keyWord != "" {
  430. condition += ` AND (real_name LIKE ? OR phone LIKE ? OR email LIKE ?) `
  431. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  432. }
  433. if pageSize <= 0 {
  434. pageSize = utils.PageSize20
  435. } else if pageSize > utils.PageSize100 {
  436. pageSize = utils.PageSize100
  437. }
  438. if currentIndex <= 0 {
  439. currentIndex = 1
  440. }
  441. if sellerId > 0 {
  442. condition += " AND seller_id=? "
  443. pars = append(pars, sellerId)
  444. }
  445. switch status {
  446. case "禁用":
  447. condition += " AND status=? "
  448. pars = append(pars, 0)
  449. case "潜在":
  450. condition += " AND status=? "
  451. pars = append(pars, 1)
  452. case "正式":
  453. condition += " AND status=? "
  454. pars = append(pars, 2)
  455. case "":
  456. condition += " AND (status=? OR status=?) "
  457. pars = append(pars, 0, 2)
  458. }
  459. switch IsRegistered {
  460. case "是":
  461. condition += " AND is_registered=? "
  462. pars = append(pars, true)
  463. case "否":
  464. condition += " AND is_registered=? "
  465. pars = append(pars, false)
  466. }
  467. switch IsSubscribed {
  468. case "是":
  469. condition += " AND is_subscribed=? "
  470. pars = append(pars, true)
  471. case "否":
  472. condition += " AND is_subscribed=? "
  473. pars = append(pars, false)
  474. }
  475. if registerStartDate != "" {
  476. registerStartTime, er := time.Parse("2006-01-02 15:04:05", registerStartDate)
  477. if er != nil {
  478. br.Msg = "日期格式有误"
  479. return
  480. }
  481. condition += " AND register_time>? "
  482. pars = append(pars, registerStartTime)
  483. }
  484. if registerEndDate != "" {
  485. registerEndTime, er := time.Parse("2006-01-02 15:04:05", registerEndDate)
  486. if er != nil {
  487. br.Msg = "日期格式有误"
  488. return
  489. }
  490. condition += " AND register_time<? "
  491. pars = append(pars, registerEndTime)
  492. }
  493. if createStartDate != "" {
  494. createStartTime, er := time.Parse("2006-01-02 15:04:05", createStartDate)
  495. if er != nil {
  496. br.Msg = "日期格式有误"
  497. return
  498. }
  499. condition += " AND create_time>? "
  500. pars = append(pars, createStartTime)
  501. }
  502. if createEndDate != "" {
  503. createEndTime, er := time.Parse("2006-01-02 15:04:05", createEndDate)
  504. if er != nil {
  505. br.Msg = "日期格式有误"
  506. return
  507. }
  508. condition += " AND create_time<? "
  509. pars = append(pars, createEndTime)
  510. }
  511. if pageSize <= 0 {
  512. pageSize = utils.PageSize20
  513. } else if pageSize > utils.PageSize100 {
  514. pageSize = utils.PageSize100
  515. }
  516. if currentIndex <= 0 {
  517. currentIndex = 1
  518. }
  519. startSize := utils.StartIndex(currentIndex, pageSize)
  520. total, err := models.GetUserCount(condition, pars)
  521. if err != nil {
  522. br.Msg = "获取失败"
  523. br.ErrMsg = "获取失败,Err:" + err.Error()
  524. return
  525. }
  526. userList, err := models.GetUserList(condition, pars, startSize, pageSize)
  527. if err != nil {
  528. br.Msg = "查询用户失败"
  529. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  530. return
  531. }
  532. page := paging.GetPaging(currentIndex, pageSize, total)
  533. resp := new(response.UserListResp)
  534. resp.Paging = page
  535. resp.List = userList
  536. br.Data = resp
  537. br.Ret = 200
  538. br.Success = true
  539. br.Msg = "获取成功"
  540. }
  541. // Detail
  542. // @Title 系统用户详情信息
  543. // @Description 用户详情信息
  544. // @Param UserId query int true "系统用户id"
  545. // @Success 200 {object} models.LoginResp
  546. // @router /detail [get]
  547. func (this *UserController) Detail() {
  548. br := new(models.BaseResponse).Init()
  549. defer func() {
  550. this.Data["json"] = br
  551. this.ServeJSON()
  552. }()
  553. UserId, err := this.GetInt("UserId")
  554. if err != nil {
  555. br.Msg = "参数解析错误"
  556. return
  557. }
  558. if UserId <= 0 {
  559. br.Msg = "查询用户不存在"
  560. return
  561. }
  562. user, err := models.GetUserById(UserId)
  563. if err != nil {
  564. if err == orm.ErrNoRows {
  565. br.Msg = "用户不存在或已删除,请刷新页面"
  566. return
  567. }
  568. br.Msg = "查询用户失败"
  569. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  570. return
  571. }
  572. if user == nil {
  573. br.Msg = "用户不存在或已删除,请刷新页面"
  574. return
  575. }
  576. permissionList, err := services.GetUserPermissionById(UserId)
  577. if err != nil {
  578. br.Msg = "用户权限获取失败,请重新尝试"
  579. return
  580. }
  581. permissionMap := make(map[string][]string)
  582. for _, pm := range permissionList {
  583. permissionMap[pm.ParentName] = append(permissionMap[pm.ParentName], pm.PermissionName)
  584. }
  585. resp := new(response.UserDetailResp)
  586. resp.Detail = user
  587. resp.Permission = permissionMap
  588. br.Msg = "查询成功"
  589. br.Ret = 200
  590. br.Success = true
  591. br.Data = resp
  592. }
  593. // Delete
  594. // @Title 系统用户详情信息
  595. // @Description 用户详情信息
  596. // @Param request body request.UserDeleteReq true "type json string"
  597. // @Success 200 {object} models.LoginResp
  598. // @router /delete [post]
  599. func (this *UserController) Delete() {
  600. br := new(models.BaseResponse).Init()
  601. defer func() {
  602. this.Data["json"] = br
  603. this.ServeJSON()
  604. }()
  605. var req request.UserDeleteReq
  606. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  607. if err != nil {
  608. br.Msg = "参数解析异常!"
  609. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  610. return
  611. }
  612. err = models.DeleteUserById(req.UserId)
  613. if err != nil {
  614. br.Msg = "删除失败"
  615. br.ErrMsg = "删除失败,系统错误,Err:" + err.Error()
  616. return
  617. }
  618. br.Msg = "删除成功"
  619. br.Success = true
  620. br.Ret = 200
  621. }
  622. // @Title 开启/禁用用户
  623. // @Description 开启/禁用用户接口
  624. // @Param request body system.SysuserEditReq true "type json string"
  625. // @Success 200 操作成功
  626. // @router /editEnabled [post]
  627. func (this *UserController) EditEnabled() {
  628. br := new(models.BaseResponse).Init()
  629. defer func() {
  630. this.Data["json"] = br
  631. this.ServeJSON()
  632. }()
  633. var req request.UserEditEnabledReq
  634. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  635. if err != nil {
  636. br.Msg = "参数解析异常!"
  637. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  638. return
  639. }
  640. user, err := models.GetUserById(req.UserId)
  641. if err != nil {
  642. br.Msg = "获取数据失败"
  643. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  644. return
  645. }
  646. // 修改系统用户禁用状态
  647. curTime := time.Now()
  648. if req.IsEnabled && curTime.Before(user.ValidEndTime) && curTime.After(user.ValidStartTime) {
  649. user.Status = 2
  650. }
  651. if !req.IsEnabled {
  652. user.Status = 0
  653. } else {
  654. user.Status = 1
  655. }
  656. user.ModifyTime = curTime
  657. err = user.Update([]string{"status", "modify_time"})
  658. if err != nil {
  659. br.Msg = "修改失败"
  660. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  661. return
  662. }
  663. // 记录操作
  664. record := &models.UserChangeRecord{}
  665. if user.Status == 0 {
  666. record.Content = this.SysUser.SysRealName + "禁用用户"
  667. } else {
  668. record.Content = this.SysUser.SysRealName + "启用用户"
  669. }
  670. record.Insert()
  671. br.Ret = 200
  672. br.Success = true
  673. br.IsAddLog = true
  674. br.Msg = "操作成功"
  675. }
  676. // @Title 用户信息变更记录
  677. // @Description 用户信息变更记录
  678. // @Param PageSize query int true "每页数据条数"
  679. // @Param CurrentIndex query int true "当前页页码,从1开始"
  680. // @Success 200 操作成功
  681. // @router /change_list [get]
  682. func (this *UserController) ChangeList() {
  683. br := new(models.BaseResponse).Init()
  684. defer func() {
  685. this.Data["json"] = br
  686. this.ServeJSON()
  687. }()
  688. pageSize, _ := this.GetInt("PageSize")
  689. currentIndex, _ := this.GetInt("CurrentIndex")
  690. if pageSize <= 0 {
  691. pageSize = utils.PageSize20
  692. } else if pageSize > utils.PageSize100 {
  693. pageSize = utils.PageSize100
  694. }
  695. if currentIndex <= 0 {
  696. currentIndex = 1
  697. }
  698. startSize := utils.StartIndex(currentIndex, pageSize)
  699. total, err := models.GetUserChangeRecordCount()
  700. if err != nil {
  701. br.Msg = "用户信息变更查询失败"
  702. br.ErrMsg = "用户信息变更查询失败,系统错误,Err:" + err.Error()
  703. return
  704. }
  705. userRcord, err := models.GetUserChangeRecordList(startSize, pageSize)
  706. if err != nil {
  707. br.Msg = "用户信息变更查询失败"
  708. br.ErrMsg = "用户信息变更查询失败,系统错误,Err:" + err.Error()
  709. return
  710. }
  711. page := paging.GetPaging(currentIndex, pageSize, total)
  712. resp := new(response.UserChangeRecordResp)
  713. resp.List = userRcord
  714. resp.Paging = page
  715. br.Data = resp
  716. br.Msg = "查询成功"
  717. br.Success = true
  718. br.Ret = 200
  719. }