Files
open-im-server/internal/demo/register/set_password.go
T

180 lines
6.5 KiB
Go
Raw Normal View History

2021-12-01 12:16:02 +08:00
package register
import (
2022-02-11 16:25:28 +08:00
api "Open_IM/pkg/base_info"
2021-12-01 12:16:02 +08:00
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
2022-08-15 16:51:41 +08:00
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
2022-02-11 16:25:28 +08:00
http2 "Open_IM/pkg/common/http"
2021-12-01 12:16:02 +08:00
"Open_IM/pkg/common/log"
2022-08-12 18:37:51 +08:00
pbFriend "Open_IM/pkg/proto/friend"
2021-12-17 16:43:41 +08:00
"Open_IM/pkg/utils"
2021-12-01 12:16:02 +08:00
"encoding/json"
2022-06-15 17:33:48 +08:00
"math/big"
2021-12-17 16:43:41 +08:00
"net/http"
2022-06-15 17:33:48 +08:00
"strconv"
"time"
2022-08-07 22:37:27 +08:00
"github.com/gin-gonic/gin"
2021-12-01 12:16:02 +08:00
)
type ParamsSetPassword struct {
2022-07-05 14:30:53 +08:00
UserID string `json:"userID"`
2021-12-01 12:16:02 +08:00
Email string `json:"email"`
2022-06-10 12:17:44 +08:00
Nickname string `json:"nickname"`
2021-12-01 12:16:02 +08:00
PhoneNumber string `json:"phoneNumber"`
2022-06-10 10:16:18 +08:00
Password string `json:"password" binding:"required"`
2021-12-01 12:16:02 +08:00
VerificationCode string `json:"verificationCode"`
2022-02-11 16:25:28 +08:00
Platform int32 `json:"platform" binding:"required,min=1,max=7"`
2022-02-11 17:44:02 +08:00
Ex string `json:"ex"`
2022-06-09 19:34:52 +08:00
FaceURL string `json:"faceURL"`
2022-02-11 16:25:28 +08:00
OperationID string `json:"operationID" binding:"required"`
2022-06-24 17:45:33 +08:00
AreaCode string `json:"areaCode"`
2022-08-11 16:06:35 +08:00
InvitationCode string `json:"invitationCode"`
2021-12-01 12:16:02 +08:00
}
func SetPassword(c *gin.Context) {
params := ParamsSetPassword{}
if err := c.BindJSON(&params); err != nil {
2022-02-21 16:24:25 +08:00
log.NewError(params.OperationID, utils.GetSelfFuncName(), "bind json failed", err.Error())
2021-12-01 12:16:02 +08:00
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
2022-08-15 16:51:41 +08:00
var ip string
Limited, LimitError := imdb.IsLimitRegisterIp(ip)
if LimitError != nil {
log.Error(params.OperationID, utils.GetSelfFuncName(), LimitError, ip)
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": LimitError.Error()})
2022-08-15 18:34:03 +08:00
return
2022-08-15 16:51:41 +08:00
}
if Limited {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.RegisterLimit, "errMsg": "limited"})
2022-08-15 18:34:03 +08:00
return
2022-08-15 16:51:41 +08:00
}
2021-12-01 12:16:02 +08:00
var account string
if params.Email != "" {
account = params.Email
2022-07-05 14:30:53 +08:00
} else if params.PhoneNumber != "" {
2021-12-01 12:16:02 +08:00
account = params.PhoneNumber
2022-07-05 14:30:53 +08:00
} else {
account = params.UserID
2021-12-01 12:16:02 +08:00
}
2022-06-09 19:34:52 +08:00
if params.Nickname == "" {
params.Nickname = account
2022-03-21 15:25:41 +08:00
}
2022-07-05 14:30:53 +08:00
if params.UserID == "" {
if (config.Config.Demo.UseSuperCode && params.VerificationCode != config.Config.Demo.SuperCode) || !config.Config.Demo.UseSuperCode {
accountKey := params.AreaCode + account + "_" + constant.VerificationCodeForRegisterSuffix
v, err := db.DB.GetAccountCode(accountKey)
if err != nil || v != params.VerificationCode {
log.NewError(params.OperationID, "password Verification code error", account, params.VerificationCode)
data := make(map[string]interface{})
data["PhoneNumber"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.CodeInvalidOrExpired, "errMsg": "Verification code error!", "data": data})
return
}
2022-02-11 16:25:28 +08:00
}
2022-08-13 10:41:10 +08:00
if config.Config.Demo.NeedInvitationCode {
2022-08-15 16:51:41 +08:00
err := imdb.CheckInvitationCode(params.InvitationCode)
2022-08-13 10:41:10 +08:00
if err != nil {
c.JSON(http.StatusOK, gin.H{"errCode": constant.InvitationError, "errMsg": "邀请码错误"})
}
}
2021-12-01 12:16:02 +08:00
}
2022-06-15 17:33:48 +08:00
//userID := utils.Base64Encode(account)
2022-07-05 14:30:53 +08:00
var userID string
if params.UserID == "" {
2022-07-06 16:12:17 +08:00
userID = utils.Md5(params.OperationID + strconv.FormatInt(time.Now().UnixNano(), 10))
2022-07-05 14:30:53 +08:00
bi := big.NewInt(0)
bi.SetString(userID[0:8], 16)
userID = bi.String()
} else {
userID = params.UserID
}
2022-06-15 17:33:48 +08:00
2022-05-26 19:53:13 +08:00
url := config.Config.Demo.ImAPIURL + "/auth/user_register"
2022-02-11 16:25:28 +08:00
openIMRegisterReq := api.UserRegisterReq{}
openIMRegisterReq.OperationID = params.OperationID
openIMRegisterReq.Platform = params.Platform
2022-06-09 17:19:39 +08:00
openIMRegisterReq.UserID = userID
2022-06-09 19:34:52 +08:00
openIMRegisterReq.Nickname = params.Nickname
2022-02-11 16:25:28 +08:00
openIMRegisterReq.Secret = config.Config.Secret
2022-06-09 19:34:52 +08:00
openIMRegisterReq.FaceURL = params.FaceURL
2022-08-07 22:37:27 +08:00
createIp := c.Request.Header.Get("X-Forward-For")
if createIp == "" {
createIp = c.ClientIP()
}
openIMRegisterReq.CreateIp = createIp
2022-08-13 10:41:10 +08:00
openIMRegisterReq.InvitationCode = params.InvitationCode
2022-02-11 16:25:28 +08:00
openIMRegisterResp := api.UserRegisterResp{}
2022-07-06 16:12:17 +08:00
log.NewDebug(params.OperationID, utils.GetSelfFuncName(), "register req:", openIMRegisterReq)
2022-03-03 16:23:59 +08:00
bMsg, err := http2.Post(url, openIMRegisterReq, 2)
2021-12-01 12:16:02 +08:00
if err != nil {
2022-02-11 16:25:28 +08:00
log.NewError(params.OperationID, "request openIM register error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": err.Error()})
2021-12-01 12:16:02 +08:00
return
}
2022-02-11 16:25:28 +08:00
err = json.Unmarshal(bMsg, &openIMRegisterResp)
if err != nil || openIMRegisterResp.ErrCode != 0 {
2022-02-21 18:50:11 +08:00
log.NewError(params.OperationID, "request openIM register error", account, "err", "resp: ", openIMRegisterResp.ErrCode)
if err != nil {
log.NewError(params.OperationID, utils.GetSelfFuncName(), err.Error())
}
2022-08-07 22:37:27 +08:00
if openIMRegisterResp.ErrCode == constant.RegisterLimit {
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterLimit, "errMsg": "用户注册被限制"})
2022-08-13 10:41:10 +08:00
} else if openIMRegisterResp.ErrCode == constant.InvitationError {
c.JSON(http.StatusOK, gin.H{"errCode": constant.InvitationError, "errMsg": "邀请码错误"})
2022-08-07 22:37:27 +08:00
} else {
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: " + openIMRegisterResp.ErrMsg})
}
2021-12-01 12:16:02 +08:00
return
}
2022-06-22 20:23:04 +08:00
log.Info(params.OperationID, "begin store mysql", account, params.Password, "info", params.FaceURL, params.Nickname)
2022-08-15 16:51:41 +08:00
err = imdb.SetPassword(account, params.Password, params.Ex, userID, params.AreaCode)
2021-12-01 12:16:02 +08:00
if err != nil {
2022-02-11 16:25:28 +08:00
log.NewError(params.OperationID, "set phone number password error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": err.Error()})
2021-12-01 12:16:02 +08:00
return
}
2022-08-15 16:51:41 +08:00
if config.Config.Demo.NeedInvitationCode {
//判断一下验证码的使用情况
LockSucc := imdb.TryLockInvitationCode(params.InvitationCode, userID)
if LockSucc {
imdb.FinishInvitationCode(params.InvitationCode, userID)
}
}
2022-02-11 16:25:28 +08:00
log.Info(params.OperationID, "end setPassword", account, params.Password)
2022-05-12 19:00:48 +08:00
// demo onboarding
2022-08-05 15:54:00 +08:00
if params.UserID == "" && config.Config.Demo.OnboardProcess {
2022-07-14 12:08:28 +08:00
select {
case Ch <- OnboardingProcessReq{
OperationID: params.OperationID,
2022-07-22 19:13:23 +08:00
UserID: userID,
2022-07-14 12:08:28 +08:00
NickName: params.Nickname,
FaceURL: params.FaceURL,
PhoneNumber: params.AreaCode + params.PhoneNumber,
Email: params.Email,
}:
case <-time.After(time.Second * 2):
log.NewWarn(params.OperationID, utils.GetSelfFuncName(), "to ch timeOut")
}
2022-07-05 14:30:53 +08:00
}
2022-08-12 18:37:51 +08:00
select {
case ChImportFriend <- &pbFriend.ImportFriendReq{
OperationID: params.OperationID,
FromUserID: userID,
2022-08-12 19:51:23 +08:00
OpUserID: config.Config.Manager.AppManagerUid[0],
2022-08-12 18:37:51 +08:00
}:
case <-time.After(time.Second * 2):
log.NewWarn(params.OperationID, utils.GetSelfFuncName(), "to ChImportFriend timeOut")
}
2022-02-11 16:25:28 +08:00
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMRegisterResp.UserToken})
2021-12-01 12:16:02 +08:00
return
}