appUser.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/go-playground/validator/v10"
  5. "project_management/app/e"
  6. "project_management/app/model"
  7. "project_management/app/services"
  8. "project_management/unity"
  9. "strings"
  10. )
  11. var AppUser services.AppUser = &model.AppUser{}
  12. // RegistApply 注册app用户
  13. func RegistApply(c *gin.Context) {
  14. var appuser model.AppUserRegist
  15. if err := c.ShouldBindJSON(&appuser); err != nil {
  16. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  17. return
  18. }
  19. if !model.IsRegist(appuser.AppID) {
  20. e.ResponseWithMsg(c, e.TheSystemCannotBeRegistered, e.TheSystemCannotBeRegistered.GetMsg())
  21. return
  22. }
  23. user := AppUser.RegistAppUser(appuser)
  24. if user == e.SUCCESS {
  25. e.ResponseWithMsg(c, e.SUCCESS, e.SUCCESS.GetMsg())
  26. return
  27. }
  28. e.ResponseWithMsg(c, user, user.GetMsg())
  29. }
  30. // LoginAppUser 用户登录-账号密码登录
  31. func LoginAppUser(c *gin.Context) {
  32. var appuser model.AppUserRegist
  33. if err := c.ShouldBindJSON(&appuser); err != nil {
  34. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  35. return
  36. }
  37. validate := validator.New()
  38. err := validate.Struct(appuser)
  39. if err != nil {
  40. e.ResponseWithMsg(c, e.PleaseCheckTherRquiredFields, e.PleaseCheckTherRquiredFields.GetMsg())
  41. return
  42. }
  43. login, token := AppUser.AccountLogin(appuser)
  44. if login == e.NOTTABLE {
  45. e.ResponseWithMsg(c, e.SUCCESS, e.NOTTABLE.GetMsg())
  46. return
  47. }
  48. if login == e.SUCCESS {
  49. e.ResponseSuccess(c, token)
  50. return
  51. }
  52. e.ResponseWithMsg(c, login, login.GetMsg())
  53. }
  54. // LoginAppUserByCode 验证码登录
  55. func LoginAppUserByCode(c *gin.Context) {
  56. var appuser model.AppUserRegist
  57. if err := c.ShouldBindJSON(&appuser); err != nil {
  58. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  59. return
  60. }
  61. validate := validator.New()
  62. err := validate.Var(appuser.Code, "required")
  63. err = validate.Var(appuser.Phone, "required")
  64. err = validate.Var(appuser.AppID, "required")
  65. if err != nil {
  66. e.ResponseWithMsg(c, e.PleaseCheckTherRquiredFields, e.PleaseCheckTherRquiredFields.GetMsg())
  67. return
  68. }
  69. login, s := AppUser.CodeLogin(appuser)
  70. if login == e.SUCCESS {
  71. e.ResponseSuccess(c, s)
  72. return
  73. }
  74. e.ResponseWithMsg(c, login, login.GetMsg())
  75. }
  76. // AddAppUser 添加用户
  77. func AddAppUser(c *gin.Context) {
  78. var appuser model.AppUser
  79. if err := c.ShouldBindJSON(&appuser); err != nil {
  80. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  81. return
  82. }
  83. validate := validator.New()
  84. err := validate.Struct(appuser)
  85. if err != nil {
  86. e.ResponseWithMsg(c, e.JSONParsingFailed, err.Error())
  87. }
  88. user := AppUser.AddAppUser(appuser)
  89. if user == e.SUCCESS {
  90. e.ResponseWithMsg(c, e.SUCCESS, e.SUCCESS.GetMsg())
  91. return
  92. }
  93. e.ResponseWithMsg(c, user, user.GetMsg())
  94. }
  95. // GetAppUserList 获取用户列表
  96. func GetAppUserList(c *gin.Context) {
  97. var params unity.CapQueryPageParams
  98. if err := c.ShouldBindJSON(&params); err != nil {
  99. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  100. return
  101. }
  102. tableName := "appuser_" + params.AppId
  103. queryCond := "username like ?"
  104. params.Query = "%" + params.Query + "%"
  105. result, total, err := AppUser.GetAppUserList(params, tableName, queryCond)
  106. if err != nil {
  107. if strings.Contains(err.Error(), "当前系统没有该表") {
  108. e.ResponseWithMsg(c, e.SUCCESS, "当前用户为空")
  109. return
  110. }
  111. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  112. return
  113. }
  114. e.CapResPonsePage(c, result, total, params)
  115. }
  116. // UpdateAppUser 更新用户信息
  117. func UpdateAppUser(c *gin.Context) {
  118. var appuser model.AppUser
  119. if err := c.ShouldBindJSON(&appuser); err != nil {
  120. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  121. return
  122. }
  123. validate := validator.New()
  124. if err := validate.Struct(appuser); err != nil {
  125. e.ResponseWithMsg(c, e.PleaseCheckTherRquiredFields, e.PleaseCheckTherRquiredFields.GetMsg())
  126. return
  127. }
  128. tablename := "appuser_" + appuser.AppID
  129. user := AppUser.UpdateAppUser(appuser, tablename)
  130. if user == e.SUCCESS {
  131. e.ResponseSuccess(c, "")
  132. return
  133. }
  134. e.ResponseWithMsg(c, user, user.GetMsg())
  135. }
  136. // DeleteAppUserByID 删除用户
  137. func DeleteAppUserByID(c *gin.Context) {
  138. var deldate model.AppUserDel
  139. err := c.ShouldBindJSON(&deldate)
  140. if err != nil {
  141. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  142. return
  143. }
  144. validate := validator.New()
  145. err = validate.Struct(deldate)
  146. if err != nil {
  147. e.ResponseWithMsg(c, e.PleaseCheckTherRquiredFields, e.PleaseCheckTherRquiredFields.GetMsg())
  148. }
  149. tablename := "appuser_" + deldate.AppID
  150. if err != nil {
  151. e.ResponseWithMsg(c, e.JSONParsingFailed, "id格式错误")
  152. return
  153. }
  154. byID := AppUser.DeleteAppUserByID(deldate.ID, tablename)
  155. if byID == e.SUCCESS {
  156. e.ResponseSuccess(c, "")
  157. return
  158. }
  159. e.ResponseWithMsg(c, byID, byID.GetMsg())
  160. }