register&login
This commit is contained in:
@@ -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