-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transferred weather function to back-end
- Loading branch information
1 parent
4c26920
commit 9fa7a32
Showing
7 changed files
with
67 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios'; | ||
import { env } from '~/server/env'; | ||
|
||
const weatherapi = env.WEATHER_API_KEY; | ||
const citycode = env.WEATHER_CITY_CODE; | ||
|
||
export class WeatherController { | ||
async getInfo() { | ||
const info = { | ||
status: -1, | ||
weather: '未知', | ||
temperature: 0, | ||
humidity: 0, // 湿度 | ||
reporttime: '', | ||
}; | ||
await axios.get(`https://restapi.amap.com/v3/weather/weatherInfo?key=${weatherapi}&city=${citycode}&extensions=base&output=JSON`).then((response) => { | ||
const data = response.data; | ||
info.status = data.status; | ||
info.weather = data.lives[0].weather; | ||
info.temperature = data.lives[0].temperature; | ||
info.humidity = data.lives[0].humidity; | ||
info.reporttime = data.lives[0].reporttime; | ||
// if (info.status){...} | ||
}).catch(() => { | ||
info.status = 0; | ||
// console.log('probably network error', error); | ||
}); | ||
return info; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { publicProcedure, router } from '../trpc'; | ||
|
||
export const weatherRouter = router({ | ||
info: publicProcedure | ||
.query(async ({ ctx }) => { | ||
return await ctx.weatherController.getInfo(); | ||
}), | ||
}); |