AccountAction.go
1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"pro2d/common"
"pro2d/common/db/redisproxy"
"pro2d/common/sms"
"pro2d/models"
"pro2d/pb"
)
type AccountAction struct {
HttpServer *AccountServer
}
func (h *AccountAction) Register(c *gin.Context) (int, interface{}){
var register pb.Register
if err := c.ShouldBindJSON(®ister); err != nil {
return 1, err.Error()
}
if register.Code != "0000" {
return 2, "code error"
}
account := models.NewAccount(register.Phone)
if err := account.Load(); err == nil {
return 3 , "account exists: " + register.Phone
}
account.Uid = common.SnowFlack.NextValStr()
account.Password = common.Md5V(register.Password)
if err := account.Create(); err != nil{
return 4, "account register err: " + err.Error()
}
account.Password = register.Password
return 0, "success"
}
func (h *AccountAction) Login(c *gin.Context) (int,interface{}) {
var login pb.Account
if err := c.ShouldBindJSON(&login); err != nil {
return 1, err.Error()
}
account := models.NewAccount(login.Phone)
if err := account.Load(); err != nil {
return 2, err.Error()
}
if common.Md5V(login.Password) != account.Password {
return 3, "password error"
}
var gs []*pb.ServiceInfo
for k, v := range h.HttpServer.EtcdClient.GetByPrefix(common.GlobalConf.GameConf.Name) {
gs = append(gs, &pb.ServiceInfo{
Id: k,
Name: common.GlobalConf.GameConf.Name,
Address: v,
})
}
rsp := &pb.LoginRsp{
Token: account.Uid,
GameService: gs,
}
return 0, rsp
}
func (h *AccountAction) Sms(c *gin.Context) (int,interface{}) {
c.Request.ParseForm()
phone, ok := c.GetPostForm("phone")
if !ok {
return 1, "phone not exists"
}
code := common.RandomCode()
key := fmt.Sprintf(common.SMSCode, phone)
relay, err := redisproxy.SETNX(key, code)
if relay.(int64) == 0 {
return 2, "send frequently"
}
redisproxy.ExpireKey(key, 60)
err = sms.SendSms(phone, code)
if err != nil {
redisproxy.DEL(key)
return 3, err.Error()
}
return 0, nil
}