import { View, Input } from '@tarojs/components' import Taro,{useReady} from "@tarojs/taro" import React,{useState,useEffect} from 'react' const Weather =(date)=> { const [futureli,setFutureli]=useState([]) const [today,setToday]=useState([]) const [success,setSuccess]=useState(false) const [time,setTime]=useState('') const weather=(a)=> { Taro.request({ url: a.url, //仅为示例,并非真实的接口地址 data: { app: 'weather.'+a.type, //cityId:"101210101", //citynm:'北京', weaId: a.weaId, appkey: '10003', sign: 'b59bc3ef6191eb9f747dd4e83c99f2a4', format: 'json' }, header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { //console.log(res.data.result) if(a.type==='future') { setFutureli(res.data.result); } else if(a.type==='today') { //console.log(res.data.result); setToday(res.data.result); } } }) } const getTime=()=>{ var time = new Date(); var h =time.getHours(); //h用来放时 h = h < 10? '0' + h:h; var m = time.getMinutes();//m用来放分 m= m<10?'0'+m:m; //console.log(time.getMonth()+1+'月'+time.getDate()+'日'+' '+h + ':' + m); setTime(time.getMonth()+1+'月'+time.getDate()+'日'+' '+h + ':' + m) } const intoarea=(a)=>{ if(typeof(a)==='string') return a.split('℃')[1].replace('/','')+'~'+ a.split('℃')[0]+'℃' } useReady(() => { weather({url:'https://sapi.k780.com',weaId:date.props.weaId,type:'future'}); weather({url:'https://sapi.k780.com',weaId:date.props.weaId,type:'today'}); setSuccess(true) setInterval(getTime,1000); }) // useEffect(()=>{ // setInterval(getTime,1000); // },[]) return ( 气象数据 {today.citynm}市 {today.temp_curr} {today.weather} {time} {intoarea(today.temperature)} 未来7天天气预报 { futureli.map((item)=>{ return ( {item.days.split('-')[1]}月{item.days.split('-')[2]}日 {item.weather} {intoarea(item.temperature)} ) }) } ) } export default Weather