longyvfengyun
2023-12-25 d8d792a6842832e8f6af6604274c438b25053afe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<template>
    <div class="weather-wrapper">
        <el-popover
            placement="bottom-start"
            trigger="hover"
            popper-class="weather-popover">
            <list-box :address="weather.address" :list="list"></list-box>
            <div class="weather-body" slot="reference" v-show="show">
                <span class="weather-item weather-address">{{ weather.address }}</span>
                <img
                    class="weather-image"
                    v-for="(item, index) in images" :key="index"
                    :src="item" />
                <div class="weather-details">
                    <span class="weather-item weather-temperature">{{ weather.temperature }}</span>
                    <span class="weather-item weather-style">{{weather.style}}</span>
                </div>
                <div class="weather-details">
                    <span class="weather-item weather-wind-level">{{ weather.windLevel }}</span>
                    <span class="weather-item weather-wind">{{ weather.wind }}</span>
                </div>
            </div>
        </el-popover>
    </div>
</template>
 
<script>
import weathers from "@/assets/images/weathers";
import ListBox from "./listBox";
import BMap from "BMap";
import BMapTools from "@/assets/js/tools/BMapTools";
 
export default {
    name: "weatherComponent",
    props: {
      lat: {
        type: [String, Number],
        default: 0,
      },
      lng: {
        type: [String, Number],
        default: 0,
      },
    },
    components: {
      ListBox
    },
    bMapTools: new BMapTools(BMap),
    data() {
        return {
          show: false,
          province: "",
          city: "",
          district: "",
          weather: {
              address: '',
              temperature: '',
              style: '',
              wind: '',
              windLevel: ''
          },
          list: [],
        }
    },
    watch: {
      lat() {
        this.getLocation();
      }
    },
    methods: {
        getLocation() {
          let self = this;
          let bMapTools = this.$options.bMapTools;
          let lng = this.lng;
          let lat = this.lat;
          this.show = false;
          if(lng != 0 && lat != 0) {
            let pt = new BMap.Point(lng, lat);
            bMapTools.geoc.getLocation(pt, function(rs) {
              let address = rs.addressComponents;
              let province = address.province;
              let city = address.city;
              let district = address.district;
              self.province = province ? province.substring(0, province.length - 1) : "";
              self.city = city ? city.substring(0, city.length - 1) : "";
              self.district = district ? district.substring(0, district.length - 1) : "";
              self.searchWeather();
            });
          }
        },
        searchWeather() {
            let address = {
                province: this.province,
                city: this.city,
                district: this.district,
            };
            if(address.province) {
                this.$apis.address.getWeather(address).then(res=>{
                    let rs = JSON.parse(res.data.result);
                    if(rs.code == 1) {
                        this.show = true;
                        let data = rs.data;
                        this.weather.address = address.city;
                        this.weather.temperature = data.temperature+"℃";
                        this.weather.style = data.info;
                        this.weather.wind = data.direct;
                        this.weather.windLevel = data.power;
                        let list = rs.data2.map(item=>{
                            let tmp = {
                                date: new Date(item.date).format("yyyy-MM-dd"),
                                temperature: item.temperatureLow+"~"+item.temperatureHigh+"℃",
                                weather: item.infoNight
                            };
                            return tmp
                        });
                        this.list = list.filter((item, key) => {
                            return key != 0;
                        });
                    }else {
                        console.log(rs.msg);
                    }
                }).catch(error=>{
                    console.log(error);
                });
            }
        }
    },
    computed: {
        address() {
            return this.province+"-"+this.city+"-"+this.district;
        },
        images() {
            let style = this.weather.style;
            let list = [];
            for(let i=0; i<weathers.length; i++) {
                let item = weathers[i];
                if(item.weather == style) {
                    list = item.images;
                    break;
                }
            }
            return list;
        }
    },
    mounted() {
        this.getLocation();
    },
    beforeDestroy() {
 
    }
}
</script>
 
<style scoped>
.weather-wrapper {
    display: inline-block;
    cursor: pointer;
}
.weather-item {
    font-size: 14px;
}
.weather-details {
    display: inline-block;
    margin-left: 8px;
}
.weather-image {
    width: 24px;
    margin-left: 8px;
}
</style>