236 lines
6.0 KiB
TypeScript
236 lines
6.0 KiB
TypeScript
import request from '../../utils/request';
|
|
|
|
interface BookForm {
|
|
isbn: string;
|
|
title: string;
|
|
author: string;
|
|
publisher: string;
|
|
description: string;
|
|
bookUrl: string;
|
|
coverUrl: string;
|
|
category: string;
|
|
tags: string;
|
|
language: string;
|
|
publishDate: string;
|
|
}
|
|
|
|
Component({
|
|
data: {
|
|
form: {
|
|
isbn: '',
|
|
title: '',
|
|
author: '',
|
|
publisher: '',
|
|
description: '',
|
|
bookUrl: '',
|
|
coverUrl: '',
|
|
category: '',
|
|
tags: '',
|
|
language: '汉语',
|
|
publishDate: ''
|
|
} as BookForm,
|
|
categories: ['小说', '文学', '历史', '科技', '教育', '其他']
|
|
},
|
|
|
|
methods: {
|
|
// 输入框变化处理
|
|
onInput(e: any) {
|
|
const { field } = e.currentTarget.dataset;
|
|
this.setData({
|
|
[`form.${field}`]: e.detail.value
|
|
});
|
|
},
|
|
|
|
// 选择分类
|
|
onCategoryChange(e: any) {
|
|
this.setData({
|
|
'form.category': e.detail.value
|
|
});
|
|
},
|
|
|
|
// 选择日期
|
|
onDateChange(e: any) {
|
|
const date = e.detail.value; // 格式: "2024-12-16"
|
|
// 添加时间部分,转换为标准格式
|
|
const dateTime = `${date} 00:00:00`;
|
|
this.setData({
|
|
'form.publishDate': dateTime
|
|
});
|
|
},
|
|
|
|
// 上传封面
|
|
async uploadCover() {
|
|
try {
|
|
const res = await wx.chooseMedia({
|
|
count: 1,
|
|
mediaType: ['image'],
|
|
sizeType: ['compressed']
|
|
});
|
|
|
|
wx.showLoading({ title: '上传中...' });
|
|
|
|
// 上传文件到服务器
|
|
const uploadRes = await new Promise((resolve, reject) => {
|
|
wx.uploadFile({
|
|
url: 'http://localhost:8084/api/common/uploadFile',
|
|
filePath: res.tempFiles[0].tempFilePath,
|
|
name: 'file',
|
|
formData: {
|
|
bucketName: 'photo'
|
|
},
|
|
success: (res) => {
|
|
console.log('上传成功:', res);
|
|
resolve(res);
|
|
},
|
|
fail: (error) => {
|
|
console.error('上传失败:', error);
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
console.log('上传响应:', uploadRes);
|
|
|
|
if (uploadRes.statusCode !== 200) {
|
|
throw new Error(`服务器响应错误: ${uploadRes.statusCode}`);
|
|
}
|
|
|
|
const data = JSON.parse(uploadRes.data);
|
|
console.log('解析后的数据:', data);
|
|
|
|
if(data.code === 200) {
|
|
this.setData({
|
|
'form.coverUrl': data.data
|
|
});
|
|
wx.showToast({
|
|
title: '上传成功',
|
|
icon: 'success'
|
|
});
|
|
} else {
|
|
throw new Error(data.message || '上传失败');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('上传封面失败:', error);
|
|
wx.showToast({
|
|
title: error instanceof Error ? error.message : '上传失败',
|
|
icon: 'none'
|
|
});
|
|
} finally {
|
|
wx.hideLoading();
|
|
}
|
|
},
|
|
|
|
// 上传电子书
|
|
async uploadBook() {
|
|
try {
|
|
const res = await wx.chooseMessageFile({
|
|
count: 1,
|
|
type: 'file',
|
|
extension: ['txt']
|
|
});
|
|
|
|
wx.showLoading({ title: '上传中...' });
|
|
|
|
// 上传文件到服务器
|
|
const uploadRes = await new Promise((resolve, reject) => {
|
|
wx.uploadFile({
|
|
url: 'http://localhost:8084/api/common/uploadFile',
|
|
filePath: res.tempFiles[0].path,
|
|
name: 'file',
|
|
formData: {
|
|
bucketName: 'txt'
|
|
},
|
|
success: (res) => {
|
|
console.log('上传成功:', res);
|
|
resolve(res);
|
|
},
|
|
fail: (error) => {
|
|
console.error('上传失败:', error);
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
console.log('上传响应:', uploadRes);
|
|
|
|
if (uploadRes.statusCode !== 200) {
|
|
throw new Error(`服务器响应错误: ${uploadRes.statusCode}`);
|
|
}
|
|
|
|
const data = JSON.parse(uploadRes.data);
|
|
console.log('解析后的数据:', data);
|
|
|
|
if(data.code === 200) {
|
|
this.setData({
|
|
'form.bookUrl': data.data
|
|
});
|
|
wx.showToast({
|
|
title: '上传成功',
|
|
icon: 'success'
|
|
});
|
|
} else {
|
|
throw new Error(data.message || '上传失败');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('上传文件失败:', error);
|
|
wx.showToast({
|
|
title: error instanceof Error ? error.message : '上传失败',
|
|
icon: 'none'
|
|
});
|
|
} finally {
|
|
wx.hideLoading();
|
|
}
|
|
},
|
|
|
|
// 提交表单
|
|
async submitForm() {
|
|
try {
|
|
const { form } = this.data;
|
|
|
|
// 表单验证
|
|
if (!form.title || !form.author || !form.bookUrl) {
|
|
wx.showToast({
|
|
title: '请填写必要信息',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 确保日期格式正确
|
|
if (form.publishDate && !form.publishDate.includes(':')) {
|
|
form.publishDate = `${form.publishDate} 00:00:00`;
|
|
}
|
|
|
|
wx.showLoading({ title: '提交中...' });
|
|
|
|
const res = await request.post('/books/add', {
|
|
...form,
|
|
// 添加当前时间作为创建和更新时间
|
|
createdTime: new Date().toISOString().replace('T', ' ').split('.')[0],
|
|
updatedTime: new Date().toISOString().replace('T', ' ').split('.')[0]
|
|
});
|
|
|
|
if (res.code === 200) {
|
|
wx.showToast({
|
|
title: '添加成功',
|
|
icon: 'success'
|
|
});
|
|
// 返回书架页面
|
|
setTimeout(() => {
|
|
wx.navigateBack();
|
|
}, 1500);
|
|
}
|
|
} catch (error) {
|
|
console.error('提交失败:', error);
|
|
wx.showToast({
|
|
title: '提交失败',
|
|
icon: 'none'
|
|
});
|
|
} finally {
|
|
wx.hideLoading();
|
|
}
|
|
}
|
|
}
|
|
});
|