AccountAction.go
1.44 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
package main
import (
"github.com/gin-gonic/gin"
"pro2d/common"
"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
}