Skip to content

Commit 32e9a01

Browse files
committed
2 parents 5530fa8 + 62b8d82 commit 32e9a01

6 files changed

Lines changed: 273 additions & 101 deletions

File tree

error/ErrorCode.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[{
2+
"code": 1001,
3+
"lv": "ERROR",
4+
"msg": {
5+
"en": "username over length",
6+
"cn": "用户名过长"
7+
},
8+
"details": "over length"
9+
},{
10+
"code": 1002,
11+
"lv": "ERROR",
12+
"msg": {
13+
"en": "input tooshor",
14+
"cn": "密码太短"
15+
},
16+
"details": "input tooshort"
17+
}
18+
]

error/errcode.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017~2022 The Bottos Authors
2+
// This file is part of the Bottos Chain library.
3+
// Created by Rocket Core Team of Bottos.
4+
5+
//This program is free software: you can distribute it and/or modify
6+
//it under the terms of the GNU General Public License as published by
7+
//the Free Software Foundation, either version 3 of the License, or
8+
//(at your option) any later version.
9+
10+
//This program is distributed in the hope that it will be useful,
11+
//but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
//GNU General Public License for more details.
14+
15+
//You should have received a copy of the GNU General Public License
16+
// along with bottos. If not, see <http://www.gnu.org/licenses/>.
17+
18+
/*
19+
* file description: provide a interface error definition for all modules
20+
* @Author: zl
21+
* @Date: 2018-4-25
22+
* @Last Modified by:
23+
* @Last Modified time:
24+
*/
25+
package error
26+
27+
import (
28+
"io/ioutil"
29+
"encoding/json"
30+
)
31+
32+
type ErrorCode struct {
33+
Code int64 `json:"code"`
34+
Lv string `json:"lv"`
35+
Msg struct {
36+
Cn string `json:"cn"`
37+
En string `json:"en"`
38+
} `json:"msg"`
39+
Details string `json:"details"`
40+
}
41+
42+
43+
func GetErrorInfo(code int64) ErrorCode {
44+
d := GetAllErrorInfos()
45+
46+
for _, v := range d {
47+
if code == v.Code {
48+
return v
49+
}
50+
}
51+
return ErrorCode{}
52+
}
53+
54+
func GetAllErrorInfos() []ErrorCode {
55+
fr, err := ioutil.ReadFile("./ErrorCode.json")
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
var d []ErrorCode
61+
err = json.Unmarshal(fr, &d)
62+
if err != nil {
63+
panic(err)
64+
}
65+
return d
66+
}
67+
68+

error/errcode_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package error
2+
3+
import "testing"
4+
5+
func Test_getErrorInfo(t *testing.T) {
6+
t.Log(GetErrorInfo(1001))
7+
}
8+
9+
func Test_GetAllErrorInfos(t *testing.T) {
10+
t.Log(GetAllErrorInfos())
11+
}

service/identity/ideApi/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,72 @@ import (
1111
"strconv"
1212
"regexp"
1313
"github.com/code/bottos/config"
14+
"github.com/mojocn/base64Captcha"
1415
)
1516

17+
var configCode = base64Captcha.ConfigDigit{
18+
Height: 80,
19+
Width: 240,
20+
MaxSkew: 0.7,
21+
DotCount: 80,
22+
CaptchaLen: 5,
23+
}
24+
25+
//var configCode = base64Captcha.ConfigCharacter{
26+
// Height: 60,
27+
// Width: 240,
28+
//// //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
29+
// Mode: base64Captcha.CaptchaModeNumber,
30+
// ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
31+
// ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
32+
// IsShowHollowLine: false,
33+
// IsShowNoiseDot: false,
34+
// IsShowNoiseText: false,
35+
// IsShowSlimeLine: false,
36+
// IsShowSineLine: false,
37+
// CaptchaLen: 6,
38+
//}
39+
1640
type User struct {
1741
Client user.UserClient
1842
}
1943

44+
func (u *User) GetVerificationCode(ctx context.Context, req *api.Request, rsp *api.Response) error {
45+
46+
idKeyD, capD := base64Captcha.GenerateCaptcha("", configCode)
47+
//以base64编码
48+
base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
49+
50+
rsp.StatusCode = 200
51+
b, _ := json.Marshal(map[string]interface{}{
52+
"code": 1,
53+
"data": map[string]interface{}{
54+
"id_key": idKeyD,
55+
"img_data": base64stringD,
56+
},
57+
"msg": "OK",
58+
})
59+
60+
rsp.Body = string(b)
61+
return nil
62+
}
63+
64+
2065
func (u *User) Register(ctx context.Context, req *api.Request, rsp *api.Response) error {
2166
body := req.Body
2267
//transfer to struct
2368
var registerRequest user.RegisterRequest
2469
json.Unmarshal([]byte(body), &registerRequest)
70+
71+
if !base64Captcha.VerifyCaptcha(registerRequest.IdKey, registerRequest.VerifyValue) {
72+
b, _ := json.Marshal(map[string]interface{}{
73+
"code": -8,
74+
"msg": "Verification code error",
75+
})
76+
rsp.StatusCode = 200
77+
rsp.Body = string(b)
78+
return nil
79+
}
2580
//Checkout data format
2681
ok, err := govalidator.ValidateStruct(registerRequest);
2782
if !ok {

0 commit comments

Comments
 (0)