Merge remote-tracking branch 'origin/tuoyun' into tuoyun

pull/141/head
wenxu12345 3 years ago
commit e605560e5c

@ -433,11 +433,14 @@ demo:
signName: OpenIM Corporation signName: OpenIM Corporation
verificationCodeTemplateCode: SMS_2268101641 verificationCodeTemplateCode: SMS_2268101641
superCode: 666666 superCode: 666666
# second
codeTTL: 60
mail: mail:
title: "openIM" title: "openIM"
senderMail: "1765567899@qq.com" senderMail: "1765567899@qq.com"
senderAuthorizationCode: "1gxyausfoevlzbfag" senderAuthorizationCode: "1gxyausfoevlzbfag"
smtpAddr: "smtp.qq.com" smtpAddr: "smtp.qq.com"
smtpPort: 25 smtpPort: 25

@ -1,16 +1,16 @@
package register package register
import ( import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config" "Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant" "Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model" "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log" "Open_IM/pkg/common/log"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil"
"net/http" "net/http"
) )
@ -19,16 +19,15 @@ type ParamsLogin struct {
PhoneNumber string `json:"phoneNumber"` PhoneNumber string `json:"phoneNumber"`
Password string `json:"password"` Password string `json:"password"`
Platform int32 `json:"platform"` Platform int32 `json:"platform"`
OperationID string `json:"operationID" binding:"required"`
} }
func Login(c *gin.Context) { func Login(c *gin.Context) {
log.NewDebug("Login api is statrting...")
params := ParamsLogin{} params := ParamsLogin{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return return
} }
var account string var account string
if params.Email != "" { if params.Email != "" {
account = params.Email account = params.Email
@ -36,77 +35,36 @@ func Login(c *gin.Context) {
account = params.PhoneNumber account = params.PhoneNumber
} }
log.InfoByKv("api Login get params", account) r, err := im_mysql_model.GetRegister(account)
queryParams := im_mysql_model.Register{
Account: account,
Password: params.Password,
}
canLogin := im_mysql_model.Login(&queryParams)
if canLogin == 1 {
log.ErrorByKv("Incorrect phone number password", account, "err", "Mobile phone number is not registered")
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Mobile phone number is not registered"})
return
}
if canLogin == 2 {
log.ErrorByKv("Incorrect phone number password", account, "err", "Incorrect password")
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Incorrect password"})
return
}
resp, err := OpenIMToken(account, params.Platform)
if err != nil { if err != nil {
log.ErrorByKv("get token by phone number err", account, "err", err.Error()) log.NewError(params.OperationID, "user have not register", params.Password, account)
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": err.Error()}) c.JSON(http.StatusOK, gin.H{"errCode": constant.NotRegistered, "errMsg": "Mobile phone number is not registered"})
return return
} }
response, err := ioutil.ReadAll(resp.Body) if r.Password != params.Password {
defer resp.Body.Close() log.NewError(params.OperationID, "password err", params.Password, account, r.Password, r.Account)
if err != nil { c.JSON(http.StatusOK, gin.H{"errCode": constant.PasswordErr, "errMsg": "Mobile phone number is not registered"})
log.ErrorByKv("Failed to read file", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IoError, "errMsg": err.Error()})
return return
} }
imRep := IMRegisterResp{} url := fmt.Sprintf("http://%s:10000/auth/user_token", utils.ServerIP)
err = json.Unmarshal(response, &imRep) 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 { if err != nil {
log.ErrorByKv("json parsing failed", account, "err", err.Error()) log.NewError(params.OperationID, "request openIM get user token error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": err.Error()})
return return
} }
err = json.Unmarshal(bMsg, &openIMGetUserTokenResp)
if imRep.ErrCode != 0 { if err != nil || openIMGetUserTokenResp.ErrCode != 0 {
log.ErrorByKv("openIM Login request failed", account, "err") log.NewError(params.OperationID, "request get user token", account, "err", "")
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": imRep.ErrMsg}) c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": ""})
return return
} }
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMGetUserTokenResp.UserToken})
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": imRep.Data})
return
}
func OpenIMToken(Account string, platform int32) (*http.Response, error) {
url := fmt.Sprintf("http://%s:10000/auth/user_token", utils.ServerIP)
client := &http.Client{}
params := make(map[string]interface{})
params["secret"] = config.Config.Secret
params["platform"] = platform
params["uid"] = Account
con, err := json.Marshal(params)
if err != nil {
log.ErrorByKv("json parsing failed", Account, "err", err.Error())
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(con)))
if err != nil {
log.ErrorByKv("request error", "/auth/user_token", "err", err.Error())
return nil, err
}
resp, err := client.Do(req)
return resp, err
} }

@ -10,7 +10,6 @@ import (
openapi "github.com/alibabacloud-go/darabonba-openapi/client" openapi "github.com/alibabacloud-go/darabonba-openapi/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client" dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
"github.com/alibabacloud-go/tea/tea" "github.com/alibabacloud-go/tea/tea"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gopkg.in/gomail.v2" "gopkg.in/gomail.v2"
"math/rand" "math/rand"
@ -21,39 +20,44 @@ import (
type paramsVerificationCode struct { type paramsVerificationCode struct {
Email string `json:"email"` Email string `json:"email"`
PhoneNumber string `json:"phoneNumber"` PhoneNumber string `json:"phoneNumber"`
OperationID string `json:"operationID" binding:"required"`
} }
func SendVerificationCode(c *gin.Context) { func SendVerificationCode(c *gin.Context) {
log.InfoByKv("sendCode api is statrting...", "")
params := paramsVerificationCode{} params := paramsVerificationCode{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); err != nil {
log.ErrorByKv("request params json parsing failed", params.PhoneNumber, params.Email, "err", err.Error()) log.NewError("", "BindJSON failed", "err:", err.Error(), "phoneNumber", params.PhoneNumber, "email", params.Email)
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return return
} }
var account string var account string
if params.Email != "" { if params.Email != "" {
account = params.Email account = params.Email
} else { } else {
account = params.PhoneNumber account = params.PhoneNumber
} }
_, err := im_mysql_model.GetRegister(account)
queryParams := im_mysql_model.GetRegisterParams{ if err == nil {
Account: account, log.NewError(params.OperationID, "The phone number has been registered", params)
c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""})
return
} }
_, err, rowsAffected := im_mysql_model.GetRegister(&queryParams) ok, err := db.DB.JudgeAccountEXISTS(account)
if ok || err != nil {
if err == nil && rowsAffected != 0 { log.NewError(params.OperationID, "The phone number has been registered", params)
log.ErrorByKv("The phone number has been registered", queryParams.Account, "err") c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""})
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "The phone number has been registered"})
return return
} }
log.InfoByKv("begin sendSms", account) log.InfoByKv("begin sendSms", account)
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
code := 100000 + rand.Intn(900000) code := 100000 + rand.Intn(900000)
log.NewInfo(params.OperationID, "begin store redis", account)
err = db.DB.SetAccountCode(account, code, config.Config.Demo.SuperCodeTTL)
if err != nil {
log.NewError(params.OperationID, "set redis error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return
}
log.NewDebug("", config.Config.Demo) log.NewDebug("", config.Config.Demo)
if params.Email != "" { if params.Email != "" {
m := gomail.NewMessage() m := gomail.NewMessage()
@ -63,14 +67,14 @@ func SendVerificationCode(c *gin.Context) {
m.SetBody(`text/html`, fmt.Sprintf("%d", code)) m.SetBody(`text/html`, fmt.Sprintf("%d", code))
if err := gomail.NewDialer(config.Config.Demo.Mail.SmtpAddr, config.Config.Demo.Mail.SmtpPort, config.Config.Demo.Mail.SenderMail, config.Config.Demo.Mail.SenderAuthorizationCode).DialAndSend(m); err != nil { if err := gomail.NewDialer(config.Config.Demo.Mail.SmtpAddr, config.Config.Demo.Mail.SmtpPort, config.Config.Demo.Mail.SenderMail, config.Config.Demo.Mail.SenderAuthorizationCode).DialAndSend(m); err != nil {
log.ErrorByKv("send mail error", account, "err", err.Error()) log.ErrorByKv("send mail error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": err.Error()}) c.JSON(http.StatusOK, gin.H{"errCode": constant.MailSendCodeErr, "errMsg": ""})
return return
} }
} else { } else {
client, err := CreateClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret)) client, err := CreateClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret))
if err != nil { if err != nil {
log.ErrorByKv("create sendSms client err", "", "err", err.Error()) log.NewError(params.OperationID, "create sendSms client err", "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"}) c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return return
} }
@ -83,56 +87,20 @@ func SendVerificationCode(c *gin.Context) {
response, err := client.SendSms(sendSmsRequest) response, err := client.SendSms(sendSmsRequest)
if err != nil { if err != nil {
log.ErrorByKv("sendSms error", account, "err", err.Error()) log.NewError(params.OperationID, "sendSms error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"}) c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return return
} }
if *response.Body.Code != "OK" { if *response.Body.Code != "OK" {
log.ErrorByKv("alibabacloud sendSms error", account, "err", response.Body.Code, response.Body.Message) log.NewError(params.OperationID, "alibabacloud sendSms error", account, "err", response.Body.Code, response.Body.Message)
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"}) c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return return
} }
} }
log.InfoByKv("begin store redis", account) data := make(map[string]interface{})
v, err := redis.Int(db.DB.Exec("TTL", account)) data["account"] = account
if err != nil { c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verification code has been set!", "data": data})
log.ErrorByKv("get account from redis error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return
}
switch {
case v == -2:
_, err = db.DB.Exec("SET", account, code, "EX", 600)
if err != nil {
log.ErrorByKv("set redis error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return
}
data := make(map[string]interface{})
data["account"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verification code sent successfully!", "data": data})
log.InfoByKv("send new verification code", account)
return
case v > 540:
data := make(map[string]interface{})
data["account"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Frequent operation!", "data": data})
log.InfoByKv("frequent operation", account)
return
case v < 540:
_, err = db.DB.Exec("SET", account, code, "EX", 600)
if err != nil {
c.JSON(http.StatusOK, gin.H{"errCode": constant.IntentionalError, "errMsg": "Enterthe superCode directly in the verification code box, SuperCode can be configured in config.xml"})
return
}
data := make(map[string]interface{})
data["account"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verification code has been reset!", "data": data})
log.InfoByKv("Reset verification code", account)
return
}
} }
func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) { func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) {

@ -1,18 +1,18 @@
package register package register
import ( import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config" "Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant" "Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db" "Open_IM/pkg/common/db"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model" "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log" "Open_IM/pkg/common/log"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil"
"net/http" "net/http"
) )
@ -21,135 +21,61 @@ type ParamsSetPassword struct {
PhoneNumber string `json:"phoneNumber"` PhoneNumber string `json:"phoneNumber"`
Password string `json:"password"` Password string `json:"password"`
VerificationCode string `json:"verificationCode"` VerificationCode string `json:"verificationCode"`
} Platform int32 `json:"platform" binding:"required,min=1,max=7"`
Ex string `json:"ex"`
type Data struct { OperationID string `json:"operationID" binding:"required"`
ExpiredTime int64 `json:"expiredTime"`
Token string `json:"token"`
Uid string `json:"uid"`
}
type IMRegisterResp struct {
Data Data `json:"data"`
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
} }
func SetPassword(c *gin.Context) { func SetPassword(c *gin.Context) {
log.InfoByKv("setPassword api is statrting...", "")
params := ParamsSetPassword{} params := ParamsSetPassword{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return return
} }
var account string var account string
if params.Email != "" { if params.Email != "" {
account = params.Email account = params.Email
} else { } else {
account = params.PhoneNumber account = params.PhoneNumber
} }
if params.VerificationCode != config.Config.Demo.SuperCode {
log.InfoByKv("begin store redis", account) v, err := redis.String(db.DB.Exec("GET", account))
v, err := redis.String(db.DB.Exec("GET", account)) if err != nil || v != params.VerificationCode {
log.NewError(params.OperationID, "password Verification code error", account, params.VerificationCode)
if params.VerificationCode == config.Config.Demo.SuperCode { data := make(map[string]interface{})
goto openIMRegisterTab data["PhoneNumber"] = account
} c.JSON(http.StatusOK, gin.H{"errCode": constant.CodeInvalidOrExpired, "errMsg": "Verification code error!", "data": data})
return
fmt.Println("Get Redis:", v, err) }
if err != nil {
log.ErrorByKv("password Verification code expired", account, "err", err.Error())
data := make(map[string]interface{})
data["phoneNumber"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Verification expired!", "data": data})
return
}
if v != params.VerificationCode {
log.InfoByKv("password Verification code error", account, params.VerificationCode)
data := make(map[string]interface{})
data["PhoneNumber"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Verification code error!", "data": data})
return
}
openIMRegisterTab:
log.InfoByKv("openIM register begin", account)
resp, err := OpenIMRegister(account)
log.InfoByKv("openIM register resp", account, resp, err)
if err != nil {
log.ErrorByKv("request openIM register error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": err.Error()})
return
}
response, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
c.JSON(http.StatusOK, gin.H{"errCode": constant.IoError, "errMsg": err.Error()})
return
} }
imrep := IMRegisterResp{} url := fmt.Sprintf("http://%s:10000/auth/user_register", utils.ServerIP)
err = json.Unmarshal(response, &imrep) openIMRegisterReq := api.UserRegisterReq{}
openIMRegisterReq.OperationID = params.OperationID
openIMRegisterReq.Platform = params.Platform
openIMRegisterReq.UserID = account
openIMRegisterReq.Nickname = account
openIMRegisterReq.Secret = config.Config.Secret
openIMRegisterResp := api.UserRegisterResp{}
bMsg, err := http2.Post(url, openIMRegisterReq, config.Config.MessageCallBack.CallBackTimeOut)
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) log.NewError(params.OperationID, "request openIM register error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": err.Error()})
return return
} }
if imrep.ErrCode != 0 { err = json.Unmarshal(bMsg, &openIMRegisterResp)
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": imrep.ErrMsg}) if err != nil || openIMRegisterResp.ErrCode != 0 {
log.NewError(params.OperationID, "request openIM register error", account, "err", "")
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": ""})
return return
} }
log.Info(params.OperationID, "begin store mysql", account, params.Password)
queryParams := im_mysql_model.SetPasswordParams{ err = im_mysql_model.SetPassword(account, params.Password, params.Ex)
Account: account,
Password: params.Password,
}
log.InfoByKv("begin store mysql", account, params.Password)
_, err = im_mysql_model.SetPassword(&queryParams)
if err != nil { if err != nil {
log.ErrorByKv("set phone number password error", account, "err", err.Error()) log.NewError(params.OperationID, "set phone number password error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.DatabaseError, "errMsg": err.Error()}) c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": err.Error()})
return return
} }
log.Info(params.OperationID, "end setPassword", account, params.Password)
log.InfoByKv("end setPassword", account) c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMRegisterResp.UserToken})
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": imrep.Data})
return return
} }
func OpenIMRegister(account string) (*http.Response, error) {
url := fmt.Sprintf("http://%s:10000/auth/user_register", utils.ServerIP)
fmt.Println("1:", config.Config.Secret)
client := &http.Client{}
params := make(map[string]interface{})
params["secret"] = config.Config.Secret
params["platform"] = 2
params["uid"] = account
params["name"] = account
params["icon"] = ""
params["gender"] = 0
params["mobile"] = ""
params["email"] = ""
params["birth"] = ""
params["ex"] = ""
con, err := json.Marshal(params)
if err != nil {
return nil, err
}
log.InfoByKv("openIM register params", account, params)
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(con)))
if err != nil {
return nil, err
}
resp, err := client.Do(req)
return resp, err
}

@ -6,7 +6,6 @@ import (
"Open_IM/pkg/common/db" "Open_IM/pkg/common/db"
"Open_IM/pkg/common/log" "Open_IM/pkg/common/log"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
) )
@ -15,14 +14,13 @@ type paramsCertification struct {
Email string `json:"email"` Email string `json:"email"`
PhoneNumber string `json:"phoneNumber"` PhoneNumber string `json:"phoneNumber"`
VerificationCode string `json:"verificationCode"` VerificationCode string `json:"verificationCode"`
OperationID string `json:"operationID" binding:"required"`
} }
func Verify(c *gin.Context) { func Verify(c *gin.Context) {
log.InfoByKv("Verify api is statrting...", "")
params := paramsCertification{} params := paramsCertification{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); err != nil {
log.ErrorByKv("request params json parsing failed", "", "err", err.Error()) log.NewError("", "request params json parsing failed", "", "err", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return return
} }
@ -44,27 +42,28 @@ func Verify(c *gin.Context) {
return return
} }
log.NewInfo("0", " params.VerificationCode != config.Config.Demo.SuperCode", params.VerificationCode, config.Config.Demo) log.NewInfo("0", " params.VerificationCode != config.Config.Demo.SuperCode", params.VerificationCode, config.Config.Demo)
log.InfoByKv("begin get form redis", account) log.NewInfo(params.OperationID, "begin get form redis", account)
v, err := redis.String(db.DB.Exec("GET", account))
log.InfoByKv("redis phone number and verificating Code", account, v) code, err := db.DB.GetAccountCode(account)
log.NewInfo(params.OperationID, "redis phone number and verificating Code", account, code)
if err != nil { if err != nil {
log.ErrorByKv("Verification code expired", account, "err", err.Error()) log.NewError(params.OperationID, "Verification code expired", account, "err", err.Error())
data := make(map[string]interface{}) data := make(map[string]interface{})
data["account"] = account data["account"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Verification code expired!", "data": data}) c.JSON(http.StatusOK, gin.H{"errCode": constant.CodeInvalidOrExpired, "errMsg": "Verification code expired!", "data": data})
return return
} }
if params.VerificationCode == v { if params.VerificationCode == code {
log.InfoByKv("Verified successfully", account) log.Info(params.OperationID, "Verified successfully", account)
data := make(map[string]interface{}) data := make(map[string]interface{})
data["account"] = account data["account"] = account
data["verificationCode"] = params.VerificationCode data["verificationCode"] = params.VerificationCode
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verified successfully!", "data": data}) c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verified successfully!", "data": data})
return return
} else { } else {
log.InfoByKv("Verification code error", account, params.VerificationCode) log.Info(params.OperationID, "Verification code error", account, params.VerificationCode)
data := make(map[string]interface{}) data := make(map[string]interface{})
data["account"] = account data["account"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "Verification code error!", "data": data}) c.JSON(http.StatusOK, gin.H{"errCode": constant.CodeInvalidOrExpired, "errMsg": "Verification code error!", "data": data})
} }
} }

@ -285,6 +285,7 @@ type config struct {
VerificationCodeTemplateCode string `yaml:"verificationCodeTemplateCode"` VerificationCodeTemplateCode string `yaml:"verificationCodeTemplateCode"`
} }
SuperCode string `yaml:"superCode"` SuperCode string `yaml:"superCode"`
CodeTTL int `yaml:"codeTTL"`
Mail struct { Mail struct {
Title string `yaml:"title"` Title string `yaml:"title"`
SenderMail string `yaml:"senderMail"` SenderMail string `yaml:"senderMail"`

@ -68,14 +68,22 @@ var (
) )
const ( const (
NoError = 0 NoError = 0
FormattingError = 10001 FormattingError = 10001
DatabaseError = 10002 HasRegistered = 10002
LogicalError = 10003 NotRegistered = 10003
ServerError = 10004 PasswordErr = 10004
HttpError = 10005 GetIMTokenErr = 10005
IoError = 10006 RepeatSendCode = 10006
IntentionalError = 10007 MailSendCodeErr = 10007
SmsSendCodeErr = 10008
CodeInvalidOrExpired = 10009
RegisterFailed = 10010
DatabaseError = 10002
ServerError = 10004
HttpError = 10005
IoError = 10006
IntentionalError = 10007
) )
func (e *ErrInfo) Error() string { func (e *ErrInfo) Error() string {

@ -2,6 +2,12 @@ package db
import "time" import "time"
type Register struct {
Account string `gorm:"column:account;primary_key;type:char(255)" json:"account"`
Password string `gorm:"column:password;type:varchar(255)" json:"password"`
Ex string `gorm:"column:ex;size:1024" json:"ex"`
}
// //
//message FriendInfo{ //message FriendInfo{
//string OwnerUserID = 1; //string OwnerUserID = 1;
@ -183,7 +189,6 @@ type ChatLog struct {
ContentType int32 `gorm:"column:content_type" json:"contentType"` ContentType int32 `gorm:"column:content_type" json:"contentType"`
Content string `gorm:"column:content;type:varchar(1000)" json:"content"` Content string `gorm:"column:content;type:varchar(1000)" json:"content"`
Status int32 `gorm:"column:status" json:"status"` Status int32 `gorm:"column:status" json:"status"`
Seq uint32 `gorm:"column:seq;index:index_seq;default:0" json:"seq"`
SendTime time.Time `gorm:"column:send_time" json:"sendTime"` SendTime time.Time `gorm:"column:send_time" json:"sendTime"`
CreateTime time.Time `gorm:"column:create_time" json:"createTime"` CreateTime time.Time `gorm:"column:create_time" json:"createTime"`
Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"` Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`

@ -58,7 +58,7 @@ func initMysqlDB() {
&GroupMember{}, &GroupMember{},
&GroupRequest{}, &GroupRequest{},
&User{}, &User{},
&Black{}, &ChatLog{}) &Black{}, &ChatLog{}, &Register{})
db.Set("gorm:table_options", "CHARSET=utf8") db.Set("gorm:table_options", "CHARSET=utf8")
db.Set("gorm:table_options", "collation=utf8_unicode_ci") db.Set("gorm:table_options", "collation=utf8_unicode_ci")
@ -96,6 +96,14 @@ func initMysqlDB() {
log.NewInfo("CreateTable Black") log.NewInfo("CreateTable Black")
db.CreateTable(&Black{}) db.CreateTable(&Black{})
} }
if !db.HasTable(&ChatLog{}) {
log.NewInfo("CreateTable Black")
db.CreateTable(&ChatLog{})
}
if !db.HasTable(&Register{}) {
log.NewInfo("CreateTable Black")
db.CreateTable(&Register{})
}
return return

@ -5,60 +5,25 @@ import (
_ "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm"
) )
type GetRegisterParams struct { func GetRegister(account string) (*db.Register, error) {
Account string `json:"account"`
}
type Register struct {
Account string `gorm:"column:account"`
Password string `gorm:"column:password"`
}
func GetRegister(params *GetRegisterParams) (Register, error, int64) {
var r Register
dbConn, err := db.DB.MysqlDB.DefaultGormDB() dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil { if err != nil {
return r, err, 0 return nil, err
} }
result := dbConn. var r db.Register
Where(&Register{Account: params.Account}). return &r, dbConn.Table("registers").Where("account = ?",
Find(&r) account).Take(&r).Error
return r, result.Error, result.RowsAffected
} }
func SetPassword(account, password, ex string) error {
type SetPasswordParams struct { r := db.Register{
Account string `json:"account"` Account: account,
Password string `json:"password"` Password: password,
} Ex: ex,
func SetPassword(params *SetPasswordParams) (Register, error) {
r := Register{
Account: params.Account,
Password: params.Password,
} }
dbConn, err := db.DB.MysqlDB.DefaultGormDB() dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil { if err != nil {
return r, err return err
} }
return dbConn.Table("registers").Create(&r).Error
result := dbConn.Create(&r)
return r, result.Error
}
func Login(params *Register) int64 {
var r Register
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return 3
}
result := dbConn.
Where(&Register{Account: params.Account}).
Find(&r)
if result.Error != nil && result.RowsAffected == 0 {
return 1
}
if r.Password != params.Password {
return 2
}
return 0
} }

@ -7,6 +7,7 @@ import (
) )
const ( const (
registerAccountTempCode = "REGISTER_ACCOUNT_TEMP_CODE"
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
appleDeviceToken = "DEVICE_TOKEN" appleDeviceToken = "DEVICE_TOKEN"
userMinSeq = "REDIS_USER_MIN_SEQ:" userMinSeq = "REDIS_USER_MIN_SEQ:"
@ -33,6 +34,19 @@ func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (inte
return con.Do(cmd, params...) return con.Do(cmd, params...)
} }
func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) {
key := registerAccountTempCode + account
return redis.Bool(d.Exec("EXISTS", key))
}
func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) {
key := registerAccountTempCode + account
_, err = d.Exec("Set", key, code, ttl)
return err
}
func (d *DataBases) GetAccountCode(account string) (string, error) {
key := userIncrSeq + account
return redis.String(d.Exec("GET", key))
}
//Perform seq auto-increment operation of user messages //Perform seq auto-increment operation of user messages
func (d *DataBases) IncrUserSeq(uid string) (uint64, error) { func (d *DataBases) IncrUserSeq(uid string) (uint64, error) {

Loading…
Cancel
Save