123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- const amapKey = 'b8d124e9756ebb356fd73f98ca7233b6'
- import adcodeList from "./adcode.js"
- const getAdcode = () => {
- return new Promise((resolve, reject) => {
- let adcode = null
- try {
- uni.getLocation({
- type: 'gcj02',
- geocode: true,
- cacheTimeout: 30 * 60,
- success: res => {
- try {
- const address = res.address;
- const district = address.district
- const codeInfo = adcodeList[district]
- adcode = codeInfo.adcode
- } catch (e) {
- //TODO handle the exception
- }
- resolve(adcode)
- },
- fail: err => {
- resolve(null)
- }
- });
- } catch (e) {
- //TODO handle the exception
- }
- })
- }
- export const getWeather_Fn = (info) => {
- return new Promise((resolve, reject) => {
- getAdcode().then(adcode => {
- uni.request({
- url: `https://restapi.amap.com/v3/weather/weatherInfo?key=${amapKey}&city=${adcode || '420500'}&extensions=base`,
- method: 'GET',
- success: (res) => {
- let weatherObj = null;
- try {
- const {
- lives
- } = res.data;
- weatherObj = lives[0] || null
- } catch (e) {
- //TODO handle the exception
- }
- resolve(weatherObj)
- },
- fail: (err) => {
- resolve(null)
- },
- });
- })
- })
- }
- export const getWeatherInfo_Fn = (info = null) => {
- const {
- weather,
- temperature
- } = info || {};
- const WeatherInfo = {
- icon: '',
- weather: weather,
- temperature: temperature === undefined ? temperature : temperature + ''
- }
- const types = ['晴', '少云', '晴间多云', '多云', '阴', '有风', '平静', '微风', '和风', '清风', '强风劲风', '疾风', '大风', '烈风', '风暴',
- '狂爆风', '飓风', '热带风暴', '霾', '中度霾', '重度霾', '严重霾', '阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨',
- '特大暴雨', '强阵雨', '强雷阵雨', '极端降雨', '毛毛雨细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '雨雪天气',
- '雨夹雪', '阵雨夹雪', '冻雨', '雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '浮尘', '扬沙', '沙尘暴',
- '强沙尘暴', '龙卷风', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾', '热', '冷'
- ];
-
- // WeatherInfo.icon = require("./暴雪.png");
-
- if (types.includes(weather)) {
- WeatherInfo.icon = require(`./${weather}.png`);
- } else if (weather === '强风/劲风') {
- WeatherInfo.icon = require("./强风劲风.png");
- } else if (weather === '毛毛雨/细雨') {
- WeatherInfo.icon = require("./毛毛雨细雨.png");
- } else {
- WeatherInfo.icon = require("./未知.png");
- };
- return WeatherInfo
- }
|