feat: launch PK testing application
Verify mini program and deploy preview / verify-deploy (push) Failing after 34s
Verify mini program and deploy preview / verify-deploy (push) Failing after 34s
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
.git
|
||||
.gitea
|
||||
pages
|
||||
utils
|
||||
scripts
|
||||
app.js
|
||||
app.json
|
||||
app.css
|
||||
project.config.json
|
||||
README.md
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
name: Verify mini program and deploy preview
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
concurrency:
|
||||
group: pk-lab-xhs-production
|
||||
cancel-in-progress: false
|
||||
jobs:
|
||||
verify-deploy:
|
||||
runs-on: ubuntu-latest
|
||||
container: { image: docker:27-cli }
|
||||
timeout-minutes: 20
|
||||
env:
|
||||
SOURCE_SERVER: ${{ gitea.server_url }}
|
||||
SOURCE_REPOSITORY: ${{ gitea.repository }}
|
||||
SOURCE_REVISION: ${{ gitea.sha }}
|
||||
DEPLOY_PORT: ${{ vars.DEPLOY_PORT || '3001' }}
|
||||
API_BASE_URL: ${{ vars.API_BASE_URL || 'http://47.76.92.159:18082' }}
|
||||
steps:
|
||||
- name: Fetch exact revision
|
||||
env: { SOURCE_TOKEN: "${{ secrets.GITEA_TOKEN }}" }
|
||||
run: |
|
||||
set -eu
|
||||
apk add --no-cache python3 nodejs curl
|
||||
python3 - <<'PY'
|
||||
import io,os,shutil,tarfile,tempfile,urllib.request
|
||||
from pathlib import Path
|
||||
url=f"{os.environ['SOURCE_SERVER'].rstrip('/')}/api/v1/repos/{os.environ['SOURCE_REPOSITORY']}/archive/{os.environ['SOURCE_REVISION']}.tar.gz"
|
||||
req=urllib.request.Request(url,headers={'Authorization':'token '+os.environ['SOURCE_TOKEN']})
|
||||
with urllib.request.urlopen(req,timeout=60) as r:data=r.read()
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
with tarfile.open(fileobj=io.BytesIO(data),mode='r:gz') as a:a.extractall(d,filter='data')
|
||||
roots=list(Path(d).iterdir());shutil.copytree(roots[0],Path.cwd(),dirs_exist_ok=True)
|
||||
PY
|
||||
- name: Check source and preview
|
||||
run: npm run check
|
||||
- name: Build preview image
|
||||
run: docker build -t "pk-lab-xhs:$SOURCE_REVISION" .
|
||||
- name: Update permanent preview container
|
||||
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/main'
|
||||
run: |
|
||||
set -eu
|
||||
docker rm -f pk-lab-xhs-preview 2>/dev/null || true
|
||||
docker run -d --name pk-lab-xhs-preview --restart unless-stopped --read-only --tmpfs /var/cache/nginx --tmpfs /var/run -e API_BASE_URL="$API_BASE_URL" -p "$DEPLOY_PORT:8080" "pk-lab-xhs:$SOURCE_REVISION"
|
||||
for i in 1 2 3 4 5 6; do curl -fsS "http://127.0.0.1:$DEPLOY_PORT/health" && exit 0; sleep 2; done
|
||||
docker logs pk-lab-xhs-preview
|
||||
exit 1
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
.DS_Store
|
||||
node_modules/
|
||||
project.private.config.json
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
FROM nginx:1.27-alpine
|
||||
COPY web /usr/share/nginx/html
|
||||
RUN printf 'server { listen 8080; server_name _; root /usr/share/nginx/html; location = /runtime-env.js { alias /tmp/runtime-env.js; } location / { try_files $uri $uri/ /index.html; } location = /health { access_log off; return 200 "ok\\n"; add_header Content-Type text/plain; } }' > /etc/nginx/conf.d/default.conf
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=5 CMD wget -q -O- http://127.0.0.1:8080/health || exit 1
|
||||
CMD ["/bin/sh", "-c", "envsubst '$API_BASE_URL' < /usr/share/nginx/html/runtime-env.js > /tmp/runtime-env.js && exec nginx -g 'daemon off;'"]
|
||||
@@ -0,0 +1,13 @@
|
||||
# PK 研究所(综合版)
|
||||
|
||||
原生小红书小程序源码,同时提供由 Nginx 常驻托管的公网功能预览。综合版包含瀑布流测试广场、二选一淘汰赛、我的历史、跨小程序绑定码与三种可保存的分享图模板。
|
||||
|
||||
## 接入
|
||||
|
||||
1. 在 `app.js` 将 `apiBase` 改成生产 HTTPS API。
|
||||
2. 在小红书开放平台将该域名加入 request 合法域名。
|
||||
3. 用小红书开发者工具打开仓库根目录,绑定综合版 App ID。
|
||||
4. 后端配置同一 App ID/Secret 后重新部署。
|
||||
|
||||
CI 默认把公网预览持续部署到宿主机 `3001` 端口;配置变量 `DEPLOY_PORT` 可覆盖,`API_BASE_URL` 指向后端。预览使用后端显式开启的 `TEST_AUTH_ENABLED` 测试身份,正式小程序使用 `xhs.login()`。
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
page { background: #fff8f2; color: #201d1b; font-family: system-ui, -apple-system, sans-serif; }
|
||||
.page { min-height: 100vh; padding: 28rpx; box-sizing: border-box; }
|
||||
.muted { color: #867d76; }
|
||||
.pill { display: inline-flex; padding: 10rpx 20rpx; border-radius: 999rpx; background: #fff0e4; color: #b65224; font-size: 24rpx; }
|
||||
.primary { background: #25211f; color: #fff; border-radius: 999rpx; font-weight: 700; }
|
||||
.secondary { background: #fff; color: #25211f; border: 2rpx solid #e8ded7; border-radius: 999rpx; }
|
||||
.empty { padding: 100rpx 28rpx; text-align: center; color: #867d76; }
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
App({
|
||||
globalData: {
|
||||
apiBase: 'https://api.example.com',
|
||||
appKey: 'hub'
|
||||
},
|
||||
onLaunch: function () {
|
||||
var token = xhs.getStorageSync('pk_token');
|
||||
if (token) this.globalData.token = token;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"pages": [
|
||||
"pages/index/index",
|
||||
"pages/test/test",
|
||||
"pages/result/result",
|
||||
"pages/me/me"
|
||||
],
|
||||
"window": {
|
||||
"navigationBarBackgroundColor": "#fff8f2",
|
||||
"navigationBarTextStyle": "black",
|
||||
"navigationBarTitleText": "PK 研究所",
|
||||
"backgroundColor": "#fff8f2"
|
||||
},
|
||||
"networkTimeout": {
|
||||
"request": 10000
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "pk-lab-xhs",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": { "check": "node scripts/check.mjs" }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
.home { padding-bottom: 70rpx; }
|
||||
.hero { position: relative; overflow: hidden; background: #ff6b35; color: #fff; border-radius: 36rpx; padding: 40rpx 34rpx 34rpx; box-shadow: 0 18rpx 50rpx rgba(168, 65, 25, .18); }
|
||||
.hero-row { display: flex; justify-content: space-between; align-items: flex-start; }
|
||||
.eyebrow { font-size: 21rpx; letter-spacing: 4rpx; opacity: .8; }
|
||||
.title { width: 480rpx; margin-top: 18rpx; font-size: 58rpx; line-height: 1.05; font-weight: 900; }
|
||||
.hero-copy { margin-top: 42rpx; width: 480rpx; font-size: 27rpx; line-height: 1.6; opacity: .92; }
|
||||
.me-button { margin: 0; padding: 0 24rpx; min-width: 0; height: 64rpx; line-height: 64rpx; font-size: 25rpx; border-radius: 999rpx; background: rgba(255,255,255,.2); color: #fff; }
|
||||
.hero-stickers { margin-top: 30rpx; display: flex; justify-content: space-between; font-size: 30rpx; font-weight: 800; }
|
||||
.section-head { display: flex; justify-content: space-between; margin: 48rpx 6rpx 24rpx; font-size: 30rpx; font-weight: 800; }
|
||||
.section-head .muted { font-size: 24rpx; font-weight: 500; }
|
||||
.waterfall { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); grid-gap: 22rpx; align-items: start; }
|
||||
.test-card { overflow: hidden; background: #fff; border-radius: 28rpx; box-shadow: 0 9rpx 30rpx rgba(62, 42, 30, .08); }
|
||||
.test-card:active { transform: scale(.98); }
|
||||
.card-art { position: relative; height: 250rpx; overflow: hidden; }
|
||||
.card-1 .card-art { height: 310rpx; }
|
||||
.card-number { position: absolute; left: 20rpx; top: 18rpx; color: rgba(255,255,255,.8); font-size: 22rpx; font-weight: 800; }
|
||||
.card-emoji { position: absolute; left: 50%; top: 52%; transform: translate(-50%, -50%) rotate(-8deg); font-size: 112rpx; }
|
||||
.card-body { padding: 24rpx; }
|
||||
.card-title { font-size: 30rpx; font-weight: 900; }
|
||||
.card-copy { margin-top: 12rpx; color: #746a63; font-size: 23rpx; line-height: 1.45; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; }
|
||||
.card-meta { margin-top: 22rpx; display: flex; justify-content: space-between; color: #9a8e86; font-size: 20rpx; }
|
||||
.retry { margin-top: 24rpx; width: 240rpx; font-size: 24rpx; }
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
var api = require('../../utils/api');
|
||||
|
||||
Page({
|
||||
data: { tests: [], loading: true, error: '' },
|
||||
onLoad: function () { this.loadTests(); },
|
||||
onPullDownRefresh: function () { this.loadTests(true); },
|
||||
loadTests: function (refreshing) {
|
||||
var self = this;
|
||||
api.request('/api/v1/tests').then(function (tests) {
|
||||
self.setData({ tests: tests, loading: false, error: '' });
|
||||
}).catch(function (error) {
|
||||
self.setData({ loading: false, error: error.message || '加载失败' });
|
||||
}).then(function () {
|
||||
if (refreshing) xhs.stopPullDownRefresh();
|
||||
}, function () {
|
||||
if (refreshing) xhs.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
openTest: function (event) {
|
||||
xhs.navigateTo({ url: '/pages/test/test?slug=' + event.currentTarget.dataset.slug });
|
||||
},
|
||||
openMe: function () { xhs.navigateTo({ url: '/pages/me/me' }); }
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
{ "navigationBarTitleText": "PK 研究所" }
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<view class="page home">
|
||||
<view class="hero">
|
||||
<view class="hero-row">
|
||||
<view>
|
||||
<view class="eyebrow">TASTE LAB · 01</view>
|
||||
<view class="title">今天,认真选一个。</view>
|
||||
</view>
|
||||
<button class="me-button" bindtap="openMe">我的</button>
|
||||
</view>
|
||||
<view class="hero-copy">没有“都可以”。每轮只选一个,让你的偏爱一路赢到底。</view>
|
||||
<view class="hero-stickers"><text>✦</text><text>二选一</text><text>↗</text></view>
|
||||
</view>
|
||||
<view class="section-head"><text>全部测试</text><text class="muted">{{tests.length}} 个题库</text></view>
|
||||
<view xhs:if="{{loading}}" class="empty">正在摆好擂台…</view>
|
||||
<view xhs:elif="{{error}}" class="empty">{{error}}<button class="retry" bindtap="loadTests">再试一次</button></view>
|
||||
<view xhs:else class="waterfall">
|
||||
<view xhs:for="{{tests}}" xhs:key="slug" class="test-card card-{{index % 3}}" data-slug="{{item.slug}}" bindtap="openTest">
|
||||
<view class="card-art" style="background: {{item.accent}}">
|
||||
<text class="card-number">0{{index + 1}}</text><text class="card-emoji">{{item.emoji}}</text>
|
||||
</view>
|
||||
<view class="card-body">
|
||||
<view class="card-title">{{item.title}}</view>
|
||||
<view class="card-copy">{{item.subtitle}}</view>
|
||||
<view class="card-meta"><text>{{item.candidate_count}} 强</text><text>{{item.run_count}} 人完成 →</text></view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
.profile-head { display: flex; align-items: center; margin: 12rpx 0 38rpx; }
|
||||
.avatar { width: 104rpx; height: 104rpx; margin-right: 24rpx; line-height: 104rpx; text-align: center; border-radius: 50%; background: #25211f; color: #fff; font-weight: 900; }
|
||||
.profile-title { font-size: 40rpx; font-weight: 900; }
|
||||
.link-card { padding: 30rpx; border-radius: 30rpx; background: #f7e9ff; }
|
||||
.link-title { margin-bottom: 10rpx; font-size: 29rpx; font-weight: 800; }
|
||||
.link-card .muted { font-size: 23rpx; line-height: 1.5; }
|
||||
.link-button { margin: 24rpx 0 0; font-size: 23rpx; }
|
||||
.link-code { margin-top: 22rpx; text-align: center; font-size: 52rpx; letter-spacing: 12rpx; font-weight: 900; }
|
||||
.section-title { margin: 48rpx 4rpx 22rpx; font-size: 29rpx; font-weight: 900; }
|
||||
.result-row { display: flex; align-items: center; margin-bottom: 18rpx; padding: 28rpx; border-radius: 28rpx; background: #fff; box-shadow: 0 8rpx 26rpx rgba(50,35,25,.07); }
|
||||
.medal { width: 74rpx; height: 74rpx; margin-right: 24rpx; line-height: 74rpx; text-align: center; border-radius: 20rpx; background: #ff6b35; color: #fff; font-weight: 900; }
|
||||
.result-main { flex: 1; min-width: 0; }
|
||||
.result-test { color: #887c74; font-size: 22rpx; }
|
||||
.result-champion { margin-top: 6rpx; font-size: 34rpx; font-weight: 900; }
|
||||
.arrow { font-size: 34rpx; }
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
var api = require('../../utils/api');
|
||||
|
||||
Page({
|
||||
data: { loading: true, results: [], error: '', linkCode: '' },
|
||||
onShow: function () { this.load(); },
|
||||
load: function () {
|
||||
var self = this;
|
||||
api.ensureLogin().then(function () { return api.request('/api/v1/me/results'); }).then(function (results) {
|
||||
self.setData({ results: results, loading: false, error: '' });
|
||||
}).catch(function (error) { self.setData({ loading: false, error: error.message || '加载失败' }); });
|
||||
},
|
||||
openResult: function (event) {
|
||||
var result = this.data.results[event.currentTarget.dataset.index];
|
||||
xhs.setStorageSync('pk_last_result', result);
|
||||
xhs.navigateTo({ url: '/pages/result/result?id=' + result.id });
|
||||
},
|
||||
makeLinkCode: function () {
|
||||
var self = this;
|
||||
api.request('/api/v1/me/link-code', { method: 'POST' }).then(function (data) {
|
||||
self.setData({ linkCode: data.code });
|
||||
}).catch(function (error) { xhs.showToast({ title: error.message || '生成失败', icon: 'none' }); });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
{ "navigationBarTitleText": "我的 PK 档案" }
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<view class="page profile">
|
||||
<view class="profile-head"><view class="avatar">我</view><view><view class="profile-title">我的偏爱档案</view><view class="muted">每次纠结,都算数。</view></view></view>
|
||||
<view class="link-card">
|
||||
<view class="link-title">连接独立饭类版</view>
|
||||
<view class="muted">两个小程序的身份标识不同,用一次性码确认后即可合并历史。</view>
|
||||
<button class="secondary link-button" bindtap="makeLinkCode">生成 10 分钟绑定码</button>
|
||||
<view xhs:if="{{linkCode}}" class="link-code">{{linkCode}}</view>
|
||||
</view>
|
||||
<view class="section-title">做过的测试 · {{results.length}}</view>
|
||||
<view xhs:if="{{loading}}" class="empty">正在翻档案…</view>
|
||||
<view xhs:elif="{{error}}" class="empty">{{error}}</view>
|
||||
<view xhs:elif="{{!results.length}}" class="empty">还没有结果,去选出第一个冠军吧。</view>
|
||||
<view xhs:else>
|
||||
<view xhs:for="{{results}}" xhs:key="id" class="result-row" data-index="{{index}}" bindtap="openResult">
|
||||
<view class="medal">#1</view><view class="result-main"><view class="result-test">{{item.test_title}}</view><view class="result-champion">{{item.champion_name}}</view></view><view class="arrow">→</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
.result-kicker { text-align: center; color: #ff6b35; font-size: 22rpx; letter-spacing: 6rpx; font-weight: 800; }
|
||||
.result-title { margin: 14rpx 0 28rpx; text-align: center; font-size: 46rpx; font-weight: 900; }
|
||||
.champion { position: relative; overflow: hidden; padding: 54rpx 36rpx; text-align: center; border-radius: 36rpx; background: #25211f; color: #fff; }
|
||||
.crown { position: absolute; right: 24rpx; top: 2rpx; color: #ffe568; font-size: 100rpx; transform: rotate(13deg); }
|
||||
.test-name { opacity: .62; font-size: 24rpx; }
|
||||
.champion-name { margin-top: 18rpx; font-size: 70rpx; line-height: 1.15; font-weight: 900; }
|
||||
.runner { margin-top: 22rpx; color: #e5dcd6; font-size: 23rpx; }
|
||||
.poster-head { display: flex; justify-content: space-between; margin: 46rpx 4rpx 20rpx; font-size: 30rpx; font-weight: 900; }
|
||||
.poster-head .muted { font-size: 23rpx; font-weight: 500; }
|
||||
.poster { width: 100%; height: 925rpx; border-radius: 30rpx; box-shadow: 0 16rpx 40rpx rgba(50,30,20,.12); }
|
||||
.theme-scroll { margin-top: 26rpx; white-space: nowrap; }
|
||||
.theme-row { display: flex; }
|
||||
.theme-chip { flex: none; margin: 0 14rpx 0 0; padding: 0 28rpx; height: 68rpx; line-height: 68rpx; border-radius: 99rpx; background: #fff; color: #6d625b; font-size: 23rpx; }
|
||||
.theme-chip.active { background: #25211f; color: #fff; }
|
||||
.sticker-row { display: flex; justify-content: space-between; margin-top: 26rpx; padding: 26rpx; border-radius: 24rpx; background: #fff; font-size: 25rpx; }
|
||||
.save, .again { margin-top: 22rpx; font-size: 28rpx; }
|
||||
@@ -0,0 +1,90 @@
|
||||
var api = require('../../utils/api');
|
||||
|
||||
var THEMES = {
|
||||
celebration: { bg: '#ff6b35', ink: '#fffaf4', card: '#25211f', accent: '#ffe568', label: '庆祝派对' },
|
||||
editorial: { bg: '#f3eadf', ink: '#201d1b', card: '#ffffff', accent: '#285ee8', label: '杂志封面' },
|
||||
sticker: { bg: '#c9f06b', ink: '#1e2810', card: '#ff72a7', accent: '#ffffff', label: '贴纸手账' }
|
||||
};
|
||||
|
||||
Page({
|
||||
data: {
|
||||
result: null,
|
||||
template: 'celebration',
|
||||
themeList: [
|
||||
{ name: 'celebration', label: '庆祝派对' },
|
||||
{ name: 'editorial', label: '杂志封面' },
|
||||
{ name: 'sticker', label: '贴纸手账' }
|
||||
],
|
||||
sticker: true,
|
||||
saving: false
|
||||
},
|
||||
onLoad: function (options) {
|
||||
var cached = xhs.getStorageSync('pk_last_result');
|
||||
this.runId = options.id;
|
||||
if (cached && cached.id === options.id) this.setData({ result: cached });
|
||||
else this.load();
|
||||
},
|
||||
onReady: function () { if (this.data.result) this.drawPoster(); },
|
||||
load: function () {
|
||||
var self = this;
|
||||
api.ensureLogin().then(function () { return api.request('/api/v1/me/results/' + self.runId); }).then(function (result) {
|
||||
self.setData({ result: result }); self.drawPoster();
|
||||
}).catch(function (error) { xhs.showModal({ title: '读取失败', content: error.message || '请稍后重试', showCancel: false }); });
|
||||
},
|
||||
pickTemplate: function (event) {
|
||||
this.setData({ template: event.currentTarget.dataset.name });
|
||||
this.drawPoster();
|
||||
},
|
||||
toggleSticker: function () { this.setData({ sticker: !this.data.sticker }); this.drawPoster(); },
|
||||
drawPoster: function () {
|
||||
var result = this.data.result;
|
||||
if (!result) return;
|
||||
var theme = THEMES[this.data.template];
|
||||
var isCelebration = this.data.template === 'celebration';
|
||||
var showSticker = this.data.sticker;
|
||||
xhs.createSelectorQuery().select('.poster').boundingClientRect(function (rect) {
|
||||
var ctx = xhs.createCanvasContext('poster');
|
||||
if (rect) ctx.scale(rect.width / 750, rect.height / 1000);
|
||||
ctx.setFillStyle(theme.bg); ctx.fillRect(0, 0, 750, 1000);
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(24); ctx.fillText('PK 研究所 / MY PICK', 54, 72);
|
||||
ctx.setFontSize(58); ctx.fillText('我的冠军是', 54, 156);
|
||||
ctx.setFillStyle(theme.card); ctx.fillRect(46, 205, 658, 440);
|
||||
ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink);
|
||||
ctx.setFontSize(26); ctx.fillText(result.test_title, 84, 270);
|
||||
ctx.setFontSize(result.champion_name.length > 7 ? 62 : 82);
|
||||
ctx.fillText(result.champion_name, 84, 405);
|
||||
ctx.setFillStyle(theme.accent); ctx.fillRect(84, 440, Math.min(500, result.champion_name.length * 80), 18);
|
||||
ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink);
|
||||
ctx.setFontSize(24); ctx.fillText('一路二选一,它赢到了最后。', 84, 525);
|
||||
var ranking = result.ranking_names || [];
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(25); ctx.fillText('我的 TOP 4', 54, 720);
|
||||
for (var i = 0; i < Math.min(4, ranking.length); i += 1) {
|
||||
ctx.setFontSize(26); ctx.fillText((i + 1) + '. ' + ranking[i], 64, 774 + i * 48);
|
||||
}
|
||||
if (showSticker) {
|
||||
ctx.setFillStyle(theme.accent); ctx.beginPath(); ctx.arc(645, 125, 58, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.setFillStyle(theme.card); ctx.setFontSize(28); ctx.fillText('WIN!', 607, 134);
|
||||
ctx.setFontSize(52); ctx.fillText('✦', 640, 700); ctx.fillText('♡', 550, 900);
|
||||
}
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(20); ctx.fillText('长按保存,把答案分享给同频的人', 54, 956);
|
||||
ctx.draw();
|
||||
}).exec();
|
||||
},
|
||||
savePoster: function () {
|
||||
var self = this;
|
||||
this.setData({ saving: true });
|
||||
xhs.canvasToTempFilePath({
|
||||
canvasId: 'poster', destWidth: 750, destHeight: 1000,
|
||||
success: function (file) {
|
||||
xhs.saveImageToPhotosAlbum({
|
||||
filePath: file.tempFilePath,
|
||||
success: function () { xhs.showToast({ title: '已保存到相册' }); },
|
||||
fail: function () { xhs.showToast({ title: '保存失败,请检查相册权限', icon: 'none' }); },
|
||||
complete: function () { self.setData({ saving: false }); }
|
||||
});
|
||||
},
|
||||
fail: function () { self.setData({ saving: false }); xhs.showToast({ title: '海报生成失败', icon: 'none' }); }
|
||||
});
|
||||
},
|
||||
replay: function () { xhs.redirectTo({ url: '/pages/test/test?slug=' + this.data.result.test_slug }); }
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
{ "navigationBarTitleText": "你的冠军" }
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<view class="page result-page" xhs:if="{{result}}">
|
||||
<view class="result-kicker">FINAL ANSWER</view>
|
||||
<view class="result-title">你的冠军诞生了!</view>
|
||||
<view class="champion"><view class="crown">♛</view><view class="test-name">{{result.test_title}}</view><view class="champion-name">{{result.champion_name}}</view><view class="runner" xhs:if="{{result.runner_up_name}}">决赛击败 {{result.runner_up_name}}</view></view>
|
||||
<view class="poster-head"><text>分享卡片</text><text class="muted">3 款模板任选</text></view>
|
||||
<canvas canvas-id="poster" class="poster"></canvas>
|
||||
<scroll-view scroll-x class="theme-scroll">
|
||||
<view class="theme-row">
|
||||
<button xhs:for="{{themeList}}" xhs:key="name" class="theme-chip {{template === item.name ? 'active' : ''}}" data-name="{{item.name}}" bindtap="pickTemplate">{{item.label}}</button>
|
||||
</view>
|
||||
</scroll-view>
|
||||
<view class="sticker-row" bindtap="toggleSticker"><text>添加手绘贴纸</text><text>{{sticker ? '已开启 ✓' : '关闭'}}</text></view>
|
||||
<button class="primary save" loading="{{saving}}" bindtap="savePoster">保存分享图</button>
|
||||
<button class="secondary again" bindtap="replay">再测一次</button>
|
||||
</view>
|
||||
@@ -0,0 +1,19 @@
|
||||
.arena { min-height: 100vh; background: #fff8f2; }
|
||||
.topline { display: flex; align-items: center; justify-content: space-between; }
|
||||
.progress { margin-top: 20rpx; height: 10rpx; overflow: hidden; border-radius: 99rpx; background: #eee4dd; }
|
||||
.progress-in { height: 100%; border-radius: inherit; background: #ff6b35; transition: width .25s; }
|
||||
.question { margin: 54rpx 8rpx 34rpx; font-size: 52rpx; line-height: 1.18; font-weight: 900; }
|
||||
.pair { position: relative; }
|
||||
.choice { position: relative; min-height: 360rpx; padding: 34rpx; box-sizing: border-box; border-radius: 38rpx; color: #fff; box-shadow: 0 20rpx 50rpx rgba(80, 35, 18, .14); }
|
||||
.choice + .choice { margin-top: 24rpx; }
|
||||
.choice-a { background: #ff6b35; transform: rotate(-1deg); }
|
||||
.choice-b { background: #2766df; transform: rotate(1deg); }
|
||||
.choice:active { transform: scale(.975); }
|
||||
.choice-index { width: 54rpx; height: 54rpx; line-height: 54rpx; text-align: center; border: 2rpx solid rgba(255,255,255,.6); border-radius: 50%; font-size: 24rpx; font-weight: 800; }
|
||||
.food-mark { position: absolute; right: 34rpx; top: 24rpx; font-size: 92rpx; opacity: .8; transform: rotate(12deg); }
|
||||
.choice-name { margin-top: 72rpx; font-size: 60rpx; line-height: 1.12; font-weight: 900; }
|
||||
.tap-hint { margin-top: 54rpx; font-size: 24rpx; opacity: .75; }
|
||||
.versus { position: absolute; z-index: 3; left: 50%; top: 50%; transform: translate(-50%, -50%) rotate(-8deg); width: 84rpx; height: 84rpx; line-height: 84rpx; text-align: center; color: #201d1b; background: #fff; border: 5rpx solid #201d1b; border-radius: 50%; font-size: 26rpx; font-weight: 900; }
|
||||
.round-note { margin: 38rpx 0; text-align: center; color: #8f827a; font-size: 24rpx; }
|
||||
.saving { position: fixed; left: 50%; bottom: 70rpx; transform: translateX(-50%); padding: 22rpx 34rpx; border-radius: 99rpx; background: #201d1b; color: #fff; font-size: 25rpx; }
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
var api = require('../../utils/api');
|
||||
var bracket = require('../../utils/bracket');
|
||||
|
||||
Page({
|
||||
data: { loading: true, error: '', test: null, left: null, right: null, round: 1, progress: 0, completed: 0, totalDecisions: 0 },
|
||||
onLoad: function (options) {
|
||||
this.slug = options.slug;
|
||||
this.load();
|
||||
},
|
||||
load: function () {
|
||||
var self = this;
|
||||
api.request('/api/v1/tests/' + this.slug).then(function (test) {
|
||||
self.test = test;
|
||||
self.state = bracket.create(test.candidates);
|
||||
self.totalDecisions = test.candidates.length - 1;
|
||||
self.refreshPair();
|
||||
self.setData({ loading: false, test: test, totalDecisions: self.totalDecisions });
|
||||
}).catch(function (error) { self.setData({ loading: false, error: error.message || '加载失败' }); });
|
||||
},
|
||||
refreshPair: function () {
|
||||
var pair = bracket.current(this.state);
|
||||
this.setData({
|
||||
left: pair && pair[0],
|
||||
right: pair && pair[1],
|
||||
round: this.state.round,
|
||||
completed: this.state.decisions.length,
|
||||
progress: Math.round(this.state.decisions.length / this.totalDecisions * 100)
|
||||
});
|
||||
},
|
||||
choose: function (event) {
|
||||
if (this.choosing) return;
|
||||
this.choosing = true;
|
||||
var side = event.currentTarget.dataset.side;
|
||||
var winner = side === 'left' ? this.data.left : this.data.right;
|
||||
var finished = bracket.choose(this.state, winner);
|
||||
if (finished) this.complete();
|
||||
else {
|
||||
this.refreshPair();
|
||||
this.choosing = false;
|
||||
}
|
||||
},
|
||||
complete: function () {
|
||||
var self = this;
|
||||
var champion = this.state.pool[0];
|
||||
var finalDecision = this.state.decisions[this.state.decisions.length - 1];
|
||||
var runnerUpId = finalDecision.left_id === champion.id ? finalDecision.right_id : finalDecision.left_id;
|
||||
this.setData({ progress: 100, submitting: true });
|
||||
api.ensureLogin().then(function () {
|
||||
return api.request('/api/v1/tests/' + self.slug + '/runs', {
|
||||
method: 'POST',
|
||||
data: {
|
||||
app_key: 'hub',
|
||||
champion_id: champion.id,
|
||||
runner_up_id: runnerUpId,
|
||||
ranking_ids: bracket.ranking(self.state),
|
||||
decisions: self.state.decisions
|
||||
}
|
||||
});
|
||||
}).then(function (result) {
|
||||
xhs.setStorageSync('pk_last_result', result);
|
||||
xhs.redirectTo({ url: '/pages/result/result?id=' + result.id });
|
||||
}).catch(function (error) {
|
||||
self.choosing = false;
|
||||
self.setData({ submitting: false });
|
||||
xhs.showModal({ title: '结果保存失败', content: error.message || '请检查网络后重试', showCancel: false });
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
{ "navigationBarTitleText": "开始 PK" }
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<view class="page arena">
|
||||
<view xhs:if="{{loading}}" class="empty">正在抽签…</view>
|
||||
<view xhs:elif="{{error}}" class="empty">{{error}}</view>
|
||||
<block xhs:else>
|
||||
<view class="topline"><text class="pill">第 {{round}} 轮</text><text class="muted">{{progress}}%</text></view>
|
||||
<view class="progress"><view class="progress-in" style="width: {{progress}}%"></view></view>
|
||||
<view class="question"><view>此刻只能留一个,</view><view>你会选谁?</view></view>
|
||||
<view class="pair">
|
||||
<view class="choice choice-a" data-side="left" bindtap="choose">
|
||||
<view class="choice-index">A</view><view class="food-mark">{{test.emoji}}</view><view class="choice-name">{{left.name}}</view><view class="tap-hint">点它晋级 ↗</view>
|
||||
</view>
|
||||
<view class="versus">VS</view>
|
||||
<view class="choice choice-b" data-side="right" bindtap="choose">
|
||||
<view class="choice-index">B</view><view class="food-mark">{{test.emoji}}</view><view class="choice-name">{{right.name}}</view><view class="tap-hint">点它晋级 ↗</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="round-note">已完成 {{completed}} / {{totalDecisions}} 次选择</view>
|
||||
<view xhs:if="{{submitting}}" class="saving">正在保存你的冠军…</view>
|
||||
</block>
|
||||
</view>
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"description": "PK 研究所 - 小红书原生小程序",
|
||||
"miniprogramRoot": "./",
|
||||
"setting": {
|
||||
"es6": true,
|
||||
"minified": true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
const required = ['app.js','app.json','pages/index/index.xhsml','pages/test/test.js','pages/result/result.js','web/index.html','web/app.js'];
|
||||
for (const file of required) if (!existsSync(file)) throw new Error(`Missing ${file}`);
|
||||
JSON.parse(readFileSync('app.json','utf8'));
|
||||
const web = readFileSync('web/index.html','utf8');
|
||||
if (!web.includes('./app.js') || !web.includes('./style.css')) throw new Error('Preview assets are not linked');
|
||||
console.log(`Static checks passed (${required.length} required files).`);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
function request(path, options) {
|
||||
var app = getApp();
|
||||
var token = app.globalData.token || xhs.getStorageSync('pk_token');
|
||||
options = options || {};
|
||||
return new Promise(function (resolve, reject) {
|
||||
xhs.request({
|
||||
url: app.globalData.apiBase + path,
|
||||
method: options.method || 'GET',
|
||||
data: options.data,
|
||||
header: token ? { 'content-type': 'application/json', Authorization: 'Bearer ' + token } : { 'content-type': 'application/json' },
|
||||
success: function (res) {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) return resolve(res.data);
|
||||
reject(new Error((res.data && res.data.detail) || '请求失败(' + res.statusCode + ')'));
|
||||
},
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function ensureLogin() {
|
||||
var app = getApp();
|
||||
if (app.globalData.token || xhs.getStorageSync('pk_token')) return Promise.resolve();
|
||||
return new Promise(function (resolve, reject) {
|
||||
xhs.login({
|
||||
success: function (loginResult) {
|
||||
request('/api/v1/auth/xhs', {
|
||||
method: 'POST',
|
||||
data: { app_key: app.globalData.appKey, code: loginResult.code }
|
||||
}).then(function (data) {
|
||||
app.globalData.token = data.token;
|
||||
xhs.setStorageSync('pk_token', data.token);
|
||||
resolve(data);
|
||||
}).catch(reject);
|
||||
},
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { request: request, ensureLogin: ensureLogin };
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
function create(candidates) {
|
||||
return {
|
||||
round: 1,
|
||||
pool: candidates.slice(),
|
||||
pairIndex: 0,
|
||||
winners: [],
|
||||
eliminated: [],
|
||||
decisions: []
|
||||
};
|
||||
}
|
||||
|
||||
function current(state) {
|
||||
if (state.pool.length === 1) return null;
|
||||
return [state.pool[state.pairIndex], state.pool[state.pairIndex + 1] || null];
|
||||
}
|
||||
|
||||
function choose(state, winner) {
|
||||
var pair = current(state);
|
||||
if (!pair || !pair[1]) throw new Error('invalid bracket pair');
|
||||
var loser = pair[0].id === winner.id ? pair[1] : pair[0];
|
||||
state.winners.push(winner);
|
||||
state.eliminated.unshift(loser);
|
||||
state.decisions.push({ round: state.round, left_id: pair[0].id, right_id: pair[1].id, winner_id: winner.id });
|
||||
state.pairIndex += 2;
|
||||
if (state.pairIndex < state.pool.length) {
|
||||
if (!state.pool[state.pairIndex + 1]) {
|
||||
state.winners.push(state.pool[state.pairIndex]);
|
||||
state.pairIndex += 1;
|
||||
}
|
||||
if (state.pairIndex < state.pool.length) return false;
|
||||
}
|
||||
state.pool = state.winners;
|
||||
state.winners = [];
|
||||
state.pairIndex = 0;
|
||||
state.round += 1;
|
||||
return state.pool.length === 1;
|
||||
}
|
||||
|
||||
function ranking(state) {
|
||||
return state.pool.concat(state.eliminated).map(function (item) { return item.id; });
|
||||
}
|
||||
|
||||
module.exports = { create: create, current: current, choose: choose, ranking: ranking };
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
var root = document.getElementById('app');
|
||||
var apiBase = (window.PK_CONFIG.apiBase || '').replace(/\/$/, '');
|
||||
var state = { tests: [], test: null, bracket: null, result: null, template: 'celebration', sticker: true };
|
||||
|
||||
function toast(message) { var el = document.getElementById('toast'); el.textContent = message; el.classList.add('show'); setTimeout(function () { el.classList.remove('show'); }, 2200); }
|
||||
function escapeHtml(value) { return String(value).replace(/[&<>"']/g, function (char) { return ({'&':'&','<':'<','>':'>','"':'"',"'":'''})[char]; }); }
|
||||
function request(path, options) {
|
||||
options = options || {}; var token = localStorage.getItem('pk_token');
|
||||
var headers = {'Content-Type':'application/json'}; if (token) headers.Authorization = 'Bearer ' + token;
|
||||
return fetch(apiBase + path, {method:options.method || 'GET',headers:headers,body:options.data ? JSON.stringify(options.data) : undefined}).then(function (response) {
|
||||
return response.json().catch(function () { return {}; }).then(function (data) { if (!response.ok) throw new Error(data.detail || '请求失败'); return data; });
|
||||
});
|
||||
}
|
||||
function ensureLogin() {
|
||||
if (localStorage.getItem('pk_token')) return Promise.resolve();
|
||||
var id = localStorage.getItem('pk_preview_id') || (Date.now().toString(36) + Math.random().toString(36).slice(2)); localStorage.setItem('pk_preview_id', id);
|
||||
return request('/api/v1/auth/xhs', {method:'POST',data:{app_key:'hub',code:'dev:web-' + id}}).then(function (data) { localStorage.setItem('pk_token', data.token); });
|
||||
}
|
||||
function loadHome() {
|
||||
root.innerHTML = '<div class="loading">正在摆好擂台…</div>';
|
||||
request('/api/v1/tests').then(function (tests) { state.tests = tests; renderHome(); }).catch(renderError);
|
||||
}
|
||||
function renderHome() {
|
||||
root.innerHTML = '<div class="shell"><section class="hero"><div class="eyebrow">TASTE LAB · PUBLIC PREVIEW</div><h1>今天,认真选一个。</h1><p>没有“都可以”。每轮只选一个,让你的偏爱一路赢到底。</p></section><div class="section-head"><h2>全部测试</h2><span class="muted">' + state.tests.length + ' 个题库</span></div><section class="grid">' + state.tests.map(function (test, index) {
|
||||
return '<button class="test-card" data-slug="' + test.slug + '"><div class="art" style="background:' + test.accent + '"><small>0' + (index + 1) + '</small><span class="emoji">' + test.emoji + '</span></div><div class="card-body"><h3>' + escapeHtml(test.title) + '</h3><p>' + escapeHtml(test.subtitle) + '</p><div class="meta"><span>' + test.candidate_count + ' 强</span><span>' + test.run_count + ' 人完成 →</span></div></div></button>';
|
||||
}).join('') + '</section></div>';
|
||||
root.querySelectorAll('.test-card').forEach(function (el) { el.addEventListener('click', function () { loadTest(el.dataset.slug); }); });
|
||||
}
|
||||
function loadTest(slug) { root.innerHTML = '<div class="loading">正在抽签…</div>'; request('/api/v1/tests/' + slug).then(function (test) { state.test = test; state.bracket = {round:1,pool:test.candidates.slice(),index:0,winners:[],eliminated:[],decisions:[]}; renderPair(); }).catch(renderError); }
|
||||
function pair() { return [state.bracket.pool[state.bracket.index], state.bracket.pool[state.bracket.index + 1] || null]; }
|
||||
function renderPair() {
|
||||
var items = pair(), done = state.bracket.decisions.length, total = state.test.candidates.length - 1;
|
||||
root.innerHTML = '<div class="shell arena"><div class="arena-top"><span>第 ' + state.bracket.round + ' 轮</span><span class="muted">' + done + ' / ' + total + '</span></div><div class="progress"><span style="width:' + Math.round(done/total*100) + '%"></span></div><h1>此刻只能留一个,<br>你会选谁?</h1><div class="pair"><button class="choice" data-side="0"><span class="choice-letter">A</span><span class="choice-emoji">' + state.test.emoji + '</span><strong class="choice-name">' + escapeHtml(items[0].name) + '</strong><span class="choice-hint">点它晋级 ↗</span></button><span class="vs">VS</span><button class="choice" data-side="1"><span class="choice-letter">B</span><span class="choice-emoji">' + state.test.emoji + '</span><strong class="choice-name">' + escapeHtml(items[1].name) + '</strong><span class="choice-hint">点它晋级 ↗</span></button></div></div>';
|
||||
root.querySelectorAll('.choice').forEach(function (el) { el.addEventListener('click', function () { choose(items[Number(el.dataset.side)]); }); });
|
||||
}
|
||||
function choose(winner) {
|
||||
var b = state.bracket, items = pair(), loser = items[0].id === winner.id ? items[1] : items[0];
|
||||
b.winners.push(winner); b.eliminated.unshift(loser); b.decisions.push({round:b.round,left_id:items[0].id,right_id:items[1].id,winner_id:winner.id}); b.index += 2;
|
||||
if (b.index < b.pool.length && !b.pool[b.index + 1]) { b.winners.push(b.pool[b.index]); b.index += 1; }
|
||||
if (b.index >= b.pool.length) { b.pool = b.winners; b.winners = []; b.index = 0; b.round += 1; }
|
||||
if (b.pool.length === 1) complete(); else renderPair();
|
||||
}
|
||||
function complete() {
|
||||
var b = state.bracket, champion = b.pool[0], final = b.decisions[b.decisions.length - 1], runner = final.left_id === champion.id ? final.right_id : final.left_id;
|
||||
root.innerHTML = '<div class="loading">正在保存你的冠军…</div>';
|
||||
ensureLogin().then(function () { return request('/api/v1/tests/' + state.test.slug + '/runs',{method:'POST',data:{app_key:'hub',champion_id:champion.id,runner_up_id:runner,ranking_ids:b.pool.concat(b.eliminated).map(function(i){return i.id;}),decisions:b.decisions}}); }).then(function (result) { state.result = result; renderResult(); }).catch(renderError);
|
||||
}
|
||||
function renderResult() {
|
||||
var r = state.result;
|
||||
root.innerHTML = '<div class="shell result-shell"><section class="champion-panel"><small>FINAL ANSWER · ' + escapeHtml(r.test_title) + '</small><h1>' + escapeHtml(r.champion_name) + '</h1><p>' + (r.runner_up_name ? '决赛击败 ' + escapeHtml(r.runner_up_name) : '一路赢到最后') + '</p></section><div class="result-layout"><canvas id="poster" class="poster" width="750" height="1000"></canvas><aside class="control"><h2>分享卡片</h2><p class="muted">换模板、加贴纸,保存后直接发小红书。</p><button class="theme active" data-theme="celebration">庆祝派对</button><button class="theme" data-theme="editorial">杂志封面</button><button class="theme" data-theme="sticker">贴纸手账</button><button id="stickerToggle" class="theme">手绘贴纸:开启</button><button id="savePoster" class="button wide">保存 PNG</button><button id="replay" class="button outline wide">再测一次</button></aside></div></div>';
|
||||
drawPoster(); root.querySelectorAll('[data-theme]').forEach(function (el) { el.addEventListener('click', function () { state.template = el.dataset.theme; root.querySelectorAll('[data-theme]').forEach(function(x){x.classList.remove('active');});el.classList.add('active');drawPoster(); }); });
|
||||
document.getElementById('stickerToggle').addEventListener('click',function(){state.sticker=!state.sticker;this.textContent='手绘贴纸:'+(state.sticker?'开启':'关闭');drawPoster();});
|
||||
document.getElementById('savePoster').addEventListener('click',function(){var a=document.createElement('a');a.download='我的PK冠军-'+r.champion_name+'.png';a.href=document.getElementById('poster').toDataURL('image/png');a.click();toast('分享图已保存');});
|
||||
document.getElementById('replay').addEventListener('click',function(){loadTest(r.test_slug);});
|
||||
}
|
||||
function drawPoster() {
|
||||
var canvas=document.getElementById('poster'),ctx=canvas.getContext('2d'),r=state.result,themes={celebration:['#ff6b35','#fffaf4','#25211f','#ffe568'],editorial:['#f3eadf','#201d1b','#fff','#285ee8'],sticker:['#c9f06b','#1e2810','#ff72a7','#fff']},t=themes[state.template];
|
||||
ctx.fillStyle=t[0];ctx.fillRect(0,0,750,1000);ctx.fillStyle=t[1];ctx.font='700 24px system-ui';ctx.fillText('PK 研究所 / MY PICK',54,72);ctx.font='900 58px system-ui';ctx.fillText('我的冠军是',54,156);ctx.fillStyle=t[2];ctx.fillRect(46,205,658,440);ctx.fillStyle=state.template==='celebration'?'#fffaf4':t[1];ctx.font='600 26px system-ui';ctx.fillText(r.test_title,84,270);ctx.font='900 '+(r.champion_name.length>7?62:82)+'px system-ui';ctx.fillText(r.champion_name,84,405);ctx.fillStyle=t[3];ctx.fillRect(84,440,Math.min(500,r.champion_name.length*80),18);ctx.fillStyle=state.template==='celebration'?'#fffaf4':t[1];ctx.font='24px system-ui';ctx.fillText('一路二选一,它赢到了最后。',84,525);ctx.fillStyle=t[1];ctx.font='700 25px system-ui';ctx.fillText('我的 TOP 4',54,720);(r.ranking_names||[]).slice(0,4).forEach(function(name,i){ctx.font='26px system-ui';ctx.fillText((i+1)+'. '+name,64,774+i*48);});if(state.sticker){ctx.fillStyle=t[3];ctx.beginPath();ctx.arc(645,125,58,0,Math.PI*2);ctx.fill();ctx.fillStyle=t[2];ctx.font='900 28px system-ui';ctx.fillText('WIN!',607,134);ctx.font='52px system-ui';ctx.fillText('✦',640,700);ctx.fillText('♡',550,900);}ctx.fillStyle=t[1];ctx.font='20px system-ui';ctx.fillText('保存这张图,把答案分享给同频的人',54,956);
|
||||
}
|
||||
function loadHistory() { root.innerHTML='<div class="loading">正在翻档案…</div>';ensureLogin().then(function(){return request('/api/v1/me/results');}).then(renderHistory).catch(renderError); }
|
||||
function renderHistory(results) { root.innerHTML='<div class="shell history"><div class="hero"><div class="eyebrow">MY TASTE ARCHIVE</div><h1>我的偏爱档案</h1><p>每一次纠结,都算数。</p></div><div class="section-head"><h2>做过的测试</h2><span class="muted">'+results.length+' 条结果</span></div><div class="link-box"><strong>连接独立饭类版</strong><p class="muted">两个小程序需要一次确认才能合并身份。</p><button id="linkCode" class="button">生成 10 分钟绑定码</button><div id="code" class="link-code"></div></div><div id="rows">'+(results.length?results.map(function(r,i){return '<button class="result-row" data-i="'+i+'"><span class="medal">#1</span><span><small>'+escapeHtml(r.test_title)+'</small><strong>'+escapeHtml(r.champion_name)+'</strong></span><span>→</span></button>';}).join(''):'<div class="empty">还没有结果,去选第一个冠军吧。</div>')+'</div></div>';document.getElementById('linkCode').addEventListener('click',function(){request('/api/v1/me/link-code',{method:'POST'}).then(function(d){document.getElementById('code').textContent=d.code;}).catch(renderError);});root.querySelectorAll('.result-row').forEach(function(el){el.addEventListener('click',function(){state.result=results[Number(el.dataset.i)];renderResult();});}); }
|
||||
function renderError(error) { root.innerHTML='<div class="shell empty"><h2>暂时没接上服务</h2><p>'+escapeHtml(error.message||'未知错误')+'</p><button class="button" id="retry">重新加载</button></div>';document.getElementById('retry').addEventListener('click',loadHome); }
|
||||
document.getElementById('homeButton').addEventListener('click',loadHome); document.getElementById('meButton').addEventListener('click',loadHistory); loadHome();
|
||||
}());
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
|
||||
<meta name="theme-color" content="#fff8f2">
|
||||
<title>PK 研究所 · 公网预览</title>
|
||||
<link rel="stylesheet" href="./style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header class="nav"><button id="homeButton" class="brand">PK LAB <i>✦</i></button><button id="meButton" class="nav-pill">我的档案</button></header>
|
||||
<main id="app" aria-live="polite"></main>
|
||||
<div id="toast" class="toast" role="status"></div>
|
||||
<script src="./runtime-env.js"></script>
|
||||
<script src="./app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
window.PK_CONFIG = { apiBase: '${API_BASE_URL}' };
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user