115 lines
3.4 KiB
JavaScript
115 lines
3.4 KiB
JavaScript
const { modules } = require('../../utils/prompt_ops');
|
|
const { request, downloadFile, buildResultView } = require('../../utils/request');
|
|
|
|
Page({
|
|
data: {
|
|
moduleKey: '',
|
|
moduleTitle: '',
|
|
ops: [],
|
|
opIndex: 0,
|
|
formData: {},
|
|
resultText: '',
|
|
resultTextCards: []
|
|
},
|
|
onLoad(options) {
|
|
const moduleKey = options.module || '';
|
|
const module = modules.find(m => m.key === moduleKey) || modules[0];
|
|
this.setData({
|
|
moduleKey: module ? module.key : '',
|
|
moduleTitle: module ? module.title : '',
|
|
ops: module ? module.ops : []
|
|
});
|
|
this.resetForm();
|
|
},
|
|
onOpChange(e) {
|
|
this.setData({ opIndex: Number(e.detail.value) || 0, resultText: '', resultTextCards: [] });
|
|
this.resetForm();
|
|
},
|
|
resetForm() {
|
|
const op = this.currentOp();
|
|
const formData = {};
|
|
if (op && op.fields) {
|
|
op.fields.forEach(f => {
|
|
if (f.default !== undefined) formData[f.key] = f.default;
|
|
});
|
|
}
|
|
this.setData({ formData });
|
|
},
|
|
currentOp() {
|
|
return this.data.ops[this.data.opIndex];
|
|
},
|
|
onInput(e) {
|
|
const key = e.currentTarget.dataset.key;
|
|
const value = e.detail.value;
|
|
const formData = { ...this.data.formData, [key]: value };
|
|
this.setData({ formData });
|
|
},
|
|
buildUrl(op, formData) {
|
|
let url = op.path;
|
|
const query = [];
|
|
if (op.fields) {
|
|
op.fields.forEach(f => {
|
|
const val = formData[f.key];
|
|
if (f.in === 'path' && val !== undefined && val !== '') {
|
|
url = url.replace(`{${f.key}}`, encodeURIComponent(String(val)));
|
|
}
|
|
if (f.in === 'query' && val !== undefined && val !== '') {
|
|
query.push(`${encodeURIComponent(f.key)}=${encodeURIComponent(String(val))}`);
|
|
}
|
|
});
|
|
}
|
|
if (query.length > 0) {
|
|
url += (url.includes('?') ? '&' : '?') + query.join('&');
|
|
}
|
|
return url;
|
|
},
|
|
buildBody(op, formData) {
|
|
if (!op.fields) return {};
|
|
const body = {};
|
|
op.fields.forEach(f => {
|
|
if (f.in === 'path' || f.in === 'query') return;
|
|
if (f.key === 'body_json' && formData.body_json) return;
|
|
const val = formData[f.key];
|
|
if (val !== undefined && val !== '') {
|
|
body[f.key] = val;
|
|
}
|
|
});
|
|
if (formData.body_json) {
|
|
try {
|
|
const parsed = JSON.parse(formData.body_json);
|
|
return parsed;
|
|
} catch (e) {}
|
|
}
|
|
return body;
|
|
},
|
|
async runOp() {
|
|
const op = this.currentOp();
|
|
if (!op) return;
|
|
const formData = this.data.formData || {};
|
|
this.setData({ resultText: '', resultTextCards: [] });
|
|
const url = this.buildUrl(op, formData);
|
|
|
|
if (op.responseType === 'file') {
|
|
try {
|
|
const tempPath = await downloadFile({ url });
|
|
const view = buildResultView(`下载成功: ${tempPath}`);
|
|
this.setData({ resultText: view.text, resultTextCards: view.cards });
|
|
} catch (e) {
|
|
const view = buildResultView('下载失败');
|
|
this.setData({ resultText: view.text, resultTextCards: view.cards });
|
|
}
|
|
return;
|
|
}
|
|
|
|
const body = this.buildBody(op, formData);
|
|
try {
|
|
const result = await request({ url, method: op.method, data: op.method === 'GET' ? undefined : body });
|
|
const view = buildResultView(result);
|
|
this.setData({ resultText: view.text, resultTextCards: view.cards });
|
|
} catch (e) {
|
|
const view = buildResultView(e.message || '请求失败');
|
|
this.setData({ resultText: view.text, resultTextCards: view.cards });
|
|
}
|
|
}
|
|
});
|