123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- const amapKey = 'b8d124e9756ebb356fd73f98ca7233b6'
- import adcodeList from "./adcode.js"
- import permision from "@/utils/permission.js"
- const getAdcode = () => {
- return new Promise(async (resolve, reject) => {
- let adcode = null
- try {
- // #ifdef APP-PLUS
- let flag = await permision.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION")
- if (!flag) return
- // #endif
- 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 => {
- // num++
- // uni.setStorageSync('ACCESS_FINE_LOCATION',num)
- // console.log(err)
- permision.showManualAuth("android.permission.ACCESS_FINE_LOCATION")
- return resolve(null)
- },
- });
- } catch (e) {
- //TODO handle the exception
- }
- })
- }
- const getWeather_Fn = () => {
- 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;
- // console.log('天气 = ' , res)
- try {
- const {
- lives
- } = res.data;
- weatherObj = lives[0] || null
- } catch (e) {
- //TODO handle the exception
- }
- resolve(weatherObj)
- },
- fail: (err) => {
- resolve(null)
- },
- });
- })
- })
- }
- const getWeatherInfo = (info = null) => {
- const {
- weather,
- temperature
- } = info || {};
- const WeatherInfo = {
- icon: '',
- weather: weather,
- temperature: temperature === undefined ? temperature : temperature + ''
- }
- // WeatherInfo.icon = require("./暴雪.png");
- switch (weather) {
- case '晴':
- WeatherInfo.icon = require("./weather_01.png");
- break;
- case '少云':
- WeatherInfo.icon = require("./weather_02.png");
- break;
- case '晴间多云':
- WeatherInfo.icon = require("./weather_03.png");
- break;
- case '多云':
- WeatherInfo.icon = require("./weather_04.png");
- break;
- case '阴':
- WeatherInfo.icon = require("./weather_05.png");
- break;
- case '有风':
- WeatherInfo.icon = require("./weather_06.png");
- break;
- case '平静':
- WeatherInfo.icon = require("./weather_07.png");
- break;
- case '微风':
- WeatherInfo.icon = require("./weather_08.png");
- break;
- case '和风':
- WeatherInfo.icon = require("./weather_09.png");
- break;
- case '清风':
- WeatherInfo.icon = require("./weather_10.png");
- break;
- case '强风劲风':
- case '强风/劲风':
- WeatherInfo.icon = require("./weather_11.png");
- break;
- case '疾风':
- WeatherInfo.icon = require("./weather_12.png");
- break;
- case '大风':
- WeatherInfo.icon = require("./weather_13.png");
- break;
- case '烈风':
- WeatherInfo.icon = require("./weather_14.png");
- break;
- case '风暴':
- WeatherInfo.icon = require("./weather_15.png");
- break;
- case '狂爆风':
- WeatherInfo.icon = require("./weather_16.png");
- break;
- case '飓风':
- WeatherInfo.icon = require("./weather_17.png");
- break;
- case '热带风暴':
- WeatherInfo.icon = require("./weather_18.png");
- break;
- case '霾':
- WeatherInfo.icon = require("./weather_19.png");
- break;
- case '中度霾':
- WeatherInfo.icon = require("./weather_20.png");
- break;
- case '重度霾':
- WeatherInfo.icon = require("./weather_21.png");
- break;
- case '严重霾':
- WeatherInfo.icon = require("./weather_22.png");
- break;
- case '阵雨':
- WeatherInfo.icon = require("./weather_23.png");
- break;
- case '雷阵雨':
- WeatherInfo.icon = require("./weather_24.png");
- break;
- case '雷阵雨并伴有冰雹':
- WeatherInfo.icon = require("./weather_25.png");
- break;
- case '小雨':
- WeatherInfo.icon = require("./weather_26.png");
- break;
- case '中雨':
- WeatherInfo.icon = require("./weather_27.png");
- break;
- case '大雨':
- WeatherInfo.icon = require("./weather_28.png");
- break;
- case '暴雨':
- WeatherInfo.icon = require("./weather_29.png");
- break;
- case '大暴雨':
- WeatherInfo.icon = require("./weather_30.png");
- break;
- case '特大暴雨':
- WeatherInfo.icon = require("./weather_31.png");
- break;
- case '强阵雨':
- WeatherInfo.icon = require("./weather_32.png");
- break;
- case '强雷阵雨':
- WeatherInfo.icon = require("./weather_33.png");
- break;
- case '极端降雨':
- WeatherInfo.icon = require("./weather_34.png");
- break;
- case '毛毛雨细雨':
- case '毛毛雨/细雨':
- WeatherInfo.icon = require("./weather_35.png");
- break;
- case '雨':
- WeatherInfo.icon = require("./weather_36.png");
- break;
- case '小雨-中雨':
- WeatherInfo.icon = require("./weather_37.png");
- break;
- case '中雨-大雨':
- WeatherInfo.icon = require("./weather_38.png");
- break;
- case '大雨-暴雨':
- WeatherInfo.icon = require("./weather_39.png");
- break;
- case '暴雨-大暴雨':
- WeatherInfo.icon = require("./weather_40.png");
- break;
- case '大暴雨-特大暴雨':
- WeatherInfo.icon = require("./weather_67.png");
- break;
- case '雨雪天气':
- WeatherInfo.icon = require("./weather_41.png");
- break;
- case '雨夹雪':
- WeatherInfo.icon = require("./weather_42.png");
- break;
- case '阵雨夹雪':
- WeatherInfo.icon = require("./weather_43.png");
- break;
- case '冻雨':
- WeatherInfo.icon = require("./weather_44.png");
- break;
- case '雪':
- WeatherInfo.icon = require("./weather_45.png");
- break;
- case '阵雪':
- WeatherInfo.icon = require("./weather_46.png");
- break;
- case '小雪':
- WeatherInfo.icon = require("./weather_47.png");
- break;
- case '中雪':
- WeatherInfo.icon = require("./weather_48.png");
- break;
- case '大雪':
- WeatherInfo.icon = require("./weather_49.png");
- break;
- case '暴雪':
- WeatherInfo.icon = require("./weather_50.png");
- break;
- case '小雪-中雪':
- WeatherInfo.icon = require("./weather_51.png");
- break;
- case '中雪-大雪':
- WeatherInfo.icon = require("./weather_66.png");
- break;
- case '大雪-暴雪':
- WeatherInfo.icon = require("./weather_52.png");
- break;
- case '浮尘':
- WeatherInfo.icon = require("./weather_53.png");
- break;
- case '扬沙':
- WeatherInfo.icon = require("./weather_54.png");
- break;
- case '沙尘暴':
- WeatherInfo.icon = require("./weather_55.png");
- break;
- case '强沙尘暴':
- WeatherInfo.icon = require("./weather_56.png");
- break;
- case '龙卷风':
- WeatherInfo.icon = require("./weather_57.png");
- break;
- case '雾':
- WeatherInfo.icon = require("./weather_58.png");
- break;
- case '浓雾':
- WeatherInfo.icon = require("./weather_59.png");
- break;
- case '强浓雾':
- WeatherInfo.icon = require("./weather_60.png");
- break;
- case '轻雾':
- WeatherInfo.icon = require("./weather_61.png");
- break;
- case '大雾':
- WeatherInfo.icon = require("./weather_62.png");
- break;
- case '特强浓雾':
- WeatherInfo.icon = require("./weather_63.png");
- break;
- case '热':
- WeatherInfo.icon = require("./weather_64.png");
- break;
- case '冷':
- WeatherInfo.icon = require("./weather_65.png");
- break;
- default:
- WeatherInfo.icon = require("./weather_68.png");
- }
- // const types = ['', '', '', '', '', '', '', '', '冷'];
- // 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");
- // };
- // console.log('WeatherInfo = ', WeatherInfo)
- return WeatherInfo
- }
- export const getWeatherInfo_Fn = () => {
- return new Promise((resolve, reject) => {
- getWeather_Fn().then(res => {
- const Weather = getWeatherInfo(res);
- resolve(Weather)
- })
- })
- }
|