提交 f933abe4 编写于 作者: programor_guo's avatar programor_guo

add check user is registered

上级 be946745
......@@ -32,6 +32,10 @@ type paramsGetUsersOnlineStatus struct {
OperationID string `json:"operationID" binding:"required"`
UserIDList []string `json:"userIDList" binding:"required,lte=200"`
}
type paramsAccountCheck struct {
OperationID string `json:"operationID" binding:"required"`
UserIDList []string `json:"userIDList" binding:"required,lte=100"`
}
func DeleteUser(c *gin.Context) {
params := paramsDeleteUsers{}
......@@ -87,6 +91,32 @@ func GetAllUsersUid(c *gin.Context) {
resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "uidList": RpcResp.UidList}
c.JSON(http.StatusOK, resp)
}
func AccountCheck(c *gin.Context) {
params := paramsAccountCheck{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
log.InfoByKv("AccountCheck req come here", params.OperationID, params.UserIDList)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
//defer etcdConn.Close()
req := &pbUser.AccountCheckReq{
OperationID: params.OperationID,
Token: c.Request.Header.Get("token"),
UidList: params.UserIDList,
}
RpcResp, err := client.AccountCheck(context.Background(), req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
log.InfoByKv("call AccountCheck rpc server is success", params.OperationID, "resp args", RpcResp.String())
resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "Result": RpcResp.Result}
c.JSON(http.StatusOK, resp)
}
func GetUsersOnlineStatus(c *gin.Context) {
params := paramsGetUsersOnlineStatus{}
......
......@@ -63,3 +63,34 @@ func (s *userServer) GetAllUsersUid(_ context.Context, req *pbUser.GetAllUsersUi
}
}
func (s *userServer) AccountCheck(_ context.Context, req *pbUser.AccountCheckReq) (*pbUser.AccountCheckResp, error) {
log.InfoByKv("rpc AccountCheck arrived server", req.OperationID, "args", req.String())
c, err := token_verify.ParseToken(req.Token)
if err != nil {
log.InfoByKv("parse token failed", req.OperationID, "err", err.Error())
return &pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrorCode: constant.ErrParseToken.ErrCode, ErrorMsg: err.Error()}}, nil
}
if !utils.IsContain(c.UID, config.Config.Manager.AppManagerUid) {
log.ErrorByKv(" Authentication failed", req.OperationID, "args", c)
return &pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrorCode: 401, ErrorMsg: "not authorized"}}, nil
}
uidList, err := im_mysql_model.SelectSomeUID(req.UidList)
if err != nil {
log.ErrorByKv("db get SelectSomeUID failed", req.OperationID, "err", err.Error())
return &pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrorCode: constant.ErrMysql.ErrCode, ErrorMsg: err.Error()}}, nil
} else {
var r []*pbUser.AccountCheckResp_SingleUserStatus
for _, v := range req.UidList {
temp := new(pbUser.AccountCheckResp_SingleUserStatus)
temp.UserID = v
if utils.IsContain(v, uidList) {
temp.AccountStatus = constant.Registered
} else {
temp.AccountStatus = constant.UnRegistered
}
r = append(r, temp)
}
return &pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrorCode: 0, ErrorMsg: ""}, Result: r}, nil
}
}
......@@ -87,6 +87,8 @@ const (
OnlineStatus = "online"
OfflineStatus = "offline"
Registered = "registered"
UnRegistered = "unregistered"
//MsgReceiveOpt
ReceiveMessage = 0
......
......@@ -133,6 +133,25 @@ func SelectAllUID() ([]string, error) {
}
return uid, nil
}
func SelectSomeUID(uids []string) ([]string, error) {
var uid []string
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return uid, err
}
rows, err := dbConn.Raw("select uid from user where uid in (" + sqlStringHandle(uids) + ")").Rows()
if err != nil {
return uid, err
}
defer rows.Close()
var strUID string
for rows.Next() {
rows.Scan(&strUID)
uid = append(uid, strUID)
}
return uid, nil
}
func IsExistUser(uid string) bool {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
......@@ -149,3 +168,12 @@ func IsExistUser(uid string) bool {
}
return true
}
func sqlStringHandle(ss []string) (s string) {
for i := 0; i < len(ss); i++ {
s += "'" + ss[i] + "'"
if i < len(ss)-1 {
s += ","
}
}
return s
}
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册