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