https.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const weather = 'https://free-api.heweather.net/s6/weather'
  2. const future = 'http://v.juhe.cn/weather/index'
  3. const air = 'https://free-api.heweather.net/s6/air/now'
  4. const locl = 'http://apis.juhe.cn/geo/'
  5. // 和风天气Api产品秘钥 自行去官网申请
  6. const QQ_MAP_KEY1 = '********************************'
  7. const QQ_MAP_KEY2 = '********************************'
  8. const QQ_MAP_KEY3 = '********************************'
  9. //添加finally事件
  10. Promise.prototype.finally = function (callback) {
  11. var Promise = this.constructor;
  12. return this.then(
  13. function (value) {
  14. Promise.resolve(callback()).then(
  15. function () {
  16. return value;
  17. }
  18. );
  19. },
  20. function (reason) {
  21. Promise.resolve(callback()).then(
  22. function () {
  23. throw reason;
  24. }
  25. );
  26. }
  27. );
  28. }
  29. // 实况天气
  30. function getWeather(location) {
  31. return new Promise((resolve, reject) => {
  32. uni.request({
  33. url: weather+"/now",
  34. data: {
  35. location:location,
  36. key: QQ_MAP_KEY1
  37. },
  38. success: (res) => {
  39. resolve({result: res.data})
  40. },
  41. fail: (e) => {
  42. reject(e)
  43. },
  44. complete: (e) => {
  45. }
  46. })
  47. })
  48. }
  49. // 未来天气
  50. function getFuture(location) {
  51. return new Promise((resolve, reject) => {
  52. uni.request({
  53. url: weather+"/forecast",
  54. data: {
  55. location:location,
  56. key: QQ_MAP_KEY1
  57. },
  58. success: (res) => {
  59. resolve({result: res.data})
  60. },
  61. fail: (e) => {
  62. reject(e)
  63. },
  64. complete: (e) => {
  65. }
  66. })
  67. })
  68. }
  69. // 生活指数
  70. function getLift(location) {
  71. return new Promise((resolve, reject) => {
  72. uni.request({
  73. url: weather+"/lifestyle",
  74. data: {
  75. location:location,
  76. key: QQ_MAP_KEY1,
  77. },
  78. success: (res) => {
  79. resolve({result:res.data})
  80. },
  81. fail: (e) => {
  82. reject(e)
  83. }
  84. })
  85. })
  86. }
  87. // 空气质量---废弃
  88. function getAir(location) {
  89. return new Promise((resolve, reject) => {
  90. uni.request({
  91. url: air,
  92. data: {
  93. location:location,
  94. key: QQ_MAP_KEY1,
  95. },
  96. success: (res) => {
  97. resolve({result:res.data})
  98. },
  99. fail: (e) => {
  100. reject(e)
  101. }
  102. })
  103. })
  104. }
  105. // 經緯度逆解析
  106. function gelocation(obj) {
  107. return new Promise((resolve, reject) => {
  108. uni.request({
  109. url: "/api/geo/",
  110. data: {
  111. key: QQ_MAP_KEY3,
  112. lat:obj.lat,
  113. lng:obj.lng,
  114. type:1
  115. },
  116. success: (res) => {
  117. resolve({result:res.data})
  118. },
  119. fail: (e) => {
  120. reject(e)
  121. }
  122. })
  123. })
  124. }
  125. export {
  126. getWeather,
  127. getFuture,
  128. getLift,
  129. getAir,
  130. gelocation
  131. }