• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17import FormDate from '../model/FormDate'
18import MenuData from '../model/MenuData'
19import { AirData, AirIndex, City, CityListData, Forecast, HeaderData, WeekWeather, SuitData } from '../model/Main'
20import { cityData } from '../mock/CityInfo'
21import { weather } from '../mock/Mock'
22
23
24const form = new FormDate() // 时间格式修正
25
26// 获取天气类型对应的icon
27function getTypeIcon(data: string) {
28  switch (data) {
29    case '晴':
30      return $r('app.media.ic_weather_sunny')
31      break;
32    case '多云':
33      return $r('app.media.ic_weather_cloudy')
34      break;
35    case '阴':
36      return $r('app.media.ic_weather_yin')
37      break;
38    case '小雨':
39      return $r('app.media.ic_weather_lightrain')
40      break;
41    case '雷阵雨':
42      return $r('app.media.ic_weather_lightrain')
43      break;
44    case '日落':
45      return $r('app.media.ic_weather_sunset')
46      break;
47    case '中雨':
48      return $r('app.media.ic_weather_rain')
49      break;
50    case '大雨':
51      return $r('app.media.ic_weather_rain')
52      break;
53    default:
54      return $r('app.media.ic_weather_cloudy')
55      break;
56  }
57}
58
59function getUpdateTimes() {
60  let times: string[] = [
61    '1小时',
62    '2小时',
63    '4小时',
64    '6小时',
65    '12小时',
66    '24小时',
67    '不更新'
68  ]
69  return times
70}
71
72function getMenuInfo() {
73  let data: MenuData[] =
74    [
75      new MenuData('管理城市', 'pages/CityList'),
76      new MenuData('更新间隔', '')
77    ]
78  return data;
79}
80
81// 获取首页顶部显示信息
82function getHeaderDate(cityIndex: number, city?: City) {
83  // 生成空气质量对象
84  let air: AirData = { airQuality: weather[cityIndex].aqi.air, airDesc: weather[cityIndex].aqi.air_level }
85  return new HeaderData(city ? city.name : weather[cityIndex].city, weather[cityIndex].data[1].tem, weather[cityIndex].data[1].wea, weather[cityIndex].data[1].tem1, weather[cityIndex].data[1].tem2, getTypeIcon(weather[cityIndex].data[1].wea), air, weather[cityIndex].data[1].air_tips)
86}
87
88function getCityList() {
89  let cities: Array<City> = []
90  if (cityData) {
91    cityData.forEach(item => {
92      cities.push(new City(item.name, item.temp, item.weather))
93    })
94  }
95  return cities
96}
97
98function addCity(city: City) {
99  let cities: City[] = AppStorage.Get('cityList')
100  let cityIndex = cities.length % 5
101  city.weather = weather[cityIndex].data[1].wea
102  city.temp = weather[cityIndex].data[1].tem
103  cities.push(city)
104  AppStorage.SetOrCreate('cityList', cities)
105}
106
107function getCityListWeatherData() {
108  let cityListWeatherData = new Array<CityListData>()
109  // 生成空气质量对象
110
111  let cities: City[] = AppStorage.Get('cityList')
112  if (cities === undefined || cities.length === 0) {
113    cities = getCityList().splice(0, 2)
114    AppStorage.SetOrCreate('cityList', cities)
115  }
116  let titles = []
117  if (cities) {
118    cities.forEach((item, cityIndex) => {
119      let index = cityIndex % 5
120      let air: AirData = { airQuality: weather[index].aqi.air, airDesc: weather[index].aqi.air_level }
121      cityListWeatherData.push(new CityListData(item.name, getHeaderDate(index, item), getHoursData(index), getWeekWeatherData(index), air, getAirIndexData(index), getLifeData(index)))
122      titles.push('')
123    })
124  }
125  AppStorage.SetOrCreate('titleText', titles)
126  return cityListWeatherData
127}
128
129
130// 获取逐小时天气情况
131function getHoursData(cityIndex: number) {
132  let hoursData = new Array<Forecast>()
133  weather[cityIndex].data[1].hours.forEach(item => {
134    let time = item.hours.substring(0, item.hours.length - 1)
135    hoursData.push(new Forecast(`${item.tem}°`, `${time}:00`, form.formTimeSlot(parseInt(time)), getTypeIcon(item.wea), item.win, item.win_speed))
136    if (item.hours.indexOf('17') !== -1) {
137      hoursData.push(new Forecast('日落', '17:32', form.formTimeSlot(parseInt(time)), getTypeIcon('日落'), item.win, item.win_speed))
138    }
139  })
140  return hoursData
141}
142
143// 获取一周天气情况 根据获取到的数据格式化成需要的样式
144function getWeekWeatherData(cityIndex: number) {
145  let weekData = new Array<WeekWeather>()
146  weather[cityIndex].data.forEach(item => {
147    weekData.push(new WeekWeather(form.formMonthDay(item.date), item.week, getTypeIcon(item.wea), item.wea, item.air_level, item.tem1, item.tem2))
148  })
149  return weekData
150}
151
152// 获取首页空气质量部分 右侧数据
153function getAirIndexData(cityIndex: number) {
154  let aqi: any = weather[cityIndex].aqi
155  let indexDate: AirIndex[] = [
156    new AirIndex('PM10', aqi.pm10),
157    new AirIndex('PM2.5', aqi.pm25),
158    new AirIndex('NO2', aqi.no2),
159    new AirIndex('SO2', aqi.so2),
160    new AirIndex('O2', aqi.o3),
161    new AirIndex('CO', aqi.co)
162  ]
163  return indexDate
164}
165
166// 获取生活指数 下方数据
167function getLifeData(cityIndex: number) {
168  let suitDate: SuitData[] = new Array<SuitData>()
169  let iconList = [
170    $r('app.media.icon_humidity'),
171    $r('app.media.icon_cloth'),
172    $r('app.media.icon_temperature'),
173    $r('app.media.icon_sport'),
174    $r('app.media.icon_breeze'),
175    $r('app.media.icon_cold'),
176    $r('app.media.icon_southerly'),
177    $r('app.media.icon_uv')
178  ]
179  weather[cityIndex].data[1].index.forEach((item, index) => {
180    suitDate.push(new SuitData(iconList[index], item.title, item.level))
181  })
182  return suitDate
183}
184
185// 根据天气类型 获取背景图片
186function getSideBg(weatherType: string) {
187  if (weatherType === '晴') {
188    return $r('app.media.weather_fine')
189  } else if (weatherType.includes('雨')) {
190    return $r('app.media.weather_rain')
191  } else {
192    return $r('app.media.weather_yin')
193  }
194}
195
196
197// 根据天气类型 获取背景图片
198function getBg(weatherType: string) {
199  if (weatherType === '晴') {
200    return $r('app.media.sunny_bg')
201  } else if (weatherType.includes('雨')) {
202    return $r('app.media.rain_bg')
203  } else {
204    return $r('app.media.yin_bg')
205  }
206}
207
208export {
209  getUpdateTimes,
210  getMenuInfo,
211  getHeaderDate,
212  getCityList,
213  addCity,
214  getCityListWeatherData,
215  getHoursData,
216  getWeekWeatherData,
217  getAirIndexData,
218  getLifeData,
219  getSideBg,
220  getBg
221}