-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
274 lines (244 loc) · 6.15 KB
/
line.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package line
import (
"log"
"net/http"
"strings"
"time"
"github.com/Casxt/TimeLine/database"
"github.com/Casxt/TimeLine/static"
"github.com/Casxt/TimeLine/tools"
)
//Route decide static
func Route(res http.ResponseWriter, req *http.Request) {
var result []byte
var status int
subPath := req.URL.Path[len("/line"):]
switch {
//match /linecreate*
case strings.HasPrefix(strings.ToLower(subPath), "create"):
status, result, _ = static.GetPage("components", "line", "createLine.html")
//match /lineedit*
case strings.HasPrefix(strings.ToLower(subPath), "edit"):
status, result, _ = static.GetPage("components", "line", "editLine.html")
default:
status, result, _ = static.GetPage("components", "line", "line.html")
}
res.WriteHeader(status)
res.Write(result)
}
//GetLines Get Lines of User
func GetLines(res http.ResponseWriter, req *http.Request) (status int, jsonRes interface{}) {
type ReqData struct {
Operator string
SessionID string
}
type ResData struct {
State string
Msg string
Lines []string //全部Line名称
}
var reqData ReqData
if status, jsonRes = tools.GetPostJSON(req, &reqData); status != 200 {
return status, jsonRes
}
UserID, _ := tools.GetLoginStateOfOperator(req, reqData.SessionID, reqData.Operator)
if UserID == "" {
return 400, map[string]string{
"State": "Failde",
"Msg": "User Not Sign In",
}
}
Lines, err := database.GetLines(UserID)
if err != nil {
log.Println(err.Error())
return 500, map[string]string{
"State": "Failde",
"Msg": "database.GetUserLines Failed",
"Detial": err.Error(),
}
}
return 200, ResData{
State: "Success",
Msg: "Lines Get Successful",
Lines: Lines,
}
}
//GetLineInfo return LineInfo include some sum info
func GetLineInfo(res http.ResponseWriter, req *http.Request) (status int, jsonRes interface{}) {
type ReqData struct {
Operator string
SessionID string
LineName string
}
type ResData struct {
State string
Msg string
Name string
LatestImg string
Users []string
SliceNum int
ImageNum int
CreateTime time.Time
LatestTime time.Time
}
var (
reqData ReqData
resData ResData
err error
)
if status, jsonRes = tools.GetPostJSON(req, &reqData); status != 200 {
return status, jsonRes
}
UserID, _ := tools.GetLoginStateOfOperator(req, reqData.SessionID, reqData.Operator)
if UserID == "" {
return 400, map[string]string{
"State": "Failde",
"Msg": "User Not Sign In",
}
}
_, resData.Name, resData.LatestImg, resData.Users, resData.SliceNum, resData.ImageNum,
resData.CreateTime, resData.LatestTime, err = database.GetLineDetail(reqData.LineName, nil)
if err != nil {
return 500, map[string]string{
"State": "Failde",
"Msg": "Get Line Info Failed",
"Detail": err.Error(),
}
}
resData.State = "Success"
resData.Msg = "Get Line Info Success"
return 200, resData
}
//CreateLine will create a new line with specific name
func CreateLine(res http.ResponseWriter, req *http.Request) (status int, jsonRes map[string]string) {
type Data struct {
Operator string //当前登陆用户的手机
LineName string //Line的名字
SessionID string //当前SessionID
}
var data Data
if status, jsonRes = tools.GetPostJSON(req, &data); status != 200 {
return status, jsonRes
}
UserID, session := tools.GetLoginStateOfOperator(req, data.SessionID, data.Operator)
if session == nil {
return 400, map[string]string{
"State": "Failde",
"Msg": `User Not Sign In`,
}
}
if len(data.LineName) < 4 {
return 400, map[string]string{
"State": "Failde",
"Msg": "Line Name too short",
}
}
Lines, err := database.GetLines(UserID)
if err != nil {
log.Println(err.Error())
return 400, map[string]string{
"State": "Failde",
"Msg": "Unknow Error Happend",
"Detail": err.Error(),
}
}
//Limit Line num of User
if len(Lines) > 3 {
return 400, map[string]string{
"State": "Failde",
"Msg": "User Have too Much Lines",
}
}
err = database.CreateLine(strings.ToLower(data.LineName), UserID)
if err != nil {
switch err.Error() {
case "Line Already Exist":
return 400, map[string]string{
"State": "Failde",
"Msg": "Line Name Already be Used",
}
default:
return 400, map[string]string{
"State": "Failde",
"Msg": "Line Name Already be Used",
}
}
}
return 200, map[string]string{
"State": "Success",
"Msg": "Line create success",
}
}
//AddUser will will add user to specific line
func AddUser(res http.ResponseWriter, req *http.Request) (status int, jsonRes interface{}) {
type ReqData struct {
Operator string //当前登录用户
SessionID string //当前登录用户的SessionID
LineName string //Line名
NickName string //被添加的用户昵称
UserPhone string //被添加用户手机
}
type ResData struct {
State string
Msg string
Detail string
}
var reqData ReqData
if status, jsonRes = tools.GetPostJSON(req, &reqData); status != 200 {
return status, jsonRes
}
UserID, _ := tools.GetLoginStateOfOperator(req, reqData.SessionID, reqData.Operator)
if UserID == "" {
return 400, ResData{
State: "Failde",
Msg: "User Not Sign In",
}
}
//Check if user belong to line
var contain bool
lines, DBErr := database.GetLines(UserID)
if DBErr != nil {
return 500, ResData{
State: "Failde",
Msg: "GetLines failed",
}
}
for _, line := range lines {
if line == reqData.LineName {
contain = true
break
}
}
if !contain {
return 400, ResData{
State: "Failde",
Msg: "User Dose Not Belong to this Line",
}
}
//Check User Phone and NickName match
UserID, _, nickName, _, _, _, _, _, DBErr := database.GetUserByPhone(reqData.UserPhone, nil)
if DBErr != nil {
return 500, ResData{
State: "Failde",
Msg: "Get GetUserByPhone Failed",
Detail: DBErr.Error(),
}
}
if reqData.NickName != nickName {
return 400, ResData{
State: "Failde",
Msg: "Invilde Parameter",
}
}
if DBErr := database.AddUser(reqData.LineName, UserID, nil); DBErr != nil {
return 500, ResData{
State: "Failde",
Msg: "AddUser Failed",
Detail: DBErr.Error(),
}
}
return 200, ResData{
State: "Success",
Msg: "User Add Success",
}
}