1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| // pages/weather/weather.js var defaultcity, getweather, gettemp, getwind, getpic, gettype,getdate; var vurl = 'http://wthrcdn.etouch.cn/weather_mini?city='
Page({
/** * 页面的初始数据 */ data: {}, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { defaultcity = '北京' this.weather() }, weather() { wx.showLoading({ title: 'Loading', }) wx.request({ url: vurl + defaultcity, success: res => { console.log(res.data) if (!res.data) { wx.showToast({ title: '获取天气接口失败', }) wx.hideLoading() } //头部-日期 getdate = res.data.data.forecast[0].date
//图片 getpic ='' gettype=res.data.data.forecast[0].type switch(gettype){ case '小雨': getpic='/images/rain.png' break; case '阴': getpic='/images/yin.png' break; case '多云': getpic='/images/duoyun.png' break; case '晴': getpic='/images/qing.png' break; default: console.log("default"); } //底部 getweather = res.data.data.forecast[0].high + '\n' + res.data.data.forecast[0].low gettemp = res.data.data.forecast[0].high getwind = res.data.data.forecast[0].fengxiang + ',' + res.data.data.forecast[0].fengli.replace(/<\!\[CDATA\[(.*)\]\]>/, '$1') this.setData({ city: defaultcity, weather: getweather, temp: gettemp, wind: getwind, pic: getpic, type: gettype, date: getdate }) wx.hideLoading() } }) }, bindKeyInput(e){ defaultcity=e.detail.value }, search(){ this.weather() } })
|