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/api/user/user.go

130 lines
5.4 KiB

3 years ago
package user
import (
3 years ago
jsonData "Open_IM/internal/utils"
3 years ago
api "Open_IM/pkg/base_info"
3 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
3 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
3 years ago
rpc "Open_IM/pkg/proto/user"
3 years ago
"Open_IM/pkg/utils"
3 years ago
"context"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
3 years ago
func GetUsersInfo(c *gin.Context) {
params := api.GetUsersInfoReq{}
3 years ago
if err := c.BindJSON(&params); err != nil {
3 years ago
log.NewError("0", "BindJSON failed ", err.Error())
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()})
return
}
req := &rpc.GetUserInfoReq{}
utils.CopyStructFields(req, &params)
3 years ago
var ok bool
ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok {
log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
return
}
log.NewInfo(params.OperationID, "GetUserInfo args ", req.String())
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := rpc.NewUserClient(etcdConn)
RpcResp, err := client.GetUserInfo(context.Background(), req)
if err != nil {
log.NewError(req.OperationID, "GetUserInfo failed ", err.Error(), req.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
return
}
3 years ago
var publicUserInfoList []*open_im_sdk.PublicUserInfo
for _, v := range RpcResp.UserInfoList {
publicUserInfoList = append(publicUserInfoList,
&open_im_sdk.PublicUserInfo{UserID: v.UserID, Nickname: v.Nickname, FaceURL: v.FaceURL, Gender: v.Gender, AppMangerLevel: v.AppMangerLevel})
3 years ago
}
3 years ago
3 years ago
resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList}
3 years ago
resp.Data = jsonData.JsonDataList(resp.UserInfoList)
3 years ago
log.NewInfo(req.OperationID, "GetUserInfo api return ", resp)
3 years ago
c.JSON(http.StatusOK, resp)
3 years ago
}
func UpdateUserInfo(c *gin.Context) {
3 years ago
params := api.UpdateSelfUserInfoReq{}
3 years ago
if err := c.BindJSON(&params); err != nil {
3 years ago
log.NewError("0", "BindJSON failed ", err.Error())
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
3 years ago
req := &rpc.UpdateUserInfoReq{UserInfo: &open_im_sdk.UserInfo{}}
utils.CopyStructFields(req.UserInfo, &params)
3 years ago
3 years ago
req.OperationID = params.OperationID
3 years ago
var ok bool
ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok {
log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
return
3 years ago
}
3 years ago
log.NewInfo(params.OperationID, "UpdateUserInfo args ", req.String())
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := rpc.NewUserClient(etcdConn)
3 years ago
RpcResp, err := client.UpdateUserInfo(context.Background(), req)
if err != nil {
3 years ago
log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), req.String())
3 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
return
}
3 years ago
resp := api.UpdateUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}}
log.NewInfo(req.OperationID, "UpdateUserInfo api return ", resp)
c.JSON(http.StatusOK, resp)
3 years ago
}
3 years ago
func GetSelfUserInfo(c *gin.Context) {
params := api.GetSelfUserInfoReq{}
if err := c.BindJSON(&params); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()})
return
}
req := &rpc.GetUserInfoReq{}
3 years ago
3 years ago
utils.CopyStructFields(req, &params)
var ok bool
ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok {
log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
return
}
3 years ago
req.UserIDList = append(req.UserIDList, req.OpUserID)
3 years ago
log.NewInfo(params.OperationID, "GetUserInfo args ", req.String())
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := rpc.NewUserClient(etcdConn)
RpcResp, err := client.GetUserInfo(context.Background(), req)
if err != nil {
log.NewError(req.OperationID, "GetUserInfo failed ", err.Error(), req.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
return
}
if len(RpcResp.UserInfoList) == 1 {
3 years ago
resp := api.GetSelfUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfo: RpcResp.UserInfoList[0]}
resp.Data = jsonData.JsonDataOne(resp.UserInfo)
3 years ago
log.NewInfo(req.OperationID, "GetUserInfo api return ", resp)
c.JSON(http.StatusOK, resp)
} else {
resp := api.GetSelfUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}}
log.NewInfo(req.OperationID, "GetUserInfo api return ", resp)
c.JSON(http.StatusOK, resp)
}
}