app_controller.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package controller
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/datamodel"
  5. "xps/service"
  6. )
  7. type AppController struct {
  8. Base
  9. Service service.AppService
  10. }
  11. func (c *AppController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET", "/", "GetList" )
  13. b.Handle("GET", "/page", "GetPage" )
  14. b.Handle("GET", "/{appId:int64}", "GetById" )
  15. b.Handle("POST", "/add", "Create" )
  16. b.Handle("PUT", "/update", "Update" )
  17. b.Handle("DELETE", "/{appId:int64}", "DeleteByID" )
  18. b.Handle("GET", "/user/permit", "GetPermit" )
  19. }
  20. func (c *AppController) GetList() *JsonResult {
  21. params := c.buildParams()
  22. data, err := c.Service.GetList(params)
  23. if err != nil {
  24. return ResultErr(err.Error(), nil)
  25. } else {
  26. return ResultOk("获取成功", data)
  27. }
  28. }
  29. func (c *AppController) GetPage() *JsonResult {
  30. params := c.buildParams()
  31. data, err := c.Service.GetPage(params)
  32. if err != nil {
  33. return ResultErr(err.Error(), nil)
  34. } else {
  35. return ResultOk("获取成功", data)
  36. }
  37. }
  38. func (c *AppController) GetById(appId int64) *JsonResult {
  39. data, err := c.Service.GetById(appId)
  40. if err != nil {
  41. return ResultErr(err.Error(), nil)
  42. } else {
  43. return ResultOk("获取成功", data)
  44. }
  45. }
  46. func (c *AppController) Create() *JsonResult {
  47. userId := c.GetCurUserId()
  48. app := datamodel.App{}
  49. app.CreatedBy = userId
  50. if err := c.Ctx.ReadJSON(&app); err != nil {
  51. return ResultErr("读取数据失败", nil)
  52. }
  53. if err := c.Service.Create(&app); err != nil {
  54. return ResultErr(err.Error(), nil)
  55. } else {
  56. return ResultOk("新增成功", nil)
  57. }
  58. }
  59. func (c *AppController) Update() *JsonResult {
  60. userId := c.GetCurUserId()
  61. app := datamodel.App{}
  62. if err := c.Ctx.ReadJSON(&app); err != nil {
  63. return ResultErr("读取数据失败", nil)
  64. }
  65. app.UpdatedBy = userId
  66. if err := c.Service.Update(&app); err != nil {
  67. return ResultErr(err.Error(), nil)
  68. } else {
  69. return ResultOk("更新成功", nil)
  70. }
  71. }
  72. func (c *AppController) DeleteByID(appId int64) *JsonResult {
  73. ocId := c.GetCurOcId()
  74. userId := c.GetCurUserId()
  75. m := make(map[string]interface{})
  76. m["ocId"] = ocId
  77. m["appId"] = appId
  78. m["deletedBy"] = userId
  79. if err := c.Service.Delete(m); err != nil {
  80. return ResultErr(err.Error(), nil)
  81. } else {
  82. return ResultOk("删除成功", nil)
  83. }
  84. }
  85. func (c *AppController) GetPermit() *JsonResult {
  86. roleId := c.GetCurUserRoleId()
  87. appKey := c.GetAppKey()
  88. data, err := c.Service.GetPermit(roleId, appKey)
  89. if err != nil {
  90. return ResultErr(err.Error(), nil)
  91. } else {
  92. return ResultOk("获取成功", data)
  93. }
  94. }