register&login
This commit is contained in:
13
SourceTrace/proto/login.go
Normal file
13
SourceTrace/proto/login.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package proto
|
||||
|
||||
type (
|
||||
LoginReq struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
LoginResult struct {
|
||||
Username string `json:"username"`
|
||||
Role int `json:"role"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
)
|
||||
11
SourceTrace/proto/register.go
Normal file
11
SourceTrace/proto/register.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package proto
|
||||
|
||||
type (
|
||||
RegisterReq struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
RegisterResult struct {
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
)
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/NothAmor/SourceTrace/proto"
|
||||
"github.com/NothAmor/SourceTrace/tools"
|
||||
"golang.org/x/exp/maps"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Arith struct{}
|
||||
@@ -200,3 +201,50 @@ func (t *Arith) Search(ctx context.Context, args proto.Args, reply *proto.Resp)
|
||||
reply.Values = values
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Arith) Register(ctx context.Context, registerReq proto.RegisterReq, registerResult *proto.RegisterResult) (err error) {
|
||||
//1.判断用户名是否存在
|
||||
var users []models.Users
|
||||
err = common.DB.Where("username=?", registerReq.Username).Find(&users).Error
|
||||
if err != nil {
|
||||
registerResult.Msg = "注册失败"
|
||||
return err
|
||||
}
|
||||
if len(users) > 0 {
|
||||
registerResult.Msg = "注册失败,用户名重复"
|
||||
return nil
|
||||
}
|
||||
//2.注册用户
|
||||
user := models.Users{Username: registerReq.Username, Password: registerReq.Password}
|
||||
err = common.DB.Create(&user).Error
|
||||
if err != nil {
|
||||
registerResult.Msg = "注册失败"
|
||||
return err
|
||||
}
|
||||
registerResult.Msg = "注册成功"
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Arith) Login(ctx context.Context, loginReq proto.LoginReq, loginResult *proto.LoginResult) (err error) {
|
||||
//1.判断用户名是否存在
|
||||
username := loginReq.Username
|
||||
password := loginReq.Password
|
||||
var user models.Users
|
||||
result := common.DB.Where("username=?", username).First(&user)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
loginResult.Msg = "登录失败,用户名不存在"
|
||||
return nil
|
||||
} else {
|
||||
return result.Error
|
||||
}
|
||||
}
|
||||
if user.Password != password {
|
||||
loginResult.Msg = "登录失败,用户名密码不匹配"
|
||||
return nil
|
||||
}
|
||||
loginResult.Msg = "登录成功"
|
||||
loginResult.Username = user.Username
|
||||
loginResult.Role = user.Role
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,60 @@
|
||||
package controllers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/NothAmor/SourceTraceFront/common"
|
||||
"github.com/NothAmor/SourceTraceFront/proto"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
c.HTML(200, "login.html", gin.H{})
|
||||
//1.获取参数
|
||||
loginReq := proto.LoginReq{}
|
||||
err := c.ShouldBindJSON(&loginReq)
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
|
||||
if loginReq.Password == "" || loginReq.Password == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "用户名&&密码不能为空",
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
println(" 测试参数获取成功,后续可删除"+loginReq.Username, loginReq.Password+"开始调用远程服务接口")
|
||||
//2.RPC调用接口实现登录功能
|
||||
var loginResult = proto.LoginResult{}
|
||||
err2 := common.RPC.Call(context.Background(), "Login", loginReq, &loginResult)
|
||||
if err2 != nil {
|
||||
log.Printf("failed to call: %v", err)
|
||||
return
|
||||
}
|
||||
cookieOfUsername := &http.Cookie{
|
||||
Name: "username",
|
||||
Value: loginResult.Username,
|
||||
Path: "/",
|
||||
Secure: false,
|
||||
HttpOnly: true,
|
||||
}
|
||||
cookieOfRole := &http.Cookie{
|
||||
Name: "role",
|
||||
Value: strconv.Itoa(loginResult.Role),
|
||||
Path: "/",
|
||||
Secure: false,
|
||||
HttpOnly: true,
|
||||
}
|
||||
http.SetCookie(c.Writer, cookieOfUsername)
|
||||
http.SetCookie(c.Writer, cookieOfRole)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": loginResult.Msg,
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,42 @@
|
||||
package controllers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/NothAmor/SourceTraceFront/common"
|
||||
"github.com/NothAmor/SourceTraceFront/proto"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Register(c *gin.Context) {
|
||||
c.HTML(200, "register.html", gin.H{})
|
||||
//1.获取参数
|
||||
registerReq := proto.RegisterReq{}
|
||||
err := c.ShouldBindJSON(®isterReq)
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
|
||||
if registerReq.Password == "" || registerReq.Password == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "用户名&&密码不能为空",
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
println(" 测试参数获取成功,后续可删除"+registerReq.Username, registerReq.Password+"开始调用远程服务接口")
|
||||
//2.RPC调用接口实现注册功能
|
||||
var registerResult = proto.RegisterResult{}
|
||||
err2 := common.RPC.Call(context.Background(), "Register", registerReq, ®isterResult)
|
||||
if err2 != nil {
|
||||
log.Printf("failed to call: %v", err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": registerResult.Msg,
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
|
||||
13
SourceTraceFront/proto/login.go
Normal file
13
SourceTraceFront/proto/login.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package proto
|
||||
|
||||
type (
|
||||
LoginReq struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
LoginResult struct {
|
||||
Username string `json:"username"`
|
||||
Role int `json:"role"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
)
|
||||
11
SourceTraceFront/proto/register.go
Normal file
11
SourceTraceFront/proto/register.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package proto
|
||||
|
||||
type (
|
||||
RegisterReq struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
RegisterResult struct {
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
)
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
|
||||
func InitRouter() {
|
||||
common.Core.GET("/", controllers.Search)
|
||||
common.Core.GET("/login", controllers.Login)
|
||||
common.Core.GET("/register", controllers.Register)
|
||||
common.Core.POST("/login", controllers.Login)
|
||||
common.Core.POST("/register", controllers.Register)
|
||||
|
||||
common.Core.GET("/submitUrl", controllers.SubmitUrls)
|
||||
common.Core.POST("/submitUrl", controllers.SubmitUrl)
|
||||
|
||||
Reference in New Issue
Block a user