提交 a885d7f0 编写于 作者: programor_guo's avatar programor_guo

demo modify

上级 609ec24c
......@@ -433,11 +433,17 @@ demo:
signName: OpenIM Corporation
verificationCodeTemplateCode: SMS_2268101641
superCode: 666666
# second
superCodeTTL: 60
mail:
title: "openIM"
senderMail: "1765567899@qq.com"
senderAuthorizationCode: "1gxyausfoevlzbfag"
smtpAddr: "smtp.qq.com"
smtpPort: 25
senderMail: "1765567899@qq.com"
senderAuthorizationCode: "1gxyausfoevlzbfag"
smtpAddr: "smtp.qq.com"
smtpPort: 25
errMsg:
hasRegistered: "用户已经注册"
mailSendCodeErr: "邮件发送失败"
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"
"Open_IM/pkg/utils"
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
......@@ -19,16 +19,15 @@ type ParamsLogin struct {
PhoneNumber string `json:"phoneNumber"`
Password string `json:"password"`
Platform int32 `json:"platform"`
OperationID string `json:"operationID" binding:"required"`
}
func Login(c *gin.Context) {
log.NewDebug("Login api is statrting...")
params := ParamsLogin{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
var account string
if params.Email != "" {
account = params.Email
......@@ -36,77 +35,36 @@ func Login(c *gin.Context) {
account = params.PhoneNumber
}
log.InfoByKv("api Login get params", 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)
r, err := im_mysql_model.GetRegister(account)
if err != nil {
log.ErrorByKv("get token by phone number err", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": err.Error()})
log.NewError(params.OperationID, "user have not register", params.Password, account)
c.JSON(http.StatusOK, gin.H{"errCode": constant.NotRegistered, "errMsg": "Mobile phone number is not registered"})
return
}
response, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.ErrorByKv("Failed to read file", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.IoError, "errMsg": err.Error()})
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": "Mobile phone number is not registered"})
return
}
imRep := IMRegisterResp{}
err = json.Unmarshal(response, &imRep)
url := fmt.Sprintf("http://%s:10000/auth/user_token", utils.ServerIP)
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.ErrorByKv("json parsing failed", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
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
}
if imRep.ErrCode != 0 {
log.ErrorByKv("openIM Login request failed", account, "err")
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": imRep.ErrMsg})
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})
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"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
"gopkg.in/gomail.v2"
"math/rand"
......@@ -21,39 +20,44 @@ import (
type paramsVerificationCode struct {
Email string `json:"email"`
PhoneNumber string `json:"phoneNumber"`
OperationID string `json:"operationID" binding:"required"`
}
func SendVerificationCode(c *gin.Context) {
log.InfoByKv("sendCode api is statrting...", "")
params := paramsVerificationCode{}
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()})
return
}
var account string
if params.Email != "" {
account = params.Email
} else {
account = params.PhoneNumber
}
queryParams := im_mysql_model.GetRegisterParams{
Account: account,
_, err := im_mysql_model.GetRegister(account)
if err == nil {
log.NewError(params.OperationID, "The phone number has been registered", params)
c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": getErrMsg(constant.HasRegistered)})
return
}
_, err, rowsAffected := im_mysql_model.GetRegister(&queryParams)
if err == nil && rowsAffected != 0 {
log.ErrorByKv("The phone number has been registered", queryParams.Account, "err")
c.JSON(http.StatusOK, gin.H{"errCode": constant.LogicalError, "errMsg": "The phone number has been registered"})
ok, err := db.DB.JudgeAccountEXISTS(account)
if ok || err != nil {
log.NewError(params.OperationID, "The phone number has been registered", params)
c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": getErrMsg(constant.RepeatSendCode)})
return
}
log.InfoByKv("begin sendSms", account)
rand.Seed(time.Now().UnixNano())
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)
if params.Email != "" {
m := gomail.NewMessage()
......@@ -63,14 +67,14 @@ func SendVerificationCode(c *gin.Context) {
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 {
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": getErrMsg(constant.MailSendCodeErr)})
return
}
} else {
client, err := CreateClient(tea.String(config.Config.Demo.AliSMSVerify.AccessKeyID), tea.String(config.Config.Demo.AliSMSVerify.AccessKeySecret))
if err != nil {
log.ErrorByKv("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"})
log.NewError(params.OperationID, "create sendSms client err", "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
}
......@@ -83,56 +87,20 @@ func SendVerificationCode(c *gin.Context) {
response, err := client.SendSms(sendSmsRequest)
if err != nil {
log.ErrorByKv("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"})
log.NewError(params.OperationID, "sendSms 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
}
if *response.Body.Code != "OK" {
log.ErrorByKv("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"})
log.NewError(params.OperationID, "alibabacloud sendSms error", account, "err", response.Body.Code, response.Body.Message)
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.InfoByKv("begin store redis", account)
v, err := redis.Int(db.DB.Exec("TTL", account))
if err != nil {
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
}
data := make(map[string]interface{})
data["account"] = account
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verification code has been set!", "data": data})
}
func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) {
......@@ -149,3 +117,13 @@ func CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsap
result, err = dysmsapi20170525.NewClient(c)
return result, err
}
func getErrMsg(errCode int) string {
switch errCode {
case constant.HasRegistered:
return config.Config.Demo.ErrMsg.HasRegistered
case constant.MailSendCodeErr:
return config.Config.Demo.ErrMsg.MailSendCodeErr
default:
return ""
}
}
package register
import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
......@@ -21,135 +21,60 @@ type ParamsSetPassword struct {
PhoneNumber string `json:"phoneNumber"`
Password string `json:"password"`
VerificationCode string `json:"verificationCode"`
}
type Data struct {
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"`
Platform int32 `json:"platform" binding:"required,min=1,max=7"`
OperationID string `json:"operationID" binding:"required"`
}
func SetPassword(c *gin.Context) {
log.InfoByKv("setPassword api is statrting...", "")
params := ParamsSetPassword{}
if err := c.BindJSON(&params); 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
}
log.InfoByKv("begin store redis", account)
v, err := redis.String(db.DB.Exec("GET", account))
if params.VerificationCode == config.Config.Demo.SuperCode {
goto openIMRegisterTab
}
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
if params.VerificationCode != config.Config.Demo.SuperCode {
v, err := redis.String(db.DB.Exec("GET", account))
if err != nil || 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.CodeInvalidOrExpired, "errMsg": "Verification code error!", "data": data})
return
}
}
imrep := IMRegisterResp{}
err = json.Unmarshal(response, &imrep)
url := fmt.Sprintf("http://%s:10000/auth/user_register", utils.ServerIP)
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 {
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
}
if imrep.ErrCode != 0 {
c.JSON(http.StatusOK, gin.H{"errCode": constant.HttpError, "errMsg": imrep.ErrMsg})
err = json.Unmarshal(bMsg, &openIMRegisterResp)
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
}
queryParams := im_mysql_model.SetPasswordParams{
Account: account,
Password: params.Password,
}
log.InfoByKv("begin store mysql", account, params.Password)
_, err = im_mysql_model.SetPassword(&queryParams)
log.Info(params.OperationID, "begin store mysql", account, params.Password)
err = im_mysql_model.SetPassword(account, params.Password)
if err != nil {
log.ErrorByKv("set phone number password error", account, "err", err.Error())
c.JSON(http.StatusOK, gin.H{"errCode": constant.DatabaseError, "errMsg": err.Error()})
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()})
return
}
log.InfoByKv("end setPassword", account)
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": imrep.Data})
log.Info(params.OperationID, "end setPassword", account, params.Password)
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMRegisterResp.UserToken})
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/log"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
"net/http"
)
......@@ -15,14 +14,13 @@ type paramsCertification struct {
Email string `json:"email"`
PhoneNumber string `json:"phoneNumber"`
VerificationCode string `json:"verificationCode"`
OperationID string `json:"operationID" binding:"required"`
}
func Verify(c *gin.Context) {
log.InfoByKv("Verify api is statrting...", "")
params := paramsCertification{}
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()})
return
}
......@@ -44,27 +42,28 @@ func Verify(c *gin.Context) {
return
}
log.NewInfo("0", " params.VerificationCode != config.Config.Demo.SuperCode", params.VerificationCode, config.Config.Demo)
log.InfoByKv("begin get form redis", account)
v, err := redis.String(db.DB.Exec("GET", account))
log.InfoByKv("redis phone number and verificating Code", account, v)
log.NewInfo(params.OperationID, "begin get form redis", account)
code, err := db.DB.GetAccountCode(account)
log.NewInfo(params.OperationID, "redis phone number and verificating Code", account, code)
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["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
}
if params.VerificationCode == v {
log.InfoByKv("Verified successfully", account)
if params.VerificationCode == code {
log.Info(params.OperationID, "Verified successfully", account)
data := make(map[string]interface{})
data["account"] = account
data["verificationCode"] = params.VerificationCode
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "Verified successfully!", "data": data})
return
} 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["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})
}
}
......@@ -284,14 +284,19 @@ type config struct {
SignName string `yaml:"signName"`
VerificationCodeTemplateCode string `yaml:"verificationCodeTemplateCode"`
}
SuperCode string `yaml:"superCode"`
Mail struct {
SuperCode string `yaml:"superCode"`
SuperCodeTTL int `yaml:"superCodeTTL"`
Mail struct {
Title string `yaml:"title"`
SenderMail string `yaml:"senderMail"`
SenderAuthorizationCode string `yaml:"senderAuthorizationCode"`
SmtpAddr string `yaml:"smtpAddr"`
SmtpPort int `yaml:"smtpPort"`
}
ErrMsg struct {
HasRegistered string `yaml:"hasRegistered"`
MailSendCodeErr string `yaml:"mailSendCodeErr"`
}
}
}
type PConversation struct {
......
......@@ -68,14 +68,22 @@ var (
)
const (
NoError = 0
FormattingError = 10001
DatabaseError = 10002
LogicalError = 10003
ServerError = 10004
HttpError = 10005
IoError = 10006
IntentionalError = 10007
NoError = 0
FormattingError = 10001
HasRegistered = 10002
NotRegistered = 10003
PasswordErr = 10004
GetIMTokenErr = 10005
RepeatSendCode = 10006
MailSendCodeErr = 10007
SmsSendCodeErr = 10008
CodeInvalidOrExpired = 10009
RegisterFailed = 10010
DatabaseError = 10002
ServerError = 10004
HttpError = 10005
IoError = 10006
IntentionalError = 10007
)
func (e *ErrInfo) Error() string {
......
......@@ -2,6 +2,12 @@ package db
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{
//string OwnerUserID = 1;
......@@ -183,7 +189,6 @@ type ChatLog struct {
ContentType int32 `gorm:"column:content_type" json:"contentType"`
Content string `gorm:"column:content;type:varchar(1000)" json:"content"`
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"`
CreateTime time.Time `gorm:"column:create_time" json:"createTime"`
Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`
......
......@@ -58,7 +58,7 @@ func initMysqlDB() {
&GroupMember{},
&GroupRequest{},
&User{},
&Black{}, &ChatLog{})
&Black{}, &ChatLog{}, &Register{})
db.Set("gorm:table_options", "CHARSET=utf8")
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
......@@ -96,6 +96,14 @@ func initMysqlDB() {
log.NewInfo("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
......
......@@ -5,60 +5,24 @@ import (
_ "github.com/jinzhu/gorm"
)
type GetRegisterParams struct {
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
func GetRegister(account string) (*db.Register, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return r, err, 0
return nil, err
}
result := dbConn.
Where(&Register{Account: params.Account}).
Find(&r)
return r, result.Error, result.RowsAffected
var r db.Register
return &r, dbConn.Table("registers").Where("account = ?",
account).Take(&r).Error
}
type SetPasswordParams struct {
Account string `json:"account"`
Password string `json:"password"`
}
func SetPassword(params *SetPasswordParams) (Register, error) {
r := Register{
Account: params.Account,
Password: params.Password,
func SetPassword(account, password string) error {
r := db.Register{
Account: account,
Password: password,
}
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
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 (
registerAccountTempCode = "REGISTER_ACCOUNT_TEMP_CODE"
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
appleDeviceToken = "DEVICE_TOKEN"
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...)
}
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
func (d *DataBases) IncrUserSeq(uid string) (uint64, error) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册