user.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. // @Param Last query string true "创建结束时间"
  413. // @Param CreateEndDate query string true "创建结束时间"
  414. // @Success 200 {object} response.UserListResp
  415. // @router /list [get]
  416. func (this *UserController) List() {
  417. br := new(models.BaseResponse).Init()
  418. defer func() {
  419. this.Data["json"] = br
  420. this.ServeJSON()
  421. }()
  422. pageSize, _ := this.GetInt("PageSize")
  423. currentIndex, _ := this.GetInt("CurrentIndex")
  424. sellerId, _ := this.GetInt("SellerId")
  425. status := this.GetString("Status")
  426. keyWord := this.GetString("KeyWord")
  427. IsRegistered := this.GetString("IsRegisterd")
  428. IsSubscribed := this.GetString("IsSubscribed")
  429. registerStartDate := this.GetString("RegisterStartDate")
  430. registerEndDate := this.GetString("RegisterEndDate")
  431. createStartDate := this.GetString("CreateStartDate")
  432. createEndDate := this.GetString("CreateEndDate")
  433. var condition string
  434. var pars []interface{}
  435. if keyWord != "" {
  436. condition += ` AND (real_name LIKE ? OR phone LIKE ? OR email LIKE ?) `
  437. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  438. }
  439. if pageSize <= 0 {
  440. pageSize = utils.PageSize20
  441. } else if pageSize > utils.PageSize100 {
  442. pageSize = utils.PageSize100
  443. }
  444. if currentIndex <= 0 {
  445. currentIndex = 1
  446. }
  447. if sellerId > 0 {
  448. condition += " AND seller_id=? "
  449. pars = append(pars, sellerId)
  450. }
  451. switch status {
  452. case "禁用":
  453. condition += " AND status=? "
  454. pars = append(pars, 0)
  455. case "潜在":
  456. condition += " AND status=? "
  457. pars = append(pars, 1)
  458. case "正式":
  459. condition += " AND status=? "
  460. pars = append(pars, 2)
  461. case "":
  462. condition += " AND (status=? OR status=?) "
  463. pars = append(pars, 0, 2)
  464. }
  465. switch IsRegistered {
  466. case "是":
  467. condition += " AND is_registered=? "
  468. pars = append(pars, true)
  469. case "否":
  470. condition += " AND is_registered=? "
  471. pars = append(pars, false)
  472. }
  473. switch IsSubscribed {
  474. case "是":
  475. condition += " AND is_subscribed=? "
  476. pars = append(pars, true)
  477. case "否":
  478. condition += " AND is_subscribed=? "
  479. pars = append(pars, false)
  480. }
  481. if registerStartDate != "" {
  482. registerStartTime, er := time.Parse("2006-01-02 15:04:05", registerStartDate)
  483. if er != nil {
  484. br.Msg = "日期格式有误"
  485. return
  486. }
  487. condition += " AND register_time>? "
  488. pars = append(pars, registerStartTime)
  489. }
  490. if registerEndDate != "" {
  491. registerEndTime, er := time.Parse("2006-01-02 15:04:05", registerEndDate)
  492. if er != nil {
  493. br.Msg = "日期格式有误"
  494. return
  495. }
  496. condition += " AND register_time<? "
  497. pars = append(pars, registerEndTime)
  498. }
  499. if createStartDate != "" {
  500. createStartTime, er := time.Parse("2006-01-02 15:04:05", createStartDate)
  501. if er != nil {
  502. br.Msg = "日期格式有误"
  503. return
  504. }
  505. condition += " AND create_time>? "
  506. pars = append(pars, createStartTime)
  507. }
  508. if createEndDate != "" {
  509. createEndTime, er := time.Parse("2006-01-02 15:04:05", createEndDate)
  510. if er != nil {
  511. br.Msg = "日期格式有误"
  512. return
  513. }
  514. condition += " AND create_time<? "
  515. pars = append(pars, createEndTime)
  516. }
  517. if pageSize <= 0 {
  518. pageSize = utils.PageSize20
  519. } else if pageSize > utils.PageSize100 {
  520. pageSize = utils.PageSize100
  521. }
  522. if currentIndex <= 0 {
  523. currentIndex = 1
  524. }
  525. startSize := utils.StartIndex(currentIndex, pageSize)
  526. total, err := models.GetUserCount(condition, pars)
  527. if err != nil {
  528. br.Msg = "获取失败"
  529. br.ErrMsg = "获取失败,Err:" + err.Error()
  530. return
  531. }
  532. userList, err := models.GetUserList(condition, pars, startSize, pageSize)
  533. if err != nil {
  534. br.Msg = "查询用户失败"
  535. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  536. return
  537. }
  538. page := paging.GetPaging(currentIndex, pageSize, total)
  539. resp := new(response.UserListResp)
  540. resp.Paging = page
  541. resp.List = userList
  542. br.Data = resp
  543. br.Ret = 200
  544. br.Success = true
  545. br.Msg = "获取成功"
  546. }
  547. // Detail
  548. // @Title 系统用户详情信息
  549. // @Description 用户详情信息
  550. // @Param UserId query int true "用户id"
  551. // @Success 200 {object} models.LoginResp
  552. // @router /detail [get]
  553. func (this *UserController) Detail() {
  554. br := new(models.BaseResponse).Init()
  555. defer func() {
  556. this.Data["json"] = br
  557. this.ServeJSON()
  558. }()
  559. UserId, _ := this.GetInt("UserId")
  560. if UserId <= 0 {
  561. br.Msg = "查询用户不存在"
  562. return
  563. }
  564. user, err := models.GetUserById(UserId)
  565. if err != nil {
  566. if err == orm.ErrNoRows {
  567. br.Msg = "用户不存在或已删除,请刷新页面"
  568. return
  569. }
  570. br.Msg = "查询用户失败"
  571. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  572. return
  573. }
  574. if user == nil {
  575. br.Msg = "用户不存在或已删除,请刷新页面"
  576. return
  577. }
  578. permissionList, err := services.GetUserPermissionById(UserId)
  579. if err != nil {
  580. br.Msg = "用户权限获取失败,请重新尝试"
  581. return
  582. }
  583. permissionMap := make(map[string][]string)
  584. for _, pm := range permissionList {
  585. permissionMap[pm.ParentName] = append(permissionMap[pm.ParentName], pm.PermissionName)
  586. }
  587. resp := new(response.UserDetailResp)
  588. resp.Detail = user
  589. resp.Permission = permissionMap
  590. br.Msg = "查询成功"
  591. br.Ret = 200
  592. br.Success = true
  593. br.Data = resp
  594. }
  595. // Delete
  596. // @Title 系统用户详情信息
  597. // @Description 用户详情信息
  598. // @Param request body request.UserDeleteReq true "type json string"
  599. // @Success 200 {object} models.LoginResp
  600. // @router /delete [post]
  601. func (this *UserController) Delete() {
  602. br := new(models.BaseResponse).Init()
  603. defer func() {
  604. this.Data["json"] = br
  605. this.ServeJSON()
  606. }()
  607. var req request.UserDeleteReq
  608. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  609. if err != nil {
  610. br.Msg = "参数解析异常!"
  611. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  612. return
  613. }
  614. err = models.DeleteUserById(req.UserId)
  615. if err != nil {
  616. br.Msg = "删除失败"
  617. br.ErrMsg = "删除失败,系统错误,Err:" + err.Error()
  618. return
  619. }
  620. br.Msg = "删除成功"
  621. br.Success = true
  622. br.Ret = 200
  623. }
  624. // @Title 开启/禁用用户
  625. // @Description 开启/禁用用户接口
  626. // @Param request body system.SysuserEditReq true "type json string"
  627. // @Success 200 操作成功
  628. // @router /editEnabled [post]
  629. func (this *UserController) EditEnabled() {
  630. br := new(models.BaseResponse).Init()
  631. defer func() {
  632. this.Data["json"] = br
  633. this.ServeJSON()
  634. }()
  635. var req request.UserEditEnabledReq
  636. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  637. if err != nil {
  638. br.Msg = "参数解析异常!"
  639. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  640. return
  641. }
  642. validStartTime, err := time.Parse("2006-01-02", req.ValidStartTime)
  643. if err != nil {
  644. br.Msg = "日期格式不正确"
  645. return
  646. }
  647. validEndTime, err := time.Parse("2006-01-02", req.ValidEndTime)
  648. if err != nil {
  649. br.Msg = "日期格式不正确"
  650. return
  651. }
  652. if !validEndTime.After(validStartTime) {
  653. br.Msg = "有效期结束日期在开始日期之前,日期格式不正确"
  654. return
  655. }
  656. validStartTime = validStartTime.Local().Add(-time.Hour * 8)
  657. validEndTime = validEndTime.Local().Add(-time.Hour*8 + time.Hour*24)
  658. curTime := time.Now()
  659. if curTime.After(validEndTime) {
  660. br.Msg = "有效期已到期,请重新设置"
  661. return
  662. }
  663. user, err := models.GetUserById(req.UserId)
  664. if err != nil {
  665. br.Msg = "获取数据失败"
  666. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  667. return
  668. }
  669. // 修改系统用户禁用状态
  670. if req.IsEnabled {
  671. user.Status = 2
  672. } else {
  673. user.Status = 0
  674. }
  675. user.ModifyTime = curTime
  676. user.ValidStartTime = validStartTime
  677. user.ValidEndTime = validEndTime
  678. err = user.Update([]string{"status", "modify_time", "valid_start_time", "valid_end_time"})
  679. if err != nil {
  680. br.Msg = "修改失败"
  681. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  682. return
  683. }
  684. // 记录操作
  685. record := &models.UserChangeRecord{}
  686. record.UserId = req.UserId
  687. record.SysUserId = this.SysUser.SysUserId
  688. if user.Status == 0 {
  689. record.Content = this.SysUser.SysRealName + "禁用用户"
  690. } else {
  691. record.Content = this.SysUser.SysRealName + "启用用户"
  692. }
  693. record.Insert()
  694. br.Ret = 200
  695. br.Success = true
  696. br.IsAddLog = true
  697. br.Msg = "操作成功"
  698. }
  699. // @Title 用户信息变更记录
  700. // @Description 用户信息变更记录
  701. // @Param UserId query int true "用户id"
  702. // @Success 200 操作成功
  703. // @router /change_list [get]
  704. func (this *UserController) ChangeList() {
  705. br := new(models.BaseResponse).Init()
  706. defer func() {
  707. this.Data["json"] = br
  708. this.ServeJSON()
  709. }()
  710. userId, _ := this.GetInt("UserId")
  711. if userId <= 0 {
  712. br.Msg = "用户信息错误"
  713. return
  714. }
  715. userRcord, err := models.GetUserChangeRecordListById(userId)
  716. if err != nil {
  717. br.Msg = "用户信息变更查询失败"
  718. br.ErrMsg = "用户信息变更查询失败,系统错误,Err:" + err.Error()
  719. return
  720. }
  721. resp := new(response.UserChangeRecordResp)
  722. resp.List = userRcord
  723. br.Data = resp
  724. br.Msg = "查询成功"
  725. br.Success = true
  726. br.Ret = 200
  727. }