Skip to content

Commit

Permalink
限制活动结束体哦阿健
Browse files Browse the repository at this point in the history
  • Loading branch information
paterleng committed Dec 19, 2024
1 parent 40e5fc0 commit ce1b184
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 28 deletions.
12 changes: 6 additions & 6 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions activity.log
Original file line number Diff line number Diff line change
Expand Up @@ -1304,3 +1304,21 @@
{"level":"INFO","time":"2024-12-19T14:25:01.712+0800","caller":"handle/create_shiled.go:26","msg":"初始化盾信息成功"}
{"level":"INFO","time":"2024-12-19T14:27:17.062+0800","caller":"handle/wshandle.go:95","msg":"用户0xb1b49f717045f0a1a667ccb56e5caaf47d16116f通过客户端主动关闭模块%!t(int=1)连接"}
{"level":"INFO","time":"2024-12-19T14:30:40.568+0800","caller":"handle/wshandle.go:95","msg":"用户0xb1b49f717045f0a1a667ccb56e5caaf47d16116f通过客户端主动关闭模块%!t(int=1)连接"}
{"level":"INFO","time":"2024-12-19T14:42:55.714+0800","caller":"utils/init.go:160","msg":"init logger success"}
{"level":"INFO","time":"2024-12-19T14:42:55.718+0800","caller":"utils/init.go:42","msg":"初始化logger成功"}
{"level":"INFO","time":"2024-12-19T14:42:56.165+0800","caller":"utils/init.go:47","msg":"初始化mysql成功"}
{"level":"INFO","time":"2024-12-19T14:43:01.635+0800","caller":"handle/create_shiled.go:26","msg":"初始化盾信息成功"}
{"level":"INFO","time":"2024-12-19T14:50:15.275+0800","caller":"handle/wshandle.go:95","msg":"用户0xb1b49f717045f0a1a667ccb56e5caaf47d16116f通过客户端主动关闭模块%!t(int=1)连接"}
{"level":"INFO","time":"2024-12-19T15:01:30.397+0800","caller":"utils/init.go:160","msg":"init logger success"}
{"level":"INFO","time":"2024-12-19T15:01:30.401+0800","caller":"utils/init.go:42","msg":"初始化logger成功"}
{"level":"INFO","time":"2024-12-19T15:01:30.838+0800","caller":"utils/init.go:47","msg":"初始化mysql成功"}
{"level":"INFO","time":"2024-12-19T15:01:35.524+0800","caller":"handle/create_shiled.go:26","msg":"初始化盾信息成功"}
{"level":"INFO","time":"2024-12-19T15:02:12.042+0800","caller":"utils/init.go:160","msg":"init logger success"}
{"level":"INFO","time":"2024-12-19T15:02:12.046+0800","caller":"utils/init.go:42","msg":"初始化logger成功"}
{"level":"INFO","time":"2024-12-19T15:02:12.452+0800","caller":"utils/init.go:47","msg":"初始化mysql成功"}
{"level":"INFO","time":"2024-12-19T15:02:17.909+0800","caller":"handle/create_shiled.go:26","msg":"初始化盾信息成功"}
{"level":"ERROR","time":"2024-12-19T15:02:55.279+0800","caller":"middleware/activity_end.go:20","msg":"活动结束"}
{"level":"INFO","time":"2024-12-19T15:07:37.899+0800","caller":"utils/init.go:160","msg":"init logger success"}
{"level":"INFO","time":"2024-12-19T15:07:37.903+0800","caller":"utils/init.go:42","msg":"初始化logger成功"}
{"level":"INFO","time":"2024-12-19T15:07:38.314+0800","caller":"utils/init.go:47","msg":"初始化mysql成功"}
{"level":"INFO","time":"2024-12-19T15:07:43.164+0800","caller":"handle/create_shiled.go:26","msg":"初始化盾信息成功"}
1 change: 1 addition & 0 deletions api/checkerboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type CheckerboardInterface interface {
GetCheckBoardInfo(c *gin.Context)
GetRecords(c *gin.Context)
AddShield(c *gin.Context)
GetAmountTotal(c *gin.Context)
}
5 changes: 3 additions & 2 deletions api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func ApiRoute(r *gin.Engine) {
api.GET("/records", GetApiManager().GetRecords)
//用户未登录时获取棋盘信息
api.GET("/boardInfo/:block_id", GetApiManager().GetBoardInfoNoLogin)
api.GET("/amount/total", GetApiManager().GetAmountTotal)
user := api.Group("/user")
user.Use(middleware.JWTAuthMiddleware())
{
Expand All @@ -34,13 +35,13 @@ func ApiRoute(r *gin.Engine) {
board.Use(middleware.JWTAuthMiddleware())
{
// 押注
board.POST("/betting", GetApiManager().UserBetting, middleware.ActivityEnd())
board.POST("/betting", GetApiManager().UserBetting)
// 获取用户的押注记录
board.GET("/record", GetApiManager().GetUserOperateRecords)
// 获取棋盘信息
board.GET("/boardInfo/:block_id", GetApiManager().GetCheckBoardInfo)
// 加盾
board.POST("/add_shield", GetApiManager().AddShield, middleware.ActivityEnd())
board.POST("/add_shield", GetApiManager().AddShield)
}
ws := api.Group("/ws")
{
Expand Down
2 changes: 2 additions & 0 deletions checkerboard/src/apis/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import instance from './api/';
export const loginUser = (loginData) => instance.post('login', loginData, { skipAuth: true });
// 用户信息
export const UserMessage = () => instance.get('user/userinfo');
// 获取总价值
export const GetAmountTotal = () => instance.get('amount/total');
// 获取棋盘信息,未登录状态下
export const BoardInfoNoLogin = (block_id) => instance.get(`boardInfo/${block_id}`,block_id, { skipAuth: true });
// 棋盘信息,登录状态下
Expand Down
17 changes: 15 additions & 2 deletions checkerboard/src/pages/home/Expand.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@ import RecordTable from '../../components/table/RecordTable';
import '../../components/chessboard/ChessBoard.css'
import './Expand.css'
import Header from '../../components/header/Header';
import Countdown from "../../components/Countdown.jsx";
import {useEffect, useState} from "react";
import {GetAmountTotal} from "../../apis/manage.js";

const Expand = () => {
const navigate = useNavigate();
const [total, setTotal] = useState(0.00);
useEffect(() => {
const getAmountTotal = async () => {
const response = await GetAmountTotal()
if (response.code === 200){
setTotal(response.data)
}
}
getAmountTotal()
}, []);
const handleClick = (index) => {
navigate(`/board/${index + 1}`);
};
Expand All @@ -26,6 +39,8 @@ const Expand = () => {
</div>
<div className="grid-style-external">
<div className="grid-style-internal">
<div>{total}</div>
<Countdown initialSeconds={ 3600} />
<div className='gridStyle'>
{Array(9).fill().map((_, index) => (
<div
Expand All @@ -39,8 +54,6 @@ const Expand = () => {
</div>
</div>
</div>


<div className='right-dev'>
<div className='record-table-dev'>
<RecordTable/>
Expand Down
39 changes: 37 additions & 2 deletions controller/checkerboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ type CheckerBoardController struct {

// UserBetting 押注
func (p *CheckerBoardController) UserBetting(c *gin.Context) {
//计算总金额,如果大于100万,就结束,阻止请求
amountTotal, err := utils.Amount()
if err != nil {
utils.Tools.LG.Error("计算总金额失败", zap.Error(err))
pkg.ResponseError(c, pkg.CodeActivityEnd)
return
}
if amountTotal >= 1000000 {
utils.Tools.LG.Error("活动结束", zap.Error(err))
pkg.ResponseError(c, pkg.CodeActivityEnd)
return
}
userId := c.GetString(pkg.USERID)
if userId == "" {
p.LG.Error("用户id在上下文中未找到")
Expand Down Expand Up @@ -181,7 +193,7 @@ func (p *CheckerBoardController) GetBoardInfoNoLogin(c *gin.Context) {
pkg.ResponseError(c, pkg.CodeServerBusy)
return
}
total, err := utils.Amonent()
total, err := utils.Amount()
if err != nil {
p.LG.Error("查询用户总金额失败", zap.Error(err))
pkg.ResponseError(c, pkg.CodeServerBusy)
Expand Down Expand Up @@ -259,7 +271,7 @@ func (p *CheckerBoardController) GetCheckBoardInfo(c *gin.Context) {
boardInfo[i].Status = pkg.HAVE
}
}
total, err := utils.Amonent()
total, err := utils.Amount()
if err != nil {
p.LG.Error("查询用户总金额失败", zap.Error(err))
pkg.ResponseError(c, pkg.CodeServerBusy)
Expand Down Expand Up @@ -296,6 +308,18 @@ func (p *CheckerBoardController) AddShield(c *gin.Context) {
5、更新用户盾数量
6、查询交易是否成功
*/
//计算总金额,如果大于100万,就结束,阻止请求
amountTotal, err := utils.Amount()
if err != nil {
utils.Tools.LG.Error("计算总金额失败", zap.Error(err))
pkg.ResponseError(c, pkg.CodeActivityEnd)
return
}
if amountTotal >= 1000000 {
utils.Tools.LG.Error("活动结束", zap.Error(err))
pkg.ResponseError(c, pkg.CodeActivityEnd)
return
}
var data model.AddShield
if err := c.ShouldBind(&data); err != nil {
p.LG.Error("参数错误:", zap.Error(err))
Expand Down Expand Up @@ -358,3 +382,14 @@ func (p *CheckerBoardController) AddShield(c *gin.Context) {
utils.ChMessage <- message
pkg.ResponseSuccess(c, pkg.CodeSuccess)
}

// GetAmountTotal 获取总价值
func (p *CheckerBoardController) GetAmountTotal(c *gin.Context) {
total, err := utils.Amount()
if err != nil {
p.LG.Error("获取总价值失败", zap.Error(err))
pkg.ResponseError(c, pkg.CodeServerBusy)
return
}
pkg.ResponseSuccess(c, total)
}
16 changes: 1 addition & 15 deletions middleware/activity_end.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
package middleware

import (
"activity/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

// 活动结束中间件
func ActivityEnd() func(c *gin.Context) {
return func(c *gin.Context) {
//计算总金额,如果大于100万,就结束,阻止请求
total, err := utils.Amonent()
if err != nil {
utils.Tools.LG.Error("计算总金额失败", zap.Error(err))
c.Abort()
return
}
if total >= 1000000 {
utils.Tools.LG.Error("活动结束", zap.Error(err))
c.Abort()
return
}
c.Next()

}
}
2 changes: 2 additions & 0 deletions pkg/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
CodeInsufficientBalance
CodeUpdateError
CodePriceError
CodeActivityEnd
)

var codeMsgMap = map[ResCode]string{
Expand All @@ -46,6 +47,7 @@ var codeMsgMap = map[ResCode]string{
CodeInsufficientBalance: "余额不足",
CodeUpdateError: "更新失败",
CodePriceError: "提交的价格过低",
CodeActivityEnd: "本期活动结束",
}

func (c ResCode) Msg() string {
Expand Down
2 changes: 1 addition & 1 deletion utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"activity/model"
)

func Amonent() (total float64, err error) {
func Amount() (total float64, err error) {
var boards []model.CheckerBoard
err = Tools.DB.Find(&boards).Error
if err != nil {
Expand Down

0 comments on commit ce1b184

Please sign in to comment.