Commit 00f15d50766dfe1c425ac4f98edac3019201cd19

Authored by fengwotao
1 parent b20fbafe

feat(src/packages): 优化天气组件ts类型定义

1 <script lang="ts" setup name="WeatherContent"> 1 <script lang="ts" setup name="WeatherContent">
2 -import { toRefs } from 'vue' 2 +import { toRefs, PropType, computed } from 'vue'
3 import SelectCity from './SelectCity.vue' 3 import SelectCity from './SelectCity.vue'
4 import { useUtils } from '../hooks/useUtils' 4 import { useUtils } from '../hooks/useUtils'
5 import weatherBg from '@/assets/external/weather/bg.png' 5 import weatherBg from '@/assets/external/weather/bg.png'
  6 +import { weatherInfoInterface, selectValueType } from '../config'
6 7
7 const props = defineProps({ 8 const props = defineProps({
8 data: { 9 data: {
9 - type: Array as any 10 + type: Array as PropType<weatherInfoInterface[]>,
  11 + required: true
10 } 12 }
11 }) 13 })
12 14
@@ -16,21 +18,31 @@ const { loadWeatherImg, loadWeatherLevel } = useUtils() @@ -16,21 +18,31 @@ const { loadWeatherImg, loadWeatherLevel } = useUtils()
16 18
17 const { casts } = toRefs(props.data[0]) 19 const { casts } = toRefs(props.data[0])
18 20
19 -const onHandleSelectValues = (values: any) => { 21 +const onHandleSelectValues = (values: selectValueType) => {
20 emits('submit', values) 22 emits('submit', values)
21 } 23 }
  24 +
  25 +const nCardComputed = computed(() => {
  26 + return {
  27 + backgroundImage: `url('${weatherBg}')`
  28 + }
  29 +})
  30 +
  31 +const segmentedComputed = computed(() => {
  32 + return {
  33 + content: true,
  34 + footer: 'soft'
  35 + }
  36 +})
  37 +
  38 +const indexMapDayFunc = (index: number) => {
  39 + return index === 0 ? '今天' : index === 1 ? '明天' : index === 2 ? '后天' : '外天'
  40 +}
22 </script> 41 </script>
23 42
24 <template> 43 <template>
25 <div> 44 <div>
26 - <n-card  
27 - :segmented="{  
28 - content: true,  
29 - footer: 'soft'  
30 - }"  
31 - :style="{ backgroundImage: `url('${weatherBg}')` }"  
32 - class="n-card"  
33 - > 45 + <n-card :segmented="segmentedComputed" :style="nCardComputed" class="n-card">
34 <template #header> 46 <template #header>
35 <div class="card-header"> 47 <div class="card-header">
36 <div class="city-text">{{ data[0]?.city }}</div> 48 <div class="city-text">{{ data[0]?.city }}</div>
@@ -59,7 +71,7 @@ const onHandleSelectValues = (values: any) => { @@ -59,7 +71,7 @@ const onHandleSelectValues = (values: any) => {
59 </div> 71 </div>
60 <template #footer> 72 <template #footer>
61 <div v-for="(item, index) in casts" :key="index" class="footer-content"> 73 <div v-for="(item, index) in casts" :key="index" class="footer-content">
62 - <div>{{ index === 0 ? '今天' : index === 1 ? '明天' : index === 2 ? '后天' : '外天' }}</div> 74 + <div>{{ indexMapDayFunc(index) }}</div>
63 <div> 75 <div>
64 <img :src="loadWeatherImg(item?.dayweather)" /> 76 <img :src="loadWeatherImg(item?.dayweather)" />
65 </div> 77 </div>
@@ -7,6 +7,8 @@ import clearDay from '@/assets/external/weather/clearDay.png' @@ -7,6 +7,8 @@ import clearDay from '@/assets/external/weather/clearDay.png'
7 import cloudy from '@/assets/external/weather/cloudy.png' 7 import cloudy from '@/assets/external/weather/cloudy.png'
8 import cloudyDay from '@/assets/external/weather/cloudyDay.png' 8 import cloudyDay from '@/assets/external/weather/cloudyDay.png'
9 import lightRain from '@/assets/external/weather/lightRain.png' 9 import lightRain from '@/assets/external/weather/lightRain.png'
  10 +import thunderstorm from '@/assets/external/weather/thunderstorm.png'
  11 +import shower from '@/assets/external/weather/shower.png'
10 12
11 //第三方 天气接口key值和api配置(高德) 13 //第三方 天气接口key值和api配置(高德)
12 export class ThirdPartyWeatherConnfig { 14 export class ThirdPartyWeatherConnfig {
@@ -23,7 +25,7 @@ export const enum areaEnum { @@ -23,7 +25,7 @@ export const enum areaEnum {
23 COUNTY = 'COUNTY' 25 COUNTY = 'COUNTY'
24 } 26 }
25 27
26 -//天气文字映射图片 28 +//天气文字图片映射
27 export const weatherTextMapImg = [ 29 export const weatherTextMapImg = [
28 { 30 {
29 text: '晴', 31 text: '晴',
@@ -40,10 +42,17 @@ export const weatherTextMapImg = [ @@ -40,10 +42,17 @@ export const weatherTextMapImg = [
40 { 42 {
41 text: '小雨', 43 text: '小雨',
42 img: lightRain 44 img: lightRain
  45 + },
  46 + {
  47 + text: '雷阵雨',
  48 + img: thunderstorm
  49 + },
  50 + {
  51 + text: '阵雨',
  52 + img: shower
43 } 53 }
44 ] 54 ]
45 55
46 -console.log(clearDay, cloudy)  
47 //风力等级文字映射 56 //风力等级文字映射
48 export const weatherSpeedMapText = [ 57 export const weatherSpeedMapText = [
49 { 58 {
@@ -100,6 +109,36 @@ export const weatherSpeedMapText = [ @@ -100,6 +109,36 @@ export const weatherSpeedMapText = [
100 } 109 }
101 ] 110 ]
102 111
  112 +export type selectValueType = {
  113 + provinceValue: null
  114 + cityValue: string
  115 + countyValue: null
  116 +}
  117 +
  118 +export interface castsInterface {
  119 + date: string
  120 + week: string
  121 + dayweather: string
  122 + nightweather: string
  123 + daytemp: string
  124 + nighttemp: string
  125 + daywind: string
  126 + nightwind: string
  127 + daypower: string
  128 + nightpower: string
  129 + daytemp_float: string
  130 + nighttemp_float: string
  131 +}
  132 +export interface weatherInfoInterface {
  133 + city: string
  134 + adcode: string
  135 + province: string
  136 + reporttime: string
  137 + casts: castsInterface[]
  138 +}
  139 +
  140 +export type weatherTextMapType = { text: string; img: string; level: Fn }
  141 +
103 export const option = { 142 export const option = {
104 dataset: { 143 dataset: {
105 provinceValue: null, 144 provinceValue: null,
@@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
6 <SettingItem name="颜色"> 6 <SettingItem name="颜色">
7 <n-color-picker v-model:value="optionData.weatherCss.backgroundColor" /> 7 <n-color-picker v-model:value="optionData.weatherCss.backgroundColor" />
8 </SettingItem> 8 </SettingItem>
  9 + </SettingItemBox>
  10 + <SettingItemBox name="背景">
9 <SettingItem> 11 <SettingItem>
10 <n-button size="small" @click="optionData.weatherCss.backgroundColor = 'transparent'"> 恢复默认颜色 </n-button> 12 <n-button size="small" @click="optionData.weatherCss.backgroundColor = 'transparent'"> 恢复默认颜色 </n-button>
11 </SettingItem> 13 </SettingItem>
@@ -73,7 +75,7 @@ @@ -73,7 +75,7 @@
73 <script setup lang="ts"> 75 <script setup lang="ts">
74 import { PropType } from 'vue' 76 import { PropType } from 'vue'
75 import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting' 77 import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting'
76 -import { option } from './config' 78 +import { option, selectValueType } from './config'
77 import SelectCity from './componnets/SelectCity.vue' 79 import SelectCity from './componnets/SelectCity.vue'
78 80
79 const props = defineProps({ 81 const props = defineProps({
@@ -83,7 +85,7 @@ const props = defineProps({ @@ -83,7 +85,7 @@ const props = defineProps({
83 } 85 }
84 }) 86 })
85 87
86 -const onHandleSelectValues = (values: any) => { 88 +const onHandleSelectValues = (values: selectValueType) => {
87 props.optionData.dataset = values 89 props.optionData.dataset = values
88 } 90 }
89 </script> 91 </script>
1 -import { weatherSpeedMapText, weatherTextMapImg } from '../config' 1 +import { weatherSpeedMapText, weatherTextMapImg, weatherTextMapType } from '../config'
2 import clearDay from '@/assets/external/weather/clearDay.png' 2 import clearDay from '@/assets/external/weather/clearDay.png'
3 3
4 export const useUtils = () => { 4 export const useUtils = () => {
5 const loadWeatherImg = (text: string) => { 5 const loadWeatherImg = (text: string) => {
6 - return weatherTextMapImg.find((item: any) => item.text === text)?.img || clearDay 6 + return weatherTextMapImg.find((item: Omit<weatherTextMapType, 'level'>) => item.text === text)?.img || clearDay
7 } 7 }
8 8
9 //风力等级 ≤3 3 9 //风力等级 ≤3 3
@@ -14,7 +14,7 @@ export const useUtils = () => { @@ -14,7 +14,7 @@ export const useUtils = () => {
14 } else { 14 } else {
15 handleSpeed = speed 15 handleSpeed = speed
16 } 16 }
17 - return weatherSpeedMapText.find((item: any) => item.level(Number(handleSpeed)))?.text 17 + return weatherSpeedMapText.find((item: { text: string; level: Fn }) => item.level(Number(handleSpeed)))?.text
18 } 18 }
19 return { 19 return {
20 loadWeatherImg, 20 loadWeatherImg,
1 <script lang="ts" setup name="Weather"> 1 <script lang="ts" setup name="Weather">
2 import { PropType, toRefs, onMounted, reactive, watch } from 'vue' 2 import { PropType, toRefs, onMounted, reactive, watch } from 'vue'
3 import { CreateComponentType } from '@/packages/index.d' 3 import { CreateComponentType } from '@/packages/index.d'
4 -import { option, ThirdPartyWeatherConnfig } from './config' 4 +import { option, ThirdPartyWeatherConnfig, selectValueType, weatherInfoInterface } from './config'
5 import axios from 'axios' 5 import axios from 'axios'
6 import WeatherContent from './componnets/WeatherContent.vue' 6 import WeatherContent from './componnets/WeatherContent.vue'
7 import { useUtils } from './hooks/useUtils' 7 import { useUtils } from './hooks/useUtils'
@@ -19,11 +19,15 @@ const { w, h } = toRefs(props.chartConfig.attr) @@ -19,11 +19,15 @@ const { w, h } = toRefs(props.chartConfig.attr)
19 19
20 const { weatherCss } = toRefs(props.chartConfig.option) 20 const { weatherCss } = toRefs(props.chartConfig.option)
21 21
22 -const weatherInfoValues = reactive<any>({ 22 +type weatherInfoValuesType = {
  23 + weatherInfo: weatherInfoInterface[]
  24 +}
  25 +
  26 +const weatherInfoValues = reactive<weatherInfoValuesType>({
23 weatherInfo: [] 27 weatherInfo: []
24 }) 28 })
25 29
26 -const getWeatherInfos = async (area: any) => { 30 +const getWeatherInfos = async (area: selectValueType) => {
27 const { cityValue, countyValue } = area || props.chartConfig.option.dataset 31 const { cityValue, countyValue } = area || props.chartConfig.option.dataset
28 const params = { 32 const params = {
29 key: ThirdPartyWeatherConnfig.ak, 33 key: ThirdPartyWeatherConnfig.ak,
@@ -52,7 +56,7 @@ watch( @@ -52,7 +56,7 @@ watch(
52 } 56 }
53 ) 57 )
54 58
55 -const onHandleSelectValues = (values: any) => { 59 +const onHandleSelectValues = (values: selectValueType) => {
56 getWeatherInfos(values) 60 getWeatherInfos(values)
57 } 61 }
58 </script> 62 </script>