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 a034f35 commit 147cb23
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
10 changes: 9 additions & 1 deletion controller/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/miaosha/redis"
"strconv"
)

func GetOrders(c *gin.Context) {
start := c.Query("start")
stop := c.Query("stop")
startNum, _ := strconv.ParseInt(start, 10, 64)
stopNum, _ := strconv.ParseInt(stop, 10, 64)
if stopNum == 0 {
stopNum = 10
}
redis.NewRedis()
list := redis.GetOrders()
list := redis.GetOrders(startNum, stopNum)
c.JSON(200, gin.H{
"code": 200,
"msg": "success",
Expand Down
3 changes: 2 additions & 1 deletion controller/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func GetProduct(c *gin.Context) {
saleTime, err := strconv.ParseInt(info["saletime"], 10, 64)
if err != nil || saleTime < now {
redis.SetProduct(id)
redis.OrderDel()
info = redis.ProductInfo(id)
}
c.JSON(200, gin.H{
Expand Down Expand Up @@ -67,7 +68,7 @@ func GetKillUrl(c *gin.Context) {
urlPath := fmt.Sprintf("product:%s,%d", id, time.Now().UnixNano())
token := utils.Md5(urlPath)
redis.SetStr(token, id, time.Second*10)
url := "/seckill/" + token
url := "seckill/" + token
c.JSON(200, gin.H{
"code": 200,
"msg": "success",
Expand Down
2 changes: 1 addition & 1 deletion front/static/js/config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var api="http://miaosha.sopans.com";
var api="/";
8 changes: 4 additions & 4 deletions front/static/js/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$(function () {
$.ajax({
type: "GET",
url: api+"/product?id=1",
url: api+"product?id=1",
success: function(res) {
var product=res.data.product;
$("#title").html(product.title);
Expand Down Expand Up @@ -69,7 +69,7 @@ $(function () {
$(".buyBtn a").addClass("btn-disabled");
$.ajax({
type: "GET",
url: api+"/buy?id=1&session="+localStorage.getItem("session"),
url: api+"buy?id=1&session="+localStorage.getItem("session"),
success: function(res) {
if(res.code==200){
url=res.data.url;
Expand All @@ -94,14 +94,14 @@ $(function () {
});
$.ajax({
type: "GET",
url: api+"/userinfo?session="+localStorage.getItem("session"),
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",
url: api + "userinfo",
data: {name:name},
success: function (res) {
if(res.code==200){
Expand Down
15 changes: 13 additions & 2 deletions redis/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func InsertOrder(user string, productId string) {
SortedSetAdd("orders", string(msg), float64(time.Now().Unix()))
SetOrderDetail(productId, user, order.Time)
}
func GetOrders() []*Order {
list := SortedSetList("orders", 0, 10)
func GetOrders(start int64, stop int64) []*Order {
list := SortedSetList("orders", start, stop)
rows := make([]*Order, 0)
for _, r := range list {
order := &Order{}
Expand All @@ -48,3 +48,14 @@ func OrderExist(user string) bool {
key := "order_detail:" + user
return HashExist(key, "ordertime")
}
func OrderDel() {
key := "orders"
itemKey := "order_detail:"
list := SortedSetList(key, 0, -1)
for _, r := range list {
order := &Order{}
json.Unmarshal([]byte(r), order)
DelKey(itemKey + order.User)
}
DelKey(key)
}

0 comments on commit 147cb23

Please sign in to comment.