|
@@ -1,5 +1,5 @@
|
|
import { Component, ElementRef, ViewChild, AfterViewInit, ChangeDetectorRef } from '@angular/core';
|
|
import { Component, ElementRef, ViewChild, AfterViewInit, ChangeDetectorRef } from '@angular/core';
|
|
-import { predict } from 'src/app/service/api'; // 引入 API 服务
|
|
|
|
|
|
+import { predict,saveCeramicDetectionRecord,getCeramicDetectionRecords} from 'src/app/service/api'; // 引入 API 服务
|
|
import { ModalController } from '@ionic/angular';
|
|
import { ModalController } from '@ionic/angular';
|
|
import { CeramicDetailsPage } from '../ceramic-details/ceramic-details.page';
|
|
import { CeramicDetailsPage } from '../ceramic-details/ceramic-details.page';
|
|
@Component({
|
|
@Component({
|
|
@@ -22,6 +22,21 @@ export class Tab3Page implements AfterViewInit{
|
|
constructor(private modalController: ModalController) {
|
|
constructor(private modalController: ModalController) {
|
|
this.checkCameraAvailability();
|
|
this.checkCameraAvailability();
|
|
}
|
|
}
|
|
|
|
+ ngOnInit() {
|
|
|
|
+ // 页面初始化时加载用户记录
|
|
|
|
+ const storedUserData = localStorage.getItem('user'); // 从 localStorage 获取登录用户数据
|
|
|
|
+ if (storedUserData) {
|
|
|
|
+ const user = JSON.parse(storedUserData);
|
|
|
|
+ if (user.username) {
|
|
|
|
+ console.log('当前登录用户:', user.username);
|
|
|
|
+ this.fetchUserRecords(user.username); // 调用获取当前用户记录的方法
|
|
|
|
+ } else {
|
|
|
|
+ console.error('未找到用户名,请检查登录状态!');
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ console.error('用户未登录或用户数据未找到');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
checkCameraAvailability() {
|
|
checkCameraAvailability() {
|
|
navigator.mediaDevices
|
|
navigator.mediaDevices
|
|
.getUserMedia({ video: true })
|
|
.getUserMedia({ video: true })
|
|
@@ -148,28 +163,67 @@ export class Tab3Page implements AfterViewInit{
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
console.log('开始检测事件触发,调用后端接口');
|
|
console.log('开始检测事件触发,调用后端接口');
|
|
- //调用Flask API进行检测
|
|
|
|
|
|
+ // 检查是否为游客登录
|
|
|
|
+ const guestFlag = localStorage.getItem('guest');
|
|
|
|
+ const isGuest = guestFlag === 'true'; // 判断是否为游客
|
|
|
|
+ // 调用 Flask API 进行检测
|
|
try {
|
|
try {
|
|
- predict(this.imageSrc)
|
|
|
|
- .then(async (response) => {
|
|
|
|
- console.log('检测结果:', response);
|
|
|
|
- // 假设后端返回的数据格式为 { prediction: string, confidence: string, details: string }
|
|
|
|
- const newResult = {
|
|
|
|
- result: response.prediction.predicted_label || '未知结果',
|
|
|
|
- confidence: response.prediction.confidence
|
|
|
|
- ? response.prediction.confidence.toFixed(3)
|
|
|
|
- : '未知置信度',
|
|
|
|
- };
|
|
|
|
- // 将新结果添加到识别结果数组
|
|
|
|
- this.recognitionResults.push(newResult);
|
|
|
|
- // 显示陶瓷详情页面
|
|
|
|
- await this.openCeramicDetails(newResult.result, newResult.confidence); // 传递两个参数
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
- catch(error) {
|
|
|
|
|
|
+ const response = await predict(this.imageSrc); // 调用后端预测接口
|
|
|
|
+ console.log('检测结果:', response);
|
|
|
|
+ // 假设后端返回的数据格式为 { prediction: { predicted_label: string, confidence: number } }
|
|
|
|
+ const newResult = {
|
|
|
|
+ result: response.prediction?.predicted_label || '未知结果',
|
|
|
|
+ confidence: response.prediction?.confidence
|
|
|
|
+ ? response.prediction.confidence.toFixed(3) // 保留 3 位小数
|
|
|
|
+ : '未知置信度',
|
|
|
|
+ };
|
|
|
|
+ // 将新结果添加到识别结果数组
|
|
|
|
+ this.recognitionResults.push(newResult);
|
|
|
|
+ // 判断是否为游客
|
|
|
|
+ if (!isGuest) {
|
|
|
|
+ console.log('用户已登录,准备保存检测记录...');
|
|
|
|
+ const storedUserData = localStorage.getItem('user');
|
|
|
|
+ const user = storedUserData ? JSON.parse(storedUserData) : { nickname: '未知用户' };
|
|
|
|
+ const record = {
|
|
|
|
+ nickname: user.username || '匿名用户', // 从用户数据中获取昵称
|
|
|
|
+ detection_time: new Date().toISOString(), // 当前时间,ISO 格式
|
|
|
|
+ result: newResult.result, // 识别结果
|
|
|
|
+ confidence: newResult.confidence, // 置信度
|
|
|
|
+ };
|
|
|
|
+ // 打印 record 对象
|
|
|
|
+ console.log('保存的检测记录:', record);
|
|
|
|
+ try {
|
|
|
|
+ const saveResponse = await saveCeramicDetectionRecord(record); // 调用保存接口
|
|
|
|
+ console.log('识别记录保存成功:', saveResponse);
|
|
|
|
+ } catch (saveError) {
|
|
|
|
+ console.error('识别记录保存失败:', saveError);
|
|
|
|
+ alert('识别记录保存失败,请检查网络或后端服务!');
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ console.log('游客登录,跳过保存检测记录');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 显示陶瓷详情页面
|
|
|
|
+ await this.openCeramicDetails(newResult.result, newResult.confidence); // 传递两个参数
|
|
|
|
+ } catch (error) {
|
|
console.error('检测失败:', error);
|
|
console.error('检测失败:', error);
|
|
alert('检测失败,请检查网络或后端服务!');
|
|
alert('检测失败,请检查网络或后端服务!');
|
|
- };
|
|
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 获取当前登录用户的识别记录
|
|
|
|
+ async fetchUserRecords(username: string) {
|
|
|
|
+ try {
|
|
|
|
+ const records = await getCeramicDetectionRecords(username); // 调用后端接口获取所有记录
|
|
|
|
+ console.log('获取到的所有识别记录:', records);
|
|
|
|
+ // 更新表格数据
|
|
|
|
+ this.recognitionResults = records.map((record: any) => ({
|
|
|
|
+ result: record.result,
|
|
|
|
+ confidence: record.confidence,
|
|
|
|
+ }));
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error('获取用户检测记录时发生错误:', error);
|
|
|
|
+ alert('获取用户检测记录失败,请检查网络或后端服务!');
|
|
|
|
+ }
|
|
}
|
|
}
|
|
// 显示陶瓷详情模态框
|
|
// 显示陶瓷详情模态框
|
|
async openCeramicDetails(ceramicName: string, confidence: string) {
|
|
async openCeramicDetails(ceramicName: string, confidence: string) {
|