Skip to content

Commit ccd014b

Browse files
committed
优化
1 parent 22f0db4 commit ccd014b

30 files changed

+1936
-1442
lines changed

Makefile

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1-
.PHONY: build run test clean docker-build docker-run dev
1+
.PHONY: build run test clean docker-build docker-run dev release
2+
3+
# 版本信息
4+
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
5+
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
6+
DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
7+
GO_VERSION := $(shell go version | awk '{print $$3}')
8+
9+
# 构建标志
10+
LDFLAGS := -ldflags "\
11+
-X main.version=$(VERSION) \
12+
-X main.commit=$(COMMIT) \
13+
-X main.date=$(DATE) \
14+
-w -s"
215

316
# 默认目标
417
all: build
518

6-
# 编译项目
19+
# 编译项目(带版本信息)
720
build:
8-
go build -o filecodebox .
21+
@echo "Building FileCodeBox $(VERSION) ($(COMMIT)) at $(DATE)"
22+
go build $(LDFLAGS) -o filecodebox .
23+
24+
# 发布构建(优化编译)
25+
release:
26+
@echo "Building FileCodeBox release $(VERSION) ($(COMMIT)) at $(DATE)"
27+
CGO_ENABLED=0 go build $(LDFLAGS) -a -installsuffix cgo -o filecodebox .
928

1029
# 运行项目
1130
run: build
1231
./filecodebox
1332

33+
# 显示版本信息
34+
version:
35+
@echo "FileCodeBox $(VERSION)"
36+
@echo "Commit: $(COMMIT)"
37+
@echo "Built: $(DATE)"
38+
@echo "Go Version: $(GO_VERSION)"
39+
1440
# 运行测试
1541
test:
1642
go test ./... -v
@@ -38,7 +64,7 @@ vet:
3864

3965
# 构建Docker镜像
4066
docker-build:
41-
docker build -t filecodebox-go .
67+
docker build --build-arg VERSION=$(VERSION) --build-arg COMMIT=$(COMMIT) --build-arg DATE=$(DATE) -t filecodebox-go .
4268

4369
# 运行Docker容器
4470
docker-run: docker-build
@@ -61,13 +87,15 @@ deps:
6187

6288
# 生成API文档
6389
docs:
64-
@echo "API文档可通过 http://localhost:12345/api/doc 访问"
90+
@echo "API文档可通过 http://localhost:12345/swagger/index.html 访问"
6591

6692
# 查看帮助
6793
help:
6894
@echo "可用的make命令:"
69-
@echo " build - 编译项目"
95+
@echo " build - 编译项目(带版本信息)"
96+
@echo " release - 发布构建(优化编译)"
7097
@echo " run - 编译并运行项目"
98+
@echo " version - 显示版本信息"
7199
@echo " test - 运行测试"
72100
@echo " dev - 开发模式(需要安装air)"
73101
@echo " clean - 清理编译文件"

go.mod

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ require (
1313
github.com/robfig/cron/v3 v3.0.1
1414
github.com/sirupsen/logrus v1.9.3
1515
github.com/studio-b12/gowebdav v0.10.0
16+
github.com/swaggo/files v1.0.1
17+
github.com/swaggo/gin-swagger v1.6.0
18+
github.com/swaggo/swag v1.16.6
19+
golang.org/x/crypto v0.41.0
1620
golang.org/x/time v0.12.0
1721
gorm.io/driver/sqlite v1.6.0
1822
gorm.io/gorm v1.30.2
1923
)
2024

2125
require (
2226
github.com/KyleBanks/depth v1.2.1 // indirect
23-
github.com/PuerkitoBio/purell v1.2.1 // indirect
24-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
2527
github.com/bytedance/sonic v1.14.0 // indirect
2628
github.com/bytedance/sonic/loader v0.3.0 // indirect
2729
github.com/cloudwego/base64x v0.1.6 // indirect
28-
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
2930
github.com/gabriel-vasile/mimetype v1.4.10 // indirect
3031
github.com/gin-contrib/sse v1.1.0 // indirect
3132
github.com/go-openapi/jsonpointer v0.22.0 // indirect
@@ -53,34 +54,22 @@ require (
5354
github.com/josharian/intern v1.0.0 // indirect
5455
github.com/json-iterator/go v1.1.12 // indirect
5556
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
56-
github.com/kr/text v0.2.0 // indirect
5757
github.com/leodido/go-urn v1.4.0 // indirect
5858
github.com/mailru/easyjson v0.9.0 // indirect
5959
github.com/mattn/go-isatty v0.0.20 // indirect
6060
github.com/mattn/go-sqlite3 v1.14.32 // indirect
6161
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6262
github.com/modern-go/reflect2 v1.0.2 // indirect
6363
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
64-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
65-
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
66-
github.com/swaggo/files v1.0.1 // indirect
67-
github.com/swaggo/gin-swagger v1.6.0 // indirect
68-
github.com/swaggo/swag v1.16.6 // indirect
6964
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
7065
github.com/ugorji/go/codec v1.3.0 // indirect
71-
github.com/urfave/cli/v2 v2.27.7 // indirect
72-
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect
73-
go.yaml.in/yaml/v2 v2.4.2 // indirect
7466
golang.org/x/arch v0.20.0 // indirect
75-
golang.org/x/crypto v0.41.0 // indirect
7667
golang.org/x/mod v0.27.0 // indirect
7768
golang.org/x/net v0.43.0 // indirect
7869
golang.org/x/sync v0.16.0 // indirect
7970
golang.org/x/sys v0.35.0 // indirect
8071
golang.org/x/text v0.28.0 // indirect
8172
golang.org/x/tools v0.36.0 // indirect
8273
google.golang.org/protobuf v1.36.8 // indirect
83-
gopkg.in/yaml.v2 v2.4.0 // indirect
8474
gopkg.in/yaml.v3 v3.0.1 // indirect
85-
sigs.k8s.io/yaml v1.6.0 // indirect
8675
)

go.sum

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
22
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
3-
github.com/PuerkitoBio/purell v1.2.1 h1:QsZ4TjvwiMpat6gBCBxEQI0rcS9ehtkKtSpiUnd9N28=
4-
github.com/PuerkitoBio/purell v1.2.1/go.mod h1:ZwHcC/82TOaovDi//J/804umJFFmbOHPngi8iYYv/Eo=
5-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
6-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
73
github.com/aws/aws-sdk-go v1.55.8 h1:JRmEUbU52aJQZ2AjX4q4Wu7t4uZjOu71uyNmaWlUkJQ=
84
github.com/aws/aws-sdk-go v1.55.8/go.mod h1:ZkViS9AqA6otK+JBBNH2++sx1sgxrPKcSzPPvQkUtXk=
95
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
@@ -12,16 +8,15 @@ github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZw
128
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
139
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
1410
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
15-
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
16-
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
17-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1811
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1912
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2013
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2114
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
2215
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
2316
github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQY=
2417
github.com/gin-contrib/cors v1.7.6/go.mod h1:Ulcl+xN4jel9t1Ry8vqph23a60FwH9xVLd+3ykmTjOk=
18+
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
19+
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
2520
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
2621
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
2722
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
@@ -87,8 +82,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
8782
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
8883
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
8984
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
90-
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
91-
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
85+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
86+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
9287
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
9388
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
9489
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
@@ -110,13 +105,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
110105
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
111106
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
112107
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
113-
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
114-
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
115108
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
116-
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
117-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
118-
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
119-
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
109+
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
120110
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
121111
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
122112
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -127,9 +117,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
127117
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
128118
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
129119
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
130-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
131-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
132120
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
121+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
133122
github.com/studio-b12/gowebdav v0.10.0 h1:Yewz8FFiadcGEu4hxS/AAJQlHelndqln1bns3hcJIYc=
134123
github.com/studio-b12/gowebdav v0.10.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
135124
github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE=
@@ -142,13 +131,7 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
142131
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
143132
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
144133
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
145-
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
146-
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
147-
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg=
148-
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
149134
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
150-
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
151-
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
152135
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
153136
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
154137
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -200,7 +183,6 @@ google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXn
200183
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
201184
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
202185
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
203-
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
204186
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
205187
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
206188
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
@@ -211,5 +193,3 @@ gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
211193
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
212194
gorm.io/gorm v1.30.2 h1:f7bevlVoVe4Byu3pmbWPVHnPsLoWaMjEb7/clyr9Ivs=
213195
gorm.io/gorm v1.30.2/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
214-
sigs.k8s.io/yaml v1.6.0 h1:G8fkbMSAFqgEFgh4b1wmtzDnioxFCUgTZhlbj5P9QYs=
215-
sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4=

internal/common/request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package common

internal/common/response.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package common
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
// Response 通用响应结构
10+
type Response struct {
11+
Code int `json:"code"`
12+
Message string `json:"message"`
13+
Detail interface{} `json:"detail,omitempty"`
14+
}
15+
16+
// SuccessResponse 成功响应
17+
func SuccessResponse(c *gin.Context, detail interface{}) {
18+
c.JSON(http.StatusOK, Response{
19+
Code: 200,
20+
Message: "success",
21+
Detail: detail,
22+
})
23+
}
24+
25+
// SuccessWithMessage 带自定义消息的成功响应
26+
func SuccessWithMessage(c *gin.Context, message string, detail map[string]interface{}) {
27+
c.JSON(http.StatusOK, Response{
28+
Code: 200,
29+
Message: message,
30+
Detail: detail,
31+
})
32+
}
33+
34+
// ErrorResponse 错误响应
35+
func ErrorResponse(c *gin.Context, code int, message string) {
36+
c.JSON(code, Response{
37+
Code: code,
38+
Message: message,
39+
})
40+
}
41+
42+
// BadRequestResponse 400 错误响应
43+
func BadRequestResponse(c *gin.Context, message string) {
44+
c.JSON(http.StatusBadRequest, Response{
45+
Code: 400,
46+
Message: message,
47+
})
48+
}
49+
50+
// UnauthorizedResponse 401 未授权响应
51+
func UnauthorizedResponse(c *gin.Context, message string) {
52+
c.JSON(http.StatusUnauthorized, Response{
53+
Code: 401,
54+
Message: message,
55+
})
56+
}
57+
58+
// ForbiddenResponse 403 禁止访问响应
59+
func ForbiddenResponse(c *gin.Context, message string) {
60+
c.JSON(http.StatusForbidden, Response{
61+
Code: 403,
62+
Message: message,
63+
})
64+
}
65+
66+
// NotFoundResponse 404 未找到响应
67+
func NotFoundResponse(c *gin.Context, message string) {
68+
c.JSON(http.StatusNotFound, Response{
69+
Code: 404,
70+
Message: message,
71+
})
72+
}
73+
74+
// InternalServerErrorResponse 500 服务器内部错误响应
75+
func InternalServerErrorResponse(c *gin.Context, message string) {
76+
c.JSON(http.StatusInternalServerError, Response{
77+
Code: 500,
78+
Message: message,
79+
})
80+
}
81+
82+
// SuccessWithCleanedCount 带清理计数的成功响应
83+
func SuccessWithCleanedCount(c *gin.Context, count int) {
84+
SuccessResponse(c, map[string]interface{}{
85+
"cleaned_count": count,
86+
})
87+
}
88+
89+
// SuccessWithFileInfo 带文件信息的成功响应
90+
func SuccessWithFileInfo(c *gin.Context, fileInfo interface{}) {
91+
SuccessResponse(c, fileInfo)
92+
}
93+
94+
// SuccessWithList 带列表数据的成功响应
95+
func SuccessWithList(c *gin.Context, list interface{}, total int) {
96+
SuccessResponse(c, map[string]interface{}{
97+
"list": list,
98+
"total": total,
99+
})
100+
}
101+
102+
// SuccessWithPagination 带分页数据的成功响应
103+
func SuccessWithPagination(c *gin.Context, list interface{}, total int, page int, pageSize int) {
104+
SuccessResponse(c, map[string]interface{}{
105+
"list": list,
106+
"total": total,
107+
"page": page,
108+
"page_size": pageSize,
109+
"pages": (total + pageSize - 1) / pageSize,
110+
})
111+
}
112+
113+
// SuccessWithToken 带令牌的成功响应
114+
func SuccessWithToken(c *gin.Context, token string, userInfo interface{}) {
115+
SuccessResponse(c, map[string]interface{}{
116+
"token": token,
117+
"user_info": userInfo,
118+
})
119+
}
120+
121+
// SuccessWithUploadInfo 带上传信息的成功响应
122+
func SuccessWithUploadInfo(c *gin.Context, shareCode string, downloadLink string) {
123+
SuccessResponse(c, map[string]interface{}{
124+
"share_code": shareCode,
125+
"download_link": downloadLink,
126+
})
127+
}

0 commit comments

Comments
 (0)