FenXiNspUniapp/pages/maps/index.vue

687 lines
20 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="container">
<!-- <image src="../../static/zddt.jpg" mode="" class="bgimg" @click="todetail()"></image> -->
<map id="map" style="width: 100%; height:2000rpx;" scale="13" :markers="markers" longitude="110.628279"
latitude="35.546105" show-location @markertap="markertap" @callouttap="callouttap">
<cover-view slot="callout">
<cover-view v-for="(item,index) in markers" :key="index" class="customCallout" :marker-id="item.id">
<cover-image class="imgs" src="@/static/zhandian_icon_xuqin_title.png"
v-if="item.stationType == 'livestock'"></cover-image>
<cover-image class="imgs" src="@/static/zhandian_icon_nongtian_title.png"
v-if="item.stationType == 'orient'"></cover-image>
<cover-image class="imgs" src="@/static/zhandian_icon_qiye_title.png"
v-if="item.stationType == 'cusLocaltion'"></cover-image>
<cover-view class="content"
v-if="item.stationType == 'livestock' || item.stationType == 'orient'|| item.stationType == 'cusLocaltion'">
{{item.stationName}}
</cover-view>
</cover-view>
</cover-view>
</map>
<view class="zdxq">
<view class="zdxqitem" @click="switch1Change">
<image src="../../static/zhandian_sahchong_dingwei.png" mode=""></image>
<view class="textss">
联网式太阳能杀虫灯
</view>
<view class="crildon" :style="!isscd?'background:#00ff37':'background: #ccc;'"></view>
</view>
<view class="zdxqitem" @click="switch2Change">
<image src="../../static/zhandian_xuqin_dingwei.png" mode=""></image>
<view class="textss">
畜禽养殖污染监测站
</view>
<view class="crildon" :style="!issscd?'background:#00ff37':'background: #ccc;'"></view>
</view>
<view class="zdxqitem" @click="switch3Change">
<image src="../../static/zhandian_nongtian_dingwei.png" mode=""></image>
<view class="textss">
农田面源污染监测站
</view>
<view class="crildon" :style="!isssscd?'background:#00ff37':'background: #ccc;'"></view>
</view>
</view>
<!-- <view class="switchs" :defaultSwitch="isscd">-->
<!-- <selectSwitch @change="switch1Change" :defaultSwitch="isscd" checked_bj_color="#13ce66"/>-->
<!-- <switch style="margin-right: 10rpx;" :checked="isscd" @change="switch1Change" />-->
<!-- &lt;!&ndash; 显示/隐藏杀虫灯 &ndash;&gt;-->
<!-- </view>-->
</view>
</template>
<script>
import api from '@/api/api'
import selectSwitch from "@/components/xuan-switch/xuan-switch.vue";
export default {
components: {
selectSwitch
},
data() {
return {
markers: [],
markersNoPest: [],
list: [],
pointers: [],
imgurl1: '@/static/zhandian_icon_nongtian_title.png',
imgurl2: '@/static/zhandian_icon_xuqin_title.png',
isscd: true,
issscd: true,
isssscd: true,
pestlight: [],
cusLocation: [],
stationInfo: [],
pestlightMarker: null,
stationlist: [],
timerSet: null,
isDataLoaded: false,
isCheckingLogin: false, // 防止重复检查
};
},
onLoad() {
console.log('地图页面加载');
// 不在这里加载数据
},
onShow() {
console.log('地图页面显示');
// 如果正在检查登录,不重复执行
if (this.isCheckingLogin) {
return;
}
this.checkLoginStatus();
},
onReady() {
this.mapContext = uni.createMapContext("alarm_map", this);
},
methods: {
// 统一的登录状态检查方法
checkLoginStatus() {
this.isCheckingLogin = true;
const token = uni.getStorageSync('constant');
const session = uni.getStorageSync('third_session');
console.log('登录检查 - token:', !!token, 'session:', !!session);
// 情况1没有任何登录凭证 -> 跳转欢迎页
if (!token && !session) {
console.log('无登录凭证,跳转欢迎页');
this.isCheckingLogin = false;
uni.reLaunch({
url: '/pages/loginindex'
});
return;
}
// 情况2有 third_session 但没有用户信息 -> 需要验证
if (session && !token) {
console.log('有session无token验证用户');
this.verifyUserInfo();
return;
}
// 情况3有完整登录信息 -> 加载数据
if (token) {
console.log('有完整登录信息,加载地图数据');
this.isCheckingLogin = false;
if (!this.isDataLoaded) {
this.getList();
}
}
},
// 验证用户信息
verifyUserInfo() {
const that = this;
uni.login({
success: (res) => {
console.log('微信登录成功code:', res.code);
const params = {
jsCode: res.code
};
api.wxlogin(params).then(res => {
console.log('wxlogin接口返回:', res.data);
// 检查接口返回状态
if (res.data.code === 0 && res.data.data) {
const userInfo = res.data.data;
// 检查用户信息是否完整
if (userInfo.id && userInfo.id !== null && userInfo.id !== 'null') {
// 登录有效,保存信息
uni.setStorageSync('constant', userInfo);
console.log('用户验证通过保存token');
that.isCheckingLogin = false;
// 加载地图数据
if (!that.isDataLoaded) {
that.getList();
}
} else {
// 用户信息不完整,跳转登录
console.log('用户信息不完整,跳转登录页');
that.isCheckingLogin = false;
that.clearCacheAndGoLogin();
}
} else {
// 接口返回错误
console.log('接口返回错误:', res.data.msg || '未知错误');
that.isCheckingLogin = false;
that.clearCacheAndGoLogin();
}
}).catch((err) => {
console.error('wxlogin请求失败:', err);
that.isCheckingLogin = false;
that.clearCacheAndGoLogin();
});
},
fail: (err) => {
console.error('微信登录失败:', err);
that.isCheckingLogin = false;
that.clearCacheAndGoLogin();
}
});
},
// 验证 token 有效性
verifyToken() {
this.isCheckingLogin = false;
if (!this.isDataLoaded) {
this.getList();
}
},
// 清理缓存并跳转登录
clearCacheAndGoLogin() {
const keys = ['constant', 'third_session', 'userName', 'password'];
keys.forEach(key => {
uni.removeStorageSync(key);
});
uni.reLaunch({
url: '/packDetail/pages/login/login'
});
},
// 获取地图数据(添加错误处理)
getList() {
// 防止重复加载
if (this.isDataLoaded) {
console.log('数据已加载,跳过');
return;
}
// 再次检查登录状态
const token = uni.getStorageSync('constant');
if (!token) {
console.log('没有登录token不加载数据');
return;
}
console.log('开始加载地图数据');
this.markers = [];
this.markersNoPest = [];
this.pointers = [];
this.list = [];
this.pestlight = [];
this.cusLocation = [];
this.stationInfo = [];
uni.showLoading({
title: '加载中'
});
this.$http.get('/applet/survStationInfo/stationMap').then(res => {
console.log('地图接口返回:', res.data);
// ⚠️ 关键:检查数据是否存在
if (!res.data || res.data.code !== 0) {
console.error('地图数据加载失败:', res.data?.msg || '数据格式错误');
uni.hideLoading();
uni.showToast({
title: res.data?.msg || '加载失败',
icon: 'none'
});
// 如果接口返回未登录,跳转登录
if (res.data?.code === 401 || res.data?.code === 1001) {
this.clearCacheAndGoLogin();
}
return;
}
// ⚠️ 检查 data 对象是否存在
if (!res.data.data) {
console.error('地图数据为空');
uni.hideLoading();
uni.showToast({
title: '暂无数据',
icon: 'none'
});
return;
}
// 安全地获取数据
const data = res.data.data;
const pestlightData = data.pestlight || [];
const cusLocationData = data.cusLocation || [];
const stationInfoData = data.stationInfo || [];
this.stationlist = [...pestlightData, ...cusLocationData, ...stationInfoData];
if (this.stationlist.length === 0) {
console.log('没有站点数据');
uni.hideLoading();
return;
}
this.stationlist.forEach((item, index) => {
item.id = index;
if (item.inType == 'pestlight') {
this.pestlight.push(item);
}
if (item.stationType == 'cusLocaltion') {
this.cusLocation.push(item);
}
if (item.stationType == 'orient' || item.stationType == 'livestock') {
this.stationInfo.push(item);
}
this.list.push({
latitude: Number(item.stationLatitude),
longitude: Number(item.stationLongitude),
stationType: item.stationType,
sortNo: item.sortNo,
stationName: item.stationName,
stationCode: item.stationCode
});
});
this.issscd = false;
this.isssscd = false;
this.qiyeChange();
this.list1query();
this.list2query();
this.isDataLoaded = true;
uni.hideLoading();
console.log('地图数据加载完成,共', this.stationlist.length, '个站点');
}).catch(err => {
console.error('加载地图数据失败:', err);
uni.hideLoading();
// 如果是网络错误,提示用户
uni.showToast({
title: '网络异常,请重试',
icon: 'none'
});
});
},
// 处理 stationlist 数据方法
processStationList(stationlist) {
if (stationlist && Array.isArray(stationlist)) {
stationlist.forEach(item => {
this.list.push({
latitude: item.inType === 'pestlight' ? Number(item.latitude) : Number(item.stationLatitude),
longitude: item.inType === 'pestlight' ? Number(item.longitude) : Number(item.stationLongitude),
stationType: item.inType === 'pestlight' ? 'pestlight' : item.stationType,
sortNo: item.sortNo,
stationName: item.stationName,
stationCode: item.stationCode
});
});
}
},
// 生成 pointers 数据方法
generatePointers() {
const iconConfig = {
orient: {
iconPath: '/static/zhandian_nongtian_dingwei.png',
width: 35,
height: 52
},
livestock: {
iconPath: '/static/zhandian_xuqin_dingwei.png',
width: 35,
height: 52
},
cusLocaltion: {
iconPath: '/static/qiyeIcon.png',
width: 35,
height: 52
}
};
this.list.forEach((item, i) => {
const config = iconConfig[item.stationType] || {};
item.iconPath = config.iconPath || '';
item.width = config.width || 0;
item.height = config.height || 0;
this.pointers.push({
id: i,
iconPath: item.iconPath,
latitude: Number(item.latitude),
longitude: Number(item.longitude),
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: item.stationType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: true
});
});
},
markertap(e) {
const index = e.detail.markerId;
if (this.list[index] && ['orient', 'livestock'].includes(this.list[index].stationType)) {
uni.navigateTo({
url: `/packDetail/pages/Site/detail?item=${encodeURIComponent(JSON.stringify(this.list[index]))}`
});
}
},
callouttap(e) {
const index = e.detail.markerId;
if (this.list[index] && ['orient', 'livestock'].includes(this.list[index].stationType)) {
uni.navigateTo({
url: `/packDetail/pages/Site/detail?item=${encodeURIComponent(JSON.stringify(this.list[index]))}`
});
}
},
todetails(item) {
console.log(item, '还是个然后');
},
switch1Change() {
this.isscd = !this.isscd;
let index = 0;
if (!this.isscd) {
this.pestlightMarker = [];
this.timerSet = setInterval(() => {
for (let i = 0; i < 50 && index < this.pestlight.length; i++) {
const item = this.pestlight[index];
item.iconPath = '/static/zhandian_sahchong_dingwei.png';
item.latitude = Number(item.latitude);
item.longitude = Number(item.longitude);
item.width = 15;
item.height = 20;
const markerItem = {
id: item.id,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
inType: item.inType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: false
};
this.markers.push(markerItem);
index++;
}
if (index >= this.pestlight.length) {
clearInterval(this.timerSet);
}
}, 1000);
} else {
clearInterval(this.timerSet);
this.markers = this.markers.filter((item) => {
return item.inType !== 'pestlight';
});
}
},
qiyeChange() {
for (let i = 0; i < this.cusLocation.length; i++) {
const item = this.cusLocation[i];
item.iconPath = '/static/qiyeIcon.png';
item.latitude = Number(item.stationLatitude);
item.longitude = Number(item.stationLongitude);
item.width = 35;
item.height = 52;
this.list.push({
latitude: Number(item.stationLatitude),
longitude: Number(item.stationLongitude),
stationType: item.stationType,
sortNo: item.sortNo,
stationName: item.stationName,
stationCode: item.stationCode
});
const markerItem = {
id: item.id,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: item.stationType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: true
};
this.markers.push(markerItem);
}
},
switch2Change() {
this.issscd = !this.issscd;
this.list1query();
},
switch3Change() {
this.isssscd = !this.isssscd;
this.list2query();
},
list1query() {
if (!this.issscd) {
for (let i = 0; i < this.stationInfo.length; i++) {
const item = this.stationInfo[i];
if (item.stationType === 'orient') {
item.iconPath = '/static/zhandian_nongtian_dingwei.png';
item.latitude = Number(item.stationLatitude);
item.longitude = Number(item.stationLongitude);
item.width = 35;
item.height = 52;
const markerItem = {
id: item.id,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: item.stationType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: true
};
this.markers.push(markerItem);
}
}
} else {
this.markers = this.markers.filter((item) => {
return item.stationType !== 'orient';
});
}
},
list2query() {
if (!this.isssscd) {
for (let i = 0; i < this.stationInfo.length; i++) {
const item = this.stationInfo[i];
if (item.stationType === 'livestock') {
item.iconPath = '/static/zhandian_xuqin_dingwei.png';
item.latitude = Number(item.stationLatitude);
item.longitude = Number(item.stationLongitude);
item.width = 35;
item.height = 52;
const markerItem = {
id: item.id,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: item.stationType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: true
};
this.markers.push(markerItem);
}
}
} else {
this.markers = this.markers.filter((item) => {
return item.stationType !== 'livestock';
});
}
}
}
}
</script>
<style lang="scss">
.bgimg {
position: fixed;
// z-index: -99;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.customCallout {
width: 349rpx;
height: 49rpx;
color: #fff;
font-size: 24rpx;
text-shadow: 1rpx 2rpx 0rpx #0D1934;
font-weight: bold;
// background-image: url('@/static/zhandian_icon_nongtian_title.png');
background-repeat: no-repeat;
background-size: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.customCallout1 {
width: 349rpx;
height: 49rpx;
color: #fff;
font-size: 24rpx;
text-shadow: 1rpx 2rpx 0rpx #0D1934;
font-weight: bold;
// background-image: url('@/static/zhandian_icon_xuqin_title.png');
background-repeat: no-repeat;
background-size: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.zdxq {
position: fixed;
left: 33rpx;
bottom: 36rpx;
}
.zdxqitem {
padding: 8rpx 0 7rpx 17rpx;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 22rpx;
font-family: Source Han Sans SC;
font-weight: 500;
color: #FFFFFF;
text-shadow: 0rpx 1rpx 0rpx rgba(4, 49, 52, 0.55);
width: 299rpx;
background: rgba(0, 0, 0, 0.52);
border: 1px solid #4B677C;
border-radius: 6rpx;
margin-bottom: 10rpx;
position: relative;
}
.crildon {
width: 10rpx;
height: 10rpx;
border-radius: 50%;
margin-right: 15rpx;
}
.zdxqitem:last-child {
margin-bottom: 0;
}
.zdxqitem image {
width: 33rpx;
height: 50rpx;
margin-right: 15rpx;
}
.zdxqitem:first-child image {
width: 30rpx;
height: 40rpx;
margin-right: 15rpx;
}
.textss {
flex: 1;
text-align: left;
}
.imgs {
position: absolute;
width: 100%;
height: 100%;
z-index: -9;
}
.switchs {
position: fixed;
top: 36rpx;
right: 40rpx;
display: flex;
align-items: center;
}
</style>