mapinfo.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * 封装地图类信息
  3. * @author Rockery(1113269755@qq.com)
  4. */
  5. import indexConfig from '@/agrcloud-config/index.config';
  6. module.exports = (params) => {
  7. return new Promise((resolve, reject) => {
  8. uni.getLocation({
  9. type: 'gcj02',
  10. success: (getLocationRes) => {
  11. const longitude = getLocationRes.longitude;
  12. const latitude = getLocationRes.latitude;
  13. uni.request({
  14. url: 'https://restapi.amap.com/v3/geocode/regeo',
  15. data: {
  16. location: longitude + ',' + latitude,
  17. key: indexConfig.amapKey
  18. },
  19. method: "GET",
  20. dataType: "json",
  21. success: (regeocodeRes) => {
  22. if (regeocodeRes.statusCode == 200) {
  23. const currAdcode = (((regeocodeRes.data || {}).regeocode || {}).addressComponent || {}).adcode ||
  24. '520123';
  25. uni.request({
  26. url: 'https://restapi.amap.com/v3/weather/weatherInfo',
  27. data: {
  28. key: indexConfig.amapKey,
  29. city: currAdcode,
  30. extensions: 'base'
  31. },
  32. method: "GET",
  33. dataType: "json",
  34. success: (weatherInfoRes) => {
  35. const weatherInfoLives = ((weatherInfoRes.data || {}).lives || [])[0] || {};
  36. resolve({
  37. locationInfo: getLocationRes,
  38. regeocode: (regeocodeRes.data || {}).regeocode,
  39. weatherInfo: weatherInfoLives
  40. });
  41. },
  42. fail: weatherInfoErr => {
  43. uni.showToast({
  44. title: weatherInfoErr.errMsg,
  45. icon: 'none'
  46. });
  47. reject('err');
  48. }
  49. });
  50. }
  51. },
  52. fail: regeocodeErr => {
  53. uni.showToast({
  54. title: regeocodeErr.errMsg,
  55. icon: 'none'
  56. });
  57. reject('err');
  58. }
  59. });
  60. },
  61. fail: (getLocationErr) => {
  62. uni.showToast({
  63. title: getLocationErr.errMsg,
  64. icon: 'none'
  65. });
  66. reject('err');
  67. }
  68. });
  69. });
  70. };