@hupu-tech/shaper 0.2.12 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +6 -0
- package/dist/auth/auth.d.ts +1 -0
- package/dist/cli.js +2 -2
- package/dist/cloud/client.d.ts +0 -1
- package/dist/cloud/service-build.d.ts +2 -4
- package/dist/index.js +1 -1
- package/dist/platform/api.d.ts +2 -0
- package/dist/project/create.d.ts +9 -3
- package/dist/project/delivery.d.ts +5 -5
- package/dist/runtime.d.ts +2 -1
- package/dist/runtime.js +1 -1
- package/dist/shared-UKIFY6QO.js +19 -0
- package/package.json +1 -1
- package/skills/business/SKILL.md +158 -0
- package/skills/business/activity/README.md +14 -0
- package/skills/business/activity/hupu-leaderboard-template/SKILL.md +52 -0
- package/skills/business/activity/hupu-leaderboard-template/example/README.md +20 -0
- package/skills/business/activity/hupu-leaderboard-template/example/cloudfunctions/activity_api/.cloudignore +3 -0
- package/skills/business/activity/hupu-leaderboard-template/example/cloudfunctions/activity_api/index.js +218 -0
- package/skills/business/activity/hupu-leaderboard-template/example/cloudfunctions/activity_api/package.json +15 -0
- package/skills/business/activity/hupu-leaderboard-template/example/cloudfunctions/activity_api/scf_bootstrap +3 -0
- package/skills/business/activity/hupu-leaderboard-template/example/index.html +291 -0
- package/skills/business/activity/hupu-leaderboard-template/example/migrations/20260828210000_create_leaderboard.sql +54 -0
- package/skills/business/activity/hupu-lottery-draw/SKILL.md +50 -0
- package/skills/business/activity/hupu-lottery-draw/example/README.md +21 -0
- package/skills/business/activity/hupu-lottery-draw/example/cloudfunctions/activity_api/.cloudignore +3 -0
- package/skills/business/activity/hupu-lottery-draw/example/cloudfunctions/activity_api/index.js +142 -0
- package/skills/business/activity/hupu-lottery-draw/example/cloudfunctions/activity_api/package.json +9 -0
- package/skills/business/activity/hupu-lottery-draw/example/cloudfunctions/activity_api/scf_bootstrap +3 -0
- package/skills/business/activity/hupu-lottery-draw/example/index.html +178 -0
- package/skills/business/activity/hupu-lottery-draw/example/migrations/20260828220000_create_draw.sql +193 -0
- package/skills/business/activity/hupu-pk-voting/SKILL.md +54 -0
- package/skills/business/activity/hupu-pk-voting/example/README.md +44 -0
- package/skills/business/activity/hupu-pk-voting/example/cloudfunctions/activity_api/.cloudignore +3 -0
- package/skills/business/activity/hupu-pk-voting/example/cloudfunctions/activity_api/index.js +370 -0
- package/skills/business/activity/hupu-pk-voting/example/cloudfunctions/activity_api/package.json +15 -0
- package/skills/business/activity/hupu-pk-voting/example/cloudfunctions/activity_api/scf_bootstrap +2 -0
- package/skills/business/activity/hupu-pk-voting/example/index.html +729 -0
- package/skills/business/activity/hupu-pk-voting/example/migrations/20260828220000_create_pk_vote.sql +168 -0
- package/skills/business/activity/hupu-request-activity-execute-vote-task/SKILL.md +77 -0
- package/skills/business/activity/hupu-request-activity-get-vote-detail/SKILL.md +84 -0
- package/skills/business/activity/hupu-request-activity-get-vote-task-list/SKILL.md +86 -0
- package/skills/business/activity/hupu-request-activity-list-by-biz-keys/SKILL.md +153 -0
- package/skills/business/activity/hupu-request-activity-report-vote-task/SKILL.md +58 -0
- package/skills/business/activity/hupu-request-activity-submit-vote/SKILL.md +58 -0
- package/skills/business/activity/hupu-vote-task/SKILL.md +47 -0
- package/skills/business/community/README.md +13 -0
- package/skills/business/community/hupu-request-bbs-detail/SKILL.md +197 -0
- package/skills/business/community/hupu-request-bbs-get-tag-thread-list/SKILL.md +257 -0
- package/skills/business/community/hupu-request-bbs-get-topic-threads/SKILL.md +159 -0
- package/skills/business/community/hupu-request-bbs-gets-thread-post-list/SKILL.md +124 -0
- package/skills/business/community/hupu-request-bbs-open-post-editor/SKILL.md +75 -0
- package/skills/business/community/hupu-request-bbs-reply-post/SKILL.md +59 -0
- package/skills/business/community/hupu-score-add-score/SKILL.md +57 -0
- package/skills/business/community/hupu-score-get-item-detail/SKILL.md +173 -0
- package/skills/business/community/hupu-score-get-score/SKILL.md +263 -0
- package/skills/business/quality/README.md +7 -0
- package/skills/business/quality/hupu-security-check-audit/SKILL.md +39 -0
- package/skills/business/quality/hupu-wasm-compression/SKILL.md +69 -0
- package/skills/business/quality/hupu-wasm-compression/agents/openai.yaml +4 -0
- package/skills/business/quality/hupu-wasm-compression/tool/checks.mjs +90 -0
- package/skills/business/quality/hupu-wasm-compression/tool/compress-worker.mjs +40 -0
- package/skills/business/quality/hupu-wasm-compression/tool/public/app.js +84 -0
- package/skills/business/quality/hupu-wasm-compression/tool/public/index.html +80 -0
- package/skills/business/quality/hupu-wasm-compression/tool/public/style.css +3 -0
- package/skills/business/quality/hupu-wasm-compression/tool/server.mjs +126 -0
- package/skills/business/quality/hupu-web-security-audit/SKILL.md +45 -0
- package/skills/business/quality/hupu-web-security-audit/agents/openai.yaml +4 -0
- package/skills/business/rewards/README.md +13 -0
- package/skills/business/rewards/hupu-vacoin/SKILL.md +70 -0
- package/skills/business/rewards/hupu-vacoin-consume-coins/SKILL.md +47 -0
- package/skills/business/rewards/hupu-vacoin-get-coin-overview/SKILL.md +52 -0
- package/skills/business/rewards/hupu-vacoin-get-session-coins/SKILL.md +44 -0
- package/skills/business/rewards/hupu-vacoin-reset-session-id/SKILL.md +39 -0
- package/skills/business/rewards/hupu-vatask/SKILL.md +63 -0
- package/skills/business/rewards/hupu-vatask-complete-reward-video/SKILL.md +43 -0
- package/skills/business/rewards/hupu-vatask-consume-activity-chance/SKILL.md +48 -0
- package/skills/business/rewards/hupu-vatask-get-activity-task-state/SKILL.md +49 -0
- package/skills/business/runtime/README.md +13 -0
- package/skills/business/runtime/hupu-add-to-desktop/SKILL.md +51 -0
- package/skills/business/runtime/hupu-close-webview/SKILL.md +48 -0
- package/skills/business/runtime/hupu-cloud-auth/SKILL.md +43 -0
- package/skills/business/runtime/hupu-cloud-request/SKILL.md +56 -0
- package/skills/business/runtime/hupu-fullscreen-safe-top/SKILL.md +33 -0
- package/skills/business/runtime/hupu-navigate-to/SKILL.md +45 -0
- package/skills/business/runtime/hupu-request/SKILL.md +67 -0
- package/skills/business/runtime/hupu-seer-chat/SKILL.md +183 -0
- package/skills/business/runtime/hupu-track-report/SKILL.md +71 -0
- package/skills/business/sharing/README.md +7 -0
- package/skills/business/sharing/hupu-oss-upload-file/SKILL.md +40 -0
- package/skills/business/sharing/hupu-result-poster-posting/SKILL.md +37 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/README.md +14 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/cloudfunctions/activity_api/.cloudignore +3 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/cloudfunctions/activity_api/index.js +187 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/cloudfunctions/activity_api/package.json +15 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/cloudfunctions/activity_api/scf_bootstrap +3 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/index.html +256 -0
- package/skills/business/sharing/hupu-result-poster-posting/example/migrations/20260828213000_create_result_poster_results.sql +60 -0
- package/skills/business/sharing/hupu-result-poster-posting/references/api-contract.md +46 -0
- package/skills/business/sharing/hupu-social-share/SKILL.md +27 -0
- package/skills/business/sports/README.md +27 -0
- package/skills/business/sports/hupu-request-basketball-player-info/SKILL.md +131 -0
- package/skills/business/sports/hupu-request-basketball-single-match/SKILL.md +158 -0
- package/skills/business/sports/hupu-request-basketball-team-info/SKILL.md +96 -0
- package/skills/business/sports/hupu-request-basketball-team-player-list/SKILL.md +108 -0
- package/skills/business/sports/hupu-request-basketball-team-salary-info/SKILL.md +85 -0
- package/skills/business/sports/hupu-request-football-coach-info/SKILL.md +76 -0
- package/skills/business/sports/hupu-request-football-live-room-news-list/SKILL.md +85 -0
- package/skills/business/sports/hupu-request-football-match-info/SKILL.md +126 -0
- package/skills/business/sports/hupu-request-football-player-info/SKILL.md +82 -0
- package/skills/business/sports/hupu-request-football-team-info/SKILL.md +83 -0
- package/skills/business/sports/hupu-request-football-team-player-season-stats/SKILL.md +73 -0
- package/skills/business/sports/hupu-request-football-team-season-year/SKILL.md +68 -0
- package/skills/business/sports/hupu-request-kog-playbyplay/SKILL.md +77 -0
- package/skills/business/sports/hupu-request-live-tab-info-list/SKILL.md +85 -0
- package/skills/business/sports/hupu-request-live-tab-list/SKILL.md +66 -0
- package/skills/business/sports/hupu-request-lol-playbyplay/SKILL.md +77 -0
- package/skills/business/sports/hupu-request-match-get-tab-detail-schedule-list/SKILL.md +113 -0
- package/skills/business/sports/hupu-request-match-query-all-diagram-info-by-id/SKILL.md +79 -0
- package/skills/business/sports/hupu-request-match-query-match-stats-by-match-info/SKILL.md +85 -0
- package/skills/business/sports/hupu-request-news-news-common-list/SKILL.md +90 -0
- package/skills/business/sports/hupu-request-news-player-news-list/SKILL.md +79 -0
- package/skills/business/sports/hupu-request-news-subject-news/SKILL.md +89 -0
- package/skills/business/sports/hupu-request-news-team-news-list/SKILL.md +76 -0
- package/skills/business/storage/README.md +9 -0
- package/skills/business/storage/hupu-cloud-save/SKILL.md +129 -0
- package/skills/business/storage/hupu-cloud-save/references/legacy-migration.md +111 -0
- package/skills/business/storage/hupu-cloud-save-load/SKILL.md +41 -0
- package/skills/business/storage/hupu-cloud-save-save/SKILL.md +45 -0
- package/skills/business/storage/hupu-storage-get-value/SKILL.md +26 -0
- package/skills/business/storage/hupu-storage-set-value/SKILL.md +42 -0
- package/skills/business/user/README.md +6 -0
- package/skills/business/user/hupu-auth-get-user-info/SKILL.md +47 -0
- package/skills/business/user/hupu-game-auth/SKILL.md +46 -0
- package/skills/core/hupu-business/SKILL.md +1 -1
- package/skills/core/hupu-business/references/runtime.md +17 -11
- package/skills/core/shaper-backend/SKILL.md +1 -1
- package/skills/core/shaper-backend/references/database.md +2 -2
- package/skills/core/shaper-backend/references/legacy.md +1 -1
- package/skills/core/shaper-backend/references/node-server.md +29 -20
- package/skills/core/shaper-development/SKILL.md +6 -1
- package/skills/core/shaper-development/references/materials.md +14 -0
- package/skills/core/shaper-development/references/project-setup.md +1 -1
- package/skills/core/shaper-game-development/SKILL.md +16 -9
- package/skills/core/shaper-game-development/references/implementation.md +13 -0
- package/skills/core/shaper-game-development/references/iteration.md +11 -0
- package/skills/core/shaper-game-development/references/playable-loop.md +12 -0
- package/skills/core/shaper-game-development/references/playtest.md +18 -0
- package/skills/core/shaper-game-development/references/sources.md +6 -0
- package/skills/core/shaper-release/SKILL.md +1 -1
- package/templates/cloud-service/README.md +4 -30
- package/templates/cloud-service/cloud.json +5 -8
- package/templates/cloud-service/package-lock.json +922 -15
- package/templates/cloud-service/release.json +4 -4
- package/templates/cloud-service/server.cjs +37 -0
- package/templates/leaderboard/README.md +3 -0
- package/templates/leaderboard/cloud/database/migrations/202611130001_leaderboard.sql +6 -0
- package/templates/leaderboard/cloud/functions/api/cloud.json +10 -0
- package/templates/leaderboard/cloud/functions/api/leaderboard.cjs +49 -0
- package/templates/leaderboard/cloud/functions/api/package-lock.json +1093 -0
- package/templates/leaderboard/cloud/functions/api/package.json +19 -0
- package/templates/leaderboard/cloud/functions/api/server.cjs +25 -0
- package/templates/leaderboard/cloud/gateway/routes.json +15 -0
- package/templates/leaderboard/docs/design.md +1 -0
- package/templates/leaderboard/docs/integration.md +12 -0
- package/templates/leaderboard/docs/product.md +1 -0
- package/templates/leaderboard/docs/validation.md +1 -0
- package/templates/leaderboard/index.html +12 -0
- package/templates/leaderboard/package-lock.json +828 -0
- package/templates/leaderboard/package.json +17 -0
- package/templates/leaderboard/public/.gitkeep +1 -0
- package/templates/leaderboard/shaper.config.json +7 -0
- package/templates/leaderboard/src/main.js +67 -0
- package/templates/leaderboard/src/style.css +69 -0
- package/templates/leaderboard/test/leaderboard.test.cjs +93 -0
- package/templates/offline-game/README.md +3 -0
- package/templates/offline-game/docs/design.md +1 -0
- package/templates/offline-game/docs/integration.md +10 -0
- package/templates/offline-game/docs/product.md +1 -0
- package/templates/offline-game/docs/validation.md +1 -0
- package/templates/offline-game/index.html +12 -0
- package/templates/offline-game/package-lock.json +828 -0
- package/templates/offline-game/package.json +16 -0
- package/templates/offline-game/public/.gitkeep +1 -0
- package/templates/offline-game/shaper.config.json +7 -0
- package/templates/offline-game/src/game.js +42 -0
- package/templates/offline-game/src/main.js +60 -0
- package/templates/offline-game/src/style.css +69 -0
- package/templates/offline-game/test/game.test.js +25 -0
- package/dist/shared-APGA5MU5.js +0 -19
- package/templates/cloud-service/handlers.cjs +0 -46
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>抽卡功能</title>
|
|
7
|
+
<script type="application/json" id="shaper-skill-usage">
|
|
8
|
+
{"skills":["hupu-cloud-auth","hupu-cloud-request","hupu-oss-upload-file","user-feedback"]}
|
|
9
|
+
</script>
|
|
10
|
+
<style>
|
|
11
|
+
:root {
|
|
12
|
+
font-family:
|
|
13
|
+
'PingFang SC',
|
|
14
|
+
-apple-system,
|
|
15
|
+
BlinkMacSystemFont,
|
|
16
|
+
'Segoe UI',
|
|
17
|
+
sans-serif;
|
|
18
|
+
color: #3d3d3d;
|
|
19
|
+
background: #f4f7f8;
|
|
20
|
+
}
|
|
21
|
+
* { box-sizing: border-box; }
|
|
22
|
+
body { min-width: 320px; min-height: 100vh; margin: 0; background: #f4f7f8; }
|
|
23
|
+
main { display: flex; width: min(100%, 390px); min-height: 100vh; flex-direction: column; padding: 34px 16px 16px; margin: 0 auto; background: #fff; }
|
|
24
|
+
header { padding-bottom: 14px; border-bottom: 1px solid #edf3f4; }
|
|
25
|
+
h1 { margin: 0; color: #1a1f26; font-size: 17px; font-weight: 600; line-height: 1.35; }
|
|
26
|
+
.card { display: grid; min-height: 185px; align-content: center; justify-items: center; gap: 7px; padding: 20px; margin: 20px 0 14px; color: #4a8b8f; background: #f0f7f7; border: 1px solid #d5e8e8; text-align: center; }
|
|
27
|
+
.card-mark { display: grid; width: 30px; height: 30px; place-items: center; border: 1px solid currentColor; font-size: 20px; }
|
|
28
|
+
.card strong { color: #1a1f26; font-size: 15px; font-weight: 600; }
|
|
29
|
+
.card span { color: #6f7e8c; font-size: 10px; }
|
|
30
|
+
.card.drawn { color: #3f7d5a; background: #eef7f1; border-color: #cce7d5; }
|
|
31
|
+
.odds { display: flex; justify-content: space-between; padding-bottom: 14px; color: #6f7e8c; font-size: 10px; border-bottom: 1px solid #edf3f4; }
|
|
32
|
+
.odds strong { color: #1a1f26; font-weight: 600; }
|
|
33
|
+
button { min-height: 36px; margin-top: 16px; color: #fff; font: inherit; font-size: 11px; background: #4a8b8f; border: 1px solid #4a8b8f; border-radius: 0; cursor: pointer; }
|
|
34
|
+
button:hover, button:focus-visible { background: #3d7579; border-color: #3d7579; outline: 2px solid #4a8b8f; outline-offset: 2px; }
|
|
35
|
+
button:disabled { opacity: 0.55; cursor: wait; }
|
|
36
|
+
.service-tools { display: none; padding-top: 18px; margin-top: 18px; border-top: 1px solid #edf3f4; }
|
|
37
|
+
main.configured .service-tools { display: block; }
|
|
38
|
+
.service-title { margin: 0 0 10px; color: #1a1f26; font-size: 12px; font-weight: 600; }
|
|
39
|
+
.prizes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
|
|
40
|
+
.prize { min-height: 64px; padding: 9px; background: #f7fafb; border: 1px solid #edf3f4; }
|
|
41
|
+
.prize strong, .prize span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
42
|
+
.prize strong { color: #1a1f26; font-size: 11px; }
|
|
43
|
+
.prize span, .hint, .status, .record { color: #6f7e8c; font-size: 10px; line-height: 1.5; }
|
|
44
|
+
.prize span { margin-top: 4px; }
|
|
45
|
+
.hint { margin: 8px 0 0; }
|
|
46
|
+
.secondary-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
|
|
47
|
+
.secondary-actions button { color: #4a8b8f; background: #fff; }
|
|
48
|
+
.upload { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
|
|
49
|
+
.upload label { display: grid; min-height: 32px; place-items: center; padding: 0 9px; color: #4a8b8f; border: 1px solid #d5e8e8; font-size: 10px; cursor: pointer; }
|
|
50
|
+
.upload input { position: absolute; width: 1px; height: 1px; opacity: 0; }
|
|
51
|
+
.upload span { min-width: 0; overflow: hidden; color: #6f7e8c; font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
|
|
52
|
+
.status { margin-top: 10px; }
|
|
53
|
+
.status.error { color: #a33b32; }
|
|
54
|
+
.records { margin-top: 14px; }
|
|
55
|
+
.record { display: flex; justify-content: space-between; gap: 8px; padding: 7px 0; border-bottom: 1px solid #edf3f4; }
|
|
56
|
+
.record strong { color: #1a1f26; font-weight: 600; }
|
|
57
|
+
@media (min-width: 480px) { body { display: grid; place-items: center; padding: 24px; } main { min-height: 640px; border: 1px solid #d8e3e8; } }
|
|
58
|
+
</style>
|
|
59
|
+
</head>
|
|
60
|
+
<body>
|
|
61
|
+
<main id="app">
|
|
62
|
+
<header><h1 id="title">抽卡活动</h1></header>
|
|
63
|
+
<div class="card" id="card">
|
|
64
|
+
<div class="card-mark" aria-hidden="true">+</div>
|
|
65
|
+
<strong id="card-title">神秘奖励</strong
|
|
66
|
+
><span id="card-text">点击下方按钮抽取</span>
|
|
67
|
+
</div>
|
|
68
|
+
<div class="odds"><span>剩余次数</span><strong id="quota">3 次</strong></div>
|
|
69
|
+
<button id="draw" type="button">立即抽取</button>
|
|
70
|
+
|
|
71
|
+
<section class="service-tools" id="service-tools" aria-label="卡池详情">
|
|
72
|
+
<h2 class="service-title">卡池与概率</h2>
|
|
73
|
+
<div class="prizes" id="prizes"></div>
|
|
74
|
+
<p class="hint">概率仅供展示,库存、保底和抽卡结果以服务端确认为准。</p>
|
|
75
|
+
<div class="upload"><label for="proof">选择结果素材</label><input id="proof" type="file" accept="image/jpeg,image/png,image/webp" /><span id="proof-name">可选,上传后随本次抽卡保存</span></div>
|
|
76
|
+
<div class="secondary-actions"><button id="draw-ten" type="button">十连抽</button><button id="refresh" type="button">刷新记录</button></div>
|
|
77
|
+
<div class="status" id="status" role="status" aria-live="polite"></div>
|
|
78
|
+
<div class="records" id="records"></div>
|
|
79
|
+
</section>
|
|
80
|
+
</main>
|
|
81
|
+
<script>
|
|
82
|
+
window.ACTIVITY_API_BASE = "";
|
|
83
|
+
window.ACTIVITY_ENV_ID = "";
|
|
84
|
+
window.ACTIVITY_POOL_ID = "default";
|
|
85
|
+
const demoPool = { poolId: "default", title: "抽卡活动", drawLimit: 3, pityThreshold: 10, prizes: [
|
|
86
|
+
{ code: "points-100", name: "积分加成卡", rarity: "普通", weight: 70, description: "恭喜你,获得 100 积分" },
|
|
87
|
+
{ code: "points-500", name: "500 积分", rarity: "稀有", weight: 25, description: "恭喜你,获得 500 积分" },
|
|
88
|
+
{ code: "badge", name: "限定徽章", rarity: "传说", weight: 5, description: "恭喜你,获得限定徽章" }
|
|
89
|
+
] };
|
|
90
|
+
const app = document.querySelector("#app");
|
|
91
|
+
const drawButton = document.querySelector("#draw");
|
|
92
|
+
const drawTenButton = document.querySelector("#draw-ten");
|
|
93
|
+
const statusNode = document.querySelector("#status");
|
|
94
|
+
let pool = demoPool;
|
|
95
|
+
let proofUrl = "";
|
|
96
|
+
let busy = false;
|
|
97
|
+
let configured = false;
|
|
98
|
+
|
|
99
|
+
function setStatus(message, error) { statusNode.textContent = message || ""; statusNode.className = "status" + (error ? " error" : ""); }
|
|
100
|
+
function setButtonsDisabled(disabled) { drawButton.disabled = disabled; drawTenButton.disabled = disabled; }
|
|
101
|
+
function escapeHtml(value) { return String(value == null ? "" : value).replace(/[&<>'"]/g, (char) => ({ "&": "&", "<": "<", ">": ">", "'": "'", '"': """ }[char])); }
|
|
102
|
+
function createRequestId() {
|
|
103
|
+
if (window.crypto && typeof window.crypto.randomUUID === "function") return window.crypto.randomUUID();
|
|
104
|
+
const bytes = new Uint8Array(16);
|
|
105
|
+
if (window.crypto && typeof window.crypto.getRandomValues === "function") window.crypto.getRandomValues(bytes); else for (let index = 0; index < bytes.length; index += 1) bytes[index] = Math.floor(Math.random() * 256);
|
|
106
|
+
bytes[6] = (bytes[6] & 15) | 64; bytes[8] = (bytes[8] & 63) | 128;
|
|
107
|
+
return Array.from(bytes, (value) => value.toString(16).padStart(2, "0")).join("").replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
|
|
108
|
+
}
|
|
109
|
+
async function request(path, options = {}) {
|
|
110
|
+
if (!window.ShaperAI?.cloud || !window.ACTIVITY_API_BASE || !window.ACTIVITY_ENV_ID) throw new Error("抽卡服务暂未配置");
|
|
111
|
+
const response = await window.ShaperAI.cloud.request({ url: window.ACTIVITY_API_BASE + path, method: options.method || "GET", data: options.data, envId: window.ACTIVITY_ENV_ID, auth: Boolean(options.auth), headers: { "X-Request-Id": createRequestId() } });
|
|
112
|
+
if (response?.statusCode === 401) throw new Error("请先登录");
|
|
113
|
+
if (!response || response.statusCode !== 200 || (response.code !== 0 && response.code !== 200)) throw new Error(response?.message || "服务暂时不可用");
|
|
114
|
+
return response.data;
|
|
115
|
+
}
|
|
116
|
+
function renderPool(data) {
|
|
117
|
+
pool = data || demoPool;
|
|
118
|
+
document.querySelector("#title").textContent = pool.title || "抽卡活动";
|
|
119
|
+
const prizes = Array.isArray(pool.prizes) ? pool.prizes : [];
|
|
120
|
+
const totalWeight = prizes.reduce((sum, item) => sum + Math.max(0, Number(item.weight) || 0), 0);
|
|
121
|
+
document.querySelector("#prizes").innerHTML = prizes.map((item) => { const probability = totalWeight ? (Number(item.weight || 0) / totalWeight * 100).toFixed(2) : "0.00"; return `<div class="prize"><strong>${escapeHtml(item.name)}</strong><span>${escapeHtml(item.rarity || "奖励")} · ${probability}%</span></div>`; }).join("") || '<p class="hint">当前卡池暂无可抽奖励</p>';
|
|
122
|
+
}
|
|
123
|
+
function renderQuota(value) { document.querySelector("#quota").textContent = `${Math.max(0, Number(value) || 0)} 次`; }
|
|
124
|
+
function renderRecords(records) { const list = Array.isArray(records) ? records : []; document.querySelector("#records").innerHTML = list.map((item) => `<div class="record"><span>${escapeHtml(item.createdAt || "")}</span><strong>${escapeHtml(item.prizeName || "奖励")}</strong></div>`).join("") || '<p class="hint">暂时没有抽卡记录</p>'; }
|
|
125
|
+
function renderDraw(data) {
|
|
126
|
+
const draws = Array.isArray(data?.draws) ? data.draws : [];
|
|
127
|
+
const first = draws[0];
|
|
128
|
+
if (!first) throw new Error("服务端未返回抽卡结果");
|
|
129
|
+
document.querySelector("#card").classList.add("drawn");
|
|
130
|
+
document.querySelector("#card-title").textContent = first.prizeName || "抽卡成功";
|
|
131
|
+
document.querySelector("#card-text").textContent = draws.length > 1 ? `本次共获得 ${draws.length} 个奖励` : (first.description || `恭喜你,获得${first.prizeName || "奖励"}`);
|
|
132
|
+
drawButton.textContent = "再抽一次";
|
|
133
|
+
renderQuota(data.remainingCount);
|
|
134
|
+
}
|
|
135
|
+
async function refreshRecords() { if (!configured) return; renderRecords(await request(`/draw/results?poolId=${encodeURIComponent(window.ACTIVITY_POOL_ID)}`, { auth: true })); }
|
|
136
|
+
async function runDraw(drawCount) {
|
|
137
|
+
if (busy) return;
|
|
138
|
+
busy = true; setButtonsDisabled(true); if (configured) setStatus(drawCount === 10 ? "正在进行十连抽..." : "正在抽取...");
|
|
139
|
+
try {
|
|
140
|
+
let result;
|
|
141
|
+
if (configured) result = await request("/draw", { method: "POST", auth: true, data: { poolId: window.ACTIVITY_POOL_ID, drawCount, ...(proofUrl ? { proofUrl } : {}) } });
|
|
142
|
+
else { const item = demoPool.prizes[0]; result = { remainingCount: Math.max(0, Number.parseInt(document.querySelector("#quota").textContent, 10) - drawCount), draws: Array.from({ length: drawCount }, () => ({ prizeName: item.name, rarity: item.rarity, description: item.description })) }; }
|
|
143
|
+
renderDraw(result);
|
|
144
|
+
if (configured) { setStatus("抽卡成功"); await refreshRecords(); }
|
|
145
|
+
} catch (error) { if (configured) setStatus(error.message || "抽卡失败,请稍后重试", true); }
|
|
146
|
+
finally { busy = false; setButtonsDisabled(false); }
|
|
147
|
+
}
|
|
148
|
+
async function uploadProof(file) {
|
|
149
|
+
if (!file) return;
|
|
150
|
+
if (!["image/jpeg", "image/png", "image/webp"].includes(file.type)) throw new Error("仅支持 JPEG、PNG 或 WebP 图片");
|
|
151
|
+
if (file.size > 10 * 1024 * 1024) throw new Error("图片不能超过 10MB");
|
|
152
|
+
if (!window.ShaperAI?.oss || typeof window.ShaperAI.oss.uploadFile !== "function") throw new Error("当前环境不支持素材上传");
|
|
153
|
+
const result = await window.ShaperAI.oss.uploadFile({ file, filename: `draw-proof-${Date.now()}.${file.type.split("/")[1] || "png"}` });
|
|
154
|
+
const url = new URL(result?.downloadUrl || "");
|
|
155
|
+
if (url.protocol !== "https:" || !url.hostname) throw new Error("素材上传失败");
|
|
156
|
+
proofUrl = url.href; document.querySelector("#proof-name").textContent = `已上传:${file.name}`; setStatus("素材已上传,可以开始抽卡");
|
|
157
|
+
}
|
|
158
|
+
async function initialize() {
|
|
159
|
+
configured = Boolean(window.ACTIVITY_API_BASE && window.ACTIVITY_ENV_ID && window.ShaperAI?.cloud);
|
|
160
|
+
if (!configured) { renderPool(demoPool); renderQuota(demoPool.drawLimit); return; }
|
|
161
|
+
app.classList.add("configured"); setButtonsDisabled(true); setStatus("正在加载卡池...");
|
|
162
|
+
try {
|
|
163
|
+
renderPool(await request(`/pool?poolId=${encodeURIComponent(window.ACTIVITY_POOL_ID)}`));
|
|
164
|
+
const auth = await window.ShaperAI.cloud.auth({ envId: window.ACTIVITY_ENV_ID });
|
|
165
|
+
if (!auth || (auth.code !== 0 && auth.code !== 200)) throw new Error(auth?.message || "请先登录");
|
|
166
|
+
const [quota, records] = await Promise.all([request(`/draw/quota?poolId=${encodeURIComponent(window.ACTIVITY_POOL_ID)}`, { auth: true }), request(`/draw/results?poolId=${encodeURIComponent(window.ACTIVITY_POOL_ID)}`, { auth: true })]);
|
|
167
|
+
renderQuota(quota?.remainingCount); renderRecords(records); setStatus("可以开始抽卡");
|
|
168
|
+
} catch (error) { setStatus(error.message || "活动加载失败", true); }
|
|
169
|
+
finally { setButtonsDisabled(false); }
|
|
170
|
+
}
|
|
171
|
+
drawButton.addEventListener("click", () => runDraw(1));
|
|
172
|
+
drawTenButton.addEventListener("click", () => runDraw(10));
|
|
173
|
+
document.querySelector("#refresh").addEventListener("click", () => refreshRecords().catch((error) => setStatus(error.message || "记录加载失败", true)));
|
|
174
|
+
document.querySelector("#proof").addEventListener("change", (event) => uploadProof(event.target.files[0]).catch((error) => { proofUrl = ""; event.target.value = ""; document.querySelector("#proof-name").textContent = "可选,上传后随本次抽卡保存"; setStatus(error.message || "素材上传失败", true); }));
|
|
175
|
+
initialize();
|
|
176
|
+
</script>
|
|
177
|
+
</body>
|
|
178
|
+
</html>
|
package/skills/business/activity/hupu-lottery-draw/example/migrations/20260828220000_create_draw.sql
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
create extension if not exists pgcrypto;
|
|
2
|
+
|
|
3
|
+
-- Reuse the platform demo table as the activity's card-pool configuration.
|
|
4
|
+
alter table public.demo_items rename to draw_pools;
|
|
5
|
+
truncate table public.draw_pools;
|
|
6
|
+
|
|
7
|
+
alter table public.draw_pools
|
|
8
|
+
add column if not exists pool_id text,
|
|
9
|
+
add column if not exists title text not null default '抽卡活动',
|
|
10
|
+
add column if not exists description text not null default '',
|
|
11
|
+
add column if not exists cover_url text not null default '',
|
|
12
|
+
add column if not exists starts_at timestamptz not null default now(),
|
|
13
|
+
add column if not exists ends_at timestamptz not null default now() + interval '30 days',
|
|
14
|
+
add column if not exists draw_limit integer not null default 3,
|
|
15
|
+
add column if not exists pity_threshold integer not null default 10,
|
|
16
|
+
add column if not exists status text not null default 'active';
|
|
17
|
+
|
|
18
|
+
update public.draw_pools set pool_id = 'default' where pool_id is null;
|
|
19
|
+
alter table public.draw_pools alter column pool_id set not null;
|
|
20
|
+
create unique index if not exists draw_pools_pool_uidx on public.draw_pools (pool_id);
|
|
21
|
+
alter table public.draw_pools add constraint draw_pools_limit_chk check (draw_limit >= 0 and pity_threshold > 0);
|
|
22
|
+
alter table public.draw_pools add constraint draw_pools_status_chk check (status in ('draft', 'active', 'paused', 'ended'));
|
|
23
|
+
|
|
24
|
+
create table if not exists public.draw_prizes (
|
|
25
|
+
id uuid primary key default gen_random_uuid(),
|
|
26
|
+
pool_id text not null references public.draw_pools(pool_id) on delete cascade,
|
|
27
|
+
prize_code text not null,
|
|
28
|
+
prize_name text not null,
|
|
29
|
+
rarity text not null default '普通',
|
|
30
|
+
description text not null default '',
|
|
31
|
+
weight integer not null default 0,
|
|
32
|
+
total_count integer not null default 0,
|
|
33
|
+
remaining_count integer not null default 0,
|
|
34
|
+
is_pity boolean not null default false,
|
|
35
|
+
status text not null default 'active',
|
|
36
|
+
created_at timestamptz not null default now(),
|
|
37
|
+
updated_at timestamptz not null default now(),
|
|
38
|
+
constraint draw_prizes_weight_chk check (weight >= 0),
|
|
39
|
+
constraint draw_prizes_stock_chk check (total_count >= 0 and remaining_count >= 0 and remaining_count <= total_count),
|
|
40
|
+
constraint draw_prizes_status_chk check (status in ('active', 'paused', 'exhausted'))
|
|
41
|
+
);
|
|
42
|
+
create unique index if not exists draw_prizes_code_uidx on public.draw_prizes (pool_id, prize_code);
|
|
43
|
+
create index if not exists draw_prizes_pool_status_idx on public.draw_prizes (pool_id, status, remaining_count);
|
|
44
|
+
|
|
45
|
+
create table if not exists public.draw_user_quota (
|
|
46
|
+
id uuid primary key default gen_random_uuid(),
|
|
47
|
+
pool_id text not null references public.draw_pools(pool_id) on delete cascade,
|
|
48
|
+
puid text not null,
|
|
49
|
+
total_count integer not null default 0,
|
|
50
|
+
remaining_count integer not null default 0,
|
|
51
|
+
pity_count integer not null default 0,
|
|
52
|
+
created_at timestamptz not null default now(),
|
|
53
|
+
updated_at timestamptz not null default now(),
|
|
54
|
+
constraint draw_quota_count_chk check (total_count >= 0 and remaining_count >= 0 and remaining_count <= total_count),
|
|
55
|
+
constraint draw_quota_pity_chk check (pity_count >= 0)
|
|
56
|
+
);
|
|
57
|
+
create unique index if not exists draw_quota_pool_puid_uidx on public.draw_user_quota (pool_id, puid);
|
|
58
|
+
|
|
59
|
+
create table if not exists public.draw_sessions (
|
|
60
|
+
id uuid primary key default gen_random_uuid(),
|
|
61
|
+
request_id uuid not null,
|
|
62
|
+
pool_id text not null references public.draw_pools(pool_id) on delete restrict,
|
|
63
|
+
puid text not null,
|
|
64
|
+
draw_count integer not null,
|
|
65
|
+
proof_url text not null default '',
|
|
66
|
+
created_at timestamptz not null default now(),
|
|
67
|
+
constraint draw_sessions_count_chk check (draw_count between 1 and 10)
|
|
68
|
+
);
|
|
69
|
+
create unique index if not exists draw_sessions_request_uidx on public.draw_sessions (request_id);
|
|
70
|
+
create index if not exists draw_sessions_user_idx on public.draw_sessions (pool_id, puid, created_at desc);
|
|
71
|
+
|
|
72
|
+
create table if not exists public.draw_records (
|
|
73
|
+
id uuid primary key default gen_random_uuid(),
|
|
74
|
+
session_id uuid not null references public.draw_sessions(id) on delete cascade,
|
|
75
|
+
pool_id text not null references public.draw_pools(pool_id) on delete restrict,
|
|
76
|
+
puid text not null,
|
|
77
|
+
sequence_no integer not null,
|
|
78
|
+
prize_code text not null,
|
|
79
|
+
prize_name text not null,
|
|
80
|
+
rarity text not null,
|
|
81
|
+
description text not null default '',
|
|
82
|
+
created_at timestamptz not null default now(),
|
|
83
|
+
constraint draw_records_sequence_chk check (sequence_no between 1 and 10)
|
|
84
|
+
);
|
|
85
|
+
create unique index if not exists draw_records_session_sequence_uidx on public.draw_records (session_id, sequence_no);
|
|
86
|
+
create index if not exists draw_records_user_idx on public.draw_records (pool_id, puid, created_at desc);
|
|
87
|
+
create index if not exists draw_records_public_idx on public.draw_records (pool_id, created_at desc);
|
|
88
|
+
|
|
89
|
+
insert into public.draw_pools (pool_id, title, description, cover_url, starts_at, ends_at, draw_limit, pity_threshold, status)
|
|
90
|
+
values ('default', '夏日惊喜卡池', '抽取活动积分与限定徽章', '', now() - interval '1 day', now() + interval '30 days', 3, 10, 'active')
|
|
91
|
+
on conflict (pool_id) do update set title = excluded.title, description = excluded.description, cover_url = excluded.cover_url, ends_at = excluded.ends_at, draw_limit = excluded.draw_limit, pity_threshold = excluded.pity_threshold, status = excluded.status;
|
|
92
|
+
|
|
93
|
+
insert into public.draw_prizes (pool_id, prize_code, prize_name, rarity, description, weight, total_count, remaining_count, is_pity, status)
|
|
94
|
+
values
|
|
95
|
+
('default', 'points-100', '100 积分', '普通', '活动积分', 70, 999, 999, false, 'active'),
|
|
96
|
+
('default', 'points-500', '500 积分', '稀有', '活动积分', 25, 100, 100, false, 'active'),
|
|
97
|
+
('default', 'badge', '限定徽章', '传说', '可在活动页展示', 5, 20, 20, true, 'active')
|
|
98
|
+
on conflict (pool_id, prize_code) do update set weight = excluded.weight, total_count = excluded.total_count, remaining_count = excluded.remaining_count, is_pity = excluded.is_pity, status = excluded.status;
|
|
99
|
+
|
|
100
|
+
alter table public.draw_pools disable row level security;
|
|
101
|
+
alter table public.draw_prizes disable row level security;
|
|
102
|
+
alter table public.draw_user_quota disable row level security;
|
|
103
|
+
alter table public.draw_sessions disable row level security;
|
|
104
|
+
alter table public.draw_records disable row level security;
|
|
105
|
+
|
|
106
|
+
create or replace function public.perform_draw(
|
|
107
|
+
p_puid text,
|
|
108
|
+
p_pool_id text,
|
|
109
|
+
p_draw_count integer,
|
|
110
|
+
p_proof_url text,
|
|
111
|
+
p_request_id uuid
|
|
112
|
+
)
|
|
113
|
+
returns table (
|
|
114
|
+
session_id uuid,
|
|
115
|
+
draw_id uuid,
|
|
116
|
+
prize_code text,
|
|
117
|
+
prize_name text,
|
|
118
|
+
rarity text,
|
|
119
|
+
description text,
|
|
120
|
+
created_at timestamptz,
|
|
121
|
+
remaining_count integer,
|
|
122
|
+
pity_count integer
|
|
123
|
+
)
|
|
124
|
+
language plpgsql
|
|
125
|
+
security definer
|
|
126
|
+
set search_path = public
|
|
127
|
+
as $$
|
|
128
|
+
declare
|
|
129
|
+
pool_row public.draw_pools;
|
|
130
|
+
quota_row public.draw_user_quota;
|
|
131
|
+
session_row public.draw_sessions;
|
|
132
|
+
existing_session public.draw_sessions;
|
|
133
|
+
prize_row public.draw_prizes;
|
|
134
|
+
total_weight bigint;
|
|
135
|
+
random_target bigint;
|
|
136
|
+
cumulative_weight bigint;
|
|
137
|
+
next_pity integer;
|
|
138
|
+
i integer;
|
|
139
|
+
begin
|
|
140
|
+
if p_puid is null or length(trim(p_puid)) = 0 then raise exception using errcode = '22023', message = '请先登录'; end if;
|
|
141
|
+
if p_draw_count is null or p_draw_count < 1 or p_draw_count > 10 then raise exception using errcode = '22023', message = '抽卡次数必须是 1 到 10'; end if;
|
|
142
|
+
|
|
143
|
+
select * into existing_session from public.draw_sessions where request_id = p_request_id;
|
|
144
|
+
if existing_session.id is not null then
|
|
145
|
+
if existing_session.puid <> p_puid or existing_session.pool_id <> p_pool_id then raise exception using errcode = '23505', message = '请求 ID 已被使用'; end if;
|
|
146
|
+
return query
|
|
147
|
+
select s.id, r.id, r.prize_code, r.prize_name, r.rarity, r.description, r.created_at, q.remaining_count, q.pity_count
|
|
148
|
+
from public.draw_sessions s join public.draw_records r on r.session_id = s.id join public.draw_user_quota q on q.pool_id = s.pool_id and q.puid = s.puid
|
|
149
|
+
where s.id = existing_session.id order by r.sequence_no;
|
|
150
|
+
return;
|
|
151
|
+
end if;
|
|
152
|
+
|
|
153
|
+
select * into pool_row from public.draw_pools where pool_id = p_pool_id and status = 'active' for update;
|
|
154
|
+
if pool_row.pool_id is null then raise exception using errcode = '22023', message = '卡池不存在或未开放'; end if;
|
|
155
|
+
if now() < pool_row.starts_at then raise exception using errcode = '22023', message = '活动尚未开始'; end if;
|
|
156
|
+
if now() >= pool_row.ends_at then raise exception using errcode = '22023', message = '活动已结束'; end if;
|
|
157
|
+
|
|
158
|
+
insert into public.draw_user_quota (pool_id, puid, total_count, remaining_count) values (p_pool_id, p_puid, pool_row.draw_limit, pool_row.draw_limit) on conflict (pool_id, puid) do nothing;
|
|
159
|
+
select * into quota_row from public.draw_user_quota where pool_id = p_pool_id and puid = p_puid for update;
|
|
160
|
+
if quota_row.remaining_count < p_draw_count then raise exception using errcode = '22023', message = '剩余次数不足'; end if;
|
|
161
|
+
|
|
162
|
+
insert into public.draw_sessions (request_id, pool_id, puid, draw_count, proof_url) values (p_request_id, p_pool_id, p_puid, p_draw_count, coalesce(p_proof_url, '')) returning * into session_row;
|
|
163
|
+
for i in 1..p_draw_count loop
|
|
164
|
+
select coalesce(sum(weight), 0) into total_weight from public.draw_prizes where pool_id = p_pool_id and status = 'active' and remaining_count > 0;
|
|
165
|
+
if total_weight <= 0 then raise exception using errcode = '22023', message = '卡池库存不足'; end if;
|
|
166
|
+
next_pity := quota_row.pity_count + 1;
|
|
167
|
+
if next_pity >= pool_row.pity_threshold then
|
|
168
|
+
select * into prize_row from public.draw_prizes where pool_id = p_pool_id and status = 'active' and remaining_count > 0 and is_pity = true order by id limit 1;
|
|
169
|
+
end if;
|
|
170
|
+
if prize_row.id is null then
|
|
171
|
+
-- gen_random_uuid() supplies fresh entropy; hashing it avoids exposing a seed to callers.
|
|
172
|
+
random_target := mod(abs(hashtextextended(gen_random_uuid()::text, extract(epoch from clock_timestamp())::bigint)), total_weight);
|
|
173
|
+
cumulative_weight := 0;
|
|
174
|
+
for prize_row in select * from public.draw_prizes where pool_id = p_pool_id and status = 'active' and remaining_count > 0 order by id loop
|
|
175
|
+
cumulative_weight := cumulative_weight + prize_row.weight;
|
|
176
|
+
if cumulative_weight > random_target then exit; end if;
|
|
177
|
+
end loop;
|
|
178
|
+
end if;
|
|
179
|
+
update public.draw_prizes set remaining_count = remaining_count - 1, status = case when remaining_count = 1 then 'exhausted' else status end, updated_at = now() where id = prize_row.id;
|
|
180
|
+
insert into public.draw_records (session_id, pool_id, puid, sequence_no, prize_code, prize_name, rarity, description) values (session_row.id, p_pool_id, p_puid, i, prize_row.prize_code, prize_row.prize_name, prize_row.rarity, prize_row.description);
|
|
181
|
+
quota_row.pity_count := case when prize_row.is_pity then 0 else next_pity end;
|
|
182
|
+
prize_row := null;
|
|
183
|
+
end loop;
|
|
184
|
+
update public.draw_user_quota set remaining_count = remaining_count - p_draw_count, pity_count = quota_row.pity_count, updated_at = now() where id = quota_row.id returning * into quota_row;
|
|
185
|
+
return query select session_row.id, r.id, r.prize_code, r.prize_name, r.rarity, r.description, r.created_at, quota_row.remaining_count, quota_row.pity_count from public.draw_records r where r.session_id = session_row.id order by r.sequence_no;
|
|
186
|
+
end;
|
|
187
|
+
$$;
|
|
188
|
+
|
|
189
|
+
grant all on table public.draw_pools, public.draw_prizes, public.draw_user_quota, public.draw_sessions, public.draw_records to public;
|
|
190
|
+
grant usage on schema public to anon, authenticated, service_role, public;
|
|
191
|
+
grant select on table public.draw_pools, public.draw_prizes, public.draw_records to anon, authenticated, service_role, public;
|
|
192
|
+
grant insert, update, delete on table public.draw_user_quota, public.draw_sessions, public.draw_records to authenticated, service_role, public;
|
|
193
|
+
grant execute on function public.perform_draw(text, text, integer, text, uuid) to public, anon, authenticated, service_role;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hupu-pk-voting
|
|
3
|
+
description: 使用活动数据存储、CloudBase 请求与 OSS 文件上传创建可运行的移动端 PK 投票 H5;适用于双选对战、方案二选一和候选人 PK 投票,不用于静态投票图片、后台报表或现金博彩。
|
|
4
|
+
metadata:
|
|
5
|
+
required-skills:
|
|
6
|
+
- act-cloudbase
|
|
7
|
+
- hupu-cloud-request
|
|
8
|
+
- hupu-oss-upload-file
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# PK 投票功能模板
|
|
12
|
+
|
|
13
|
+
当用户提出“实现一个 PK 投票功能”或同义需求时,交付一个真实可运行的移动端原生 H5。页面展示两个候选项的票数与进度,允许用户在规则允许时投票,并在服务端确认后刷新结果。
|
|
14
|
+
|
|
15
|
+
必须组合以下能力:
|
|
16
|
+
|
|
17
|
+
- `act-cloudbase`:复用活动的 `activity_api` 云函数、PostgreSQL 和 `/api` 网关保存对阵配置、投票明细与汇总票数。
|
|
18
|
+
- `hupu-cloud-request`:安全访问公开读取、投票状态和投票接口。
|
|
19
|
+
- `hupu-oss-upload-file`:用户主动选择应援图或投票凭证时,使用 `window.ShaperAI.oss.uploadFile` 上传 `Blob`/`File`,成功后把 CDN URL 作为可选的 `proofUrl` 提交;不自建上传接口。
|
|
20
|
+
|
|
21
|
+
## 实施顺序
|
|
22
|
+
|
|
23
|
+
1. 先阅读 `example/migrations/20260828220000_create_pk_vote.sql` 和 `example/README.md`,在 `act-cloudbase` 预置的 `activity_api` 基线上改造。保留环境、网关鉴权、CORS、迁移留档和 `x-cloudbase-context` 身份约定,不创建第二个云函数。
|
|
24
|
+
2. 复制 `example/index.html` 到活动的 `h5/index.html`。替换活动环境 ID、网关返回的真实 API 地址、对阵 ID 和标题;页面必须是单一原生 HTML/CSS/JS,可双击打开,不引入 Vue、React、Vite 或运行时打包器。
|
|
25
|
+
3. 页面先公开读取对阵和票数,再使用 `window.ShaperAI.cloud.request` 读取投票状态和提交投票;由服务端从网关注入的上下文识别请求用户并校验业务参数。禁止页面直接 `fetch`、拼接 Bearer、提交用户身份字段或从 URL 接受活动身份。
|
|
26
|
+
4. 应援图上传只在用户主动选择文件后执行。先校验图片类型且不超过 10 MB,再调用 `window.ShaperAI.oss.uploadFile({ file, filename })`;只有 `downloadUrl` 是绝对 `https://` URL 时才保存为 `proofUrl`。上传失败不伪造成功、不自动重试。
|
|
27
|
+
5. 投票请求只提交业务字段 `{ matchId, side, proofUrl? }`。服务端从网关注入的 context 取得用户身份,在一个数据库事务中校验对阵时间、状态、side 白名单和重复投票,再写入明细并递增汇总票数。写接口内不扫描整张投票表。
|
|
28
|
+
6. 页面覆盖加载、空数据、网络失败、上传中、投票中、已投票、活动未开始/已结束和重复投票状态。错误优先展示服务端 `message`;HTTP `401/409/429/5xx` 给出可执行提示,不能把乐观更新当成服务端成功。
|
|
29
|
+
7. 候选名称、描述、错误信息和图片地址都视为外部数据。使用 `textContent`/DOM 属性渲染,图片只接受绝对 `https://` 地址。按钮在请求期间禁用,避免重复投票和上传。
|
|
30
|
+
8. 本地 `file://` 双击预览可使用内置演示数据;部署页面不得用演示数据掩盖配置或接口错误。上线前应用迁移、部署固定 `activity_api`、创建读写鉴权匹配的网关入口,并把 `queryGateway` 返回的实际 URL 写入 H5 和活动 manifest。
|
|
31
|
+
|
|
32
|
+
## 示例目录
|
|
33
|
+
|
|
34
|
+
- `example/index.html`:保留附件简洁视觉风格的原生移动端投票页,包含公开结果、用户状态、投票、可选应援图上传和完整状态处理。
|
|
35
|
+
- `example/cloudfunctions/activity_api/`:固定 `activity_api` HTTP 云函数,提供 `/health`、公开对阵/结果、用户投票状态和事务投票路由。
|
|
36
|
+
- `example/migrations/20260828220000_create_pk_vote.sql`:对阵、候选项、投票明细、索引、权限、演示数据和原子投票 RPC。
|
|
37
|
+
- `example/README.md`:接口契约、网关鉴权拆分、配置和冒烟检查。
|
|
38
|
+
|
|
39
|
+
## 业务与数据约束
|
|
40
|
+
|
|
41
|
+
- 默认每个用户每场只能投一票,由 `(match_id, puid)` 唯一约束保证;如业务允许每日多票,应在迁移与 RPC 中增加业务日期维度,不能用进程内内存计数代替数据库约束。
|
|
42
|
+
- 公开读取只按指定 `matchId` 查询一条对阵和两条候选记录;结果读取使用候选表中的汇总票数,不在请求时聚合整张投票明细。
|
|
43
|
+
- `proofUrl` 仅作为可选业务附件,必须是长度受限的 HTTPS URL,不作为投票身份凭据。
|
|
44
|
+
- 候选项 `side` 只能为 `left` 或 `right`;活动开始、结束和冻结状态以服务端时间与数据库配置为准。
|
|
45
|
+
- 生产环境需要在上传链路或服务端补充图片内容安全检查;客户端 MIME/大小校验不能替代服务端校验。
|
|
46
|
+
|
|
47
|
+
## 交付标准
|
|
48
|
+
|
|
49
|
+
- `h5/index.html`:两个候选项、票数/百分比、投票动作、当前用户状态、可选应援图上传、状态提示和移动端适配。
|
|
50
|
+
- `activity/migrations/<14位时间戳>_*.sql`:对阵、候选项、投票明细、原子投票 RPC、必要索引/唯一约束、显式 `GRANT` 和 RLS 处理。
|
|
51
|
+
- `activity/cloudfunctions/activity_api/`:固定 `activity_api` 函数;用户身份只从 `x-cloudbase-context` 读取。
|
|
52
|
+
- `activity/activity.manifest.json`:记录实际环境、表、数据库例程、函数、网关 URL、权限和冒烟结果。
|
|
53
|
+
|
|
54
|
+
完成后说明真实对阵 ID、投票限制、截止时间、网关读写鉴权配置、应援图用途和异常降级行为。`example/` 只作可复制参考,不直接作为活动线上资源发布。
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# PK Vote Example
|
|
2
|
+
|
|
3
|
+
This directory is a reference implementation for the `hupu-pk-voting` skill.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- `index.html`: standalone mobile-first H5 using ShaperAI Cloud auth/request and optional OSS upload.
|
|
8
|
+
- `cloudfunctions/activity_api/`: the fixed Node.js HTTP function for public reads and authenticated voting.
|
|
9
|
+
- `migrations/20260828220000_create_pk_vote.sql`: PostgreSQL tables, indexes, seed data, grants, and the transactional vote RPC.
|
|
10
|
+
|
|
11
|
+
## Frontend configuration
|
|
12
|
+
|
|
13
|
+
Copy `index.html` to the activity's `h5/index.html`, then replace:
|
|
14
|
+
|
|
15
|
+
- `window.ACTIVITY_API_BASE` with the exact URL returned by `queryGateway(action="getAccess")`; do not construct a gateway domain.
|
|
16
|
+
- `window.ACTIVITY_ENV_ID` with the canonical CloudBase environment ID.
|
|
17
|
+
- `window.PK_MATCH_ID` with the deployed match ID.
|
|
18
|
+
|
|
19
|
+
Only `file://`, `localhost`, and `127.0.0.1` use the built-in demonstration match. A deployed page with missing configuration or an unavailable SDK shows a configuration error instead of silently falling back to demo data.
|
|
20
|
+
|
|
21
|
+
## API contract
|
|
22
|
+
|
|
23
|
+
| Method | Path | Gateway auth | Description |
|
|
24
|
+
| --- | --- | --- | --- |
|
|
25
|
+
| `GET` | `/api/health` | public | Function health check. |
|
|
26
|
+
| `GET` | `/api/pk/matches/:matchId` | public | Match configuration and both candidates. |
|
|
27
|
+
| `GET` | `/api/pk/matches/:matchId/results` | public | Current counts, percentages, and total votes. |
|
|
28
|
+
| `GET` | `/api/pk/me?matchId=:matchId` | authenticated | Current user's vote and remaining count. |
|
|
29
|
+
| `POST` | `/api/pk/votes` | authenticated | Body `{ "matchId": "...", "side": "left|right", "proofUrl": "https://..." }`. |
|
|
30
|
+
|
|
31
|
+
Responses use `{ "code": 0, "message": "success", "data": ..., "requestId": "..." }`. The gateway context is the only identity source; neither the page nor the function accepts a client `puid`.
|
|
32
|
+
|
|
33
|
+
If the gateway cannot split authentication by method/path in one access entry, create separate public read and authenticated write access paths that still target the same `activity_api` function. Keep `/health`, match, and result reads public; require auth for `/pk/me` and `/pk/votes`.
|
|
34
|
+
|
|
35
|
+
## Deployment and smoke checks
|
|
36
|
+
|
|
37
|
+
1. Replace the seed match, candidates, schedule, and copy in the migration, then apply it to a new activity environment.
|
|
38
|
+
2. Deploy `cloudfunctions/activity_api` as one Node.js HTTP Function listening on port `9000`.
|
|
39
|
+
3. Configure explicit CORS, gateway access, and invoke permission. Read back the real public URL and write it into both the H5 and manifest.
|
|
40
|
+
4. Verify `GET /health`, public match/result reads, and `OPTIONS /pk/votes` against that exact URL.
|
|
41
|
+
5. Verify unauthenticated `/pk/me` and `/pk/votes` return `401`, an authenticated vote returns `201`, a duplicate vote returns `409`, and the result count and `/pk/me` state agree afterward.
|
|
42
|
+
6. Inspect all three table schemas and query the seeded match. Generate the activity's manifest from the actual deployed resources, then record the schema and route verification evidence.
|
|
43
|
+
|
|
44
|
+
The candidate table stores bounded summary counts so public result reads never scan the vote detail table. `submit_pk_vote` serializes attempts per match/user with a PostgreSQL advisory transaction lock, validates the schedule and side, inserts the detail row, and increments the candidate count in one transaction.
|