feat: Add plant identification feature with image upload, classification results display, and integration into the wiki page.

This commit is contained in:
Blizzard
2026-02-10 14:02:35 +08:00
parent 6ea77c00ce
commit 6f88bc656b
15 changed files with 1481 additions and 491 deletions
+88
View File
@@ -0,0 +1,88 @@
// pages/wiki/identify/index.js
import request from '../../../utils/request';
Page({
data: {
imagePath: '',
results: [],
isLoading: true,
hasError: false,
topResult: null
},
onLoad(options) {
// Image path is passed via global data (too long for URL params)
const app = getApp();
const imagePath = app.globalData._identifyImagePath || '';
if (!imagePath) {
this.setData({ isLoading: false, hasError: true });
return;
}
this.setData({ imagePath });
this.classifyPlant(imagePath);
},
classifyPlant(filePath) {
this.setData({ isLoading: true, hasError: false });
request.uploadToUrl('/classify/plant', filePath, 'file').then(res => {
const results = res.result || [];
// Map results with percentage scores
const mappedResults = results.map((item, index) => ({
index: index,
name: item.name,
score: item.score,
percent: (item.score * 100).toFixed(2),
description: (item.baike_info && item.baike_info.description) || '',
baikeUrl: (item.baike_info && item.baike_info.baike_url) || '',
isTop: index === 0
}));
this.setData({
results: mappedResults,
topResult: mappedResults.length > 0 ? mappedResults[0] : null,
isLoading: false
});
}).catch(err => {
console.error('Classify failed', err);
this.setData({ isLoading: false, hasError: true });
});
},
// Retry identification
handleRetry() {
if (this.data.imagePath) {
this.classifyPlant(this.data.imagePath);
}
},
// Go back and re-select image
handleReselect() {
wx.navigateBack();
},
// Search in wiki
searchInWiki(e) {
const name = e.currentTarget.dataset.name;
// Navigate back to wiki and trigger search
const pages = getCurrentPages();
if (pages.length >= 2) {
const wikiPage = pages[pages.length - 2];
wikiPage.setData({ searchQuery: name }, () => {
wikiPage.fetchWikiList(true);
});
}
wx.navigateBack();
},
// Preview uploaded image
previewImage() {
wx.previewImage({
urls: [this.data.imagePath],
current: this.data.imagePath
});
}
});
+10
View File
@@ -0,0 +1,10 @@
{
"navigationBarTitleText": "识别结果",
"usingComponents": {
"t-icon": "tdesign-miniprogram/icon/icon",
"t-tag": "tdesign-miniprogram/tag/tag",
"t-loading": "tdesign-miniprogram/loading/loading",
"t-button": "tdesign-miniprogram/button/button",
"t-empty": "tdesign-miniprogram/empty/empty"
}
}
+127
View File
@@ -0,0 +1,127 @@
<!--pages/wiki/identify/index.wxml-->
<view class="identify-page">
<!-- Loading State -->
<view wx:if="{{isLoading}}" class="state-container">
<view class="state-card">
<view class="loading-image-wrap">
<image src="{{imagePath}}" mode="aspectFill" class="loading-preview" bindtap="previewImage" />
<view class="scan-line"></view>
</view>
<view class="loading-info">
<view class="loading-dots">
<view class="dot dot1"></view>
<view class="dot dot2"></view>
<view class="dot dot3"></view>
</view>
<text class="state-title">正在识别中...</text>
<text class="state-hint">AI 正在分析植物特征</text>
</view>
</view>
</view>
<!-- Error State -->
<view wx:elif="{{hasError}}" class="state-container">
<view class="state-card">
<view class="error-icon-wrap">
<t-icon name="close-circle" size="96rpx" color="#EF5350" />
</view>
<text class="state-title">识别失败</text>
<text class="state-hint">请检查网络连接后重试</text>
<view class="state-actions">
<view class="action-btn primary" bindtap="handleRetry">
<t-icon name="refresh" size="32rpx" color="#fff" />
<text>重新识别</text>
</view>
<view class="action-btn outline" bindtap="handleReselect">
<t-icon name="arrow-left" size="32rpx" color="#558B2F" />
<text>返回重选</text>
</view>
</view>
</view>
</view>
<!-- Results State -->
<scroll-view wx:else scroll-y class="results-scroll" enhanced show-scrollbar="{{false}}">
<!-- Hero Section: Image + Top Result -->
<view class="hero-section">
<image src="{{imagePath}}" mode="aspectFill" class="hero-image" bindtap="previewImage" />
<view class="hero-gradient"></view>
<view wx:if="{{topResult}}" class="hero-overlay">
<view class="hero-badge">
<t-icon name="check-circle" size="28rpx" color="#fff" />
<text>匹配度 {{topResult.percent}}%</text>
</view>
<text class="hero-plant-name">{{topResult.name}}</text>
</view>
</view>
<!-- Top Result Detail Card -->
<view wx:if="{{topResult}}" class="detail-card-wrapper">
<view class="detail-card">
<view class="detail-card-header">
<view class="result-rank best">1</view>
<view class="result-name-area">
<text class="result-main-name">{{topResult.name}}</text>
<text class="confidence-text">置信度 {{topResult.percent}}%</text>
</view>
</view>
<!-- Confidence Bar -->
<view class="confidence-bar-wrap">
<view class="confidence-bar-bg">
<view class="confidence-bar-fill" style="width: {{topResult.percent}}%;"></view>
</view>
</view>
<text wx:if="{{topResult.description}}" class="result-description">{{topResult.description}}</text>
<view class="detail-card-actions">
<view class="action-btn primary" bindtap="searchInWiki" data-name="{{topResult.name}}">
<t-icon name="search" size="32rpx" color="#fff" />
<text>在百科中搜索</text>
</view>
</view>
</view>
</view>
<!-- Other Results -->
<view wx:if="{{results.length > 1}}" class="other-section">
<text class="section-title">其他可能的结果</text>
<view class="other-list">
<view
wx:for="{{results}}"
wx:key="index"
wx:if="{{index > 0}}"
class="other-item"
bindtap="searchInWiki"
data-name="{{item.name}}"
>
<view class="result-rank normal">{{index + 1}}</view>
<view class="other-item-info">
<text class="other-item-name">{{item.name}}</text>
<view class="mini-bar-wrap">
<view class="mini-bar-bg">
<view class="mini-bar-fill" style="width: {{item.percent > 1 ? item.percent : 1}}%;"></view>
</view>
<text class="mini-bar-text">{{item.percent}}%</text>
</view>
</view>
<t-icon name="chevron-right" size="36rpx" color="#D1D5DB" />
</view>
</view>
</view>
<!-- Bottom Actions -->
<view class="bottom-section">
<view class="action-btn outline full" bindtap="handleReselect">
<t-icon name="refresh" size="32rpx" color="#558B2F" />
<text>重新识别</text>
</view>
</view>
<view style="height: 80rpx;"></view>
</scroll-view>
</view>
+407
View File
@@ -0,0 +1,407 @@
/* pages/wiki/identify/index.wxss */
.identify-page {
min-height: 100vh;
background: #F5F7F5;
}
/* ========== Shared State Container ========== */
.state-container {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 48rpx;
}
.state-card {
background: #fff;
border-radius: 40rpx;
padding: 56rpx 48rpx;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
gap: 32rpx;
box-shadow: 0 12rpx 40rpx rgba(85, 139, 47, 0.08);
}
/* ========== Loading State ========== */
.loading-image-wrap {
width: 280rpx;
height: 280rpx;
border-radius: 32rpx;
overflow: hidden;
position: relative;
box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.1);
}
.loading-preview {
width: 100%;
height: 100%;
display: block;
}
/* Scan line animation */
.scan-line {
position: absolute;
left: 0;
right: 0;
height: 4rpx;
background: linear-gradient(90deg, transparent, #558B2F, transparent);
animation: scan 2s ease-in-out infinite;
box-shadow: 0 0 16rpx rgba(85, 139, 47, 0.5);
}
@keyframes scan {
0% { top: 0; }
50% { top: 100%; }
100% { top: 0; }
}
.loading-info {
display: flex;
flex-direction: column;
align-items: center;
gap: 12rpx;
}
/* Animated dots */
.loading-dots {
display: flex;
gap: 12rpx;
margin-bottom: 8rpx;
}
.dot {
width: 16rpx;
height: 16rpx;
border-radius: 50%;
background: #558B2F;
animation: dotPulse 1.4s infinite ease-in-out;
}
.dot2 { animation-delay: 0.2s; }
.dot3 { animation-delay: 0.4s; }
@keyframes dotPulse {
0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
40% { transform: scale(1); opacity: 1; }
}
.state-title {
font-size: 34rpx;
font-weight: 700;
color: #1F2937;
}
.state-hint {
font-size: 26rpx;
color: #9CA3AF;
}
/* ========== Error State ========== */
.error-icon-wrap {
width: 140rpx;
height: 140rpx;
border-radius: 50%;
background: #FFF5F5;
display: flex;
align-items: center;
justify-content: center;
}
.state-actions {
display: flex;
gap: 24rpx;
margin-top: 16rpx;
width: 100%;
justify-content: center;
}
/* ========== Shared Action Buttons ========== */
.action-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 10rpx;
padding: 20rpx 36rpx;
border-radius: 48rpx;
font-size: 28rpx;
font-weight: 600;
transition: all 0.2s;
}
.action-btn:active {
transform: scale(0.96);
}
.action-btn.primary {
background: linear-gradient(135deg, #558B2F, #689F38);
color: #fff;
box-shadow: 0 8rpx 24rpx rgba(85, 139, 47, 0.25);
}
.action-btn.primary text {
color: #fff;
}
.action-btn.outline {
background: #fff;
color: #558B2F;
border: 2rpx solid #C5E1A5;
}
.action-btn.outline text {
color: #558B2F;
}
.action-btn.full {
width: 100%;
}
/* ========== Results: Hero Section ========== */
.results-scroll {
height: 100vh;
}
.hero-section {
position: relative;
height: 480rpx;
overflow: hidden;
}
.hero-image {
width: 100%;
height: 100%;
display: block;
}
.hero-gradient {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 280rpx;
background: linear-gradient(180deg, transparent, rgba(0,0,0,0.65));
}
.hero-overlay {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 40rpx;
}
.hero-badge {
display: inline-flex;
align-items: center;
gap: 8rpx;
background: rgba(85, 139, 47, 0.85);
backdrop-filter: blur(8rpx);
color: #fff;
font-size: 24rpx;
font-weight: 600;
padding: 8rpx 20rpx;
border-radius: 24rpx;
margin-bottom: 12rpx;
}
.hero-badge text {
color: #fff;
}
.hero-plant-name {
font-size: 52rpx;
font-weight: 800;
color: #fff;
display: block;
text-shadow: 0 4rpx 12rpx rgba(0,0,0,0.3);
}
/* ========== Results: Detail Card ========== */
.detail-card-wrapper {
padding: 0 32rpx;
margin-top: -40rpx;
position: relative;
z-index: 10;
}
.detail-card {
background: #fff;
border-radius: 32rpx;
padding: 36rpx;
box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.06);
}
.detail-card-header {
display: flex;
align-items: center;
gap: 20rpx;
margin-bottom: 24rpx;
}
/* Rank badge */
.result-rank {
width: 56rpx;
height: 56rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 28rpx;
font-weight: 800;
flex-shrink: 0;
}
.result-rank.best {
background: linear-gradient(135deg, #558B2F, #689F38);
color: #fff;
box-shadow: 0 6rpx 16rpx rgba(85, 139, 47, 0.3);
}
.result-rank.normal {
background: #F3F4F6;
color: #6B7280;
}
.result-name-area {
flex: 1;
}
.result-main-name {
font-size: 36rpx;
font-weight: 800;
color: #1F2937;
display: block;
margin-bottom: 4rpx;
}
.confidence-text {
font-size: 24rpx;
color: #558B2F;
font-weight: 600;
}
/* Confidence Bar */
.confidence-bar-wrap {
margin-bottom: 24rpx;
}
.confidence-bar-bg {
height: 12rpx;
background: #F3F4F6;
border-radius: 6rpx;
overflow: hidden;
}
.confidence-bar-fill {
height: 100%;
background: linear-gradient(90deg, #689F38, #558B2F);
border-radius: 6rpx;
transition: width 0.8s ease-out;
}
.result-description {
font-size: 26rpx;
color: #6B7280;
line-height: 1.8;
display: block;
margin-bottom: 28rpx;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 5;
-webkit-box-orient: vertical;
}
.detail-card-actions {
display: flex;
gap: 16rpx;
}
/* ========== Results: Other Results ========== */
.other-section {
padding: 32rpx 32rpx 0;
}
.section-title {
font-size: 30rpx;
font-weight: 700;
color: #374151;
display: block;
margin-bottom: 20rpx;
padding-left: 8rpx;
}
.other-list {
background: #fff;
border-radius: 28rpx;
overflow: hidden;
box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.03);
}
.other-item {
display: flex;
align-items: center;
padding: 28rpx 32rpx;
gap: 20rpx;
border-bottom: 1rpx solid #F3F4F6;
transition: background 0.15s;
}
.other-item:last-child {
border-bottom: none;
}
.other-item:active {
background: #FAFFF5;
}
.other-item-info {
flex: 1;
min-width: 0;
}
.other-item-name {
font-size: 30rpx;
font-weight: 600;
color: #374151;
display: block;
margin-bottom: 10rpx;
}
/* Mini bars */
.mini-bar-wrap {
display: flex;
align-items: center;
gap: 12rpx;
}
.mini-bar-bg {
flex: 1;
height: 8rpx;
background: #F3F4F6;
border-radius: 4rpx;
overflow: hidden;
}
.mini-bar-fill {
height: 100%;
background: linear-gradient(90deg, #A5D6A7, #66BB6A);
border-radius: 4rpx;
}
.mini-bar-text {
font-size: 22rpx;
color: #9CA3AF;
font-weight: 600;
flex-shrink: 0;
width: 80rpx;
text-align: right;
}
/* ========== Bottom Section ========== */
.bottom-section {
padding: 32rpx 32rpx 0;
}
+31 -1
View File
@@ -168,5 +168,35 @@ Page({
});
},
closeIdentifyModal() { this.setData({ showIdentifyModal: false }); }
closeIdentifyModal() { this.setData({ showIdentifyModal: false }); },
// Handle plant identification: camera or album
handleIdentify(e) {
const source = e.currentTarget.dataset.source; // 'camera' or 'album'
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: [source],
camera: 'back',
success: (res) => {
const tempFilePath = res.tempFiles[0].tempFilePath;
// Close popup
this.setData({ showIdentifyModal: false });
// Store image path in global data for the results page
const app = getApp();
app.globalData._identifyImagePath = tempFilePath;
// Navigate to identify results page
wx.navigateTo({
url: '/pages/wiki/identify/index'
});
},
fail: () => {
// User cancelled, do nothing
}
});
}
})
+10 -9
View File
@@ -119,26 +119,27 @@
<t-popup visible="{{showIdentifyModal}}" bind:visible-change="onPopupVisibleChange" placement="bottom">
<view class="popup-content">
<view class="popup-header">
<text class="popup-title">识别植物</text>
<text class="popup-title">🌿 植物识别</text>
</view>
<text class="popup-subtitle">拍照或上传图片,AI 帮你识别植物</text>
<view class="upload-options-grid">
<view class="upload-opt-item">
<view class="opt-icon-circle" style="background: #E8F5E9;">
<t-icon name="camera" size="64rpx" color="#2E7D32" />
<view class="upload-opt-item" bindtap="handleIdentify" data-source="camera">
<view class="opt-icon-circle" style="background: linear-gradient(135deg, #E8F5E9, #C8E6C9);">
<t-icon name="camera" size="56rpx" color="#2E7D32" />
</view>
<text>拍照识别</text>
</view>
<view class="upload-opt-item">
<view class="opt-icon-circle" style="background: #E3F2FD;">
<t-icon name="image" size="64rpx" color="#1565C0" />
<view class="upload-opt-item" bindtap="handleIdentify" data-source="album">
<view class="opt-icon-circle" style="background: linear-gradient(135deg, #E3F2FD, #BBDEFB);">
<t-icon name="image" size="56rpx" color="#1565C0" />
</view>
<text>相册上传</text>
<text>相册选取</text>
</view>
</view>
<view class="popup-footer">
<t-button block variant="outline" bind:tap="closeIdentifyModal">取消</t-button>
<view class="cancel-btn" bindtap="closeIdentifyModal">取消</view>
</view>
</view>
</t-popup>
+68 -13
View File
@@ -104,37 +104,92 @@
/* Popup Styles */
.popup-content {
background: white;
border-radius: 40rpx 40rpx 0 0;
padding: 40rpx;
padding-bottom: env(safe-area-inset-bottom);
border-radius: 48rpx 48rpx 0 0;
padding: 48rpx 40rpx;
padding-bottom: calc(48rpx + env(safe-area-inset-bottom));
}
.popup-header {
text-align: center;
margin-bottom: 48rpx;
margin-bottom: 16rpx;
}
.popup-title {
font-size: 36rpx;
font-size: 40rpx;
font-weight: 800;
color: var(--text-main);
background: linear-gradient(120deg, #33691E, #689F38);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
display: inline-block;
}
.popup-subtitle {
font-size: 26rpx;
color: #9CA3AF;
text-align: center;
display: block;
margin-bottom: 48rpx;
}
.upload-options-grid {
display: flex; gap: 40rpx; justify-content: center;
margin-bottom: 48rpx;
display: flex;
gap: 32rpx;
justify-content: center;
margin-bottom: 48rpx;
}
.upload-opt-item {
display: flex; flex-direction: column; align-items: center; gap: 16rpx;
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
gap: 20rpx;
background: #F9FAFB;
border-radius: 32rpx;
padding: 40rpx 24rpx;
transition: all 0.2s;
}
.upload-opt-item:active {
transform: scale(0.96);
background: #F0F7EB;
}
.opt-icon-circle {
width: 120rpx; height: 120rpx; border-radius: 40rpx;
display: flex; align-items: center; justify-content: center;
width: 112rpx;
height: 112rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.06);
}
.upload-opt-item text {
font-size: 28rpx;
font-weight: 600;
color: #374151;
}
.popup-footer {
display: flex;
justify-content: center;
display: flex;
justify-content: center;
padding-top: 8rpx;
}
.popup-footer .cancel-btn {
width: 100%;
height: 88rpx;
line-height: 88rpx;
text-align: center;
font-size: 32rpx;
font-weight: 600;
color: #6B7280;
background: #F3F4F6;
border-radius: 44rpx;
border: none;
}
.popup-footer .cancel-btn:active {
background: #E5E7EB;
}