Skip to content

Commit

Permalink
验证
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxiaopang committed Jan 24, 2018
1 parent 6c05966 commit 0ba9716
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 33 deletions.
6 changes: 4 additions & 2 deletions src/controller/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

func success(c *gin.Context, data interface{}) error {

result := map[string]interface{}{
"ok": 1,
"ok": 0,
"msg": "操作成功",
"data": data,
}
Expand All @@ -19,9 +20,10 @@ func success(c *gin.Context, data interface{}) error {
if err != nil {
return err
}
// 能够成功转换

c.JSON(200, gin.H{
"code": 1,
"code": 0,
"msg": "操作成功",
"data": data,
})
Expand Down
33 changes: 21 additions & 12 deletions src/controller/regist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@ package controller

import (
"GoWeibo/src/logic"
"GoWeibo/src/model"
"fmt"

"github.com/gin-gonic/gin"
)

// Regist 注册
type Regist struct{}
// RegistController 注册
type RegistController struct{}

// RegisterRoute 注册路由
func (c Regist) RegisterRoute(e *gin.Engine) {
e.GET("/regist", userRegist)
func (c RegistController) RegisterRoute(e *gin.Engine) {
e.GET("/regist", UserRegist)
e.POST("/login", Login)
}

func userRegist(c *gin.Context) {
// UserRegist 用户注册函数
func UserRegist(c *gin.Context) {

userLogic := login.DefaultUser
logic.CreateUser(c)

err := model.InsertUser(&u)
info, err := userLogic.CreateUser(c)
fmt.Println(info)
if err != nil {
fail(c, err.Error())
return
}
success(c, "注册成功")
// fmt.Println(email)
// fmt.Println(password)
// success(c, "userRegist")
}

// Login 用户登录
func Login(c *gin.Context) {
userLogic := login.DefaultUser
data, err := userLogic.UserLogin(c)
if err != nil {
fail(c, err.Error())
return
}
success(c, data)
}
2 changes: 1 addition & 1 deletion src/controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import "github.com/gin-gonic/gin"
func RegisterRoutes(e *gin.Engine) {
// 在这里调用控制器注册路由
new(WeiboListController).RegisterRoute(e)
new(Regist).RegisterRoute(e)
new(RegistController).RegisterRoute(e)
}
70 changes: 52 additions & 18 deletions src/logic/registLogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,55 @@ package login

import (
"GoWeibo/src/model"
"crypto/md5"
"encoding/hex"
"GoWeibo/util"
"errors"
"fmt"
"io"
"strings"
"time"
"unicode/utf8"
"weibo/db"

"github.com/gin-gonic/gin"
)

// UserLogic 用户登录逻辑
type UserLogic struct{}

// DefaultUser 空Struct
var DefaultUser = UserLogic{}

// CreateUser 创建用户
func (self UserLogic) CreateUser(e *gin.Engine) (errMsg string, err error) {
email := e.Query("email")
password := e.Query("password")
func (userLogic UserLogic) CreateUser(c *gin.Context) (errMsg string, err error) {
email := c.Query("email")
password := c.Query("password")
u := model.User{Email: email, Password: password}

if u.Email == "" {
return nil, errors.New("邮箱不能为空")
return "", errors.New("邮箱不能为空")
}

if u.Password == "" {
return nil, errors.New("密码不能为空")
return "", errors.New("密码不能为空")
}

if emailExist := Emailexists(u.Email); emailExist {
return "邮箱已经存在", errors.New("邮箱已经存在")
}

// 计算MD5
md5Pre := u.Password
md5 := md5.New()
io.WriteString(md5, md5Pre)
md5Later := md5.Sum(nil)
u.Password = hex.EncodeToString(md5Later)
fmt.Println("md5值:", u.Password)
u.Password = util.GenerateStringMD5(u.Password)

insert := db.MasterDB.Debug().Create(&u).RecordNotFound()
if insert {
return errors.New("注册用户失败")
return "", errors.New("注册用户失败")
}
return nil
return "", nil
}

// Emailexists 邮箱是否已经存在
func Emailexists(email string) bool {
user := User{}
db.MasterDB.Where(&User{Email: email}).First(&user)
user := model.User{}
db.MasterDB.Where(&model.User{Email: email}).First(&user)
fmt.Println(user.Email)
if utf8.RuneCountInString(user.Email) > 0 {
fmt.Println("数据库已经存在email: ", email)
Expand All @@ -61,3 +59,39 @@ func Emailexists(email string) bool {
fmt.Println("数据库不存在email: ", email)
return false
}

// UserLogin 用户登录
func (userLogic UserLogic) UserLogin(c *gin.Context) (data interface{}, err error) {
userName := c.PostForm("username")
passWord := c.PostForm("password")
if userName == "" || passWord == "" {
return "", errors.New("账号/密码不能为空")
}

if !Emailexists(userName) {
return "", errors.New("账号/密码错误")
}

// 查数据库验证账号密码是否正确
// 计算加密后的password
md5Str := util.GenerateStringMD5(passWord)

user := model.User{}
db.MasterDB.Where(&model.User{Email: userName, Password: md5Str}).First(&user)
if (strings.Compare(userName, user.Email) == 0) && (strings.Compare(md5Str, user.Password) == 0) {
// 密码正确
fmt.Println(user.Email, "验证成功")
token := GenerateToken(&user)
// token 写入数据库
db.MasterDB.Model(&user).Update("Token", token)
data := map[string]interface{}{"token": token}
return data, nil
}
return "", errors.New("账号密码错误")
}

// GenerateToken 生成token
func GenerateToken(u *model.User) string {
md5Pre := u.UID + u.Password + u.Email + string(time.Now().Unix())
return util.GenerateStringMD5(md5Pre)
}
16 changes: 16 additions & 0 deletions util/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

import(
"crypto/md5"
"encoding/hex"
"io"
)

// 计算字符串的MD5
func GenerateStringMD5(str string) string {
md5 := md5.New()
io.WriteString(md5, str)
md5Later := md5.Sum(nil)
md5Str := hex.EncodeToString(md5Later)
return md5Str
}

0 comments on commit 0ba9716

Please sign in to comment.