修改湖畔社区页面的数据展示逻辑

This commit is contained in:
赵毅 2025-09-17 15:46:56 +08:00
parent 9bffd147ba
commit 4d6e41ed1c

View File

@ -4,7 +4,9 @@
<view class="community-item-box" v-for="(item, index) in communityList" :key="index">
<view class="community-item">
<view class="community-image" @tap="enterCommunity(item)">
<image :src="item.pic" mode="aspectFill"></image>
<!-- 使用默认图片作为 fallback -->
<image :src="item.pic || defaultCommunityImage" mode="aspectFill"
@error="handleImageError(item)"></image>
</view>
<view class="community-info">
<view class="community-name" @tap="enterCommunity(item)">{{ item.name }}</view>
@ -15,12 +17,12 @@
<uni-icons type="paperplane-filled" size="18"></uni-icons>
<text class="btn-text">导航</text>
</view>
<view class="community-action-btn" @tap="callPhone(item)">
<view class="community-action-btn" @tap="callPhone(item)" v-if="item.property_server_phone">
<uni-icons type="phone-filled" size="18"></uni-icons>
<view class="btn-text">电话</view>
</view>
</view>
<view class="enter-btn" @tap="enterCommunity(item)">进入小区</view>
<view v-if="item.is_me" class="enter-btn" @tap="enterCommunity(item)">进入小区</view>
</view>
</view>
<view class="community-address">
@ -42,6 +44,7 @@ import {
uniqueByField,
menuButtonInfo,
NavgateTo,
calculateDistance
} from "../../../utils";
import { apiArr } from "../../../api/community";
@ -54,7 +57,9 @@ export default {
currentPage: 1,
pageSize: 16,
hasMoreData: true,
isLoading: false
isLoading: false,
tencentMapKey: '55NBZ-MUQYW-EAJRL-YIWPA-ZXCR6-4NBPP', // API Key
defaultCommunityImage: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com/community_no_image3.png' //
};
},
methods: {
@ -72,12 +77,23 @@ export default {
this.isLoading = true;
try {
//
const locationData = await this.getUserLocation();
if (!locationData) {
throw new Error('无法获取用户位置');
}
const userLat = parseFloat(locationData.lat);
const userLng = parseFloat(locationData.lng);
//
const res = await request(apiArr.getAllList, "POST", {
page_num: 1,
page_size: 9999
});
if (res && res.rows) {
// 1km
let processedList = res.rows.map(item => {
//
if (item.pic) {
@ -85,51 +101,80 @@ export default {
}
try {
let locationData = uni.getStorageSync('location');
if (locationData) {
// locationData
const location = typeof locationData === 'string' ? JSON.parse(locationData) : locationData;
const userLat = parseFloat(location.lat) || 0;
const userLng = parseFloat(location.lng) || 0;
const parkLat = parseFloat(item.lat) || 0;
const parkLng = parseFloat(item.lng) || 0;
//
if (userLat && userLng && item.lat && item.lng) {
const parkLat = parseFloat(item.lat);
const parkLng = parseFloat(item.lng);
//
if (userLat && userLng && parkLat && parkLng) {
// 使Haversine
const R = 6371; //
const dLat = (parkLat - userLat) * Math.PI / 180;
const dLng = (parkLng - userLng) * Math.PI / 180;
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(userLat * Math.PI / 180) * Math.cos(parkLat * Math.PI / 180) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = (R * c).toFixed(2);
item.distance = distance + 'km';
}
// 使Haversine
const distance = calculateDistance(userLat, userLng, parkLat, parkLng);
item.distance = distance.toFixed(2) + 'km';
item.distanceValue = distance; //
}
} catch (error) {
console.error('计算距离失败:', error);
item.distanceValue = Infinity;
}
return item;
});
// 1km
processedList = processedList.filter(item =>
item.distanceValue && item.distanceValue <= 1
);
// API1km
const nearbyCommunities = await this.getNearbyCommunities(userLat, userLng);
// API
let mergedList = [...processedList];
if (nearbyCommunities && nearbyCommunities.length > 0) {
//
const tencentCommunities = nearbyCommunities.map(item => {
//
const distance = calculateDistance(userLat, userLng, item.lat, item.lng);
// POI
let communityImage = '';
if (item.photos && item.photos.length > 0) {
//
communityImage = item.photos[0].url;
}
return {
name: item.title,
addr: item.address,
lat: item.lat,
lng: item.lng,
distance: distance.toFixed(2) + 'km',
distanceValue: distance,
//
pic: communityImage || this.defaultCommunityImage, // 使
property: item.extension && item.extension.property_company ? item.extension.property_company : '-',
property_server_phone: item.tel || '',
community_id: `tencent_${item.id}` // ID
};
});
//
mergedList = [...processedList, ...tencentCommunities];
// community_id
mergedList = uniqueByField(mergedList, 'community_id');
}
//
processedList.sort((a, b) => {
const distanceA = parseFloat(String(a.distance).trim());
const distanceB = parseFloat(String(b.distance).trim());
mergedList.sort((a, b) => {
const distanceA = a.distanceValue || Infinity;
const distanceB = b.distanceValue || Infinity;
// NaN Infinity
const valueA = isNaN(distanceA) ? Infinity : distanceA;
const valueB = isNaN(distanceB) ? Infinity : distanceB;
return valueA - valueB;
return distanceA - distanceB;
});
// allCommunityList
this.allCommunityList = processedList;
this.allCommunityList = mergedList;
this.currentPage = 1;
this.updateDisplayList();
@ -148,6 +193,100 @@ export default {
}
},
//
getUserLocation() {
return new Promise((resolve, reject) => {
try {
let locationData = uni.getStorageSync('location');
if (locationData) {
// locationData
const location = typeof locationData === 'string' ? JSON.parse(locationData) : locationData;
if (location.lat && location.lng) {
resolve(location);
return;
}
}
} catch (error) {
console.error('从缓存获取位置失败:', error);
}
//
uni.getLocation({
type: 'gcj02',
altitude: true,
success: (res) => {
const location = {
lat: res.latitude,
lng: res.longitude
};
//
uni.setStorageSync('location', location);
resolve(location);
},
fail: (err) => {
console.error('获取位置失败:', err);
reject(err);
}
});
});
},
// API1km
async getNearbyCommunities(lat, lng) {
try {
const keyword = '小区';
const radius = 1000; // 1km
const pageSize = 20;
// 使uni.requestAPIextensions=all
return new Promise((resolve, reject) => {
uni.request({
url: 'https://apis.map.qq.com/ws/place/v1/search',
method: 'GET',
data: {
keyword: keyword,
boundary: `nearby(${lat},${lng},${radius})`,
key: this.tencentMapKey,
page_size: pageSize,
extensions: 'all' //
},
success: (res) => {
if (res.statusCode === 200 && res.data.status === 0) {
//
const results = res.data.data.map(item => ({
id: item.id,
title: item.title,
address: item.address,
lat: item.location.lat,
lng: item.location.lng,
tel: item.tel || '',
photos: item.photos || [], //
extension: item.extension || {}, //
is_me: false
}));
resolve(results);
} else {
console.error('腾讯地图API请求失败:', res.data);
resolve([]);
}
},
fail: (err) => {
console.error('腾讯地图API请求错误:', err);
resolve([]);
}
});
});
} catch (error) {
console.error('获取附近小区失败:', error);
return [];
}
},
// 使
handleImageError(item) {
item.pic = this.defaultCommunityImage;
},
//
updateDisplayList() {
const startIndex = 0;
@ -198,7 +337,6 @@ export default {
success: () => {
},
fail: (error) => {
console.error('拨打电话失败:', error);
}
});
} else {
@ -223,13 +361,6 @@ export default {
if (this.hasMoreData && !this.isLoading) {
this.currentPage += 1;
this.updateDisplayList();
// if (!this.hasMoreData) {
// uni.showToast({
// title: '',
// icon: 'none',
// duration: 1500
// });
// }
}
}
},
@ -253,4 +384,4 @@ export default {
<style>
@import url("./index.css");
</style>
</style>