mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-04 11:22:10 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			72 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package register
 | 
						|
 | 
						|
import (
 | 
						|
	api "Open_IM/pkg/base_info"
 | 
						|
	"Open_IM/pkg/common/config"
 | 
						|
	"Open_IM/pkg/common/constant"
 | 
						|
	"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
 | 
						|
	http2 "Open_IM/pkg/common/http"
 | 
						|
	"Open_IM/pkg/common/log"
 | 
						|
	"encoding/json"
 | 
						|
	"fmt"
 | 
						|
	"net/http"
 | 
						|
 | 
						|
	"github.com/gin-gonic/gin"
 | 
						|
	"github.com/spf13/viper"
 | 
						|
)
 | 
						|
 | 
						|
type ParamsLogin struct {
 | 
						|
	Email       string `json:"email"`
 | 
						|
	PhoneNumber string `json:"phoneNumber"`
 | 
						|
	Password    string `json:"password"`
 | 
						|
	Platform    int32  `json:"platform"`
 | 
						|
	OperationID string `json:"operationID" binding:"required"`
 | 
						|
}
 | 
						|
 | 
						|
func Login(c *gin.Context) {
 | 
						|
	params := ParamsLogin{}
 | 
						|
	if err := c.BindJSON(¶ms); err != nil {
 | 
						|
		c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
 | 
						|
		return
 | 
						|
	}
 | 
						|
	var account string
 | 
						|
	if params.Email != "" {
 | 
						|
		account = params.Email
 | 
						|
	} else {
 | 
						|
		account = params.PhoneNumber
 | 
						|
	}
 | 
						|
 | 
						|
	r, err := im_mysql_model.GetRegister(account)
 | 
						|
	if err != nil {
 | 
						|
		log.NewError(params.OperationID, "user have not register", params.Password, account, err.Error())
 | 
						|
		c.JSON(http.StatusOK, gin.H{"errCode": constant.NotRegistered, "errMsg": "Mobile phone number is not registered"})
 | 
						|
		return
 | 
						|
	}
 | 
						|
	if r.Password != params.Password {
 | 
						|
		log.NewError(params.OperationID, "password err", params.Password, account, r.Password, r.Account)
 | 
						|
		c.JSON(http.StatusOK, gin.H{"errCode": constant.PasswordErr, "errMsg": "password err"})
 | 
						|
		return
 | 
						|
	}
 | 
						|
	url := fmt.Sprintf("http://%s:10000/auth/user_token", viper.GetString("endpoints.api"))
 | 
						|
	openIMGetUserToken := api.UserTokenReq{}
 | 
						|
	openIMGetUserToken.OperationID = params.OperationID
 | 
						|
	openIMGetUserToken.Platform = params.Platform
 | 
						|
	openIMGetUserToken.Secret = config.Config.Secret
 | 
						|
	openIMGetUserToken.UserID = account
 | 
						|
	openIMGetUserTokenResp := api.UserTokenResp{}
 | 
						|
	bMsg, err := http2.Post(url, openIMGetUserToken, config.Config.MessageCallBack.CallBackTimeOut)
 | 
						|
	if err != nil {
 | 
						|
		log.NewError(params.OperationID, "request openIM get user token error", account, "err", err.Error())
 | 
						|
		c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": err.Error()})
 | 
						|
		return
 | 
						|
	}
 | 
						|
	err = json.Unmarshal(bMsg, &openIMGetUserTokenResp)
 | 
						|
	if err != nil || openIMGetUserTokenResp.ErrCode != 0 {
 | 
						|
		log.NewError(params.OperationID, "request get user token", account, "err", "")
 | 
						|
		c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": ""})
 | 
						|
		return
 | 
						|
	}
 | 
						|
	c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMGetUserTokenResp.UserToken})
 | 
						|
 | 
						|
}
 |