11package model
22
33import (
4+ "fmt"
5+ "math/rand"
46 "time"
7+ "util"
58)
69
7- const (
8- tablename = "user_login"
9- )
10-
10+ // 用户登录信息
1111type 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
2222func NewUserLogin () * UserLogin {
2323 return & UserLogin {
24- Dao : & Dao {tablename : tablename },
24+ Dao : & Dao {tablename : "user_login" },
2525 }
2626}
2727
2828func (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
3737func (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
4545func (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+ // 用户基本信息
5154type 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
7075func 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}
0 commit comments