| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803 |
- <template>
- <view class="device-photo-page">
- <!-- 顶部导航栏 -->
- <view class="device-detail__header">
- <u-icon
- size="36"
- class="arrow-left"
- name="arrow-left"
- @click="handleBack"
- ></u-icon>
- {{ title }}
- </view>
- <!-- 日期选择器 -->
- <view class="date-container">
- <view class="select-year">
- <view class="select-year-item" @click="showPicker = true">
- {{ currentYear }}
- <u-icon name="arrow-down" size="18" class="arrow-down"></u-icon>
- </view>
- </view>
- <view class="date-picker" @click="show = true">
- <view class="date-input">
- <text class="date-label">{{ time_begin || '-' }}</text>
- </view>
- <view class="date-separator">-</view>
- <view class="date-input">
- <text class="date-label">{{ time_end || '-' }}</text>
- </view>
- <u-icon name="calendar" class="calendar"></u-icon>
- </view>
- </view>
- <!-- 主图片区域 -->
- <view class="main-photo" v-if="currentImg.addr">
- <movable-area class="movable-area" :style="{ height: containerHeight }">
- <movable-view
- class="movable-view"
- direction="all"
- scale="true"
- scale-min="1"
- scale-max="5"
- >
- <view class="image-container">
- <!-- 原图 -->
- <image
- :src="currentImg.addr"
- class="main-photo-image"
- mode="widthFix"
- @load="onImageLoad"
- />
- <!-- 虫子标记层 -->
- <view class="bug-markers" v-if="bugMarkers.length > 0">
- <view
- v-for="(marker, index) in bugMarkers"
- :key="index"
- class="bug-marker"
- :style="{
- left: marker.x + '%',
- top: marker.y + '%',
- width: marker.width + '%',
- height: marker.height + '%',
- borderColor: marker.color
- }"
- >
- <view class="bug-label" :style="{ color: marker.color }">
- {{ getPestName(marker.id) }}
- </view>
- </view>
- </view>
- </view>
- </movable-view>
- </movable-area>
- <view class="photo-timestamp">{{ formatDate(currentImg.addtime) }}</view>
- </view>
- <!-- 缩略图预览 -->
- <view class="thumbnail-preview">
- <view class="thumbnail-scroll">
- <view
- class="thumbnail-item"
- v-for="(item, index) in thumbnails"
- :key="index"
- :class="{ active: img_id == item.ids }"
- @click="selectThumbnail(item)"
- >
- <image
- :src="item.addr"
- class="thumbnail-image"
- />
- </view>
- </view>
- </view>
- <!-- 识别结果 -->
- <view class="recognition-result">
- <view class="result-title">当前图片识别结果</view>
-
- <!-- 一类害虫 -->
- <view class="pest-category" v-for="(pest,index) in pestList" :key="index">
- <view class="category-header">
- <text class="category-title">{{ pest[0] }}</text>
- <text class="category-count">数量</text>
- </view>
- <view class="pest-item" v-for="(p, i) in pest[1]" :key="i">
- <view class="pest-info">
- <view class="pest-color" :style="{ backgroundColor: getPestColor(p.pest_name) }"></view>
- <text class="pest-name">{{ p.pest_name }}</text>
- </view>
- <text class="pest-count">{{ p.pest_num }}</text>
- </view>
- </view>
- </view>
- <u-calendar v-model="show" :mode="mode" @change="handleChange" :max-date="maxDate" :min-date="minDate" :defaultDate="defaultDate"></u-calendar>
- <u-picker v-model="showPicker" mode="selector" :range="selectorRange" range-key="id" :default-selector="[0]" @confirm="confirmHandler"></u-picker>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- show: false,
- mode: 'range',
- maxDate: this.formatTime(new Date()),
- minDate: this.formatTime(new Date(new Date().getFullYear(), 0, 1)),
- defaultDate: this.formatTime(new Date(new Date().getFullYear(), 0, 1)),
- showPicker: false,
- selectorRange: [],
- title: '查看图片',
- currentThumbnail: 2,
- pestYype:{},
- device_id: '',
- time_begin: this.formatTime(new Date(new Date().getFullYear(), 0, 1)),
- time_end: this.formatTime(new Date()),
- img_id: '',
- pest_list:[],
- pestList:[],
- colors:[
- '#FF5951',
- '#66EDED',
- '#E67B3E',
- '#6DE28B',
- '#FFC97A',
- '#E7EB4B',
- '#1561F3',
- '#FA73F5',
- '#159AFF',
- '#FA73F5'
- ],
- currentYear:'',
- pest_list_arr:[],
- thumbnails: [],
- currentImg:{},
- bugMarkers: [],
- imageWidth: 0,
- imageHeight: 0,
- imgOld_id:'',
- containerHeight: '300px',
- pestDict: {}, // 虫子字典 {id: name}
- pestColorMap: {}, // 害虫颜色映射 {pestName: color}
- };
- },
- async onLoad(options) {
- const {id,img_id,currentYear} = options
- this.device_id = id
- this.imgOld_id = img_id
- this.currentYear = currentYear
- this.time_begin = this.currentYear + '-01-01'
- this.time_end = this.currentYear + '-12-31'
- this.maxDate = this.formatTime(this.time_end)
- this.minDate = this.formatTime(this.time_begin)
- this.selectorRange = [];
- const nowYear = new Date().getFullYear();
- for(let i = 0;i<50;i++){
- const item = {
- label: nowYear - i,
- id: nowYear - i
- }
- this.selectorRange.push(item);
- }
- this.pestList = [];
- await this.getPestDict();
- this.getPestLevelMap();
- this.getPestList();
- // this.getDevicePhotoDetails();
- },
- methods: {
- getPestName(id){
- return this.pestDict[id] || id;
- },
- async confirmHandler(e){
- this.currentYear = this.selectorRange[e].id;
- if(this.currentYear == new Date().getFullYear()){
- this.time_begin = this.formatTime(new Date(new Date().getFullYear(), 0, 1));
- this.time_end = this.formatTime(new Date());
- this.maxDate = this.formatTime(new Date());
- this.minDate = this.formatTime(new Date(new Date().getFullYear(), 0, 1));
- }else{
- this.time_begin = this.formatTime(new Date(this.currentYear, 0, 1));
- this.time_end = this.formatTime(new Date(this.currentYear, 11, 31));
- this.maxDate = this.formatTime(new Date(this.currentYear, 11, 31));
- this.minDate = this.formatTime(new Date(this.currentYear, 0, 1));
- this.defaultDate = this.minDate;
- }
- this.showPicker = false;
- this.pestList = [];
- this.thumbnails = [];
- await this.getPestList();
- },
- async handleChange(e){
- console.log(e,'e')
- this.time_begin = e.startDate;
- this.time_begin = e.endDate;
- this.pestList = [];
- await this.getPestList()
- this.currentImg = {};
- },
- getPestColor(pestName) {
- // 如果该害虫已有颜色,直接返回
- if (this.pestColorMap[pestName]) {
- return this.pestColorMap[pestName];
- }
- // 为新害虫分配颜色
- const usedColors = Object.values(this.pestColorMap);
- // 找一个未使用的颜色
- let color;
- for (const c of this.colors) {
- if (!usedColors.includes(c)) {
- color = c;
- break;
- }
- }
- // 如果所有颜色都用完了,使用哈希值确定颜色
- if (!color) {
- const hash = pestName.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
- color = this.colors[hash % this.colors.length];
- }
- this.pestColorMap[pestName] = color;
- return color;
- },
- getLevelDisplayName(level) {
- const levelMap = {
- '-1': '其他害虫',
- 1: '一类害虫',
- 2: '二类害虫',
- 3: '三类害虫',
- 4: '四类害虫',
- 5: '五类害虫'
- };
- return levelMap[String(level)] || `${level}`;
- },
- async getPestList() {
- const res = await this.$myRequest({
- url: '/api/api_gateway?method=forecast.new_cbd.photo_list',
- method: 'POST',
- data: {
- identify_model:'B',
- cmd:'cbd',
- time_begin: this.time_begin + ' 00:00:00',
- time_end: this.time_end + ' 23:59:59',
- identify_model:'B',
- device_id: this.device_id,
- },
- });
- this.thumbnails = res?.data || [];
- //如果和旧的id相同,就用旧的id不然就用第一个
- if(this.thumbnails.find(item => item.ids == this.imgOld_id)){
- this.img_id = this.imgOld_id
- }else{
- this.img_id = this.thumbnails[0].ids
- }
- this.getDevicePhotoDetails();
- // if(this.img_id){
- // this.currentImg = this.thumbnails.find(item => item.ids == this.img_id) || this.thumbnails[0] || {}
- // }else{
- // this.currentImg = this.thumbnails[0] || {}
- // }
- },
- formatTime(dateString) {
- const date = new Date(dateString);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- return `${year}-${month}-${day}`;
- },
- async getPestLevelMap() {
- const res = await this.$myRequest({
- url: '/api/api_gateway?method=forecast.new_cbd.pest_level_map',
- method: 'POST',
- });
- const pestYype = {}
- for(let key in res){
- pestYype[this.getLevelDisplayName(key)] = res[key]
- }
- this.pestYype = pestYype
- },
- // 获取虫子字典
- async getPestDict() {
- try {
- const res = await this.$myRequest({
- url: '/api/api_gateway?method=forecast.pest_info.pest_dict',
- method: 'POST',
- data:{
- type_name: '1'
- }
- });
- this.pestDict = res || {};
- } catch (error) {
- console.error('获取虫子字典失败:', error);
- this.pestDict = {};
- }
- },
- formatDate(dateString) {
- const date = new Date(dateString*1000);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- const hour = String(date.getHours()).padStart(2, '0');
- const minute = String(date.getMinutes()).padStart(2, '0');
- const second = String(date.getSeconds()).padStart(2, '0');
- return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
- },
- async getDevicePhotoDetails() {
- const res = await this.$myRequest({
- url: '/api/api_gateway?method=forecast.forecast_system.device_photo_details',
- method: 'POST',
- data: {
- identify_model:'B',
- cmd:'cbd',
- img_id: this.img_id,
- },
- });
- console.log('获取图片详情成功:', res);
- this.currentImg = res;
- // 处理label参数生成bugMarkers
- this.processBugMarkers();
- const pest_list = res.pest_list
- const pestArr = new Map()
- for(let key in this.pestYype){
- for(let j in this.pestYype[key]){
- const pestName = this.pestYype[key][j]
- pest_list.forEach(item => {
- if(item.pest_name == pestName){
- if(pestArr.has(item.level)){
- pestArr.get(key).push(item)
- }else{
- pestArr.set(key, [item])
- }
- }
- })
- }
- }
- const pestList = []
- for(let key of pestArr){
- pestList.push(key)
- }
- this.pestList = pestList
- },
- onImageLoad(e) {
- this.imageWidth = e.detail.width;
- this.imageHeight = e.detail.height;
- // 计算容器显示高度(根据屏幕宽度和图片宽高比)
- const systemInfo = uni.getSystemInfoSync();
- const screenWidth = systemInfo.windowWidth;
- const containerWidth = screenWidth - 24; // 减去左右margin (24rpx ≈ 12px)
- const displayHeight = (containerWidth / this.imageWidth) * this.imageHeight;
- this.containerHeight = displayHeight + 'px';
- console.log('图片尺寸:', this.imageWidth, this.imageHeight, '容器高度:', this.containerHeight);
- // 图片加载完成后处理bugMarkers
- this.processBugMarkers();
- },
-
- processBugMarkers() {
- console.log('开始处理label参数:', this.currentImg.label);
- if (!this.currentImg.label) {
- console.log('没有label参数');
- this.bugMarkers = [];
- return;
- }
- // 如果没有图片尺寸,先设置默认值
- if (!this.imageWidth || !this.imageHeight) {
- console.log('没有图片尺寸,使用默认值');
- this.imageWidth = 1000;
- this.imageHeight = 1000;
- }
- try {
- // 处理label参数,将单引号替换为双引号
- let labelStr = this.currentImg.label;
- labelStr = labelStr.replace(/'/g, '"');
- const label = JSON.parse(labelStr);
- const markers = [];
- // 根据原图宽度计算缩放比例
- let scaleRatio;
- if (this.imageWidth >= 5000) {
- scaleRatio = 1;
- } else if (this.imageWidth >= 4000) {
- scaleRatio = 1;
- } else {
- scaleRatio = 1;
- }
- console.log('原图宽度:', this.imageWidth, '缩放比例:', scaleRatio);
- label.forEach(item => {
- for (let key in item) {
- const [x1, y1, x2, y2] = item[key];
- // 使用缩放比例计算标记位置和尺寸
- const x = (x1 * scaleRatio / this.imageWidth) * 100;
- const y = (y1 * scaleRatio / this.imageHeight) * 100;
- const width = ((x2 - x1) * scaleRatio / this.imageWidth) * 100;
- const height = ((y2 - y1) * scaleRatio / this.imageHeight) * 100;
- // 从字典中获取虫子名字(尝试字符串和数字两种类型)
- const pestName = this.pestDict[key] || this.pestDict[String(key)] || key;
- const color = this.getPestColor(pestName);
- markers.push({
- id: pestName, // 显示虫子名字
- x,
- y,
- width,
- height,
- color
- });
- }
- });
- this.bugMarkers = markers;
- } catch (error) {
- console.error('处理label参数失败:', error);
- this.bugMarkers = [];
- }
- },
- handleBack() {
- uni.navigateBack({
- delta: 1
- });
- },
- goBack() {
- uni.navigateBack();
- },
- selectThumbnail(item) {
- this.img_id = item.ids;
- this.getDevicePhotoDetails();
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- ::v-deep .u-calendar__action{
- display:flex;
- justify-content: space-between;
- }
- ::v-deep .u-hover-class{
- .u-calendar__content__item__inner{
- color:#aaa !important;
- }
- }
- .device-photo-page {
- background-color: #F5F5F5;
- min-height: 100vh;
- width: 100%;
- padding-top: 112rpx;
- background: linear-gradient(180deg, #ffffff00 0%, #F5F6FA 23.64%, #F5F6FA 100%), linear-gradient(102deg, #BFEADD 6.77%, #B8F1E7 40.15%, #B9EEF5 84.02%);
- .device-detail__header {
- width: 100%;
- font-size: 28rpx;
- color: #999;
- color: #042118;
- font-family: 'Source Han Sans CN VF';
- font-weight: 700;
- position: relative;
- text-align: center;
- .arrow-left {
- position: absolute;
- left: 32rpx;
- margin-right: 12rpx;
- }
- }
- /* 顶部导航栏 */
- .nav-bar {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 24rpx;
- background-color: #E6F7EF;
- .nav-left {
- .back-icon {
- font-size: 36rpx;
- color: #042118;
- }
- }
- .nav-title {
- font-size: 32rpx;
- font-weight: 700;
- color: #042118;
- }
- .nav-right {
- display: flex;
- align-items: center;
- .more-icon {
- font-size: 32rpx;
- color: #042118;
- margin-right: 24rpx;
- }
- .eye-icon {
- font-size: 32rpx;
- }
- }
- }
- .date-container{
- padding: 0 20rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- .select-year{
- width: 150rpx;
- height: 80rpx;
- display: flex;
- align-items: center;
- justify-content: space-around;
- color: #656565;
- font-size: 26rpx;
- line-height: 80rpx;
- text-align: center;
- border-radius: 48rpx;
- background-color: #FFFFFF;
- margin-right: 10rpx;
- padding: 0 26rpx;
- .select-year-item{
- width: 100%;
- display: flex;
- align-items: center;
- justify-content: space-around;
- }
- }
- }
- /* 日期选择器 */
- .date-picker {
- display: flex;
- align-items: center;
- padding: 20rpx 24rpx;
- background-color: #FFFFFF;
- margin-bottom: 24rpx;
- width: 90%;
- margin:20rpx auto;
- border-radius: 48rpx;
- position: relative;
- .date-input {
- display: flex;
- flex-direction: column;
- width: 35%;
- .date-label {
- font-size: 24rpx;
- color: #999999;
- margin-bottom: 8rpx;
- }
- .date-value {
- font-size: 28rpx;
- color: #042118;
- font-weight: 500;
- }
- }
- .calendar {
- position: absolute;
- right: 24rpx;
- top: 50%;
- transform: translateY(-50%);
- font-size: 32rpx;
- color: #999999;
- }
- .date-separator {
- margin: 0 24rpx;
- font-size: 28rpx;
- color: #999999;
- }
- }
- /* 主图片区域 */
- .main-photo {
- position: relative;
- margin: 0 24rpx 24rpx;
- background-color: #FFFFFF;
- border-radius: 16rpx;
- padding: 0;
- overflow: hidden;
- .movable-area {
- width: 100%;
- overflow: hidden;
- }
- .movable-view {
- width: 100%;
- }
- .image-container {
- position: relative;
- width: 100%;
- }
- .main-photo-image {
- width: 100%;
- display: block;
- }
- /* 虫子标记层 */
- .bug-markers {
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- pointer-events: none;
- }
- /* 虫子标记 */
- .bug-marker {
- position: absolute;
- border: 2rpx solid;
- border-radius: 4rpx;
- }
- /* 虫子标记标签 */
- .bug-label {
- position: absolute;
- top: -24rpx;
- left: 0;
- padding: 2rpx 6rpx;
- border-radius: 4rpx;
- font-size: 20rpx;
- color: white;
- white-space: nowrap;
- }
- /* 虫子标记层 */
- .bug-markers {
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- pointer-events: none;
- }
- /* 虫子标记 */
- .bug-marker {
- position: absolute;
- border: 2rpx solid;
- border-radius: 4rpx;
- pointer-events: auto;
- }
- /* 虫子标记标签 */
- .bug-label {
- position: absolute;
- top: -30rpx;
- left: 0;
- padding: 4rpx 8rpx;
- border-radius: 4rpx;
- font-size: 20rpx;
- font-weight: bold;
- }
- .photo-timestamp {
- position: absolute;
- bottom: 0rpx;
- left: 0rpx;
- width:100%;
- background-color: rgba(0, 0, 0, 0.6);
- color: #FFFFFF;
- padding: 8rpx 16rpx;
- border-radius: 8rpx;
- font-size: 24rpx;
- }
- }
- /* 缩略图预览 */
- .thumbnail-preview {
- margin: 0 24rpx 24rpx;
- .thumbnail-scroll {
- gap: 16rpx;
- padding-bottom: 16rpx;
- box-sizing: border-box;
- white-space: nowrap;
- overflow-x: auto;
- overflow-y: hidden;
- width: 100%;
- // 隐藏滚动条
- -ms-overflow-style: none;
- scrollbar-width: none;
- .thumbnail-item {
- width: 120rpx;
- height: 120rpx;
- border-radius: 8rpx;
- overflow: hidden;
- border: 2rpx solid transparent;
- display: inline-block;
- box-sizing: border-box;
- margin-right: 10rpx;
- &.active {
- border-color: #0BBC58;
- }
- .thumbnail-image {
- width: 100%;
- height: 100%;
- object-fit: cover;
- }
- }
- }
- }
- /* 识别结果 */
- .recognition-result {
- margin: 0 24rpx 24rpx;
- background-color: #FFFFFF;
- border-radius: 16rpx;
- padding: 24rpx;
- .result-title {
- font-size: 28rpx;
- font-weight: 700;
- color: #042118;
- margin-bottom: 24rpx;
- }
- /* 害虫类别 */
- .pest-category {
- margin-bottom: 32rpx;
- border: 1px solid #E4E7ED;
- border-radius: 16rpx;
- &:last-child {
- margin-bottom: 0;
- }
- .category-header {
- display: flex;
- justify-content: space-between;
- margin-bottom: 16rpx;
- background: #F6F8FC;
- padding: 18rpx 24rpx;
- .category-title {
- font-size: 24rpx;
- font-weight: 500;
- color: #042118;
- }
- .category-count {
- font-size: 24rpx;
- color: #999999;
- }
- }
- .pest-item {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 12rpx;
- padding: 6rpx 24rpx;
- &:last-child {
- margin-bottom: 0;
- }
- .pest-info {
- display: flex;
- align-items: center;
- .pest-color {
- width: 16rpx;
- height: 16rpx;
- border-radius: 50%;
- margin-right: 12rpx;
- }
- .pest-name {
- font-size: 24rpx;
- color: #042118;
- }
- }
- .pest-count {
- font-size: 24rpx;
- color: #042118;
- }
- }
- }
- }
- }
- </style>
|