user_controller.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package controllers
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/service/system"
  5. "xps/viewmodels"
  6. )
  7. type UserController struct {
  8. Base
  9. Service system.UserService
  10. }
  11. func (c *UserController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET", "/page", "GetPage" )
  13. b.Handle("GET", "/{groupId:int64}/{accountId:int64}", "GetById" )
  14. b.Handle("POST", "/add", "Create" )
  15. b.Handle("PUT", "/update", "Update" )
  16. b.Handle("DELETE", "/{accountId:int64}", "DeleteById")
  17. }
  18. func (c *UserController) GetPage() *JsonResult {
  19. params := c.buildParams()
  20. data, err := c.Service.GetPage(params)
  21. if err != nil {
  22. return ResultErr(err.Error(), nil)
  23. } else {
  24. return ResultOk("获取成功", data)
  25. }
  26. }
  27. func (c *UserController) GetById(groupId, accountId int64) *JsonResult {
  28. ocId := c.GetCurOcId()
  29. data, err := c.Service.GetById(ocId, groupId, accountId)
  30. if err != nil {
  31. return ResultErr(err.Error(), nil)
  32. } else {
  33. return ResultOk("获取成功", data)
  34. }
  35. }
  36. func (c *UserController) Create() *JsonResult {
  37. ocId := c.GetCurOcId()
  38. ocTypeId := c.GetOcTypeId()
  39. user := &viewmodels.UserInfo{}
  40. err := c.Ctx.ReadJSON(user)
  41. if err != nil {
  42. return ResultErr("读取数据失败", nil)
  43. }
  44. user.OcId = ocId
  45. err = c.Service.Create(ocTypeId, user)
  46. if err != nil {
  47. return ResultErr(err.Error(), nil)
  48. } else {
  49. return ResultOk("新增成功", nil)
  50. }
  51. }
  52. func (c *UserController) Update() *JsonResult {
  53. user := &viewmodels.UserInfo{}
  54. err := c.Ctx.ReadJSON(user)
  55. if err != nil {
  56. return ResultErr("读取数据失败", nil)
  57. }
  58. err = c.Service.Update(user)
  59. if err != nil {
  60. return ResultErr(err.Error(), nil)
  61. } else {
  62. return ResultOk("更新成功", nil)
  63. }
  64. }
  65. func (c *UserController) DeleteById(accountId int64) *JsonResult {
  66. ocId := c.GetCurOcId()
  67. userId := c.GetCurUserId()
  68. var m = make(map[string]interface{})
  69. m["ocId"] = ocId
  70. m["accountId"] = accountId
  71. m["deletedBy"] = userId
  72. err := c.Service.Delete(m)
  73. if err != nil {
  74. return ResultErr(err.Error(), nil)
  75. } else {
  76. return ResultOk("删除成功", nil)
  77. }
  78. }