124 lines
3.6 KiB
JavaScript
124 lines
3.6 KiB
JavaScript
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 isRobotLike(item) {
|
|
if (!item || typeof item !== 'object') return false;
|
|
return item.id !== undefined || item.name || item.description || item.avatar || item.status !== undefined;
|
|
}
|
|
|
|
function buildRobotCards(parsed) {
|
|
const list = toArray(parsed).filter(isRobotLike).slice(0, 6);
|
|
return list.map(item => {
|
|
const name = item.name || item.title || item.robot_name || '';
|
|
const desc = item.description || item.desc || item.detail || '';
|
|
return {
|
|
id: item.id || item.robot_id || item.robotId || '',
|
|
name: name || (item.id || item.robot_id ? `机器人 ${item.id || item.robot_id}` : '机器人'),
|
|
status: item.status,
|
|
avatar: item.avatar || item.avatar_url || '',
|
|
description: desc ? String(desc).slice(0, 80) : ''
|
|
};
|
|
});
|
|
}
|
|
|
|
function buildResultState(text) {
|
|
const content = text || '';
|
|
return { resultHasMore: content.length > RESULT_PREVIEW_LIMIT, resultExpanded: false };
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
id: '',
|
|
name: '',
|
|
description: '',
|
|
status: '',
|
|
avatar: '',
|
|
body_json: '',
|
|
result: '',
|
|
resultCards: [],
|
|
resultExpanded: false,
|
|
resultHasMore: false,
|
|
robotCards: []
|
|
},
|
|
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 robotCards = buildRobotCards(parsed);
|
|
const state = buildResultState(view.text);
|
|
this.setData({ result: view.text, resultCards: view.cards, robotCards, ...state });
|
|
},
|
|
async list() {
|
|
const result = await request({ url: '/api/prompt/robots' });
|
|
this.setResult(result);
|
|
},
|
|
async detail() {
|
|
const result = await request({ url: `/api/prompt/robots/${encodeURIComponent(this.data.id)}` });
|
|
this.setResult(result);
|
|
},
|
|
async create() {
|
|
const payload = this.buildPayload();
|
|
const result = await request({ url: '/api/prompt/robots', method: 'POST', data: payload });
|
|
this.setResult(result);
|
|
},
|
|
async update() {
|
|
const payload = this.buildPayload();
|
|
const result = await request({ url: `/api/prompt/robots/${encodeURIComponent(this.data.id)}`, method: 'PUT', data: payload });
|
|
this.setResult(result);
|
|
},
|
|
async remove() {
|
|
const result = await request({ url: `/api/prompt/robots/${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,
|
|
description: this.data.description,
|
|
status: this.data.status ? Number(this.data.status) : undefined,
|
|
avatar: this.data.avatar
|
|
};
|
|
}
|
|
});
|