chore: sync
This commit is contained in:
6
build-frontend.bat
Normal file
6
build-frontend.bat
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
cd /d "%~dp0"
|
||||||
|
cd mengyastore-frontend
|
||||||
|
npm run build
|
||||||
|
endlocal
|
||||||
4
build-frontend.sh
Normal file
4
build-frontend.sh
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
cd mengyastore-frontend
|
||||||
|
npm run build
|
||||||
10
dev.bat
Normal file
10
dev.bat
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
cd /d "%~dp0"
|
||||||
|
|
||||||
|
rem Backend (new window)
|
||||||
|
start "mengyastore-backend" cmd /k "cd /d ""%~dp0mengyastore-backend"" && go run ."
|
||||||
|
|
||||||
|
rem Frontend (new window)
|
||||||
|
start "mengyastore-frontend" cmd /k "cd /d ""%~dp0mengyastore-frontend"" && npm run dev"
|
||||||
|
endlocal
|
||||||
15
dev.sh
Normal file
15
dev.sh
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
if [ -n "${BACK_PID:-}" ]; then
|
||||||
|
kill "$BACK_PID" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
(cd mengyastore-backend && go run .) &
|
||||||
|
BACK_PID=$!
|
||||||
|
|
||||||
|
cd mengyastore-frontend
|
||||||
|
npm run dev
|
||||||
68
mengyastore-backend/data/json/products.json
Normal file
68
mengyastore-backend/data/json/products.json
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "seed-1",
|
||||||
|
"name": "Linux Do 邀请码",
|
||||||
|
"price": 7,
|
||||||
|
"quantity": 10,
|
||||||
|
"coverUrl": "https://img.shumengya.top/i/2026/01/04/695a55058c37f.png",
|
||||||
|
"description": "Linux.do论坛邀请码 默认每天可以生成一个,先到先得.",
|
||||||
|
"active": true,
|
||||||
|
"createdAt": "2026-03-15T10:00:00+08:00",
|
||||||
|
"updatedAt": "2026-03-18T22:04:26.9875796+08:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "seed-2",
|
||||||
|
"name": "ChatGPT普号",
|
||||||
|
"price": 1,
|
||||||
|
"quantity": 20,
|
||||||
|
"coverUrl": "https://img.shumengya.top/i/2026/01/04/695a55058c37f.png",
|
||||||
|
"description": "ChatGPT 普号 纯手工注册 数量不多",
|
||||||
|
"active": true,
|
||||||
|
"createdAt": "2026-03-15T10:05:00+08:00",
|
||||||
|
"updatedAt": "2026-03-18T22:04:44.9242847+08:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "2b6b6051-bca7-42da-b127-c7b721c50c06",
|
||||||
|
"name": "谷歌账号",
|
||||||
|
"price": 20,
|
||||||
|
"quantity": 8,
|
||||||
|
"coverUrl": "https://img.shumengya.top/i/2026/01/04/695a55058c37f.png",
|
||||||
|
"description": "谷歌账号 现货 可绑定F2A验证",
|
||||||
|
"active": true,
|
||||||
|
"createdAt": "2026-03-15T20:52:52.0381722+08:00",
|
||||||
|
"updatedAt": "2026-03-18T21:55:26.3289587+08:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "b9922892-c197-44be-be87-637ccb6bebeb",
|
||||||
|
"name": "萌芽币",
|
||||||
|
"price": 999999,
|
||||||
|
"quantity": 888,
|
||||||
|
"coverUrl": "https://img.shumengya.top/i/2026/01/04/695a55058c37f.png",
|
||||||
|
"description": "非买品 仅展示",
|
||||||
|
"active": true,
|
||||||
|
"createdAt": "2026-03-15T21:03:00.0164528+08:00",
|
||||||
|
"updatedAt": "2026-03-18T22:04:48.6124945+08:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ee8e0140-221c-4bfa-b10a-13b1f98ea4e5",
|
||||||
|
"name": "Keep校园跑 代刷4公里",
|
||||||
|
"price": 1,
|
||||||
|
"quantity": 999,
|
||||||
|
"coverUrl": "https://img.shumengya.top/i/2026/01/04/695a55058c37f.png",
|
||||||
|
"description": "keep校园跑带刷 每天4-5公里 下单后直接联系我发账号",
|
||||||
|
"active": true,
|
||||||
|
"createdAt": "2026-03-15T21:06:11.9820102+08:00",
|
||||||
|
"updatedAt": "2026-03-18T22:04:53.0357081+08:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "00bbf5db-b99e-4e88-a8ee-e7747b5969fe",
|
||||||
|
"name": "学习通/慕课挂科脚本",
|
||||||
|
"price": 25,
|
||||||
|
"quantity": 0,
|
||||||
|
"coverUrl": "https://img.shumengya.top/i/2026/01/04/695a55058c37f.png",
|
||||||
|
"description": "学习通,慕课挂科脚本 手机 电脑都可以挂 不会弄可联系教你",
|
||||||
|
"active": true,
|
||||||
|
"createdAt": "2026-03-15T21:06:45.3807471+08:00",
|
||||||
|
"updatedAt": "2026-03-18T22:04:56.3154497+08:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
3
mengyastore-backend/data/json/settings.json
Normal file
3
mengyastore-backend/data/json/settings.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"adminToken": "shumengya520"
|
||||||
|
}
|
||||||
39
mengyastore-backend/go.mod
Normal file
39
mengyastore-backend/go.mod
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
module mengyastore-backend
|
||||||
|
|
||||||
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gin-contrib/cors v1.7.2
|
||||||
|
github.com/gin-gonic/gin v1.9.1
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||||
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
|
github.com/kr/text v0.2.0 // indirect
|
||||||
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
|
golang.org/x/arch v0.7.0 // indirect
|
||||||
|
golang.org/x/crypto v0.22.0 // indirect
|
||||||
|
golang.org/x/net v0.24.0 // indirect
|
||||||
|
golang.org/x/sys v0.19.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.34.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
101
mengyastore-backend/go.sum
Normal file
101
mengyastore-backend/go.sum
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||||
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
|
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||||
|
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||||
|
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||||
|
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
|
||||||
|
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
||||||
|
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
|
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||||
|
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
|
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||||
|
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||||
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
|
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
|
||||||
|
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||||
|
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||||
|
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||||
|
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||||
|
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
|
||||||
|
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||||
26
mengyastore-backend/internal/config/config.go
Normal file
26
mengyastore-backend/internal/config/config.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
AdminToken string `json:"adminToken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(path string) (*Config, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read config: %w", err)
|
||||||
|
}
|
||||||
|
var cfg Config
|
||||||
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||||
|
return nil, fmt.Errorf("parse config: %w", err)
|
||||||
|
}
|
||||||
|
if cfg.AdminToken == "" {
|
||||||
|
cfg.AdminToken = "shumengya520"
|
||||||
|
}
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
179
mengyastore-backend/internal/handlers/admin.go
Normal file
179
mengyastore-backend/internal/handlers/admin.go
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"mengyastore-backend/internal/config"
|
||||||
|
"mengyastore-backend/internal/models"
|
||||||
|
"mengyastore-backend/internal/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AdminHandler struct {
|
||||||
|
store *storage.JSONStore
|
||||||
|
cfg *config.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
type productPayload struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
Quantity int `json:"quantity"`
|
||||||
|
CoverURL string `json:"coverUrl"`
|
||||||
|
ScreenshotURLs []string `json:"screenshotUrls"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Active *bool `json:"active"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type togglePayload struct {
|
||||||
|
Active bool `json:"active"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAdminHandler(store *storage.JSONStore, cfg *config.Config) *AdminHandler {
|
||||||
|
return &AdminHandler{store: store, cfg: cfg}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) GetAdminToken(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"token": h.cfg.AdminToken})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) ListAllProducts(c *gin.Context) {
|
||||||
|
if !h.requireAdmin(c) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
items, err := h.store.ListAll()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": items})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) CreateProduct(c *gin.Context) {
|
||||||
|
if !h.requireAdmin(c) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var payload productPayload
|
||||||
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
screenshotURLs, valid := normalizeScreenshotURLs(payload.ScreenshotURLs)
|
||||||
|
if !valid {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "screenshot urls must be 10 or fewer"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
active := true
|
||||||
|
if payload.Active != nil {
|
||||||
|
active = *payload.Active
|
||||||
|
}
|
||||||
|
product := models.Product{
|
||||||
|
Name: payload.Name,
|
||||||
|
Price: payload.Price,
|
||||||
|
Quantity: payload.Quantity,
|
||||||
|
CoverURL: strings.TrimSpace(payload.CoverURL),
|
||||||
|
ScreenshotURLs: screenshotURLs,
|
||||||
|
Description: payload.Description,
|
||||||
|
Active: active,
|
||||||
|
}
|
||||||
|
created, err := h.store.Create(product)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": created})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) UpdateProduct(c *gin.Context) {
|
||||||
|
if !h.requireAdmin(c) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id := c.Param("id")
|
||||||
|
var payload productPayload
|
||||||
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
screenshotURLs, valid := normalizeScreenshotURLs(payload.ScreenshotURLs)
|
||||||
|
if !valid {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "screenshot urls must be 10 or fewer"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
active := false
|
||||||
|
if payload.Active != nil {
|
||||||
|
active = *payload.Active
|
||||||
|
}
|
||||||
|
patch := models.Product{
|
||||||
|
Name: payload.Name,
|
||||||
|
Price: payload.Price,
|
||||||
|
Quantity: payload.Quantity,
|
||||||
|
CoverURL: strings.TrimSpace(payload.CoverURL),
|
||||||
|
ScreenshotURLs: screenshotURLs,
|
||||||
|
Description: payload.Description,
|
||||||
|
Active: active,
|
||||||
|
}
|
||||||
|
updated, err := h.store.Update(id, patch)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": updated})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) ToggleProduct(c *gin.Context) {
|
||||||
|
if !h.requireAdmin(c) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id := c.Param("id")
|
||||||
|
var payload togglePayload
|
||||||
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
updated, err := h.store.Toggle(id, payload.Active)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": updated})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) DeleteProduct(c *gin.Context) {
|
||||||
|
if !h.requireAdmin(c) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id := c.Param("id")
|
||||||
|
if err := h.store.Delete(id); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) requireAdmin(c *gin.Context) bool {
|
||||||
|
token := c.Query("token")
|
||||||
|
if token == "" {
|
||||||
|
token = c.GetHeader("Authorization")
|
||||||
|
}
|
||||||
|
if token == h.cfg.AdminToken {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeScreenshotURLs(urls []string) ([]string, bool) {
|
||||||
|
cleaned := make([]string, 0, len(urls))
|
||||||
|
for _, url := range urls {
|
||||||
|
trimmed := strings.TrimSpace(url)
|
||||||
|
if trimmed == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cleaned = append(cleaned, trimmed)
|
||||||
|
if len(cleaned) > 10 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cleaned, true
|
||||||
|
}
|
||||||
26
mengyastore-backend/internal/handlers/public.go
Normal file
26
mengyastore-backend/internal/handlers/public.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"mengyastore-backend/internal/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PublicHandler struct {
|
||||||
|
store *storage.JSONStore
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPublicHandler(store *storage.JSONStore) *PublicHandler {
|
||||||
|
return &PublicHandler{store: store}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *PublicHandler) ListProducts(c *gin.Context) {
|
||||||
|
items, err := h.store.ListActive()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": items})
|
||||||
|
}
|
||||||
16
mengyastore-backend/internal/models/product.go
Normal file
16
mengyastore-backend/internal/models/product.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Product struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
Quantity int `json:"quantity"`
|
||||||
|
CoverURL string `json:"coverUrl"`
|
||||||
|
ScreenshotURLs []string `json:"screenshotUrls"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Active bool `json:"active"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
|
}
|
||||||
192
mengyastore-backend/internal/storage/jsonstore.go
Normal file
192
mengyastore-backend/internal/storage/jsonstore.go
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
|
||||||
|
"mengyastore-backend/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JSONStore struct {
|
||||||
|
path string
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewJSONStore(path string) (*JSONStore, error) {
|
||||||
|
if err := ensureFile(path); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &JSONStore{path: path}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureFile(path string) error {
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
return fmt.Errorf("mkdir data dir: %w", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
return nil
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("stat data file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
initial := []models.Product{}
|
||||||
|
bytes, err := json.MarshalIndent(initial, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("init json: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, bytes, 0o644); err != nil {
|
||||||
|
return fmt.Errorf("write init json: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) ListAll() ([]models.Product, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
return s.readAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) ListActive() ([]models.Product, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
items, err := s.readAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
active := make([]models.Product, 0, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
if item.Active {
|
||||||
|
active = append(active, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return active, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) Create(p models.Product) (models.Product, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
items, err := s.readAll()
|
||||||
|
if err != nil {
|
||||||
|
return models.Product{}, err
|
||||||
|
}
|
||||||
|
p = normalizeProduct(p)
|
||||||
|
p.ID = uuid.NewString()
|
||||||
|
now := time.Now()
|
||||||
|
p.CreatedAt = now
|
||||||
|
p.UpdatedAt = now
|
||||||
|
items = append(items, p)
|
||||||
|
if err := s.writeAll(items); err != nil {
|
||||||
|
return models.Product{}, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) Update(id string, patch models.Product) (models.Product, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
items, err := s.readAll()
|
||||||
|
if err != nil {
|
||||||
|
return models.Product{}, err
|
||||||
|
}
|
||||||
|
for i, item := range items {
|
||||||
|
if item.ID == id {
|
||||||
|
item.Name = patch.Name
|
||||||
|
item.Price = patch.Price
|
||||||
|
item.Quantity = patch.Quantity
|
||||||
|
item.CoverURL = patch.CoverURL
|
||||||
|
item.ScreenshotURLs = normalizeProduct(patch).ScreenshotURLs
|
||||||
|
item.Description = patch.Description
|
||||||
|
item.Active = patch.Active
|
||||||
|
item.UpdatedAt = time.Now()
|
||||||
|
items[i] = item
|
||||||
|
if err := s.writeAll(items); err != nil {
|
||||||
|
return models.Product{}, err
|
||||||
|
}
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return models.Product{}, fmt.Errorf("product not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) Toggle(id string, active bool) (models.Product, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
items, err := s.readAll()
|
||||||
|
if err != nil {
|
||||||
|
return models.Product{}, err
|
||||||
|
}
|
||||||
|
for i, item := range items {
|
||||||
|
if item.ID == id {
|
||||||
|
item.Active = active
|
||||||
|
item.UpdatedAt = time.Now()
|
||||||
|
items[i] = item
|
||||||
|
if err := s.writeAll(items); err != nil {
|
||||||
|
return models.Product{}, err
|
||||||
|
}
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return models.Product{}, fmt.Errorf("product not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) Delete(id string) error {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
items, err := s.readAll()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
filtered := make([]models.Product, 0, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
if item.ID != id {
|
||||||
|
filtered = append(filtered, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := s.writeAll(filtered); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) readAll() ([]models.Product, error) {
|
||||||
|
bytes, err := os.ReadFile(s.path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read products: %w", err)
|
||||||
|
}
|
||||||
|
var items []models.Product
|
||||||
|
if err := json.Unmarshal(bytes, &items); err != nil {
|
||||||
|
return nil, fmt.Errorf("parse products: %w", err)
|
||||||
|
}
|
||||||
|
for i, item := range items {
|
||||||
|
items[i] = normalizeProduct(item)
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *JSONStore) writeAll(items []models.Product) error {
|
||||||
|
for i, item := range items {
|
||||||
|
items[i] = normalizeProduct(item)
|
||||||
|
}
|
||||||
|
bytes, err := json.MarshalIndent(items, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("encode products: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(s.path, bytes, 0o644); err != nil {
|
||||||
|
return fmt.Errorf("write products: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeProduct(item models.Product) models.Product {
|
||||||
|
if item.ScreenshotURLs == nil {
|
||||||
|
item.ScreenshotURLs = []string{}
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
}
|
||||||
57
mengyastore-backend/main.go
Normal file
57
mengyastore-backend/main.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/cors"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"mengyastore-backend/internal/config"
|
||||||
|
"mengyastore-backend/internal/handlers"
|
||||||
|
"mengyastore-backend/internal/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cfg, err := config.Load("data/json/settings.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("load config failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
store, err := storage.NewJSONStore("data/json/products.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("init store failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := gin.Default()
|
||||||
|
r.Use(cors.New(cors.Config{
|
||||||
|
AllowOrigins: []string{"*"},
|
||||||
|
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
||||||
|
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
|
||||||
|
ExposeHeaders: []string{"Content-Length"},
|
||||||
|
AllowCredentials: false,
|
||||||
|
MaxAge: 12 * time.Hour,
|
||||||
|
}))
|
||||||
|
|
||||||
|
r.GET("/api/health", func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
|
})
|
||||||
|
|
||||||
|
publicHandler := handlers.NewPublicHandler(store)
|
||||||
|
adminHandler := handlers.NewAdminHandler(store, cfg)
|
||||||
|
|
||||||
|
r.GET("/api/products", publicHandler.ListProducts)
|
||||||
|
|
||||||
|
r.GET("/api/admin/token", adminHandler.GetAdminToken)
|
||||||
|
r.GET("/api/admin/products", adminHandler.ListAllProducts)
|
||||||
|
r.POST("/api/admin/products", adminHandler.CreateProduct)
|
||||||
|
r.PUT("/api/admin/products/:id", adminHandler.UpdateProduct)
|
||||||
|
r.PATCH("/api/admin/products/:id/status", adminHandler.ToggleProduct)
|
||||||
|
r.DELETE("/api/admin/products/:id", adminHandler.DeleteProduct)
|
||||||
|
|
||||||
|
log.Println("萌芽小店后端启动于 http://localhost:8080")
|
||||||
|
if err := r.Run(":8080"); err != nil {
|
||||||
|
log.Fatalf("server run failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
mengyastore-frontend/favicon.ico
Normal file
BIN
mengyastore-frontend/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 100 KiB |
12
mengyastore-frontend/index.html
Normal file
12
mengyastore-frontend/index.html
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>萌芽小店</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script type="module" src="/src/main.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
mengyastore-frontend/logo.png
Normal file
BIN
mengyastore-frontend/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.3 MiB |
1624
mengyastore-frontend/package-lock.json
generated
Normal file
1624
mengyastore-frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
22
mengyastore-frontend/package.json
Normal file
22
mengyastore-frontend/package.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "mengyastore-frontend",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.1.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^1.6.8",
|
||||||
|
"markdown-it": "^14.1.0",
|
||||||
|
"pinia": "^2.1.7",
|
||||||
|
"vue": "^3.4.21",
|
||||||
|
"vue-router": "^4.3.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@vitejs/plugin-vue": "^5.0.4",
|
||||||
|
"vite": "^5.2.7"
|
||||||
|
}
|
||||||
|
}
|
||||||
45
mengyastore-frontend/src/App.vue
Normal file
45
mengyastore-frontend/src/App.vue
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<template>
|
||||||
|
<div class="app">
|
||||||
|
<header class="top-bar">
|
||||||
|
<div class="brand">
|
||||||
|
<div class="brand-mark"></div>
|
||||||
|
<div>
|
||||||
|
<h1>萌芽小店</h1>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="top-actions">
|
||||||
|
<button class="ghost" @click="goHome">商店</button>
|
||||||
|
<button class="primary" @click="goAdmin">管理员后台</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main class="main-content">
|
||||||
|
<router-view />
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer class="footer">
|
||||||
|
<span>萌芽小店 · @2025-2026</span>
|
||||||
|
<a class="footer-mail" href="mailto:mail@smyhub.com">邮箱售后联系: mail@smyhub.com</a>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
import { fetchAdminToken } from './modules/shared/api'
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
const goHome = () => {
|
||||||
|
router.push('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
const goAdmin = async () => {
|
||||||
|
try {
|
||||||
|
const token = await fetchAdminToken()
|
||||||
|
router.push(`/admin?token=${encodeURIComponent(token)}`)
|
||||||
|
} catch (error) {
|
||||||
|
router.push('/admin')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
334
mengyastore-frontend/src/assets/styles.css
Normal file
334
mengyastore-frontend/src/assets/styles.css
Normal file
@@ -0,0 +1,334 @@
|
|||||||
|
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;600&family=Source+Sans+3:wght@300;400;600&display=swap');
|
||||||
|
|
||||||
|
:root {
|
||||||
|
--bg-1: #f8f5f2;
|
||||||
|
--bg-2: #e9f0f7;
|
||||||
|
--bg-3: #f4eaf1;
|
||||||
|
--glass: rgba(255, 255, 255, 0.55);
|
||||||
|
--glass-strong: rgba(255, 255, 255, 0.72);
|
||||||
|
--line: rgba(255, 255, 255, 0.35);
|
||||||
|
--text: #2c2b2d;
|
||||||
|
--muted: #6c6870;
|
||||||
|
--accent: #b49acb;
|
||||||
|
--accent-2: #91a8d0;
|
||||||
|
--shadow: 0 20px 50px rgba(33, 33, 40, 0.12);
|
||||||
|
--radius: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
* {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
min-height: 100vh;
|
||||||
|
font-family: 'Source Sans 3', sans-serif;
|
||||||
|
color: var(--text);
|
||||||
|
background: radial-gradient(circle at top, var(--bg-2), transparent 60%),
|
||||||
|
radial-gradient(circle at 10% 20%, var(--bg-3), transparent 55%),
|
||||||
|
linear-gradient(140deg, var(--bg-1), #fefefe 65%);
|
||||||
|
}
|
||||||
|
|
||||||
|
h1,
|
||||||
|
h2,
|
||||||
|
h3,
|
||||||
|
h4 {
|
||||||
|
font-family: 'Playfair Display', serif;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
p {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 100vh;
|
||||||
|
padding: 28px 5vw 36px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.top-bar {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 20px;
|
||||||
|
padding: 20px 26px;
|
||||||
|
background: var(--glass);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
box-shadow: var(--shadow);
|
||||||
|
backdrop-filter: blur(18px);
|
||||||
|
position: sticky;
|
||||||
|
top: 20px;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
.brand {
|
||||||
|
display: flex;
|
||||||
|
gap: 16px;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.brand h1 {
|
||||||
|
font-size: 26px;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.brand p {
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.brand-mark {
|
||||||
|
width: 46px;
|
||||||
|
height: 46px;
|
||||||
|
border-radius: 16px;
|
||||||
|
background: linear-gradient(140deg, var(--accent), var(--accent-2));
|
||||||
|
box-shadow: inset 0 0 18px rgba(255, 255, 255, 0.6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.top-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
font-family: 'Source Sans 3', sans-serif;
|
||||||
|
padding: 10px 18px;
|
||||||
|
border-radius: 12px;
|
||||||
|
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
transform: translateY(-1px);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.primary {
|
||||||
|
background: linear-gradient(135deg, var(--accent), var(--accent-2));
|
||||||
|
color: white;
|
||||||
|
box-shadow: 0 10px 30px rgba(145, 168, 208, 0.35);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.ghost {
|
||||||
|
background: rgba(255, 255, 255, 0.6);
|
||||||
|
color: var(--text);
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
}
|
||||||
|
|
||||||
|
.main-content {
|
||||||
|
flex: 1;
|
||||||
|
padding: 28px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer-mail {
|
||||||
|
color: var(--accent-2);
|
||||||
|
text-decoration: none;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer-mail:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-card {
|
||||||
|
background: var(--glass);
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
padding: 26px;
|
||||||
|
box-shadow: var(--shadow);
|
||||||
|
backdrop-filter: blur(16px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(5, minmax(0, 1fr));
|
||||||
|
gap: 20px;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-card {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 18px;
|
||||||
|
border-radius: 14px;
|
||||||
|
background: var(--glass-strong);
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
box-shadow: 0 12px 30px rgba(33, 33, 40, 0.1);
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-card img {
|
||||||
|
width: 100%;
|
||||||
|
height: 140px;
|
||||||
|
object-fit: cover;
|
||||||
|
border-radius: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-meta {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: baseline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-price {
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--accent-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge {
|
||||||
|
padding: 4px 10px;
|
||||||
|
border-radius: 10px;
|
||||||
|
font-size: 12px;
|
||||||
|
background: rgba(255, 255, 255, 0.7);
|
||||||
|
}
|
||||||
|
|
||||||
|
.admin-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 320px 1fr;
|
||||||
|
gap: 24px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-layout {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1.1fr 1fr;
|
||||||
|
gap: 24px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-image {
|
||||||
|
width: 100%;
|
||||||
|
height: 360px;
|
||||||
|
object-fit: cover;
|
||||||
|
border-radius: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
margin-top: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.buy-button {
|
||||||
|
background: linear-gradient(135deg, var(--accent), var(--accent-2));
|
||||||
|
color: white;
|
||||||
|
padding: 12px 22px;
|
||||||
|
border-radius: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-field {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 6px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-field input,
|
||||||
|
.form-field textarea {
|
||||||
|
border-radius: 14px;
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
background: rgba(255, 255, 255, 0.7);
|
||||||
|
padding: 10px 12px;
|
||||||
|
font-family: 'Source Sans 3', sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-field textarea {
|
||||||
|
min-height: 120px;
|
||||||
|
resize: vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
.admin-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
.table th,
|
||||||
|
.table td {
|
||||||
|
text-align: left;
|
||||||
|
padding: 12px;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.table tr {
|
||||||
|
border-bottom: 1px solid var(--line);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tag {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.markdown {
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--muted);
|
||||||
|
line-height: 1.6;
|
||||||
|
display: -webkit-box;
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
|
-webkit-line-clamp: 3;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.app {
|
||||||
|
padding: 18px 4vw 28px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.top-bar {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
padding: 16px 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.grid {
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
gap: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.admin-grid {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-layout {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-image {
|
||||||
|
height: 240px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer {
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-card {
|
||||||
|
padding: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-card {
|
||||||
|
padding: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-card img {
|
||||||
|
height: 120px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.markdown {
|
||||||
|
-webkit-line-clamp: 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
mengyastore-frontend/src/main.js
Normal file
10
mengyastore-frontend/src/main.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { createApp } from 'vue'
|
||||||
|
import { createPinia } from 'pinia'
|
||||||
|
import App from './App.vue'
|
||||||
|
import router from './router'
|
||||||
|
import './assets/styles.css'
|
||||||
|
|
||||||
|
const app = createApp(App)
|
||||||
|
app.use(createPinia())
|
||||||
|
app.use(router)
|
||||||
|
app.mount('#app')
|
||||||
388
mengyastore-frontend/src/modules/admin/AdminPage.vue
Normal file
388
mengyastore-frontend/src/modules/admin/AdminPage.vue
Normal file
@@ -0,0 +1,388 @@
|
|||||||
|
<template>
|
||||||
|
<section class="page-card">
|
||||||
|
<div class="hero">
|
||||||
|
<div>
|
||||||
|
<h2>管理员后台</h2>
|
||||||
|
<p class="tag">默认地址:/admin?token=shumengya520</p>
|
||||||
|
</div>
|
||||||
|
<div class="admin-actions">
|
||||||
|
<button class="primary" @click="openCreate">添加新商品</button>
|
||||||
|
<button class="ghost" @click="refresh">刷新列表</button>
|
||||||
|
<button class="primary" @click="autoGetToken">自动获取 Token</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="token-row">
|
||||||
|
<div class="form-field token-field">
|
||||||
|
<label>管理 Token</label>
|
||||||
|
<input v-model="token" placeholder="请输入 token" />
|
||||||
|
</div>
|
||||||
|
<p class="tag" v-if="message">{{ message }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>商品</th>
|
||||||
|
<th>价格</th>
|
||||||
|
<th>库存</th>
|
||||||
|
<th>状态</th>
|
||||||
|
<th>操作</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="item in products" :key="item.id">
|
||||||
|
<td>
|
||||||
|
<div>{{ item.name }}</div>
|
||||||
|
<div class="tag">{{ item.id }}</div>
|
||||||
|
</td>
|
||||||
|
<td>¥ {{ item.price.toFixed(2) }}</td>
|
||||||
|
<td>{{ item.quantity }}</td>
|
||||||
|
<td>
|
||||||
|
<span class="badge">{{ item.active ? '上架' : '下架' }}</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div class="admin-actions">
|
||||||
|
<button class="ghost" @click="openEdit(item)">编辑</button>
|
||||||
|
<button class="ghost" @click="toggle(item)">
|
||||||
|
{{ item.active ? '下架' : '上架' }}
|
||||||
|
</button>
|
||||||
|
<button class="ghost" @click="remove(item)">删除</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<div v-if="editorOpen" class="modal-mask" @click.self="closeEditor">
|
||||||
|
<section class="modal-card">
|
||||||
|
<div class="modal-header">
|
||||||
|
<div>
|
||||||
|
<h3>{{ form.id ? '编辑商品' : '添加新商品' }}</h3>
|
||||||
|
<p class="tag">封面图和最多 10 张商品截图共用这一套编辑表单。</p>
|
||||||
|
</div>
|
||||||
|
<button class="ghost" @click="closeEditor">关闭</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="modal-body">
|
||||||
|
<div class="form-field">
|
||||||
|
<label>商品名称</label>
|
||||||
|
<input v-model="form.name" placeholder="商品名称" />
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<div class="form-field">
|
||||||
|
<label>价格(元)</label>
|
||||||
|
<input v-model.number="form.price" type="number" step="0.01" />
|
||||||
|
</div>
|
||||||
|
<div class="form-field">
|
||||||
|
<label>库存数量</label>
|
||||||
|
<input v-model.number="form.quantity" type="number" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-field">
|
||||||
|
<label>封面链接(http)</label>
|
||||||
|
<input v-model="form.coverUrl" placeholder="http://..." />
|
||||||
|
</div>
|
||||||
|
<div class="form-field">
|
||||||
|
<label>商品截图链接(最多 10 个)</label>
|
||||||
|
<div class="screenshot-grid">
|
||||||
|
<input
|
||||||
|
v-for="(_, index) in screenshotInputSlots"
|
||||||
|
:key="index"
|
||||||
|
v-model="form.screenshotUrls[index]"
|
||||||
|
:placeholder="`截图链接 ${index + 1}(http://...)`"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-field">
|
||||||
|
<label>商品介绍(Markdown)</label>
|
||||||
|
<textarea v-model="form.description"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="form-field">
|
||||||
|
<label>是否上架</label>
|
||||||
|
<select v-model="form.active">
|
||||||
|
<option :value="true">上架</option>
|
||||||
|
<option :value="false">下架</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="modal-actions">
|
||||||
|
<button class="primary" @click="submit">{{ form.id ? '更新商品' : '新增商品' }}</button>
|
||||||
|
<button class="ghost" @click="reset">重置表单</button>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { onMounted, reactive, ref, watch } from 'vue'
|
||||||
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
import {
|
||||||
|
fetchAdminProducts,
|
||||||
|
fetchAdminToken,
|
||||||
|
createProduct,
|
||||||
|
updateProduct,
|
||||||
|
toggleProduct,
|
||||||
|
deleteProduct
|
||||||
|
} from '../shared/api'
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
const token = ref(route.query.token || '')
|
||||||
|
const products = ref([])
|
||||||
|
const message = ref('')
|
||||||
|
const editorOpen = ref(false)
|
||||||
|
const MAX_SCREENSHOT_URLS = 10
|
||||||
|
const screenshotInputSlots = Array.from({ length: MAX_SCREENSHOT_URLS })
|
||||||
|
|
||||||
|
const createScreenshotSlots = (values = []) =>
|
||||||
|
Array.from({ length: MAX_SCREENSHOT_URLS }, (_, index) => values[index] || '')
|
||||||
|
|
||||||
|
const normalizeScreenshotUrls = (values = []) =>
|
||||||
|
values.map((item) => item.trim()).filter(Boolean).slice(0, MAX_SCREENSHOT_URLS)
|
||||||
|
|
||||||
|
const emptyForm = () => ({
|
||||||
|
id: '',
|
||||||
|
name: '',
|
||||||
|
price: 0,
|
||||||
|
quantity: 0,
|
||||||
|
coverUrl: '',
|
||||||
|
screenshotUrls: createScreenshotSlots(),
|
||||||
|
description: '',
|
||||||
|
active: true
|
||||||
|
})
|
||||||
|
|
||||||
|
const form = reactive(emptyForm())
|
||||||
|
|
||||||
|
const syncQuery = () => {
|
||||||
|
if (token.value) {
|
||||||
|
router.replace({ query: { token: token.value } })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const refresh = async () => {
|
||||||
|
if (!token.value) {
|
||||||
|
message.value = '请先输入 token'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
products.value = await fetchAdminProducts(token.value)
|
||||||
|
message.value = '数据已更新'
|
||||||
|
} catch (error) {
|
||||||
|
message.value = '获取失败,请检查 token'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const autoGetToken = async () => {
|
||||||
|
const fetched = await fetchAdminToken()
|
||||||
|
token.value = fetched
|
||||||
|
syncQuery()
|
||||||
|
await refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
const submit = async () => {
|
||||||
|
if (!token.value) {
|
||||||
|
message.value = '请先输入 token'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (form.id) {
|
||||||
|
await updateProduct(token.value, form.id, {
|
||||||
|
name: form.name,
|
||||||
|
price: form.price,
|
||||||
|
quantity: form.quantity,
|
||||||
|
coverUrl: form.coverUrl,
|
||||||
|
screenshotUrls: normalizeScreenshotUrls(form.screenshotUrls),
|
||||||
|
description: form.description,
|
||||||
|
active: form.active
|
||||||
|
})
|
||||||
|
message.value = '已更新商品'
|
||||||
|
} else {
|
||||||
|
await createProduct(token.value, {
|
||||||
|
name: form.name,
|
||||||
|
price: form.price,
|
||||||
|
quantity: form.quantity,
|
||||||
|
coverUrl: form.coverUrl,
|
||||||
|
screenshotUrls: normalizeScreenshotUrls(form.screenshotUrls),
|
||||||
|
description: form.description,
|
||||||
|
active: form.active
|
||||||
|
})
|
||||||
|
message.value = '已新增商品'
|
||||||
|
}
|
||||||
|
reset()
|
||||||
|
closeEditor()
|
||||||
|
await refresh()
|
||||||
|
} catch (error) {
|
||||||
|
message.value = '操作失败,请检查输入'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fillForm = (item) => {
|
||||||
|
Object.assign(form, {
|
||||||
|
id: item?.id || '',
|
||||||
|
name: item?.name || '',
|
||||||
|
price: item?.price || 0,
|
||||||
|
quantity: item?.quantity || 0,
|
||||||
|
coverUrl: item?.coverUrl || '',
|
||||||
|
screenshotUrls: createScreenshotSlots(item?.screenshotUrls || []),
|
||||||
|
description: item?.description || '',
|
||||||
|
active: item?.active ?? true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const openCreate = () => {
|
||||||
|
reset()
|
||||||
|
editorOpen.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const openEdit = (item) => {
|
||||||
|
fillForm(item)
|
||||||
|
editorOpen.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const closeEditor = () => {
|
||||||
|
editorOpen.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggle = async (item) => {
|
||||||
|
if (!token.value) {
|
||||||
|
message.value = '请先输入 token'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await toggleProduct(token.value, item.id, !item.active)
|
||||||
|
await refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
const remove = async (item) => {
|
||||||
|
if (!token.value) {
|
||||||
|
message.value = '请先输入 token'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await deleteProduct(token.value, item.id)
|
||||||
|
await refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
fillForm()
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(token, () => {
|
||||||
|
syncQuery()
|
||||||
|
})
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
if (token.value) {
|
||||||
|
await refresh()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.hero {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
margin-bottom: 22px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.token-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-end;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 16px;
|
||||||
|
margin-bottom: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.token-field {
|
||||||
|
width: min(420px, 100%);
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
border-radius: 14px;
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
background: rgba(255, 255, 255, 0.7);
|
||||||
|
padding: 10px 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.screenshot-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-mask {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
z-index: 40;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 24px;
|
||||||
|
background: rgba(30, 28, 32, 0.35);
|
||||||
|
backdrop-filter: blur(8px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-card {
|
||||||
|
width: min(920px, 100%);
|
||||||
|
max-height: calc(100vh - 48px);
|
||||||
|
overflow: auto;
|
||||||
|
padding: 24px;
|
||||||
|
border-radius: 20px;
|
||||||
|
background: rgba(255, 255, 255, 0.92);
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
box-shadow: 0 24px 60px rgba(33, 33, 40, 0.18);
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 16px;
|
||||||
|
margin-bottom: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-body {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: flex-end;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.hero,
|
||||||
|
.token-row,
|
||||||
|
.modal-header {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-row,
|
||||||
|
.screenshot-grid {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-mask {
|
||||||
|
padding: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-card {
|
||||||
|
padding: 18px;
|
||||||
|
max-height: calc(100vh - 24px);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
48
mengyastore-frontend/src/modules/shared/api.js
Normal file
48
mengyastore-frontend/src/modules/shared/api.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
|
const api = axios.create({
|
||||||
|
baseURL: 'http://localhost:8080'
|
||||||
|
})
|
||||||
|
|
||||||
|
export const fetchProducts = async () => {
|
||||||
|
const { data } = await api.get('/api/products')
|
||||||
|
return data.data || []
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fetchAdminToken = async () => {
|
||||||
|
const { data } = await api.get('/api/admin/token')
|
||||||
|
return data.token || ''
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fetchAdminProducts = async (token) => {
|
||||||
|
const { data } = await api.get('/api/admin/products', { params: { token } })
|
||||||
|
return data.data || []
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createProduct = async (token, payload) => {
|
||||||
|
const { data } = await api.post('/api/admin/products', payload, {
|
||||||
|
params: { token }
|
||||||
|
})
|
||||||
|
return data.data
|
||||||
|
}
|
||||||
|
|
||||||
|
export const updateProduct = async (token, id, payload) => {
|
||||||
|
const { data } = await api.put(`/api/admin/products/${id}`, payload, {
|
||||||
|
params: { token }
|
||||||
|
})
|
||||||
|
return data.data
|
||||||
|
}
|
||||||
|
|
||||||
|
export const toggleProduct = async (token, id, active) => {
|
||||||
|
const { data } = await api.patch(`/api/admin/products/${id}/status`, { active }, {
|
||||||
|
params: { token }
|
||||||
|
})
|
||||||
|
return data.data
|
||||||
|
}
|
||||||
|
|
||||||
|
export const deleteProduct = async (token, id) => {
|
||||||
|
const { data } = await api.delete(`/api/admin/products/${id}`, {
|
||||||
|
params: { token }
|
||||||
|
})
|
||||||
|
return data
|
||||||
|
}
|
||||||
277
mengyastore-frontend/src/modules/store/ProductDetail.vue
Normal file
277
mengyastore-frontend/src/modules/store/ProductDetail.vue
Normal file
@@ -0,0 +1,277 @@
|
|||||||
|
<template>
|
||||||
|
<section class="page-card" v-if="!loading && product">
|
||||||
|
<div class="detail-body">
|
||||||
|
<div class="detail-summary">
|
||||||
|
<h2>{{ product.name }}</h2>
|
||||||
|
<p class="tag">库存:{{ product.quantity }}</p>
|
||||||
|
<p class="product-price">¥ {{ product.price.toFixed(2) }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="markdown detail-description" v-html="renderMarkdown(product.description)"></div>
|
||||||
|
|
||||||
|
<div class="detail-gallery" v-if="galleryImages.length">
|
||||||
|
<div class="detail-gallery-main">
|
||||||
|
<button
|
||||||
|
v-if="galleryImages.length > 1"
|
||||||
|
class="gallery-nav prev"
|
||||||
|
type="button"
|
||||||
|
aria-label="上一张"
|
||||||
|
@click="prevImage"
|
||||||
|
>
|
||||||
|
‹
|
||||||
|
</button>
|
||||||
|
<img
|
||||||
|
class="detail-image"
|
||||||
|
:src="galleryImages[currentImageIndex].url"
|
||||||
|
:alt="galleryImages[currentImageIndex].label"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
v-if="galleryImages.length > 1"
|
||||||
|
class="gallery-nav next"
|
||||||
|
type="button"
|
||||||
|
aria-label="下一张"
|
||||||
|
@click="nextImage"
|
||||||
|
>
|
||||||
|
›
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-gallery-meta">
|
||||||
|
<span class="badge">{{ galleryImages[currentImageIndex].label }}</span>
|
||||||
|
<span class="tag">{{ currentImageIndex + 1 }} / {{ galleryImages.length }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-thumbs" v-if="galleryImages.length > 1">
|
||||||
|
<button
|
||||||
|
v-for="(image, index) in galleryImages"
|
||||||
|
:key="`${image.url}-${index}`"
|
||||||
|
class="detail-thumb"
|
||||||
|
:class="{ active: index === currentImageIndex }"
|
||||||
|
type="button"
|
||||||
|
@click="currentImageIndex = index"
|
||||||
|
>
|
||||||
|
<img :src="image.url" :alt="image.label" />
|
||||||
|
<span>{{ image.label }}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-actions">
|
||||||
|
<button class="buy-button" @click="buy">立即购买</button>
|
||||||
|
<button class="ghost" @click="goBack">返回商店首页</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="page-card" v-else-if="!loading && !product">
|
||||||
|
<h2>未找到该商品</h2>
|
||||||
|
<p class="tag">请返回商店查看其他商品。</p>
|
||||||
|
<div class="detail-actions">
|
||||||
|
<button class="ghost" @click="goBack">返回商店首页</button>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="page-card" v-else>
|
||||||
|
<div class="status">加载中...</div>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed, onMounted, ref, watch } from 'vue'
|
||||||
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
import MarkdownIt from 'markdown-it'
|
||||||
|
import { fetchProducts } from '../shared/api'
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
const md = new MarkdownIt()
|
||||||
|
const loading = ref(true)
|
||||||
|
const product = ref(null)
|
||||||
|
const currentImageIndex = ref(0)
|
||||||
|
|
||||||
|
const renderMarkdown = (content) => md.render(content || '')
|
||||||
|
|
||||||
|
const galleryImages = computed(() => {
|
||||||
|
if (!product.value) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
const images = []
|
||||||
|
const seen = new Set()
|
||||||
|
const appendImage = (url, label) => {
|
||||||
|
const trimmed = (url || '').trim()
|
||||||
|
if (!trimmed || seen.has(trimmed)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen.add(trimmed)
|
||||||
|
images.push({ url: trimmed, label })
|
||||||
|
}
|
||||||
|
|
||||||
|
appendImage(product.value.coverUrl, '商品封面')
|
||||||
|
|
||||||
|
const screenshots = Array.isArray(product.value.screenshotUrls)
|
||||||
|
? product.value.screenshotUrls
|
||||||
|
: []
|
||||||
|
|
||||||
|
screenshots.forEach((url, index) => {
|
||||||
|
appendImage(url, `商品截图 ${index + 1}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
return images
|
||||||
|
})
|
||||||
|
|
||||||
|
const goBack = () => {
|
||||||
|
router.push('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
const buy = () => {
|
||||||
|
alert('已加入购买清单(示例)')
|
||||||
|
}
|
||||||
|
|
||||||
|
const prevImage = () => {
|
||||||
|
if (!galleryImages.value.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
currentImageIndex.value =
|
||||||
|
(currentImageIndex.value - 1 + galleryImages.value.length) % galleryImages.value.length
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextImage = () => {
|
||||||
|
if (!galleryImages.value.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
currentImageIndex.value = (currentImageIndex.value + 1) % galleryImages.value.length
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
galleryImages,
|
||||||
|
(images) => {
|
||||||
|
if (!images.length || currentImageIndex.value >= images.length) {
|
||||||
|
currentImageIndex.value = 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true }
|
||||||
|
)
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
try {
|
||||||
|
const list = await fetchProducts()
|
||||||
|
product.value = list.find((item) => item.id === route.params.id) || null
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.detail-body {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-summary {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-description {
|
||||||
|
display: block;
|
||||||
|
color: var(--text);
|
||||||
|
font-size: 15px;
|
||||||
|
line-height: 1.8;
|
||||||
|
-webkit-line-clamp: initial;
|
||||||
|
-webkit-box-orient: initial;
|
||||||
|
overflow: visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-gallery {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-gallery-main {
|
||||||
|
position: relative;
|
||||||
|
overflow: hidden;
|
||||||
|
border-radius: 18px;
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
background: rgba(255, 255, 255, 0.45);
|
||||||
|
}
|
||||||
|
|
||||||
|
.gallery-nav {
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
z-index: 1;
|
||||||
|
width: 42px;
|
||||||
|
height: 42px;
|
||||||
|
border-radius: 999px;
|
||||||
|
padding: 0;
|
||||||
|
background: rgba(255, 255, 255, 0.88);
|
||||||
|
color: var(--text);
|
||||||
|
font-size: 28px;
|
||||||
|
line-height: 1;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
box-shadow: 0 10px 20px rgba(33, 33, 40, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.gallery-nav.prev {
|
||||||
|
left: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gallery-nav.next {
|
||||||
|
right: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-gallery-meta {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-thumbs {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-thumb {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
background: rgba(255, 255, 255, 0.58);
|
||||||
|
border: 1px solid transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-thumb.active {
|
||||||
|
border-color: var(--accent-2);
|
||||||
|
box-shadow: 0 10px 24px rgba(145, 168, 208, 0.16);
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-thumb img {
|
||||||
|
width: 100%;
|
||||||
|
height: 76px;
|
||||||
|
object-fit: cover;
|
||||||
|
border-radius: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-thumb span {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.status {
|
||||||
|
padding: 24px 0;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.detail-thumb {
|
||||||
|
padding: 6px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
108
mengyastore-frontend/src/modules/store/StorePage.vue
Normal file
108
mengyastore-frontend/src/modules/store/StorePage.vue
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<template>
|
||||||
|
<section class="page-card">
|
||||||
|
<div class="hero">
|
||||||
|
<div>
|
||||||
|
<h2>所有商品</h2>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="loading" class="status">加载中...</div>
|
||||||
|
<div v-else>
|
||||||
|
<div class="grid">
|
||||||
|
<router-link
|
||||||
|
v-for="item in pagedProducts"
|
||||||
|
:key="item.id"
|
||||||
|
:to="`/product/${item.id}`"
|
||||||
|
class="product-link"
|
||||||
|
>
|
||||||
|
<article class="product-card">
|
||||||
|
<img :src="item.coverUrl" :alt="item.name" />
|
||||||
|
<div class="product-meta">
|
||||||
|
<h3>{{ item.name }}</h3>
|
||||||
|
<span class="product-price">¥ {{ item.price.toFixed(2) }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="tag">库存:{{ item.quantity }}</div>
|
||||||
|
<div class="markdown" v-html="renderMarkdown(item.description)"></div>
|
||||||
|
</article>
|
||||||
|
</router-link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="pagination" v-if="totalPages > 1">
|
||||||
|
<button class="ghost" :disabled="page === 1" @click="page--">上一页</button>
|
||||||
|
<span class="tag">第 {{ page }} / {{ totalPages }} 页</span>
|
||||||
|
<button class="ghost" :disabled="page === totalPages" @click="page++">下一页</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||||
|
import MarkdownIt from 'markdown-it'
|
||||||
|
import { fetchProducts } from '../shared/api'
|
||||||
|
|
||||||
|
const products = ref([])
|
||||||
|
const loading = ref(true)
|
||||||
|
const md = new MarkdownIt()
|
||||||
|
const page = ref(1)
|
||||||
|
const perPage = ref(20)
|
||||||
|
|
||||||
|
const renderMarkdown = (content) => md.render(content || '')
|
||||||
|
|
||||||
|
const updatePerPage = () => {
|
||||||
|
perPage.value = window.innerWidth <= 900 ? 6 : 20
|
||||||
|
page.value = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalPages = computed(() =>
|
||||||
|
Math.max(1, Math.ceil(products.value.length / perPage.value))
|
||||||
|
)
|
||||||
|
|
||||||
|
const pagedProducts = computed(() => {
|
||||||
|
const start = (page.value - 1) * perPage.value
|
||||||
|
return products.value.slice(start, start + perPage.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
updatePerPage()
|
||||||
|
window.addEventListener('resize', updatePerPage)
|
||||||
|
try {
|
||||||
|
products.value = await fetchProducts()
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
window.removeEventListener('resize', updatePerPage)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.hero {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-link {
|
||||||
|
display: block;
|
||||||
|
text-decoration: none;
|
||||||
|
color: inherit;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status {
|
||||||
|
padding: 24px 0;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 12px;
|
||||||
|
margin-top: 22px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
15
mengyastore-frontend/src/router/index.js
Normal file
15
mengyastore-frontend/src/router/index.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
|
import StorePage from '../modules/store/StorePage.vue'
|
||||||
|
import AdminPage from '../modules/admin/AdminPage.vue'
|
||||||
|
import ProductDetail from '../modules/store/ProductDetail.vue'
|
||||||
|
|
||||||
|
const router = createRouter({
|
||||||
|
history: createWebHistory(),
|
||||||
|
routes: [
|
||||||
|
{ path: '/', name: 'store', component: StorePage },
|
||||||
|
{ path: '/product/:id', name: 'product-detail', component: ProductDetail },
|
||||||
|
{ path: '/admin', name: 'admin', component: AdminPage }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
export default router
|
||||||
9
mengyastore-frontend/vite.config.js
Normal file
9
mengyastore-frontend/vite.config.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import vue from '@vitejs/plugin-vue'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [vue()],
|
||||||
|
server: {
|
||||||
|
port: 5173
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user