Skip to content

Commit

Permalink
增加身份认证
Browse files Browse the repository at this point in the history
  • Loading branch information
taoshihan1991 committed Dec 8, 2020
1 parent 2579a96 commit e47f6da
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func initRouter(engine *gin.Engine) {
engine.GET("/buy", controller.GetKillUrl)
engine.GET("/timestamp", controller.GetTimestamp)
engine.GET("/orders", controller.GetOrders)
engine.GET("/userinfo", controller.GetUserInfo)
engine.POST("/userinfo", controller.PostUserInfo)
engine.GET("/seckill/:token", controller.PostSale)
}
func initBackendService() {
Expand Down
20 changes: 20 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controller

import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/miaosha/redis"
)

func checkUserLogin(c *gin.Context) (string, bool) {
session := c.Query("session")
info := redis.GetUserInfo(session)
if session == "" || info == "" {
c.JSON(200, gin.H{
"code": 400,
"msg": "error please login",
})
return "", false
}

return info, true
}
13 changes: 12 additions & 1 deletion controller/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ func GetProduct(c *gin.Context) {
}
func GetKillUrl(c *gin.Context) {
id := c.Query("id")
redis.NewRedis()

redis.NewRedis()
user, bool := checkUserLogin(c)
if !bool {
return
}
if redis.OrderExist(user) {
c.JSON(200, gin.H{
"code": 400,
"msg": "error Order exist",
})
return
}
product := redis.ProductInfo(id)
saletime, _ := strconv.Atoi(product["saletime"])
nowTime := time.Now().UnixNano() / 1e6
Expand Down
13 changes: 12 additions & 1 deletion controller/sale.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func PostSale(c *gin.Context) {
return
}
redis.NewRedis()
username, bool := checkUserLogin(c)
if !bool {
return
}
id := redis.GetStr(p.Token)
if id == "" {
c.JSON(200, gin.H{
Expand All @@ -41,7 +45,14 @@ func PostSale(c *gin.Context) {
})
return
}
redis.PushRequestQueue("taoshihan:" + id)
if redis.OrderExist(username) {
c.JSON(200, gin.H{
"code": 400,
"msg": "error Order exist",
})
return
}
redis.PushRequestQueue(username + ":" + id)

c.JSON(200, gin.H{
"code": 200,
Expand Down
46 changes: 46 additions & 0 deletions controller/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package controller

import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/miaosha/redis"
"github.com/taoshihan1991/miaosha/utils"
)

func GetUserInfo(c *gin.Context) {
session := c.Query("session")
redis.NewRedis()
info := redis.GetUserInfo(session)
if info == "" {
c.JSON(200, gin.H{
"code": 400,
"msg": "error please login",
})
return
}
c.JSON(200, gin.H{
"code": 200,
"msg": "success",
"data": gin.H{
"username": info,
},
})
}
func PostUserInfo(c *gin.Context) {
name := c.PostForm("name")
session := utils.Md5(name)
redis.NewRedis()
info := redis.GetUserInfo(session)
if info != "" {
c.JSON(200, gin.H{
"code": 400,
"msg": "error username exist",
})
return
}
redis.SetUserInfo(session, name)
c.JSON(200, gin.H{
"code": 200,
"msg": "success",
"data": session,
})
}
2 changes: 2 additions & 0 deletions front/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<span class="time-item s" id="s">00</span>
</div>
<div class="buyBtn"><a href="javascript:void(0)" class="btn btn-disabled">立即抢购</a></div>
<br/>
<div><a href="/orders" target="_blank" class="btn btn-primary">查看订单</a></div>
</div>
</body>
</html>
30 changes: 28 additions & 2 deletions front/static/js/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ $(function () {
$(".buyBtn a").addClass("btn-disabled");
$.ajax({
type: "GET",
url: api+"/buy?id=1",
url: api+"/buy?id=1&session="+localStorage.getItem("session"),
success: function(res) {
if(res.code==200){
url=res.data.url;
$.ajax({
type: "GET",
url: api+url,
url: api+url+"?session="+localStorage.getItem("session"),
success: function(res) {
$(".buyBtn a").removeClass("btn-disabled");
$(".buyBtn a").addClass("btn-primary");
Expand All @@ -92,4 +92,30 @@ $(function () {
}
});
});
$.ajax({
type: "GET",
url: api+"/userinfo?session="+localStorage.getItem("session"),
success: function(res) {
if(res.code!=200){
var name=prompt(res.msg);
if(name!=null&&name!=""){
$.ajax({
type: "POST",
url: api + "/userinfo",
data: {name:name},
success: function (res) {
if(res.code==200){
localStorage.setItem("session", res.data);
}else{
alert(res.msg);
}
}
});
}else{
alert("not null")
window.location.reload();
}
}
}
});
})
12 changes: 12 additions & 0 deletions redis/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package redis

import "time"

func GetUserInfo(session string) string {
key := "session_" + session
return GetStr(key)
}
func SetUserInfo(session string, str string) string {
key := "session_" + session
return SetStr(key, str, 30*time.Minute)
}

0 comments on commit e47f6da

Please sign in to comment.