apply.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. )
  10. var Apply services.Apply = &model.Apply{}
  11. // GetApplyList 用户获取自己的应用列表
  12. func GetApplyList(c *gin.Context) {
  13. var params unity.QueryPageParams
  14. var apply model.Apply
  15. if err := c.ShouldBindJSON(&params); err != nil {
  16. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  17. return
  18. }
  19. queryCond := "app_name like ?"
  20. params.Query = "%" + params.Query + "%"
  21. uId, _ := unity.GetUId(c)
  22. apply.UserId = uId
  23. result, total, err := Apply.GetApplyList(params, apply, queryCond)
  24. if err != nil {
  25. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  26. return
  27. } else {
  28. e.ResPonsePage(c, result, total, params)
  29. return
  30. }
  31. }
  32. // GetApplyState 根据状态筛选
  33. func GetApplyState(c *gin.Context) {
  34. var params unity.QueryPageParams
  35. var apply model.Apply
  36. if err := c.ShouldBindJSON(&params); err != nil {
  37. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  38. return
  39. }
  40. queryCond := "app_name like ?"
  41. params.Query = "%" + params.Query + "%"
  42. uId, _ := unity.GetUId(c)
  43. apply.UserId = uId
  44. result, total, err := Apply.GetApplyList(params, apply, queryCond)
  45. if err != nil {
  46. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  47. return
  48. } else {
  49. e.ResPonsePage(c, result, total, params)
  50. return
  51. }
  52. }
  53. // AdminApplyList 管理员获取所有应用列表
  54. func AdminApplyList(c *gin.Context) {
  55. var params unity.QueryPageParams
  56. if err := c.ShouldBindJSON(&params); err != nil {
  57. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  58. return
  59. }
  60. queryCond := "app_name like ?"
  61. params.Query = "%" + params.Query + "%"
  62. result, total, err := unity.PaginateWithCondition(params, model.Apply{}, queryCond)
  63. if err != nil {
  64. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  65. return
  66. } else {
  67. e.ResPonsePage(c, result, total, params)
  68. return
  69. }
  70. }
  71. // AddApply 用户添加应用
  72. func AddApply(c *gin.Context) {
  73. var apply model.Apply
  74. if err := c.ShouldBindJSON(&apply); err != nil {
  75. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  76. return
  77. }
  78. validate := validator.New()
  79. if err := validate.Struct(apply); err != nil {
  80. e.ResponseWithMsg(c, e.ERROR, "请检查必填项")
  81. return
  82. }
  83. // 生成应用ID
  84. id := unity.RandomAppID()
  85. for model.AppIdISRepeat(id) {
  86. id = unity.RandomAppID()
  87. }
  88. //获取当前用户ID
  89. uId, username := unity.GetUId(c)
  90. apply.UserId = uId
  91. apply.AppID = id
  92. apply.UserName = username
  93. addApply := Apply.AddApply(apply)
  94. if addApply != e.SUCCESS {
  95. e.ResponseWithMsg(c, addApply, addApply.GetMsg())
  96. return
  97. } else {
  98. e.ResponseSuccess(c, nil)
  99. }
  100. }
  101. // UserUpdateApply 用户更新应用
  102. func UserUpdateApply(c *gin.Context) {
  103. var apply model.Apply
  104. if err := c.ShouldBindJSON(&apply); err != nil {
  105. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  106. return
  107. }
  108. id, _ := unity.GetUId(c)
  109. apply.UserId = id
  110. updateApply := Apply.UserUpdateApply(apply)
  111. if updateApply != e.SUCCESS {
  112. e.ResponseWithMsg(c, updateApply, updateApply.GetMsg())
  113. return
  114. }
  115. e.ResponseSuccess(c, nil)
  116. }
  117. // GetApplyByAPPID 用户获取应用详情
  118. func GetApplyByAPPID(c *gin.Context) {
  119. appid := c.Query("appid")
  120. validate := validator.New()
  121. if err := validate.Var(appid, "required"); err != nil {
  122. e.ResponseWithMsg(c, e.ERROR, err.Error())
  123. return
  124. }
  125. apply, err := Apply.GetApplyById(appid)
  126. if err != nil {
  127. e.ResponseWithMsg(c, e.ERROR, "当前应用不可用")
  128. return
  129. }
  130. e.ResponseSuccess(c, apply)
  131. }
  132. // GetApplyByName 未登录状态下的模糊查询获取应用
  133. func GetApplyByName(c *gin.Context) {
  134. appname := c.Query("appname")
  135. validate := validator.New()
  136. if err := validate.Var(appname, "required"); err != nil {
  137. e.ResponseWithMsg(c, e.ERROR, err.Error())
  138. return
  139. }
  140. apply, err := Apply.QueryApplyByAppName(appname)
  141. if err != nil {
  142. e.ResponseWithMsg(c, e.ERROR, "未查找到相关应用")
  143. return
  144. }
  145. e.ResponseSuccess(c, apply)
  146. }