Skip to content

Commit

Permalink
MOD:
Browse files Browse the repository at this point in the history
  • Loading branch information
timidsmile committed Jun 10, 2018
1 parent a8627fe commit f45ed8b
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 201 deletions.
292 changes: 144 additions & 148 deletions .idea/workspace.xml

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions action/passport/getUserInfo.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package passport

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/timidsmile/pspace/components"
"net/http"
"github.com/timidsmile/pspace/service"
"strconv"
"fmt"
"net/http"
)

func GetUserInfoAction(c *gin.Context) {
fmt.Println("in getU")
response := components.NewResponse()
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Origin", "https://www.pspace.com")
defer c.JSON(http.StatusOK, response)

// 直接从表单中取数据形式获取参数
userIdStr := c.PostForm("userID")

userID, _ := strconv.ParseInt(userIdStr, 10, 64)
userID := c.MustGet("userID").(int64)
fmt.Println(userID)

userServ := service.UserBasicService{}
userBasic := userServ.GetByUserID(userID)

response.Data = userBasic;
response.Data = userBasic
return
}
8 changes: 6 additions & 2 deletions action/session/Login.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package session

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/timidsmile/pspace/components"
"github.com/timidsmile/pspace/service"
Expand All @@ -10,6 +11,7 @@ import (

func LoginAction(c *gin.Context) {
response := components.NewResponse()
c.Header("Access-Control-Allow-Origin", "https://timidsmile.com")
defer c.JSON(http.StatusOK, response)

userName, exist := c.GetPostForm("userName")
Expand Down Expand Up @@ -44,16 +46,18 @@ func LoginAction(c *gin.Context) {

// 登陆成功,记录session
userID := curUser.UserID
fmt.Println(userID)

cur := time.Now()
timestamp := int64(cur.UnixNano() / 1000000000) //UnitNano获取的是纳秒,除以1000000获取秒级的时间戳

userSession := components.Session{
UserID: userID,
LoginTime: timestamp,
}
token, _ := userSession.Save()
c.SetCookie("token", token, 86400, "/", "timidsmile.com", false, false)

userSession.Save()
fmt.Println("token is :" + token)

return
}
15 changes: 15 additions & 0 deletions action/session/Register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/timidsmile/pspace/model"
"github.com/timidsmile/pspace/service"
"net/http"
"time"
)

func RegisterAction(c *gin.Context) {
Expand Down Expand Up @@ -92,5 +93,19 @@ func RegisterAction(c *gin.Context) {
response.Msg = err.Error()
}

// set session
cur := time.Now()
timestamp := int64(cur.UnixNano() / 1000000000) //UnitNano获取的是纳秒,除以1000000获取秒级的时间戳
userSession := components.Session{
UserID: userID,
LoginTime: timestamp,
}

token, _ := userSession.Save()

c.SetCookie("token", token, 1111111111111111, "/", "www.pspace.com", true, true)

c.String(http.StatusOK, "register successful")

return
}
8 changes: 5 additions & 3 deletions components/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ type Session struct {
LoginTime int64 // 登陆时间
}

func (s *Session) Save() error {
func (s *Session) Save() (string, error) {
accessToken := s.genAccessToken()

sessionVal := strconv.FormatInt(s.UserID, 10) + "-" + strconv.FormatInt(s.LoginTime, 10)

fmt.Println("session key: ", accessToken)
fmt.Println("session value: ", sessionVal)

err := SetEx(accessToken, sessionVal, TTL_SESSION)
if err != nil {
fmt.Println("save token failed!")
return err
return "", err
}

return nil
return accessToken, nil
}

func (s *Session) Get(accessToken string) *Session {
Expand All @@ -43,6 +44,7 @@ func (s *Session) Get(accessToken string) *Session {

if len(arr) != 2 {
fmt.Println("invalid token!")
return nil
}

userID, _ := strconv.ParseInt(arr[0], 10, 64)
Expand Down
2 changes: 0 additions & 2 deletions components/validate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package components

import (
"fmt"
"regexp"
)

Expand Down Expand Up @@ -57,6 +56,5 @@ func (obj UserNameValidate) Validate(userName interface{}) bool {
result = reg.MatchString(userNameStr)
}

fmt.Println("user name is not ok!")
return result
}
Binary file modified dump.rdb
Binary file not shown.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func main() {
}

router := router.LoadRouters()

router.Run() // listen and serve on 0.0.0.0:8080
}
57 changes: 30 additions & 27 deletions middleware/CheckLogin.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
package middleware

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/timidsmile/pspace/components"
"net/http"
"github.com/timidsmile/pspace/service"
)

func CheckLogin(c *gin.Context) {
response := components.NewResponse()
defer c.JSON(http.StatusOK, response)

token, exist := c.GetPostForm("token")
if !exist {
response.Code = 1
response.Msg = "用户未登录!"
c.Abort()
return
}

// 验证token是否正确
userSession := components.Session{}
tokenInRedis := userSession.Get(token)

if tokenInRedis == nil {
response.Code = 2
response.Msg = "用户未登录!"
c.Abort()
return
if cookie, err := c.Request.Cookie("token"); err == nil {
token := cookie.Value

if token == "" {
response := components.NewResponse()
response.Code = 1
response.Msg = "用户未登录!"
c.JSON(http.StatusOK, response)

c.Abort()
return
}

userSession := components.Session{}
tokenInRedis := userSession.Get(token)
if tokenInRedis == nil {
response := components.NewResponse()
response.Code = 1
response.Msg = "用户未登录!"
c.JSON(http.StatusOK, response)

c.Abort()
return
}

userID := tokenInRedis.UserID
fmt.Println(userID)
c.Set("userID", userID)
}

// 用户已登陆
userID := tokenInRedis.UserID

userServ := service.UserBasicService{}
users := userServ.GetByUserID(userID)

response.Data = users
c.Next()

return
}
15 changes: 6 additions & 9 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
func LoadRouters() *gin.Engine {
router := gin.New()
router.Use(gin.Recovery())
router.Use(middleware.CheckLogin)

fmt.Println("after midware")

router.HTMLRender = pongo2gin.New(
pongo2gin.RenderOptions{
Expand All @@ -28,32 +25,32 @@ func LoadRouters() *gin.Engine {
fmt.Println(router.HTMLRender)

// 测试
testGroup := router.Group("/test")
testGroup := router.Group("/api/test")
{
testGroup.GET("/welcome", test.WelcomeAction)
testGroup.GET("/goodbye", test.GoodbyeAction)
testGroup.GET("/testdb", test.TestdbAction)
}

// 首页
indexGroup := router.Group("/")
indexGroup := router.Group("/api")
{
indexGroup.GET("/", index.IndexAction)
}

// session 模块
sessionsGroup := router.Group("/session")
sessionsGroup := router.Group("/api/session")
{
sessionsGroup.POST("/register", session.RegisterAction)
sessionsGroup.POST("/login", session.LoginAction)
sessionsGroup.POST("/checkLogin", session.CheckLoginAction)
}

// passport 模块
passportGroup := router.Group("/passport")
passportGroup := router.Group("/api/passport").Use(middleware.CheckLogin)
{
passportGroup.POST("/setting", passport.SettingAction)
passportGroup.POST("/getUserInfo", passport.GetUserInfoAction)
passportGroup.GET("/setting", passport.SettingAction)
passportGroup.GET("/getUserInfo", passport.GetUserInfoAction)
}

return router
Expand Down

0 comments on commit f45ed8b

Please sign in to comment.