一、获取当前日期
getTime() {
let yy = new Date().getFullYear();
let mm = new Date().getMonth() + 1;
let dd = new Date().getDate();
let hh = new Date().getHours();
let wk = new Date().getDay();
let mf =
new Date().getMinutes() < 10
? "0" + new Date().getMinutes()
: new Date().getMinutes();
var date = yy + " " + mm + "月" + dd + "日 " + hh + ":" + mf;
let weeks = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
];
let week = weeks[wk];
this.date = date;
this.week = week;
}
二、获取天气
weatherType 用来区分显示哪个图标 因可能返回的天气太多 所以分了一下类
getCity().then((res) => {
var city = res.data.city;
city.replace("市", "");
getWeather({ city: city }).then((res) => {
var lives = res.data.lives[0];
if (lives.weather.indexOf("晴") !== -1 ) {
this.weatherType = 1;
} else if (lives.weather.indexOf("云") !== -1 || lives.weather.indexOf("阴") !== -1) {
this.weatherType = 2;
} else if (lives.weather.indexOf("雨") !== -1) {
this.weatherType = 3;
} else if (lives.weather.indexOf("雪") !== -1) {
this.weatherType = 4;
} else if (lives.weather.indexOf("风") !== -1) {
this.weatherType = 5;
}else if (lives.weather.indexOf("雾") !== -1 || lives.weather.indexOf("霾")) {
this.weatherType = 6;
}else {
this.weatherType = 1;
}
this.weatherTitle = lives.weather;
});
});
export function getCity(params) {
return request ({
method: 'get',
url: 'https://restapi.amap.com/v3/ip?key=你自己的key',
data: params
})
}
export function getWeather(params) {
return request ({
method: 'get',
url: 'https://restapi.amap.com/v3/weather/weatherInfo?key=你自己的key&city='+ params.city,
data: params
})
}