Browse Source

登录注册完成

layui
yirenyishi 6 years ago
parent
commit
578615092d
  1. 5
      conf/app.conf
  2. 120
      controllers/UserController.go
  3. 11
      models/Attachment.go
  4. 20
      models/Blog.go
  5. 13
      models/Comment.go
  6. 3
      models/DataInit.go
  7. 10
      models/Like.go
  8. 10
      models/Note.go
  9. 22
      models/Result.go
  10. 19
      models/User.go
  11. 8
      models/struct.go
  12. 15
      models/topic.go
  13. 12
      routers/UserRouter.go
  14. 4
      routers/router.go
  15. 35
      service/UserService.go
  16. 39
      tests/default_test.go

5
conf/app.conf

@ -1,3 +1,8 @@
appname = beeblog
httpport = 8082
runmode = dev
EnableGzip = true
EnableDocs = true
host = aiprose.com

120
controllers/UserController.go

@ -1,15 +1,127 @@
package controllers
import "github.com/astaxie/beego"
import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models"
"strings"
"crypto/md5"
"time"
"strconv"
"encoding/hex"
)
type UserController struct {
beego.Controller
}
func (u *UserController) LoginPage() {
//o := orm.NewOrm()
//
//category := &models.Category{Title: "slene", Views: 5, TopicCount: 0}
//
//id,err := o.Insert(category)
//fmt.Printf("ID: %d, ERR: %v\n", id, err)
//
//category.Title = "nelson"
//num, err := o.Update(&category)
//fmt.Printf("NUM: %d, ERR: %v\n", num, err)
//
//c := &models.Category{Id:1}
//err := o.Read(c)
//fmt.Printf("ERR: %v\n", err)
//fmt.Println(c.Title)
////c.Title = "nelson"
////num, err := o.Update(c)
////fmt.Printf("NUM: %d, ERR: %v\n", num, err)
//qs := o.QueryTable(&models.Category{})
//var tests []*models.Category
//qs.Filter("Title","slene")
//qs.All(&tests)
//
//for i:=0; i<len(tests) ; i++ {
// fmt.Println(tests[i].Title,tests[i].Id)
//}
//fmt.Println(len(tests))
//num, err = o.Delete(&u)
//fmt.Printf("NUM: %d, ERR: %v\n", num, err)
u.Ctx.WriteString("login page")
}
func (u *UserController) Login() {
u.Ctx.WriteString("login method")
}
func (this *UserController) Login() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
if username == "" {
this.Data["json"] = models.ReurnError("用户名为空")
this.ServeJSON()
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError("用户名最低4位")
this.ServeJSON()
}
if userpwd == "" {
this.Data["json"] = models.ReurnError("密码为空")
this.ServeJSON()
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError("密码最低6位")
this.ServeJSON()
}
user, error := service.FindByUserName(username)
if error == nil && user != nil {
h := md5.New()
h.Write([]byte(userpwd + user.Salt))
userpwd = hex.EncodeToString(h.Sum(nil))
if userpwd == user.UserPwd {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError("用户名或密码错误")
}
} else {
this.Data["json"] = models.ReurnError("用户名不存在")
}
this.ServeJSON()
}
func (this *UserController) Regist() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
username = strings.Replace(username, " ", "", -1)
userpwd = strings.Replace(userpwd, " ", "", -1)
if username == "" {
this.Data["json"] = models.ReurnError("用户名为空")
this.ServeJSON()
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError("用户名最低4位")
this.ServeJSON()
}
if userpwd == "" {
this.Data["json"] = models.ReurnError("密码为空")
this.ServeJSON()
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError("密码最低6位")
this.ServeJSON()
}
user, _ := service.FindByUserName(username)
if user != nil {
this.Data["json"] = models.ReurnError("用户已经存在")
this.ServeJSON()
}
h := md5.New()
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + beego.AppConfig.String("host")))
salt := hex.EncodeToString(h.Sum(nil))
h = md5.New()
h.Write([]byte(userpwd + salt))
userpwd = hex.EncodeToString(h.Sum(nil))
user = &models.User{UserName: username, UserPwd: userpwd, Salt: salt}
err := service.SaveUser(user)
if err == nil {
this.Data["json"] = user
} else {
this.Data["json"] = models.ReurnError("注册失败")
}
this.ServeJSON()
}

11
models/Attachment.go

@ -0,0 +1,11 @@
package models
import "time"
type Attachment struct {
Id int64
Name string
Alias string
Path string `orm:"index"`
UploadTime time.Time `orm:"datetime"`
}

20
models/Blog.go

@ -0,0 +1,20 @@
package models
import "time"
type Blog struct {
Id int64
UserId int64
Title string
BlogValue string `orm:"size(5000)"`
BlogHtml string `orm:"size(5000)"`
Ctime time.Time `orm:"datetime"`
Utime time.Time `orm:"datetime"`
Browses int64 `orm:"datetime"`
Top int
Hot int
Ttime time.Time `orm:"datetime"`
Htime time.Time `orm:"datetime"`
Delflag int
CategoryId int64
}

13
models/Comment.go

@ -0,0 +1,13 @@
package models
import "time"
type Comment struct {
Id int64
CuserId int64
BuserId int64
BlogId int64
Ctime time.Time `orm:"datetime"`
Pid int64
ComVal string `orm:"size(2000)"`
}

3
models/datainit.go → models/DataInit.go

@ -18,7 +18,8 @@ func RegistDB() {
os.MkdirAll(path.Dir(_DB_NAME),os.ModePerm)
os.Create(_DB_NAME)
}
orm.RegisterModel(new(Category),new(Topic))
//orm.RegisterModel(new(Attachment),new(Topic))
orm.RegisterModel(new(Attachment),new(User))
orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite)
orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10)
}

10
models/Like.go

@ -0,0 +1,10 @@
package models
import "time"
type Like struct {
Id int64
UserId int64
BlogId int64
Ltime time.Time `orm:"datetime"`
}

10
models/Note.go

@ -0,0 +1,10 @@
package models
type Note struct {
Id int64
UserId int64
Title string
NoteVal string `orm:"size(3500)"`
NoteHtml string `orm:"size(5000)"`
Pid int64
}

22
models/Result.go

@ -0,0 +1,22 @@
package models
type Error struct {
Status byte
Msg string
}
func ReurnError(msg string) *Error {
return &Error{Status: byte(1), Msg: msg}
}
type Success struct {
Status byte
Msg string
}
func ReurnSuccess(msg string) *Error {
if msg == "" {
msg = "success"
}
return &Error{Status: byte(0), Msg: msg}
}

19
models/User.go

@ -0,0 +1,19 @@
package models
import "time"
type User struct {
Id int64
UserName string `orm:"unique"`
UserPwd string
Salt string
Headimg string
Birthday time.Time `orm:"null;type(date)"`
Email string
Mobile string
QQ string
HomeUrl string
Sex int
DescInfo string
Ctime time.Time `orm:"auto_now_add;type(datetime)"`
}

8
models/struct.go

@ -1,8 +0,0 @@
package models
type Category struct {
Id int64
Title string
Views int64 `orm:"index"`
TopicCount int64
}

15
models/topic.go

@ -1,15 +0,0 @@
package models
import "time"
type Topic struct {
Id int64
Uid int64
Title string
Content string `orm:"size(5000)"`
Attachment string
Created time.Time `orm:"index"`
ViewCount int64 `orm:"index"`
Author string
ReplayCount int64
}

12
routers/UserRouter.go

@ -0,0 +1,12 @@
package routers
import (
"github.com/astaxie/beego"
"beeblog/controllers"
)
func init() {
beego.Router("/login", &controllers.UserController{}, "get:LoginPage")
beego.Router("/login", &controllers.UserController{}, "post:Login")
beego.Router("/regist", &controllers.UserController{}, "post:Regist")
}

4
routers/router.go

@ -6,8 +6,8 @@ import (
)
func init() {
//beego.Router("/login", &controllers.UserController{}, "get:LoginPage")
beego.Router("/", &controllers.IndexController{})
beego.Router("/login", &controllers.UserController{}, "Get:LoginPage")
beego.Router("/login", &controllers.UserController{}, "post:Login")
//beego.Router("/login", &controllers.UserController{}, "post:Login")
}

35
service/UserService.go

@ -0,0 +1,35 @@
package service
import (
"github.com/astaxie/beego/orm"
"beeblog/models"
)
type UserService struct {
}
func FindByUserName(username string) (*models.User, error) {
o := orm.NewOrm()
qs := o.QueryTable(&models.User{})
var users []*models.User
_, err := qs.Filter("UserName", username).All(&users)
if err != nil {
return nil, err
}
if len(users) != 0 {
return users[0], nil
}
return nil, nil
}
func SaveUser(user *models.User) error {
o := orm.NewOrm()
id, eror := o.Insert(user)
if eror != nil {
return eror
} else {
user.Id = id
o.Commit()
}
return nil
}

39
tests/default_test.go

@ -1,39 +0,0 @@
package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "beeblog/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(1)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
Loading…
Cancel
Save