tidy code

This commit is contained in:
wenxu12345 2021-12-27 18:22:32 +08:00
parent 3446442eca
commit 30024c9814
8 changed files with 167 additions and 213 deletions

81
internal/api/auth/auth.go Normal file
View File

@ -0,0 +1,81 @@
package apiAuth
import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
rpc "Open_IM/pkg/proto/auth"
"context"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func UserRegister(c *gin.Context) {
params := api.UserRegisterReq{}
if err := c.BindJSON(&params); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
if params.Secret != config.Config.Secret {
log.NewError(params.OperationID, "params.Secret != config.Config.Secret", params.Secret, config.Config.Secret)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": "not authorized"})
return
}
req := &rpc.UserRegisterReq{}
log.NewInfo("UserRegister args ", req.String())
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName)
client := rpc.NewAuthClient(etcdConn)
reply, err := client.UserRegister(context.Background(), req)
if err != nil || reply.CommonResp.ErrCode != 0 {
log.NewError("0", "UserRegister failed ", err.Error(), req.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
pbDataToken := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID}
replyToken, err := client.UserToken(context.Background(), pbDataToken)
if err != nil {
log.NewError(req.OperationID, "UserToken failed ", err.Error(), pbDataToken)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
resp := api.UserRegisterResp{CommResp: api.CommResp{ErrCode: replyToken.CommonResp.ErrCode, ErrMsg: replyToken.CommonResp.ErrMsg},
UserToken: api.UserTokenInfo{UserID: req.UserInfo.UserID, Token: replyToken.Token, ExpiredTime: replyToken.ExpiredTime}}
c.JSON(http.StatusOK, resp)
log.NewInfo(req.OperationID, "UserRegister return ", resp)
}
func UserToken(c *gin.Context) {
params := api.UserTokenReq{}
if err := c.BindJSON(&params); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
if params.Secret != config.Config.Secret {
log.NewError(params.OperationID, "params.Secret != config.Config.Secret", params.Secret, config.Config.Secret)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": "not authorized"})
return
}
req := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID}
log.NewInfo("UserToken args ", req.String())
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName)
client := rpc.NewAuthClient(etcdConn)
reply, err := client.UserToken(context.Background(), req)
if err != nil {
log.NewError(req.OperationID, "UserToken failed ", err.Error(), req.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
resp := api.UserTokenResp{CommResp: api.CommResp{ErrCode: reply.CommonResp.ErrCode, ErrMsg: reply.CommonResp.ErrMsg},
UserToken: api.UserTokenInfo{UserID: req.FromUserID, Token: reply.Token, ExpiredTime: reply.ExpiredTime}}
c.JSON(http.StatusOK, resp)
log.NewInfo(req.OperationID, "UserRegister return ", resp)
}

View File

@ -1,95 +0,0 @@
package apiAuth
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbAuth "Open_IM/pkg/proto/auth"
"context"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
type paramsUserRegister struct {
Secret string `json:"secret" binding:"required,max=32"`
Platform int32 `json:"platform" binding:"required,min=1,max=7"`
UID string `json:"uid" binding:"required,min=1,max=64"`
Name string `json:"name" binding:"required,min=1,max=64"`
Icon string `json:"icon" binding:"omitempty,max=1024"`
Gender int32 `json:"gender" binding:"omitempty,oneof=0 1 2"`
Mobile string `json:"mobile" binding:"omitempty,max=32"`
Birth string `json:"birth" binding:"omitempty,max=16"`
Email string `json:"email" binding:"omitempty,max=64"`
Ex string `json:"ex" binding:"omitempty,max=1024"`
}
func newUserRegisterReq(params *paramsUserRegister) *pbAuth.UserRegisterReq {
pbData := pbAuth.UserRegisterReq{
UID: params.UID,
Name: params.Name,
Icon: params.Icon,
Gender: params.Gender,
Mobile: params.Mobile,
Birth: params.Birth,
Email: params.Email,
Ex: params.Ex,
}
return &pbData
}
func UserRegister(c *gin.Context) {
log.Info("", "", "api user_register init ....")
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName)
client := pbAuth.NewAuthClient(etcdConn)
//defer etcdConn.Close()
params := paramsUserRegister{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
if params.Secret != config.Config.Secret {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": "not authorized"})
return
}
pbData := newUserRegisterReq(&params)
log.Info("", "", "api user_register is server, [data: %s]", pbData.String())
reply, err := client.UserRegister(context.Background(), pbData)
if err != nil || !reply.Success {
log.Error("", "", "api user_register call rpc fail, [data: %s] [err: %s]", pbData.String(), err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
log.Info("", "", "api user_register call rpc success, [data: %s] [reply: %s]", pbData.String(), reply.String())
pbDataToken := &pbAuth.UserTokenReq{
Platform: params.Platform,
UID: params.UID,
}
replyToken, err := client.UserToken(context.Background(), pbDataToken)
if err != nil {
log.Error("", "", "api user_register call rpc fail, [data: %s] [err: %s]", pbData.String(), err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
log.Info("", "", "api user_register call success, [data: %s] [reply: %s]", pbData.String(), reply.String())
if replyToken.ErrCode == 0 {
c.JSON(http.StatusOK, gin.H{
"errCode": replyToken.ErrCode,
"errMsg": replyToken.ErrMsg,
"data": gin.H{
"uid": pbData.UID,
"token": replyToken.Token,
"expiredTime": replyToken.ExpiredTime,
},
})
} else {
c.JSON(http.StatusOK, gin.H{
"errCode": replyToken.ErrCode,
"errMsg": replyToken.ErrMsg,
})
}
}

View File

@ -1,72 +0,0 @@
package apiAuth
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbAuth "Open_IM/pkg/proto/auth"
"context"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
type paramsUserToken struct {
Secret string `json:"secret" binding:"required,max=32"`
Platform int32 `json:"platform" binding:"required,min=1,max=8"`
UID string `json:"uid" binding:"required,min=1,max=64"`
}
func newUserTokenReq(params *paramsUserToken) *pbAuth.UserTokenReq {
pbData := pbAuth.UserTokenReq{
Platform: params.Platform,
UID: params.UID,
}
return &pbData
}
func UserToken(c *gin.Context) {
log.Info("", "", "api user_token init ....")
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName)
client := pbAuth.NewAuthClient(etcdConn)
//defer etcdConn.Close()
params := paramsUserToken{}
if err := c.BindJSON(&params); err != nil {
log.Error("", "", params.UID, params.Platform, params.Secret)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
if params.Secret != config.Config.Secret {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": "not authorized"})
return
}
pbData := newUserTokenReq(&params)
log.Info("", "", "api user_token is server, [data: %s]", pbData.String())
reply, err := client.UserToken(context.Background(), pbData)
if err != nil {
log.Error("", "", "api user_token call rpc fail, [data: %s] [err: %s]", pbData.String(), err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
return
}
log.Info("", "", "api user_token call rpc success, [data: %s] [reply: %s]", pbData.String(), reply.String())
if reply.ErrCode == 0 {
c.JSON(http.StatusOK, gin.H{
"errCode": reply.ErrCode,
"errMsg": reply.ErrMsg,
"data": gin.H{
"uid": pbData.UID,
"token": reply.Token,
"expiredTime": reply.ExpiredTime,
},
})
} else {
c.JSON(http.StatusOK, gin.H{
"errCode": reply.ErrCode,
"errMsg": reply.ErrMsg,
})
}
}

View File

@ -48,6 +48,7 @@ func AddBlacklist(c *gin.Context) {
func ImportFriend(c *gin.Context) {
params := api.ImportFriendReq{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
log.NewError("0", "BindJSON failed ", err.Error())
return
@ -104,7 +105,7 @@ func AddFriend(c *gin.Context) {
return
}
resp := api.AddFriendResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}}
resp := api.AddFriendResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}}
c.JSON(http.StatusOK, resp)
log.NewInfo(req.CommID.OperationID, "AddFriend api return ", resp)
}

View File

@ -66,7 +66,8 @@ func GetGroupMembersInfo(c *gin.Context) {
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"})
//c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
api.SetErrCodeMsg(c, http.StatusInternalServerError)
return
}
log.NewInfo(req.OperationID, "GetGroupMembersInfo args ", req.String())

View File

@ -1,15 +1,13 @@
package user
import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
"Open_IM/pkg/utils"
// rpc "Open_IM/pkg/proto/relay"
api "Open_IM/pkg/base_info"
rpc "Open_IM/pkg/proto/user"
"Open_IM/pkg/utils"
"context"
"github.com/gin-gonic/gin"
"net/http"
@ -77,22 +75,6 @@ import (
//
// c.JSON(http.StatusOK, resp)
//}
//params := api.AddBlacklistReq{}
// if err := c.BindJSON(&params); err != nil {
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
// log.NewError("0", "BindJSON failed ", err.Error())
// return
// }
// req := &rpc.AddBlacklistReq{}
// utils.CopyStructFields(req.CommID, params)
// var ok bool
// ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
// if !ok {
// log.NewError(req.CommID.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
// return
// }
// log.NewInfo(params.OperationID, "AddBlacklist args ", req.String())
func GetUserInfo(c *gin.Context) {
params := api.GetUserInfoReq{}
@ -128,37 +110,30 @@ func GetUserInfo(c *gin.Context) {
}
func UpdateUserInfo(c *gin.Context) {
log.InfoByKv("api update userinfo init...", "")
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
//defer etcdConn.Close()
params := paramsStruct{}
params := api.GetUserInfoReq{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
req := &pbUser.UpdateUserInfoReq{
OperationID: params.OperationID,
Token: c.Request.Header.Get("token"),
Name: params.Name,
Icon: params.Icon,
Gender: params.Gender,
Mobile: params.Mobile,
Birth: params.Birth,
Email: params.Email,
Ex: params.Ex,
Uid: params.Uid,
req := &rpc.UpdateUserInfoReq{}
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
}
log.InfoByKv("api update user info is server", req.OperationID, req.Token)
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)
RpcResp, err := client.UpdateUserInfo(context.Background(), req)
if err != nil {
log.Error(req.Token, req.OperationID, "err=%s,call get user info rpc server failed", err)
log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), req.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
return
}
log.InfoByKv("call update user info rpc server success", params.OperationID)
c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg})
log.InfoByKv("api update user info return success", params.OperationID, "args=%s", RpcResp.String())
c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.CommonResp.ErrCode, "errMsg": RpcResp.CommonResp.ErrMsg})
log.NewInfo(req.OperationID, "UpdateUserInfo api return ", RpcResp.CommonResp)
}

View File

@ -0,0 +1,39 @@
package base_info
//UserID string `protobuf:"bytes,1,opt,name=UserID" json:"UserID,omitempty"`
// Nickname string `protobuf:"bytes,2,opt,name=Nickname" json:"Nickname,omitempty"`
// FaceUrl string `protobuf:"bytes,3,opt,name=FaceUrl" json:"FaceUrl,omitempty"`
// Gender int32 `protobuf:"varint,4,opt,name=Gender" json:"Gender,omitempty"`
// PhoneNumber string `protobuf:"bytes,5,opt,name=PhoneNumber" json:"PhoneNumber,omitempty"`
// Birth string `protobuf:"bytes,6,opt,name=Birth" json:"Birth,omitempty"`
// Email string `protobuf:"bytes,7,opt,name=Email" json:"Email,omitempty"`
// Ex string `protobuf:"bytes,8,opt,name=Ex" json:"Ex,omitempty"`
type UserRegisterReq struct {
Secret string `json:"secret" binding:"required,max=32"`
Platform int32 `json:"platform" binding:"required,min=1,max=7"`
UserInfo
OperationID string `json:"operationID" binding:"required"`
}
type UserTokenInfo struct {
UserID string `json:"secret"`
Token string `json:"token"`
ExpiredTime int64 `json:"expiredTime"`
}
type UserRegisterResp struct {
CommResp
UserToken UserTokenInfo `json:"data"`
}
type UserTokenReq struct {
Secret string `json:"secret" binding:"required,max=32"`
Platform int32 `json:"platform" binding:"required,min=1,max=8"`
UserID string `json:"userID" binding:"required,min=1,max=64"`
OperationID string `json:"operationID" binding:"required"`
}
type UserTokenResp struct {
CommResp
UserToken UserTokenInfo `json:"data"`
}

View File

@ -1,4 +1,28 @@
//package base_info
package base_info
import (
"github.com/gin-gonic/gin"
"net/http"
)
type UserInfo struct {
UserID string `json:"userID" binding:"required,min=1,max=64"`
Nickname string `json:"nickname" binding:"required,min=1,max=64"`
FaceUrl string `json:"faceUrl" binding:"omitempty,max=1024"`
Gender int32 `json:"gender" binding:"omitempty,oneof=0 1 2"`
PhoneNumber string `json:"phoneNumber" binding:"omitempty,max=32"`
Birth string `json:"birth" binding:"omitempty,max=16"`
Email string `json:"email" binding:"omitempty,max=64"`
Ex string `json:"ex" binding:"omitempty,max=1024"`
}
//c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
func SetErrCodeMsg(c *gin.Context, status int) *CommResp {
resp := CommResp{ErrCode: int32(status), ErrMsg: http.StatusText(status)}
c.JSON(status, resp)
return &resp
}
//
//type GroupInfo struct {
// GroupID string `json:"groupID"`