157 lines
5.0 KiB
JavaScript
157 lines
5.0 KiB
JavaScript
const { modules } = require('../../utils/console_ops');
|
|
const { request, uploadFile, downloadFile, buildResultView } = require('../../utils/request');
|
|
|
|
Page({
|
|
data: {
|
|
moduleKey: '',
|
|
moduleTitle: '',
|
|
ops: [],
|
|
opIndex: 0,
|
|
formData: {},
|
|
resultText: '',
|
|
resultTextCards: [],
|
|
filePath: '',
|
|
imagePath: ''
|
|
},
|
|
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: [], imagePath: '' });
|
|
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, filePath: '' });
|
|
},
|
|
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 });
|
|
},
|
|
async chooseFile() {
|
|
try {
|
|
const res = await wx.chooseMessageFile({ count: 1, type: 'file' });
|
|
const file = res.tempFiles && res.tempFiles[0];
|
|
if (file && file.path) {
|
|
this.setData({ filePath: file.path });
|
|
}
|
|
} catch (e) {}
|
|
},
|
|
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) {}
|
|
}
|
|
if (body.include && typeof body.include === 'string') {
|
|
body.include = body.include.split(',').map(s => s.trim()).filter(Boolean);
|
|
}
|
|
if (body.mask_money || body.mask_names) {
|
|
body.mask = {
|
|
money: String(body.mask_money).toLowerCase() === 'true',
|
|
names: String(body.mask_names).toLowerCase() === 'true'
|
|
};
|
|
delete body.mask_money;
|
|
delete body.mask_names;
|
|
}
|
|
if (body.auto_start !== undefined) {
|
|
body.auto_start = String(body.auto_start).toLowerCase() === 'true';
|
|
}
|
|
if (body.saleTime) {
|
|
body.saleTime = Number(body.saleTime);
|
|
}
|
|
return body;
|
|
},
|
|
async runOp() {
|
|
const op = this.currentOp();
|
|
if (!op) return;
|
|
const formData = this.data.formData || {};
|
|
this.setData({ resultText: '', resultTextCards: [], imagePath: '' });
|
|
|
|
if (op.type === 'upload') {
|
|
if (!this.data.filePath) {
|
|
const view = buildResultView('请先选择文件');
|
|
this.setData({ resultText: view.text, resultTextCards: view.cards });
|
|
return;
|
|
}
|
|
const formDataPayload = { ...formData };
|
|
delete formDataPayload.file;
|
|
const result = await uploadFile({ url: op.path, filePath: this.data.filePath, name: 'file', formData: formDataPayload });
|
|
const view = buildResultView(result);
|
|
this.setData({ resultText: view.text, resultTextCards: view.cards });
|
|
return;
|
|
}
|
|
|
|
const url = this.buildUrl(op, formData);
|
|
if (op.responseType === 'file') {
|
|
try {
|
|
const tempPath = await downloadFile({ url });
|
|
const view = buildResultView('下载成功');
|
|
this.setData({ imagePath: tempPath, 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 });
|
|
}
|
|
}
|
|
});
|