Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e56a180

Browse files
committed
完成 参数校验、创建新用户
1 parent 1b586b3 commit e56a180

File tree

20 files changed

+379
-138
lines changed

20 files changed

+379
-138
lines changed

websites/code/studygolang/install.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ goto end
1111
set OLDGOPATH=%GOPATH%
1212
set GOPATH=%~dp0;%~dp0..\thirdparty
1313

14+
gofmt -w src
15+
1416
go install studygolang
1517
go install test
1618

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package config
22

33
import (
4-
"process"
54
"path"
5+
"process"
66
)
77

88
// 项目根目录
@@ -11,9 +11,7 @@ var ROOT string
1111
func init() {
1212
binDir, err := process.ExecutableDir()
1313
if err != nil {
14-
panic(err)
14+
panic(err)
1515
}
1616
ROOT = path.Dir(binDir)
1717
}
18-
19-

websites/code/studygolang/src/controller/admin/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package admin
22

33
import (
44
"config"
5-
"html/template"
65
"fmt"
6+
"html/template"
77
"net/http"
88
)
99

1010
var ROOT = config.ROOT
1111

1212
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
13-
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/index.html")
13+
tpl, err := template.ParseFiles(ROOT+"/template/admin/common.html", ROOT+"/template/admin/index.html")
1414
if err != nil {
1515
fmt.Fprintln(rw, err)
1616
return
1717
}
1818
tpl.Execute(rw, nil)
19-
}
19+
}
Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package admin
22

33
import (
4-
"html/template"
54
"fmt"
5+
"html/template"
66
"net/http"
7-
"net/url"
7+
"service"
8+
"util"
89
)
910

1011
func UsersHandler(rw http.ResponseWriter, req *http.Request) {
11-
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/users.html")
12+
tpl, err := template.ParseFiles(ROOT+"/template/admin/common.html", ROOT+"/template/admin/users.html")
1213
if err != nil {
1314
fmt.Fprintln(rw, err)
1415
return
@@ -18,72 +19,58 @@ func UsersHandler(rw http.ResponseWriter, req *http.Request) {
1819

1920
// 添加新用户表单页面
2021
func NewUserHandler(rw http.ResponseWriter, req *http.Request) {
21-
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/newuser.html")
22+
tpl, err := template.ParseFiles(ROOT+"/template/admin/common.html", ROOT+"/template/admin/newuser.html")
2223
if err != nil {
2324
fmt.Fprintln(rw, err)
2425
return
2526
}
2627
tpl.Execute(rw, nil)
2728
}
2829

29-
func Validate(data url.Values, rules map[string]map[string]string) (errMsg string) {
30-
for field, rule := range rules {
31-
val := data.Get(field)
32-
if _, ok := rule["require"]; ok {
33-
if val == "" {
34-
errMsg = field + "不能为空"
35-
return
36-
}
37-
}
38-
39-
if valRange, ok := rule["range"]; ok {
40-
41-
}
42-
}
43-
}
44-
4530
// 执行添加新用户(异步请求,返回json)
4631
func AddUserHandler(rw http.ResponseWriter, req *http.Request) {
47-
errMsg := ""
48-
49-
username := req.FormValue("username")
50-
email := req.FormValue("email")
51-
// name := req.FormValue("name")
52-
passwd := req.FormValue("passwd")
53-
pass2 := req.FormValue("pass2")
54-
// send_password := req.FormValue("send_password")
55-
56-
if username == "" {
57-
errMsg = "用户名不能空!"
58-
}
59-
60-
if email == "" {
61-
errMsg = "邮箱不能空!"
62-
}
63-
64-
if passwd == "" {
65-
errMsg = "密码不能空!"
32+
rules := map[string]map[string]map[string]string{
33+
"username": {
34+
"require": {"error": "用户名不能为空!"},
35+
"length": {"range": "4,20", "error": "用户名长度必须在%d个字符和%d个字符之间"},
36+
},
37+
"email": {
38+
"require": {"error": "邮箱不能为空!"},
39+
"email": {"error": "邮箱格式不正确!"},
40+
},
41+
"passwd": {
42+
"require": {"error": "密码不能为空!"},
43+
"length": {"range": "6,32", "error": "密码长度必须在%d个字符和%d个字符之间"},
44+
},
45+
"pass2": {
46+
"require": {"error": "确认密码不能为空!"},
47+
"compare": {"field": "passwd", "rule": "=", "error": "两次密码不一致"},
48+
},
6649
}
67-
68-
if passwd != pass2 {
69-
errMsg = "两次密码不一致!"
70-
}
71-
// TODO: 其他判断
50+
req.ParseForm()
51+
errMsg := util.Validate(req.Form, rules)
52+
7253
header := rw.Header()
7354
header.Set("Content-Type", "application/json; charset=utf-8")
7455
if errMsg != "" {
75-
fmt.Fprint(rw, `{"error":"`, errMsg, `"}`)
76-
return
56+
fmt.Fprint(rw, `{"errno": 1,"error":"`, errMsg, `"}`)
57+
return
7758
}
78-
59+
7960
// 入库
61+
errMsg, err := service.CreateUser(req.Form)
62+
if err != nil {
63+
fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
64+
return
65+
}
66+
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
8067
}
8168

8269
func ProfilerHandler(rw http.ResponseWriter, req *http.Request) {
83-
tpl, err := template.ParseFiles(ROOT + "/template/admin/common.html", ROOT + "/template/admin/profiler.html")
70+
tpl, err := template.ParseFiles(ROOT+"/template/admin/common.html", ROOT+"/template/admin/profiler.html")
8471
if err != nil {
8572
fmt.Fprintln(rw, err)
8673
return
8774
}
8875
tpl.Execute(rw, nil)
89-
}
76+
}

websites/code/studygolang/src/controller/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package controller
22

33
import (
44
"config"
5-
"html/template"
65
"fmt"
6+
"html/template"
77
"net/http"
88
)
99

1010
var ROOT = config.ROOT
1111

1212
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
13-
tpl, err := template.ParseFiles(ROOT + "/template/index.html", ROOT + "/template/common/header.html", ROOT + "/template/common/footer.html")
13+
tpl, err := template.ParseFiles(ROOT+"/template/index.html", ROOT+"/template/common/header.html", ROOT+"/template/common/footer.html")
1414
if err != nil {
1515
fmt.Fprintln(rw, err)
1616
return
1717
}
1818
tpl.Execute(rw, nil)
19-
}
19+
}

websites/code/studygolang/src/controller/topic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package controller
22

33
import (
4-
"html/template"
54
"fmt"
5+
"html/template"
66
"net/http"
77
)
88

99
func NewTopicHandler(rw http.ResponseWriter, req *http.Request) {
10-
tpl, err := template.ParseFiles(ROOT + "/template/topics/new.html", ROOT + "/template/common/header.html", ROOT + "/template/common/footer.html")
10+
tpl, err := template.ParseFiles(ROOT+"/template/topics/new.html", ROOT+"/template/common/header.html", ROOT+"/template/common/footer.html")
1111
if err != nil {
1212
fmt.Fprintln(rw, err)
1313
return

websites/code/studygolang/src/model/dao.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Dao struct {
1212
// 构造sql语句相关
1313
tablename string
1414
where string
15-
whereVal []interface{} // where条件对应中字段对应的值
15+
whereVal []interface{} // where条件对应中字段对应的值
1616
limit string
1717
order string
1818
// 插入需要
@@ -73,7 +73,7 @@ func (this *Dao) SetWhere(args ...string) {
7373
for i, arg := range args {
7474
parts := strings.SplitN(arg, "=", 2)
7575
if len(parts) != 2 {
76-
// TODO:怎么处理
76+
// TODO:怎么处理
7777
}
7878
fields[i] = parts[0] + "=?"
7979
this.whereVal[i] = parts[1]
@@ -83,7 +83,7 @@ func (this *Dao) SetWhere(args ...string) {
8383

8484
func (this *Dao) SelectCols() string {
8585
if this.selectCols == "" {
86-
return "*"
86+
return "*"
8787
}
8888
return this.selectCols
8989
}
Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package model
22

33
import (
4+
"fmt"
5+
"math/rand"
46
"time"
7+
"util"
58
)
69

7-
const (
8-
tablename = "user_login"
9-
)
10-
10+
// 用户登录信息
1111
type UserLogin struct {
12-
Uid int `json:"uid"`
13-
Username string `json:"username"`
14-
Passwd string `json:"passwd"`
15-
Email string `json:"email"`
12+
Uid int `json:"uid"`
13+
Username string `json:"username"`
14+
Passwd string `json:"passwd"`
15+
Email string `json:"email"`
1616
passcode string // 加密随机串
1717

1818
// 数据库访问对象
@@ -21,52 +21,81 @@ type UserLogin struct {
2121

2222
func NewUserLogin() *UserLogin {
2323
return &UserLogin{
24-
Dao: &Dao{tablename: tablename},
24+
Dao: &Dao{tablename: "user_login"},
2525
}
2626
}
2727

2828
func (this *UserLogin) Insert() (int64, error) {
2929
this.prepareInsertData()
3030
result, err := this.Dao.Insert()
3131
if err != nil {
32-
return 0, err
32+
return 0, err
3333
}
3434
return result.RowsAffected()
3535
}
3636

3737
func (this *UserLogin) Find() error {
3838
row, err := this.Dao.Find()
3939
if err != nil {
40-
return err
40+
return err
4141
}
4242
return row.Scan(&this.Uid, &this.Username, &this.Passwd, &this.Email, &this.passcode)
4343
}
4444

4545
func (this *UserLogin) prepareInsertData() {
4646
this.columns = []string{"uid", "username", "passwd", "email", "passcode"}
47-
this.passcode = "123ff"
47+
this.passcode = fmt.Sprintf("%x", rand.Int31())
48+
// 密码经过md5(passwd+passcode)加密保存
49+
this.Passwd = util.Md5(this.Passwd + this.passcode)
4850
this.colValues = []interface{}{this.Uid, this.Username, this.Passwd, this.Email, this.passcode}
4951
}
5052

53+
// 用户基本信息
5154
type User struct {
52-
Uid int
53-
Username string
54-
Passwd string
55-
Email string
56-
Name string
57-
Avatar string
58-
City string
59-
Company string
60-
Github string
61-
Weibo string
62-
Website string
63-
Status string
64-
Introduce string
55+
Uid int `json:"uid"`
56+
Username string `json:"username"`
57+
Email string `json:"email"`
58+
Name string `json:"name"`
59+
Avatar string `json:"avatar"`
60+
City string `json:"city"`
61+
Company string `json:"company"`
62+
Github string `json:"github"`
63+
Weibo string `json:"weibo"`
64+
Website string `json:"website"`
65+
Status string `json:"status"`
66+
Introduce string `json:"introduce"`
6567
// 不导出
6668
open int
6769
ctime time.Time
70+
71+
// 内嵌
72+
*Dao
6873
}
6974

7075
func NewUser() *User {
71-
return nil
76+
return &User{
77+
Dao: &Dao{tablename: "user_info"},
78+
}
79+
}
80+
81+
func (this *User) Insert() (int64, error) {
82+
this.prepareInsertData()
83+
result, err := this.Dao.Insert()
84+
if err != nil {
85+
return 0, err
86+
}
87+
return result.LastInsertId()
88+
}
89+
90+
func (this *User) Find() error {
91+
row, err := this.Dao.Find()
92+
if err != nil {
93+
return err
94+
}
95+
return row.Scan(&this.Uid, &this.Username, &this.Email, &this.Name, &this.open)
96+
}
97+
98+
func (this *User) prepareInsertData() {
99+
this.columns = []string{"username", "email", "name", "avatar", "city", "company", "github", "weibo", "website", "status", "introduce"}
100+
this.colValues = []interface{}{this.Username, this.Email, this.Name, this.Avatar, this.City, this.Company, this.Github, this.Weibo, this.Website, this.Status, this.Introduce}
72101
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package model_test
22

33
import (
4+
"encoding/json"
45
. "model"
56
"testing"
6-
//"encoding/json"
77
)
88

99
func TestNewUserLogin(t *testing.T) {
1010
userLogin := NewUserLogin()
11-
// userData := `{"uid":123234,"username":"poalris","email":"[email protected]","passwd":"123456"}`
12-
// json.Unmarshal([]byte(userData), userLogin)
13-
err := userLogin.Find()
14-
// affectedNum, err := userLogin.Insert()
11+
userData := `{"uid":"1111","username":"poalris","email":"[email protected]","passwd":"123456"}`
12+
json.Unmarshal([]byte(userData), userLogin)
13+
// err := userLogin.Find()
14+
affectedNum, err := userLogin.Insert()
1515
if err != nil {
16-
t.Fatal(err)
16+
t.Fatal(err)
1717
}
18-
t.Error(userLogin.Uid)
19-
}
18+
t.Log(affectedNum)
19+
}

0 commit comments

Comments
 (0)