user.go 20 KB

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