You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package api
  15. import (
  16. "github.com/gin-gonic/gin"
  17. "github.com/OpenIMSDK/OpenKF/server/internal/common"
  18. "github.com/OpenIMSDK/OpenKF/server/internal/common/response"
  19. requestparams "github.com/OpenIMSDK/OpenKF/server/internal/params/request"
  20. "github.com/OpenIMSDK/OpenKF/server/internal/service"
  21. "github.com/OpenIMSDK/OpenKF/server/internal/utils"
  22. "github.com/OpenIMSDK/OpenKF/server/pkg/log"
  23. )
  24. // AdminRegister
  25. // @Tags user
  26. // @Summary AdminRegister
  27. // @Description Create a new admin
  28. // @Produce application/json
  29. // @Param data body requestparams.RegisterAdminParams true "RegisterAdminParams"
  30. // @Success 200 {object} response.Response{msg=string} "Success"
  31. // @Router /api/v1/register/admin [post].
  32. func AdminRegister(c *gin.Context) {
  33. var params requestparams.RegisterAdminParams
  34. err := c.ShouldBindJSON(&params)
  35. if err != nil {
  36. response.FailWithCode(common.INVALID_PARAMS, c)
  37. return
  38. }
  39. svc := service.NewUserService(c)
  40. _, _, err = svc.CreateAdmin(&params)
  41. if err != nil {
  42. log.Debug("AdminRegister error", err)
  43. response.FailWithCode(common.ERROR, c)
  44. return
  45. }
  46. response.Success(c)
  47. }
  48. // AccountLogin
  49. // @Tags user
  50. // @Summary AccountLogin
  51. // @Description login with account
  52. // @Produce application/json
  53. // @Param data body requestparams.LoginParamsWithAccount true "LoginParamsWithAccount"
  54. // @Success 200 {object} response.Response{msg=string} "Success"
  55. // @Router /api/v1/login/account [post].
  56. func AccountLogin(c *gin.Context) {
  57. var params requestparams.LoginParamsWithAccount
  58. err := c.ShouldBindJSON(&params)
  59. if err != nil {
  60. response.FailWithCode(common.INVALID_PARAMS, c)
  61. return
  62. }
  63. svc := service.NewUserService(c)
  64. u, err := svc.LoginWithAccount(&params)
  65. if err != nil {
  66. log.Debug("AdminRegister error", err)
  67. response.FailWithCode(common.ERROR, c)
  68. return
  69. }
  70. response.SuccessWithData(u, c)
  71. }
  72. // GetUserInfo
  73. // @Tags user
  74. // @Summary GetUserInfo
  75. // @Description get user info
  76. // @Security ApiKeyAuth
  77. // Param data body param.GetUserInfoParams true "GetUserInfoParams"
  78. // @Success 200 {object} response.Response{msg=string} "Success"
  79. // @Router /api/v1/user/info [post].
  80. func GetUserInfo(c *gin.Context) {
  81. param := requestparams.GetUserInfoParams{}
  82. err := c.ShouldBindJSON(&param)
  83. if err != nil {
  84. response.FailWithCode(common.INVALID_PARAMS, c)
  85. return
  86. }
  87. svc := service.NewUserService(c)
  88. resp, err := svc.GetUserInfoByUUID(param.UUID)
  89. if err != nil {
  90. response.FailWithCode(common.KF_RECORD_NOT_FOUND, c)
  91. return
  92. }
  93. response.SuccessWithData(resp, c)
  94. }
  95. // GetMyInfo
  96. // @Tags user
  97. // @Summary GetMyInfo
  98. // @Description get my user info
  99. // @Security ApiKeyAuth
  100. // @Success 200 {object} response.Response{msg=string} "Success"
  101. // @Router /api/v1/user/me [get].
  102. func GetMyInfo(c *gin.Context) {
  103. uuid, err := utils.GetUserUUID(c)
  104. if err != nil {
  105. response.FailWithCode(common.UNAUTHORIZED, c)
  106. return
  107. }
  108. svc := service.NewUserService(c)
  109. resp, err := svc.GetUserInfoByUUID(uuid)
  110. if err != nil {
  111. response.FailWithCode(common.KF_RECORD_NOT_FOUND, c)
  112. return
  113. }
  114. response.SuccessWithData(resp, c)
  115. }
  116. // GetCommunityUserList
  117. // @Tags user
  118. // @Summary GetCommunityUserList
  119. // @Description get community user info
  120. // @Security ApiKeyAuth
  121. // Param data body param.ListPageParams true "ListPageParams"
  122. // @Success 200 {object} response.Response{msg=string} "Success"
  123. // @Router /api/v1/user/userlist [post].
  124. func GetCommunityUserList(c *gin.Context) {
  125. uuid, err := utils.GetCommunityUUID(c)
  126. if err != nil {
  127. response.FailWithCode(common.UNAUTHORIZED, c)
  128. return
  129. }
  130. svc := service.NewUserService(c)
  131. var params requestparams.ListPageParams
  132. err = c.ShouldBindJSON(&params)
  133. if err != nil {
  134. response.FailWithCode(common.INVALID_PARAMS, c)
  135. return
  136. }
  137. resp, err := svc.GetCommunityUserList(uuid, &params)
  138. if err != nil {
  139. response.FailWithCode(common.ERROR, c)
  140. return
  141. }
  142. response.SuccessWithData(resp, c)
  143. }
  144. // UpdatePassword
  145. // @Tags user
  146. // @Summary UpdatePassword
  147. // @Description update user password
  148. // @Security ApiKeyAuth
  149. // Param data body param.UpdateUserPasswordParams true "UpdateUserPasswordParams"
  150. // @Success 200 {object} response.Response{msg=string} "Success"
  151. // @Router /api/v1/user/update-password [post].
  152. func UpdatePassword(c *gin.Context) {
  153. uuid, err := utils.GetUserUUID(c)
  154. if err != nil {
  155. response.FailWithCode(common.UNAUTHORIZED, c)
  156. return
  157. }
  158. var params requestparams.UpdateUserPasswordParams
  159. err = c.ShouldBindJSON(&params)
  160. if err != nil {
  161. response.FailWithCode(common.INVALID_PARAMS, c)
  162. return
  163. }
  164. svc := service.NewUserService(c)
  165. err = svc.UpdateUserPassword(uuid, &params)
  166. if err != nil {
  167. response.FailWithCode(common.ERROR, c)
  168. return
  169. }
  170. response.Success(c)
  171. }
  172. // UpdateInfo
  173. // @Tags user
  174. // @Summary UpdateInfo
  175. // @Description update user info
  176. // @Security ApiKeyAuth
  177. // Param data body param.UpdateUserInfoParams true "UpdateUserInfoParams"
  178. // @Success 200 {object} response.Response{msg=string} "Success"
  179. // @Router /api/v1/user/update [post].
  180. func UpdateInfo(c *gin.Context) {
  181. uuid, err := utils.GetUserUUID(c)
  182. if err != nil {
  183. response.FailWithCode(common.UNAUTHORIZED, c)
  184. return
  185. }
  186. var params requestparams.UpdateUserInfoParams
  187. err = c.ShouldBindJSON(&params)
  188. if err != nil {
  189. response.FailWithCode(common.INVALID_PARAMS, c)
  190. return
  191. }
  192. svc := service.NewUserService(c)
  193. info, err := svc.UpdateUserInfo(uuid, &params)
  194. if err != nil {
  195. response.FailWithCode(common.ERROR, c)
  196. return
  197. }
  198. response.SuccessWithData(info, c)
  199. }