-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
88 lines (76 loc) · 2.1 KB
/
common.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
package main
import (
"encoding/json"
"net/http"
"time"
)
const (
UNKNOWN_INNER_ERROR = 1
ITEM_DOES_NOT_EXIST = 2
CREDENTIAL_NOT_MATCH = 14
REQUEST_PARAM_FORMAT_ERROR = 20
EXPIRE = 10 * time.Minute
)
type GeneralResult struct {
ErrCode int `json:"errorCode"`
ErrorDescription string `json:"errorDescription,omitempty"`
ErrorFile string `json:"errorFile,omitempty"`
ErrorLine int `json:"errorLine,omitempty"`
ErrorParam string `json:"errorParam,omitempty"`
Item string `json:"item,omitempty"`
Credential string `json:"credential,omitempty"`
UserDefinedRootData string `json:"user-defined-root-data,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
type CaptchaData struct {
Width int `json:"width"`
Height int `json:"height"`
JpegBase64 string `json:"jpegBase64"`
PhraseLen int `json:"phraseLen"`
}
type CaptchaRes struct {
CaptchaId string `json:"captcha_id"`
ExpireTime int64 `json:"expire_time"`
CaptchaDATA CaptchaData `json:"captcha_data"`
}
func ConvertStringToByte(digits string) []byte {
if digits == "" {
return nil
}
ns := make([]byte, len(digits))
for i := range ns {
d := digits[i]
switch {
case '0' <= d && d <= '9':
ns[i] = d - '0'
case d == ' ' || d == ',':
// ignore
default:
return nil
}
}
return ns
}
func ThrowError(w http.ResponseWriter, ErrorType int, ErrorDescription string, opts ...string) {
_newErr := &GeneralResult{
ErrCode: ErrorType,
ErrorDescription: ErrorDescription,
}
switch ErrorType {
case REQUEST_PARAM_FORMAT_ERROR:
_newErr.ErrorParam = opts[0]
case CREDENTIAL_NOT_MATCH:
_newErr.Credential = opts[0]
}
ret, _ := json.Marshal(_newErr)
w.Write(ret)
}
func WriteResult(w http.ResponseWriter, httpCode int, ret interface{}) {
_newRet := &GeneralResult{
ErrCode: 0,
}
_newRet.Data, _ = json.Marshal(ret)
_ret, _ := json.Marshal(_newRet)
w.WriteHeader(httpCode)
w.Write(_ret)
}