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 16import { weather } from '../mock/WeatherData' 17import { Forecast, WeekWeather } from '../model/Main' 18import FormDate from '../model/FormDate' 19 20// 动态切换天气 21function getUpdateTemperature() { 22 let temperature: number[] = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35] 23 return (temperature[Math.floor(Math.random() * temperature.length)]) 24} 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 59// 获取逐小时天气情况 60function getHoursData(cityIndex: number) { 61 let hoursData = new Array<Forecast>() 62 weather[cityIndex].data[1].hours.forEach(item => { 63 let time = item.hours.substring(0, item.hours.length - 1) 64 hoursData.push(new Forecast(`${item.tem}°`, `${time}:00`, new FormDate().formTimeSlot(parseInt(time)), getTypeIcon(item.wea), item.win, item.win_speed)) 65 if (item.hours.indexOf('17') !== -1) { 66 hoursData.push(new Forecast('日落', '17:32', new FormDate().formTimeSlot(parseInt(time)), getTypeIcon('日落'), item.win, item.win_speed)) 67 } 68 }) 69 return hoursData 70} 71 72// 获取一周天气情况 根据获取到的数据格式化成需要的样式 73function getWeekWeatherData(cityIndex: number) { 74 let weekData = new Array<WeekWeather>() 75 weather[cityIndex].data.forEach(item => { 76 weekData.push(new WeekWeather(new FormDate().formMonthDay(item.date), item.week, getTypeIcon(item.wea), item.wea, item.air_level, item.tem1, item.tem2)) 77 }) 78 return weekData 79} 80 81export { getUpdateTemperature, getHoursData, getWeekWeatherData }