wechat_rob_mini/pages/console/books.js

149 lines
5.6 KiB
JavaScript

const { request, uploadFile, downloadFile, buildResultView } = require('../../utils/request');
Page({
data: {
status: '',
keyword: '',
page: 1,
pageSize: 20,
bookId: '',
bookPage: 1,
filePath: '',
path: '',
creator: '',
taskBookId: '',
days: '',
push_times: '',
channel: '',
email: '',
wechat_remark: '',
taskId: '',
result: '',
resultCards: [],
imagePath: ''
},
onInput(e) {
const key = e.currentTarget.dataset.key;
this.setData({ [key]: e.detail.value });
},
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) {}
},
async listBooks() {
const qs = [];
if (this.data.status) qs.push(`status=${encodeURIComponent(this.data.status)}`);
if (this.data.keyword) qs.push(`keyword=${encodeURIComponent(this.data.keyword)}`);
qs.push(`page=${encodeURIComponent(this.data.page)}`);
qs.push(`pageSize=${encodeURIComponent(this.data.pageSize)}`);
const result = await request({ url: `/api/console/books?${qs.join('&')}` });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async importByPath() {
const result = await request({
url: '/api/console/books',
method: 'POST',
data: { path: this.data.path, creator: this.data.creator }
});
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async uploadPDF() {
if (!this.data.filePath) {
const view = buildResultView('请选择PDF文件');
this.setData({ result: view.text, resultCards: view.cards });
return;
}
const result = await uploadFile({
url: '/api/console/books',
filePath: this.data.filePath,
name: 'file',
formData: { creator: this.data.creator }
});
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async bookDetail() {
const result = await request({ url: `/api/console/books/${encodeURIComponent(this.data.bookId)}` });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async deleteBook() {
const result = await request({ url: `/api/console/books/${encodeURIComponent(this.data.bookId)}`, method: 'DELETE' });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async previewPage() {
if (!this.data.bookId || !this.data.bookPage) {
const view = buildResultView('请输入书籍ID和页码');
this.setData({ result: view.text, resultCards: view.cards });
return;
}
try {
const tempPath = await downloadFile({ url: `/api/console/books/${encodeURIComponent(this.data.bookId)}/page/${encodeURIComponent(this.data.bookPage)}` });
const view = buildResultView('下载成功');
this.setData({ imagePath: tempPath, result: view.text, resultCards: view.cards });
} catch (e) {
const view = buildResultView('下载失败');
this.setData({ result: view.text, resultCards: view.cards });
}
},
async listTasks() {
const qs = [];
if (this.data.status) qs.push(`status=${encodeURIComponent(this.data.status)}`);
if (this.data.taskBookId) qs.push(`book_id=${encodeURIComponent(this.data.taskBookId)}`);
qs.push(`page=${encodeURIComponent(this.data.page)}`);
qs.push(`pageSize=${encodeURIComponent(this.data.pageSize)}`);
const result = await request({ url: `/api/console/read_task?${qs.join('&')}` });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async createTask() {
const data = {
book_id: Number(this.data.taskBookId),
days: this.data.days ? Number(this.data.days) : undefined,
push_times: this.data.push_times,
channel: this.data.channel,
email: this.data.email,
wechat_remark: this.data.wechat_remark,
auto_start: true
};
const result = await request({ url: '/api/console/read_task', method: 'POST', data });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async updateTask() {
const data = {
status: undefined,
days: this.data.days ? Number(this.data.days) : undefined,
push_times: this.data.push_times,
channel: this.data.channel,
email: this.data.email,
wechat_remark: this.data.wechat_remark,
auto_start: true
};
const result = await request({ url: `/api/console/read_task/${encodeURIComponent(this.data.taskId)}`, method: 'PUT', data });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async deleteTask() {
const result = await request({ url: `/api/console/read_task/${encodeURIComponent(this.data.taskId)}`, method: 'DELETE' });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async pushOnce() {
const result = await request({ url: `/api/console/read_task/${encodeURIComponent(this.data.taskId)}/push_once`, method: 'POST', data: {} });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
},
async previewNext() {
const result = await request({ url: `/api/console/read_task/${encodeURIComponent(this.data.taskId)}/next` });
const view = buildResultView(result);
this.setData({ result: view.text, resultCards: view.cards });
}
});