You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/cms_api/user/user.go

197 lines
8.0 KiB

3 years ago
package user
import (
"Open_IM/pkg/cms_api_struct"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
2 years ago
pbAdminCms "Open_IM/pkg/proto/admin_cms"
3 years ago
commonPb "Open_IM/pkg/proto/sdk_ws"
pb "Open_IM/pkg/proto/user"
3 years ago
"Open_IM/pkg/utils"
"context"
"net/http"
"strings"
3 years ago
"github.com/gin-gonic/gin"
3 years ago
)
func AddUser(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.AddUserRequest
reqPb pb.AddUserReq
3 years ago
)
3 years ago
if err := c.BindJSON(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
2 years ago
respPb, err := client.AddUser(context.Background(), &reqPb)
3 years ago
if err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), reqPb.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
3 years ago
}
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg})
3 years ago
}
func BlockUser(c *gin.Context) {
3 years ago
var (
2 years ago
req cms_api_struct.BlockUserRequest
3 years ago
reqPb pb.BlockUserReq
3 years ago
)
3 years ago
if err := c.BindJSON(&req); err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
2 years ago
respPb, err := client.BlockUser(context.Background(), &reqPb)
3 years ago
if err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), reqPb.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
3 years ago
}
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg})
3 years ago
}
func UnblockUser(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.UnblockUserRequest
resp cms_api_struct.UnBlockUserResponse
reqPb pb.UnBlockUserReq
3 years ago
)
2 years ago
if err := c.BindJSON(&req); err != nil {
log.NewError(req.OperationID, "BindJSON failed ", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
2 years ago
respPb, err := client.UnBlockUser(context.Background(), &reqPb)
3 years ago
if err != nil {
2 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), err.Error(), reqPb.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
3 years ago
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg})
3 years ago
}
func GetBlockUsers(c *gin.Context) {
3 years ago
var (
req cms_api_struct.GetBlockUsersRequest
3 years ago
resp cms_api_struct.GetBlockUsersResponse
3 years ago
reqPb pb.GetBlockUsersReq
respPb *pb.GetBlockUsersResp
)
3 years ago
reqPb.Pagination = &commonPb.RequestPagination{}
2 years ago
if err := c.BindJSON(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.Pagination, &req)
3 years ago
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "blockUsers", reqPb.Pagination, req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
respPb, err := client.GetBlockUsers(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetBlockUsers rpc", err.Error())
2 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
3 years ago
}
3 years ago
for _, v := range respPb.BlockUsers {
3 years ago
resp.BlockUsers = append(resp.BlockUsers, cms_api_struct.BlockUser{
3 years ago
UserResponse: cms_api_struct.UserResponse{
2 years ago
UserID: v.UserInfo.UserID,
FaceURL: v.UserInfo.FaceURL,
Nickname: v.UserInfo.Nickname,
PhoneNumber: v.UserInfo.PhoneNumber,
Email: v.UserInfo.Email,
Gender: int(v.UserInfo.Gender),
3 years ago
},
BeginDisableTime: v.BeginDisableTime,
EndDisableTime: v.EndDisableTime,
})
}
3 years ago
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
resp.CurrentPage = int(respPb.Pagination.CurrentPage)
3 years ago
resp.UserNums = respPb.UserNums
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}
2 years ago
func GetUserIDByEmailAndPhoneNumber(c *gin.Context) {
var (
req cms_api_struct.GetUserIDByEmailAndPhoneNumberRequest
resp cms_api_struct.GetUserIDByEmailAndPhoneNumberResponse
reqPb pbAdminCms.GetUserIDByEmailAndPhoneNumberReq
respPb *pbAdminCms.GetUserIDByEmailAndPhoneNumberResp
)
if err := c.BindJSON(&req); err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
reqPb.OperationID = req.OperationID
reqPb.Email = req.Email
reqPb.PhoneNumber = req.PhoneNumber
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAdminCMSName, reqPb.OperationID)
2 years ago
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
client := pbAdminCms.NewAdminCMSClient(etcdConn)
respPb, err := client.GetUserIDByEmailAndPhoneNumber(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "rpc failed", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
resp.UserIDList = respPb.UserIDList
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", resp)
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
}