apply.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. uId, _, 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. apply.UserId = uId
  44. if role == "admin" {
  45. result, total, err := Apply.GetApplyAminList(params, apply, queryCond)
  46. if err != nil {
  47. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  48. return
  49. }
  50. e.ResPonsePage(c, result, total, params)
  51. return
  52. }
  53. result, total, err := Apply.GetApplyList(params, apply, queryCond)
  54. if err != nil {
  55. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  56. return
  57. } else {
  58. e.ResPonsePage(c, result, total, params)
  59. return
  60. }
  61. }
  62. // AddApply 用户添加应用
  63. func AddApply(c *gin.Context) {
  64. var apply model.Apply
  65. if err := c.ShouldBindJSON(&apply); err != nil {
  66. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  67. return
  68. }
  69. validate := validator.New()
  70. if err := validate.Struct(apply); err != nil {
  71. e.ResponseWithMsg(c, e.ERROR, "请检查必填项")
  72. return
  73. }
  74. // 生成应用ID
  75. id := unity.RandomAppID()
  76. for model.AppIdISRepeat(id) {
  77. id = unity.RandomAppID()
  78. }
  79. //获取当前用户ID
  80. uId, username, _ := unity.GetUId(c)
  81. apply.UserId = uId
  82. apply.AppID = id
  83. apply.UserName = username
  84. addApply := Apply.AddApply(apply)
  85. if addApply != e.SUCCESS {
  86. e.ResponseWithMsg(c, addApply, addApply.GetMsg())
  87. return
  88. } else {
  89. e.ResponseSuccess(c, nil)
  90. }
  91. }
  92. // UserUpdateApply 用户更新应用
  93. func UserUpdateApply(c *gin.Context) {
  94. var apply model.Apply
  95. if err := c.ShouldBindJSON(&apply); err != nil {
  96. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  97. return
  98. }
  99. id, _, role := unity.GetUId(c)
  100. if role == "admin" {
  101. byId, err := unity.UpdateById(apply.ID, apply)
  102. if err != nil {
  103. e.ResponseWithMsg(c, e.UPDATEFAIL, e.UPDATEFAIL.GetMsg())
  104. return
  105. }
  106. e.ResponseSuccess(c, byId)
  107. return
  108. }
  109. apply.UserId = id
  110. updateApply := Apply.UserUpdateApply(apply)
  111. if updateApply != e.SUCCESS {
  112. e.ResponseWithMsg(c, updateApply, "更新失败")
  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. value, _ := c.Get("phone")
  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, value.(string))
  142. if err != nil {
  143. e.ResponseWithMsg(c, e.ERROR, "未查找到相关应用")
  144. return
  145. }
  146. e.ResponseSuccess(c, apply)
  147. }
  148. // GetApplyByNameAll 未登录状态下获取所有应用列表
  149. func GetApplyByNameAll(c *gin.Context) {
  150. var params unity.QueryPageParams
  151. var apply model.Apply
  152. if err := c.ShouldBindJSON(&params); err != nil {
  153. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  154. return
  155. }
  156. phone, _ := c.Get("phone")
  157. result, total, err := Apply.CollectionList(params, apply, phone.(string))
  158. if err != nil {
  159. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  160. return
  161. }
  162. e.ResPonsePage(c, result, total, params)
  163. }