const { request, buildResultView } = require('../../utils/request'); const RESULT_PREVIEW_LIMIT = 240; function safeParseResult(raw) { if (raw === null || raw === undefined) return null; if (typeof raw === 'string') { try { return JSON.parse(raw); } catch (e) { return null; } } return raw; } function pickList(obj) { if (!obj || typeof obj !== 'object') return null; const keys = ['list', 'items', 'records', 'rows', 'data', 'result']; for (let i = 0; i < keys.length; i += 1) { const value = obj[keys[i]]; if (Array.isArray(value)) return value; } return null; } function toArray(parsed) { if (!parsed || typeof parsed !== 'object') return []; if (Array.isArray(parsed)) return parsed; const list = pickList(parsed); if (list) return list; return [parsed]; } function isTemplateLike(item) { if (!item || typeof item !== 'object') return false; return item.id !== undefined || item.name || item.category || item.content; } function buildTemplateCards(parsed) { const list = toArray(parsed).filter(isTemplateLike).slice(0, 6); return list.map(item => { const name = item.name || item.title || item.template_name || ''; const content = item.content || item.body || item.desc || item.description || ''; return { id: item.id || item.template_id || item.templateId || '', name: name || (item.id || item.template_id ? `模板 ${item.id || item.template_id}` : '模板'), category: item.category || item.group || '', contentPreview: content ? String(content).slice(0, 80) : '' }; }); } function buildResultState(text) { const content = text || ''; return { resultHasMore: content.length > RESULT_PREVIEW_LIMIT, resultExpanded: false }; } Page({ data: { id: '', name: '', category: '', content: '', body_json: '', result: '', resultCards: [], resultExpanded: false, resultHasMore: false, templateCards: [] }, onInput(e) { const key = e.currentTarget.dataset.key; this.setData({ [key]: e.detail.value }); }, toggleResult() { this.setData({ resultExpanded: !this.data.resultExpanded }); }, setResult(result) { const view = buildResultView(result); const parsed = safeParseResult(result); const templateCards = buildTemplateCards(parsed); const state = buildResultState(view.text); this.setData({ result: view.text, resultCards: view.cards, templateCards, ...state }); }, async list() { const url = this.data.category ? `/api/prompt/templates?category=${encodeURIComponent(this.data.category)}` : '/api/prompt/templates'; const result = await request({ url }); this.setResult(result); }, async detail() { const result = await request({ url: `/api/prompt/templates/${encodeURIComponent(this.data.id)}` }); this.setResult(result); }, async create() { const payload = this.buildPayload(); const result = await request({ url: '/api/prompt/templates', method: 'POST', data: payload }); this.setResult(result); }, async update() { const payload = this.buildPayload(); const result = await request({ url: `/api/prompt/templates/${encodeURIComponent(this.data.id)}`, method: 'PUT', data: payload }); this.setResult(result); }, async remove() { const result = await request({ url: `/api/prompt/templates/${encodeURIComponent(this.data.id)}`, method: 'DELETE' }); this.setResult(result); }, buildPayload() { if (this.data.body_json) { try { return JSON.parse(this.data.body_json); } catch (e) {} } return { name: this.data.name, category: this.data.category, content: this.data.content }; } });