Files
pk-lab-xhs/pages/result/result.js
T
codex-bot 350aae797f
Verify mini program and deploy preview / verify-deploy (push) Failing after 34s
feat: launch PK testing application
2026-09-18 21:50:25 +08:00

91 lines
4.3 KiB
JavaScript

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 }); }
});