Commit 00f15d50766dfe1c425ac4f98edac3019201cd19

Authored by fengwotao
1 parent b20fbafe

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

1 1 <script lang="ts" setup name="WeatherContent">
2   -import { toRefs } from 'vue'
  2 +import { toRefs, PropType, computed } from 'vue'
3 3 import SelectCity from './SelectCity.vue'
4 4 import { useUtils } from '../hooks/useUtils'
5 5 import weatherBg from '@/assets/external/weather/bg.png'
  6 +import { weatherInfoInterface, selectValueType } from '../config'
6 7
7 8 const props = defineProps({
8 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 18
17 19 const { casts } = toRefs(props.data[0])
18 20
19   -const onHandleSelectValues = (values: any) => {
  21 +const onHandleSelectValues = (values: selectValueType) => {
20 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 41 </script>
23 42
24 43 <template>
25 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 46 <template #header>
35 47 <div class="card-header">
36 48 <div class="city-text">{{ data[0]?.city }}</div>
... ... @@ -59,7 +71,7 @@ const onHandleSelectValues = (values: any) => {
59 71 </div>
60 72 <template #footer>
61 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 75 <div>
64 76 <img :src="loadWeatherImg(item?.dayweather)" />
65 77 </div>
... ...
... ... @@ -7,6 +7,8 @@ import clearDay from '@/assets/external/weather/clearDay.png'
7 7 import cloudy from '@/assets/external/weather/cloudy.png'
8 8 import cloudyDay from '@/assets/external/weather/cloudyDay.png'
9 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 13 //第三方 天气接口key值和api配置(高德)
12 14 export class ThirdPartyWeatherConnfig {
... ... @@ -23,7 +25,7 @@ export const enum areaEnum {
23 25 COUNTY = 'COUNTY'
24 26 }
25 27
26   -//天气文字映射图片
  28 +//天气文字图片映射
27 29 export const weatherTextMapImg = [
28 30 {
29 31 text: '晴',
... ... @@ -40,10 +42,17 @@ export const weatherTextMapImg = [
40 42 {
41 43 text: '小雨',
42 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 57 export const weatherSpeedMapText = [
49 58 {
... ... @@ -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 142 export const option = {
104 143 dataset: {
105 144 provinceValue: null,
... ...
... ... @@ -6,6 +6,8 @@
6 6 <SettingItem name="颜色">
7 7 <n-color-picker v-model:value="optionData.weatherCss.backgroundColor" />
8 8 </SettingItem>
  9 + </SettingItemBox>
  10 + <SettingItemBox name="背景">
9 11 <SettingItem>
10 12 <n-button size="small" @click="optionData.weatherCss.backgroundColor = 'transparent'"> 恢复默认颜色 </n-button>
11 13 </SettingItem>
... ... @@ -73,7 +75,7 @@
73 75 <script setup lang="ts">
74 76 import { PropType } from 'vue'
75 77 import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting'
76   -import { option } from './config'
  78 +import { option, selectValueType } from './config'
77 79 import SelectCity from './componnets/SelectCity.vue'
78 80
79 81 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 89 props.optionData.dataset = values
88 90 }
89 91 </script>
... ...
1   -import { weatherSpeedMapText, weatherTextMapImg } from '../config'
  1 +import { weatherSpeedMapText, weatherTextMapImg, weatherTextMapType } from '../config'
2 2 import clearDay from '@/assets/external/weather/clearDay.png'
3 3
4 4 export const useUtils = () => {
5 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 9 //风力等级 ≤3 3
... ... @@ -14,7 +14,7 @@ export const useUtils = () => {
14 14 } else {
15 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 19 return {
20 20 loadWeatherImg,
... ...
1 1 <script lang="ts" setup name="Weather">
2 2 import { PropType, toRefs, onMounted, reactive, watch } from 'vue'
3 3 import { CreateComponentType } from '@/packages/index.d'
4   -import { option, ThirdPartyWeatherConnfig } from './config'
  4 +import { option, ThirdPartyWeatherConnfig, selectValueType, weatherInfoInterface } from './config'
5 5 import axios from 'axios'
6 6 import WeatherContent from './componnets/WeatherContent.vue'
7 7 import { useUtils } from './hooks/useUtils'
... ... @@ -19,11 +19,15 @@ const { w, h } = toRefs(props.chartConfig.attr)
19 19
20 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 27 weatherInfo: []
24 28 })
25 29
26   -const getWeatherInfos = async (area: any) => {
  30 +const getWeatherInfos = async (area: selectValueType) => {
27 31 const { cityValue, countyValue } = area || props.chartConfig.option.dataset
28 32 const params = {
29 33 key: ThirdPartyWeatherConnfig.ak,
... ... @@ -52,7 +56,7 @@ watch(
52 56 }
53 57 )
54 58
55   -const onHandleSelectValues = (values: any) => {
  59 +const onHandleSelectValues = (values: selectValueType) => {
56 60 getWeatherInfos(values)
57 61 }
58 62 </script>
... ...