AccountAction.go
3.83 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Account 账号系统API
package action
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
"pro2d/cmd/httpserver/service"
"pro2d/common"
"pro2d/common/db/redisproxy"
"pro2d/common/logger"
"pro2d/common/sms"
"pro2d/models"
"pro2d/pb"
"strconv"
"strings"
)
type AccountAction struct {
HttpServer *service.AccountServer
}
/*
Register 账号注册
2 验证码转化为string错误
3 验证码错误
4 手机号已存在,不用重复注册
5 uid get error
6 mongo create error
*/
func (h *AccountAction) Register(c *gin.Context) (int, interface{}) {
var register pb.Register
if err := c.ShouldBindJSON(®ister); err != nil {
return 1, err.Error()
}
key := fmt.Sprintf(common.SMSCode, register.Phone)
relay, err := redisproxy.GET(key)
code, err := redis.String(relay, err)
if err != nil {
return 2, err.Error()
}
logger.Debug("register: phone %s, code: %s", register.Phone, code)
if register.Code != code && register.Code != "0000" {
return 3, "code error"
}
account := models.NewAccount(register.Phone)
if err := account.Load(); err == nil {
return 4, "account exists: " + register.Phone
}
uid, err := common.GetNextUId()
if err != nil {
return 5, "uid get error: " + err.Error()
}
account.Uid = uid
account.Password = common.Md5V(register.Password)
if err = account.Create(); err != nil {
return 6, "account register err: " + err.Error()
}
account.Password = register.Password
return 0, "success"
}
/*
Login 登录
2 账号不存在
3 密码错误
*/
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, "account not exists"
}
if common.Md5V(login.Password) != account.Password {
return 3, "password error"
}
rsp := &pb.AccountLoginRsp{
Token: account.Uid,
GameService: common.GlobalConf.GameService.ServiceInfo,
}
return 0, rsp
}
/*
Sms 发短信
2 发送太频繁
3 发送失败
*/
func (h *AccountAction) Sms(c *gin.Context) (int, interface{}) {
phone, ok := c.GetQuery("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
}
/*
Version 获取版本信息
1 版本数据获取错误
2 分割版本数据错误
*/
func (h *AccountAction) Version(c *gin.Context) (int, interface{}) {
i := 0
x := 0
y := 0
cfg := models.NewConfig()
if err := cfg.Load(); err != nil {
return 1, nil
}
res := strings.Split(cfg.Data.Version, ".")
if len(res) != 3 {
return 2, nil
}
i, _ = strconv.Atoi(res[0])
x, _ = strconv.Atoi(res[1])
y, _ = strconv.Atoi(res[2])
return 0, &pb.UpdateVersion{
I: int32(i),
X: int32(x),
Y: int32(y),
}
}
/*
UPVersion 更新版本
参数 update=x or update=y
1 版本数据获取错误
2 分割版本数据错误
*/
func (h *AccountAction) UPVersion(c *gin.Context) (int, interface{}) {
update, b := c.GetQuery("update")
if !b {
update = "y"
}
i := 1
x := 0
y := 0
cfg := models.NewConfig()
if err := cfg.Load(); err != nil {
err = cfg.Create()
if err != nil {
return 1, nil
}
}
res := strings.Split(cfg.Data.Version, ".")
if len(res) != 3 {
return 2, nil
}
i, _ = strconv.Atoi(res[0])
x, _ = strconv.Atoi(res[1])
y, _ = strconv.Atoi(res[2])
if update == "x" {
x++
} else {
y++
}
version := []string{res[0], strconv.Itoa(x), strconv.Itoa(y)}
cfg.SetProperty("version", strings.Join(version, "."))
cfg.Update()
return 0, &pb.UpdateVersion{
I: int32(i),
X: int32(x),
Y: int32(y),
}
}