appUser.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "project_management/app/e"
  5. "project_management/app/model"
  6. "project_management/app/services"
  7. "project_management/unity"
  8. "strconv"
  9. )
  10. var AppUser services.AppUser = &model.AppUser{}
  11. // RegistApply 注册app用户
  12. func RegistApply(c *gin.Context) {
  13. var appuser model.AppUserRegist
  14. if err := c.ShouldBindJSON(&appuser); err != nil {
  15. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  16. return
  17. }
  18. if !model.IsRegist(appuser.AppID) {
  19. e.ResponseWithMsg(c, e.TheSystemCannotBeRegistered, e.TheSystemCannotBeRegistered.GetMsg())
  20. return
  21. }
  22. user := AppUser.RegistAppUser(appuser)
  23. if user == e.SUCCESS {
  24. e.ResponseWithMsg(c, e.SUCCESS, e.SUCCESS.GetMsg())
  25. return
  26. }
  27. e.ResponseWithMsg(c, user, user.GetMsg())
  28. }
  29. // AddAppUser 添加用户
  30. func AddAppUser(c *gin.Context) {
  31. var appuser model.AppUser
  32. if err := c.ShouldBindJSON(&appuser); err != nil {
  33. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  34. return
  35. }
  36. //validate := validator.New()
  37. err := unity.Validate.Struct(appuser)
  38. if err != nil {
  39. e.ResponseWithMsg(c, e.JSONParsingFailed, err.Error())
  40. }
  41. //if err := validate.Struct(appuser); err != nil {
  42. // e.ResponseWithMsg(c, e.JSONParsingFailed, err.Error())
  43. // return
  44. //}
  45. user := AppUser.AddAppUser(appuser)
  46. if user == e.SUCCESS {
  47. e.ResponseWithMsg(c, e.SUCCESS, e.SUCCESS.GetMsg())
  48. return
  49. }
  50. e.ResponseWithMsg(c, user, user.GetMsg())
  51. }
  52. // GetAppUserList 获取用户列表
  53. func GetAppUserList(c *gin.Context) {
  54. var params unity.QueryPageParams
  55. if err := c.ShouldBindJSON(&params); err != nil {
  56. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  57. return
  58. }
  59. app_id := c.Query("app_id")
  60. err := unity.Validate.Var(app_id, "required")
  61. if err != nil {
  62. e.ResponseWithMsg(c, e.JSONParsingFailed, "缺少appid")
  63. return
  64. }
  65. tableName := "appuser_" + app_id
  66. queryCond := "username like ?"
  67. params.Query = "%" + params.Query + "%"
  68. result, total, err := AppUser.GetAppUserList(params, tableName, queryCond)
  69. if err != nil {
  70. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  71. return
  72. }
  73. e.ResPonsePage(c, result, total, params)
  74. }
  75. // UpdateAppUser 更新用户信息
  76. func UpdateAppUser(c *gin.Context) {
  77. var appuser model.AppUser
  78. if err := c.ShouldBindJSON(&appuser); err != nil {
  79. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  80. return
  81. }
  82. app_id := c.Query("app_id")
  83. if err := unity.Validate.Var(app_id, "required"); err != nil {
  84. e.ResponseWithMsg(c, e.JSONParsingFailed, "缺少appid")
  85. return
  86. }
  87. tablename := "appuser_" + app_id
  88. user := AppUser.UpdateAppUser(appuser, tablename)
  89. if user == e.SUCCESS {
  90. e.ResponseSuccess(c, "")
  91. return
  92. }
  93. e.ResponseWithMsg(c, user, user.GetMsg())
  94. }
  95. // DeleteAppUserByID 删除用户
  96. func DeleteAppUserByID(c *gin.Context) {
  97. id := c.PostForm("id")
  98. app_id := c.PostForm("app_id")
  99. if err := unity.Validate.Var(id, "required"); err != nil {
  100. e.ResponseWithMsg(c, e.JSONParsingFailed, "缺少id")
  101. return
  102. }
  103. if err := unity.Validate.Var(app_id, "required"); err != nil {
  104. e.ResponseWithMsg(c, e.JSONParsingFailed, "缺少appid")
  105. return
  106. }
  107. tablename := "appuser_" + app_id
  108. atoi, err := strconv.Atoi(id)
  109. if err != nil {
  110. e.ResponseWithMsg(c, e.JSONParsingFailed, "id格式错误")
  111. return
  112. }
  113. byID := AppUser.DeleteAppUserByID(atoi, tablename)
  114. if byID == e.SUCCESS {
  115. e.ResponseSuccess(c, "")
  116. return
  117. }
  118. e.ResponseWithMsg(c, byID, byID.GetMsg())
  119. }