index - 副本.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const amapKey = 'b8d124e9756ebb356fd73f98ca7233b6'
  2. import adcodeList from "./adcode.js"
  3. const getAdcode = () => {
  4. return new Promise((resolve, reject) => {
  5. let adcode = null
  6. try {
  7. uni.getLocation({
  8. type: 'gcj02',
  9. geocode: true,
  10. cacheTimeout: 30 * 60,
  11. success: res => {
  12. try {
  13. const address = res.address;
  14. const district = address.district
  15. const codeInfo = adcodeList[district]
  16. adcode = codeInfo.adcode
  17. } catch (e) {
  18. //TODO handle the exception
  19. }
  20. resolve(adcode)
  21. },
  22. fail: err => {
  23. resolve(null)
  24. }
  25. });
  26. } catch (e) {
  27. //TODO handle the exception
  28. }
  29. })
  30. }
  31. export const getWeather_Fn = (info) => {
  32. return new Promise((resolve, reject) => {
  33. getAdcode().then(adcode => {
  34. uni.request({
  35. url: `https://restapi.amap.com/v3/weather/weatherInfo?key=${amapKey}&city=${adcode || '420500'}&extensions=base`,
  36. method: 'GET',
  37. success: (res) => {
  38. let weatherObj = null;
  39. try {
  40. const {
  41. lives
  42. } = res.data;
  43. weatherObj = lives[0] || null
  44. } catch (e) {
  45. //TODO handle the exception
  46. }
  47. resolve(weatherObj)
  48. },
  49. fail: (err) => {
  50. resolve(null)
  51. },
  52. });
  53. })
  54. })
  55. }
  56. export const getWeatherInfo_Fn = (info = null) => {
  57. const {
  58. weather,
  59. temperature
  60. } = info || {};
  61. const WeatherInfo = {
  62. icon: '',
  63. weather: weather,
  64. temperature: temperature === undefined ? temperature : temperature + ''
  65. }
  66. const types = ['晴', '少云', '晴间多云', '多云', '阴', '有风', '平静', '微风', '和风', '清风', '强风劲风', '疾风', '大风', '烈风', '风暴',
  67. '狂爆风', '飓风', '热带风暴', '霾', '中度霾', '重度霾', '严重霾', '阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨',
  68. '特大暴雨', '强阵雨', '强雷阵雨', '极端降雨', '毛毛雨细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '雨雪天气',
  69. '雨夹雪', '阵雨夹雪', '冻雨', '雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '浮尘', '扬沙', '沙尘暴',
  70. '强沙尘暴', '龙卷风', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾', '热', '冷'
  71. ];
  72. // WeatherInfo.icon = require("./暴雪.png");
  73. if (types.includes(weather)) {
  74. WeatherInfo.icon = require(`./${weather}.png`);
  75. } else if (weather === '强风/劲风') {
  76. WeatherInfo.icon = require("./强风劲风.png");
  77. } else if (weather === '毛毛雨/细雨') {
  78. WeatherInfo.icon = require("./毛毛雨细雨.png");
  79. } else {
  80. WeatherInfo.icon = require("./未知.png");
  81. };
  82. return WeatherInfo
  83. }