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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| <template> <div class="home" :style="{ backgroundImage: 'url(' + coverImgUrl + ')' }"> <div class="weather-box"> <el-cascader size="medium" separator=">" :options="options" placeholder="请选择/搜索" v-model="selectedOptions" :props="{ expandTrigger: 'hover' }" filterable @change="selectCity" /> <div class="now-cond" v-if="Object.keys(weathData).length"> <p class="address"> <i class="el-icon-place" />{{ weathData.basic.city }} </p> <p class="upTime">{{ weathData.basic.update.loc }}</p> <i class="condTxt">{{ weathData.now.cond.txt }}</i> <p class="wind">{{ weathData.now.wind.dir }}</p> <p class="tmp"> {{ weathData.daily_forecast[0].tmp.min }}℃~{{ weathData.daily_forecast[0].tmp.max }}℃ </p> <h1>未来3日天气预报</h1> <el-table :data="weathData.daily_forecast" border style="width: 100%" stripe > <el-table-column label="日期" width="180"> <template slot-scope="scope"> <i class="el-icon-time"></i> <span style="margin-left: 10px">{{ scope.row.date }}</span> </template> </el-table-column> <el-table-column prop="cond.txt_d" label="天气" /> <el-table-column prop="tmp.min" label="最低温度(℃)" width="120" /> <el-table-column prop="tmp.max" label="最高温度(℃)" width="120" /> <el-table-column prop="wind.dir" label="风向" /> </el-table> </div> <div class="tip" v-else> <h1>选择城市开始查询吧</h1> </div> </div> </div> </template>
<script>
import { regionData, CodeToText } from "element-china-area-data";
export default { name: "Home", data() { return { options: regionData, selectedOptions: [], addressAll: [], address: "", weathData: {}, coverImgUrl: require("@/assets/img/bg.jpg"), }; }, methods: { selectCity() { this.addressAll = []; this.selectedOptions.forEach((city) => { this.addressAll.push(CodeToText[city]); }); this.inquireWeather(); }, async inquireWeather() { if (this.addressAll[2] == "市辖区" || this.addressAll.length == 2) { this.address = this.addressAll[1]; } else { this.address = this.addressAll[2]; } const res1 = await this.$api.getCityWeather(this.address); if (res1.status === "ok") { this.$notify({ title: "查询成功", type: "success", }); this.weathData = res1; this.weathData.daily_forecast.shift(); this.weathData.daily_forecast.splice(3, 3); } else if (res1.status === "unknown location") { this.address = this.addressAll[1]; const res2 = await this.$api.getCityWeather(this.address); if (res2.status === "ok") { this.$notify({ title: "查询成功", type: "success", }); this.weathData = res2; this.weathData.daily_forecast.shift(); this.weathData.daily_forecast.splice(3, 3); } else { this.address = this.addressAll[0]; const res3 = await this.$api.getCityWeather(this.address); if (res3.status === "ok") { this.$notify({ title: "查询成功", type: "success", }); this.weathData = res3; this.weathData.daily_forecast.shift(); this.weathData.daily_forecast.splice(3, 3); } else { this.$message({ message: "抱歉,无法获取该地相关信息", type: "error", }); } } } this.bgChange(); }, bgChange() { const weather = this.weathData.now.cond.txt; this.coverImgUrl = require(`@/assets/img/${weather}.jpg`); }, }, }; </script>
<style lang="scss" scoped> .home { @mixin xy($top: auto, $right: auto, $bottom: auto, $left: auto) { top: $top; right: $right; bottom: $bottom; left: $left; } @mixin wh($wid, $hei) { width: $wid; height: $hei; } @mixin font($size, $fw: bold, $color: #fff) { font-size: $size; font-weight: $fw; color: $color; } position: relative; min-height: 100vh; // background: url("~"); background-repeat: no-repeat; background-position: center center; background-size: cover; .weather-box { position: absolute; text-align: center; z-index: 999; @include xy(5%, "", "", 50%); transform: translateX(-50%); .now-cond { h1 { @include font(20px); } .address, .upTime { @include font(20px); text-shadow: 5px 5px 5px #000; } .condTxt { @include font(80px, bolder); text-shadow: 5px 5px 5px #ccc; } .wind, .tmp { @include font(20px, bolder, #000); text-shadow: 2px 2px 5px rgb(144, 149, 216); } } .tip { h1 { @include font(50px, bolder); text-shadow: 2px 2px 5px rgb(144, 149, 216); } } } } </style>
|