mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-03 16:45:59 +08:00
add api
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package apiAuth
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/log"
|
||||
pbAuth "Open_IM/src/proto/auth"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/skiffer-git/grpc-etcdv3/getcdv3"
|
||||
"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.RpcGetTokenName)
|
||||
client := pbAuth.NewAuthClient(etcdConn)
|
||||
|
||||
params := paramsUserRegister{}
|
||||
if err := c.BindJSON(¶ms); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
pbData := newUserRegisterReq(¶ms)
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package apiAuth
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/log"
|
||||
pbAuth "Open_IM/src/proto/auth"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/skiffer-git/grpc-etcdv3/getcdv3"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type paramsUserToken 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"`
|
||||
}
|
||||
|
||||
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.RpcGetTokenName)
|
||||
client := pbAuth.NewAuthClient(etcdConn)
|
||||
|
||||
params := paramsUserToken{}
|
||||
if err := c.BindJSON(¶ms); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
pbData := newUserTokenReq(¶ms)
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user