鸿蒙初开,开天辟地
import { weatherModel } from './weatherModel' import http from '@ohos.net.http'; import key from '../const/key'; class getWeatherUtil{ // 发送一个URL请求,返回对应的数据 getWeather(cityCode:number){ return new Promise((resolve,reject)=>{ let request = http.createHttp(); let url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${cityCode}&extensions=all&key=${key}`; let result = request.request(url); result.then((res)=>{ if(res.responseCode===200){ resolve(JSON.parse(res.result.toString())); } }).catch((err:object)=>{ console.log(err.toString()); }); }); } // 多个URL,一并返回结果 async getWeathers(cityCodes:Array){ let promises:Array<Promise> = []; let weatherModels:Array = []; for (let i = 0; i < citycodes.length i promises.pushthis.getweathercitycodesi await> { weatherModels = result; }); return weatherModels; } } export default new getWeatherUtil() as getWeatherUtil;
import { weatherModel } from './weatherModel'
import http from '@ohos.net.http';
import key from '../const/key';class getWeatherUtil{
// 发送一个URL请求,返回对应的数据
getWeather(cityCode:number){
return new Promise((resolve,reject)=>{
let request = http.createHttp();
let url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${cityCode}&extensions=all&key=${key}`;
let result = request.request(url);
result.then((res)=>{
if(res.responseCode===200){
resolve(JSON.parse(res.result.toString()));
}
}).catch((err:object)=>{
console.log(err.toString());
});
});
}
// 多个URL,一并返回结果
async getWeathers(cityCodes:Array){
let promises:Array<Promise> = [];
let weatherModels:Array = [];
for (let i = 0; i < citycodes.length i promises.pushthis.getweathercitycodesi await> {
weatherModels = result;
});
return weatherModels;
}
}export default new getWeatherUtil() as getWeatherUtil;
import { cast } from './cast'; export class forecast { city: string; adcode: number; casts: Array; constructor(city: string, adcode: number, casts: Array) { this.city = city; this.adcode = adcode; this.casts = casts; } }
import { cast } from './cast';
export class forecast {
city: string;
adcode: number;
casts: Array;
constructor(city: string, adcode: number, casts: Array) {
this.city = city;
this.adcode = adcode;
this.casts = casts;
}
}
export class cast { date: string; week: number; dayweather: string; nightweather: string; daytemp: number; nighttemp: number; daywind: string; nightwind: string; daypower: string; nightpower: string; daytemp_float: number; nighttemp_float: number; constructor(date: string, week: number, dayweather: string, nightweather: string, daytemp: number, nighttemp: number, daywind: string, nightwind: string, daypower: string, nightpower: string, daytemp_float: number, nighttemp_float: number) { this.date = date; this.week = week; this.dayweather = dayweather; this.nightweather = nightweather; this.daytemp = daytemp; this.nighttemp = nighttemp; this.daywind = daywind; this.nightwind = nightwind; this.daypower = daypower; this.nightpower = nightpower; this.daytemp_float = daytemp_float; this.nighttemp_float = nighttemp_float; } }
export class cast {
date: string;
week: number;
dayweather: string;
nightweather: string;
daytemp: number;
nighttemp: number;
daywind: string;
nightwind: string;
daypower: string;
nightpower: string;
daytemp_float: number;
nighttemp_float: number;constructor(date: string, week: number, dayweather: string, nightweather: string, daytemp: number, nighttemp: number, daywind: string, nightwind: string, daypower: string, nightpower: string, daytemp_float: number, nighttemp_float: number) {
this.date = date;
this.week = week;
this.dayweather = dayweather;
this.nightweather = nightweather;
this.daytemp = daytemp;
this.nighttemp = nighttemp;
this.daywind = daywind;
this.nightwind = nightwind;
this.daypower = daypower;
this.nightpower = nightpower;
this.daytemp_float = daytemp_float;
this.nighttemp_float = nighttemp_float;
}
}
import { forecast } from './forecast'; export class weatherModel { status: string; count: number; infocode: number; forecasts: Array = []; constructor(status: string, count: number, infocode: number, forecasts: Array) { this.status = status; this.count = count; this.infocode = infocode; this.forecasts = forecasts; } }
import { forecast } from './forecast';
export class weatherModel {
status: string;
count: number;
infocode: number;
forecasts: Array = [];constructor(status: string, count: number, infocode: number, forecasts: Array) {
this.status = status;
this.count = count;
this.infocode = infocode;
this.forecasts = forecasts;
}
}
import getWeatherUtil from "../viewmodel/getWeatherUtil"; @Entry @Component struct Index { build() { Column(){ Button("测试按钮").fontSize(40) .onClick(()=>{ getWeatherUtil.getWeather(120000); }) Button("获取城市名").fontSize(40) .onClick(()=>{ getWeatherUtil.getWeathers([120000,110000]); }) } } }
import getWeatherUtil from "../viewmodel/getWeatherUtil";
@Entry
@Component
struct Index {
build() {
Column(){
Button("测试按钮").fontSize(40)
.onClick(()=>{
getWeatherUtil.getWeather(120000);
})
Button("获取城市名").fontSize(40)
.onClick(()=>{
getWeatherUtil.getWeathers([120000,110000]);
})
}
}
}
完整封装并获取数据对象