You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
385 lines
8.3 KiB
385 lines
8.3 KiB
// ocr-import.ts
|
|
|
|
// 课程接口定义
|
|
interface Course {
|
|
id?: number;
|
|
courseName: string;
|
|
classroom: string;
|
|
teacherName?: string;
|
|
dayOfWeek: number;
|
|
startTime: string;
|
|
endTime: string;
|
|
notes?: string;
|
|
selected?: boolean;
|
|
}
|
|
|
|
// OCR结果接口
|
|
interface OcrResult {
|
|
success: boolean;
|
|
rawText?: string;
|
|
textLines?: string[];
|
|
courses?: Course[];
|
|
errorMessage?: string;
|
|
}
|
|
|
|
// 导入结果接口
|
|
interface ImportResult {
|
|
successCount: number;
|
|
errorCount: number;
|
|
errorMessages?: string[];
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
currentStep: 1, // 1: 选择图片, 2: OCR识别, 3: 导入结果
|
|
selectedImage: '',
|
|
ocrLoading: false,
|
|
importLoading: false,
|
|
ocrResult: {} as OcrResult,
|
|
importResult: {} as ImportResult,
|
|
weekNames: ['一', '二', '三', '四', '五', '六', '日'],
|
|
hasSelectedCourses: false,
|
|
|
|
// 编辑相关
|
|
showEditModal: false,
|
|
editingCourse: {} as Course,
|
|
editingIndex: -1,
|
|
weekTypeOptions: ['每周', '单周', '双周']
|
|
},
|
|
|
|
onLoad() {
|
|
// 页面加载时的初始化
|
|
},
|
|
|
|
// 选择图片
|
|
onChooseImage() {
|
|
wx.showActionSheet({
|
|
itemList: ['拍照', '从相册选择'],
|
|
success: (res) => {
|
|
if (res.tapIndex === 0) {
|
|
this.takePhoto();
|
|
} else if (res.tapIndex === 1) {
|
|
this.chooseFromAlbum();
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
// 拍照
|
|
takePhoto() {
|
|
wx.chooseMedia({
|
|
count: 1,
|
|
mediaType: ['image'],
|
|
sourceType: ['camera'],
|
|
camera: 'back',
|
|
success: (res) => {
|
|
const tempFilePath = res.tempFiles[0].tempFilePath;
|
|
this.setData({
|
|
selectedImage: tempFilePath
|
|
});
|
|
},
|
|
fail: (error) => {
|
|
console.error('拍照失败:', error);
|
|
wx.showToast({
|
|
title: '拍照失败',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
// 从相册选择
|
|
chooseFromAlbum() {
|
|
wx.chooseMedia({
|
|
count: 1,
|
|
mediaType: ['image'],
|
|
sourceType: ['album'],
|
|
success: (res) => {
|
|
const tempFilePath = res.tempFiles[0].tempFilePath;
|
|
this.setData({
|
|
selectedImage: tempFilePath
|
|
});
|
|
},
|
|
fail: (error) => {
|
|
console.error('选择图片失败:', error);
|
|
wx.showToast({
|
|
title: '选择图片失败',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
// 开始OCR识别
|
|
async onStartOcr() {
|
|
if (!this.data.selectedImage) {
|
|
wx.showToast({
|
|
title: '请先选择图片',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
this.setData({
|
|
currentStep: 2,
|
|
ocrLoading: true
|
|
});
|
|
|
|
try {
|
|
// 上传图片并进行OCR识别
|
|
const uploadResult = await this.uploadImageForOcr(this.data.selectedImage);
|
|
|
|
if (uploadResult.success) {
|
|
// 为课程添加选中状态
|
|
const coursesWithSelection = (uploadResult.courses || []).map(course => ({
|
|
...course,
|
|
selected: true
|
|
}));
|
|
|
|
this.setData({
|
|
ocrResult: {
|
|
...uploadResult,
|
|
courses: coursesWithSelection
|
|
},
|
|
hasSelectedCourses: coursesWithSelection.length > 0
|
|
});
|
|
} else {
|
|
this.setData({
|
|
ocrResult: uploadResult
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('OCR识别失败:', error);
|
|
this.setData({
|
|
ocrResult: {
|
|
success: false,
|
|
errorMessage: '网络错误,请重试'
|
|
}
|
|
});
|
|
} finally {
|
|
this.setData({
|
|
ocrLoading: false
|
|
});
|
|
}
|
|
},
|
|
|
|
// 上传图片进行OCR识别
|
|
async uploadImageForOcr(imagePath: string): Promise<OcrResult> {
|
|
try {
|
|
const api = require('../../utils/api').default;
|
|
const userOpenid = api.getUserOpenid();
|
|
|
|
const res = await api.ocr.uploadImage(imagePath, userOpenid);
|
|
|
|
if (res.code === 200) {
|
|
return res.data;
|
|
} else {
|
|
return {
|
|
success: false,
|
|
errorMessage: res.message || 'OCR识别失败'
|
|
};
|
|
}
|
|
} catch (error) {
|
|
console.error('OCR识别失败:', error);
|
|
return {
|
|
success: false,
|
|
errorMessage: '网络错误,请重试'
|
|
};
|
|
}
|
|
},
|
|
|
|
// 切换课程选中状态
|
|
onCourseToggle(e: any) {
|
|
const index = e.currentTarget.dataset.index;
|
|
const checked = e.detail.value;
|
|
|
|
this.setData({
|
|
[`ocrResult.courses[${index}].selected`]: checked
|
|
});
|
|
|
|
// 检查是否有选中的课程
|
|
const hasSelected = this.data.ocrResult.courses?.some(course => course.selected) || false;
|
|
this.setData({
|
|
hasSelectedCourses: hasSelected
|
|
});
|
|
},
|
|
|
|
// 导入选中的课程
|
|
async onImportCourses() {
|
|
const selectedCourses = this.data.ocrResult.courses?.filter(course => course.selected) || [];
|
|
|
|
if (selectedCourses.length === 0) {
|
|
wx.showToast({
|
|
title: '请选择要导入的课程',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
this.setData({
|
|
currentStep: 3,
|
|
importLoading: true
|
|
});
|
|
|
|
try {
|
|
const api = require('../../utils/api').default;
|
|
const userOpenid = api.getUserOpenid();
|
|
|
|
const res = await api.ocr.importCourses(selectedCourses, userOpenid);
|
|
|
|
if (res.code === 200) {
|
|
const result = res.data;
|
|
this.setData({
|
|
importResult: {
|
|
successCount: result.successCourses?.length || 0,
|
|
errorCount: result.errorMessages?.length || 0,
|
|
errorMessages: result.errorMessages || []
|
|
}
|
|
});
|
|
} else {
|
|
throw new Error(res.message || '导入失败');
|
|
}
|
|
} catch (error) {
|
|
console.error('导入课程失败:', error);
|
|
const api = require('../../utils/api').default;
|
|
api.handleError(error, '导入失败');
|
|
// 回到上一步
|
|
this.setData({
|
|
currentStep: 2
|
|
});
|
|
} finally {
|
|
this.setData({
|
|
importLoading: false
|
|
});
|
|
}
|
|
},
|
|
|
|
// 返回第一步
|
|
onBackToStep1() {
|
|
this.setData({
|
|
currentStep: 1,
|
|
selectedImage: '',
|
|
ocrResult: {},
|
|
hasSelectedCourses: false
|
|
});
|
|
},
|
|
|
|
// 再次导入
|
|
onImportAgain() {
|
|
this.setData({
|
|
currentStep: 1,
|
|
selectedImage: '',
|
|
ocrResult: {},
|
|
importResult: {},
|
|
hasSelectedCourses: false
|
|
});
|
|
},
|
|
|
|
// 编辑课程
|
|
onEditCourse(e: any) {
|
|
const index = e.currentTarget.dataset.index;
|
|
const course = this.data.ocrResult.courses[index];
|
|
|
|
this.setData({
|
|
showEditModal: true,
|
|
editingCourse: { ...course },
|
|
editingIndex: index
|
|
});
|
|
},
|
|
|
|
// 关闭编辑弹窗
|
|
onCloseEditModal() {
|
|
this.setData({
|
|
showEditModal: false,
|
|
editingCourse: {},
|
|
editingIndex: -1
|
|
});
|
|
},
|
|
|
|
// 阻止事件冒泡
|
|
stopPropagation() {
|
|
// 空方法,用于阻止事件冒泡
|
|
},
|
|
|
|
// 编辑输入
|
|
onEditInput(e: any) {
|
|
const field = e.currentTarget.dataset.field;
|
|
const value = e.detail.value;
|
|
|
|
this.setData({
|
|
[`editingCourse.${field}`]: value
|
|
});
|
|
},
|
|
|
|
// 星期选择
|
|
onDayChange(e: any) {
|
|
const dayOfWeek = parseInt(e.detail.value) + 1;
|
|
this.setData({
|
|
'editingCourse.dayOfWeek': dayOfWeek
|
|
});
|
|
},
|
|
|
|
// 开始时间选择
|
|
onStartTimeChange(e: any) {
|
|
this.setData({
|
|
'editingCourse.startTime': e.detail.value
|
|
});
|
|
},
|
|
|
|
// 结束时间选择
|
|
onEndTimeChange(e: any) {
|
|
this.setData({
|
|
'editingCourse.endTime': e.detail.value
|
|
});
|
|
},
|
|
|
|
// 周次类型选择
|
|
onWeekTypeChange(e: any) {
|
|
this.setData({
|
|
'editingCourse.weekType': parseInt(e.detail.value)
|
|
});
|
|
},
|
|
|
|
// 保存编辑
|
|
onSaveEdit() {
|
|
const { editingCourse, editingIndex } = this.data;
|
|
|
|
// 验证必填字段
|
|
if (!editingCourse.courseName?.trim()) {
|
|
wx.showToast({
|
|
title: '请输入课程名称',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (!editingCourse.classroom?.trim()) {
|
|
wx.showToast({
|
|
title: '请输入上课地点',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 更新课程信息
|
|
this.setData({
|
|
[`ocrResult.courses[${editingIndex}]`]: editingCourse,
|
|
showEditModal: false,
|
|
editingCourse: {},
|
|
editingIndex: -1
|
|
});
|
|
|
|
wx.showToast({
|
|
title: '保存成功',
|
|
icon: 'success'
|
|
});
|
|
},
|
|
|
|
// 查看课表
|
|
onViewSchedule() {
|
|
wx.switchTab({
|
|
url: '/pages/index/index'
|
|
});
|
|
}
|
|
})
|