https.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. const QQ_MAP_KEY1 = '630ac2e3d80941b48c80fe3a8ed1a042'
  6. const QQ_MAP_KEY2 = 'c649beb3621c84ee57c365f45a9a6ed4'
  7. const QQ_MAP_KEY3 = 'dd35eaa26348f268f1f02b4a8fbe4aaf'
  8. //添加finally事件
  9. Promise.prototype.finally = function (callback) {
  10. var Promise = this.constructor;
  11. return this.then(
  12. function (value) {
  13. Promise.resolve(callback()).then(
  14. function () {
  15. return value;
  16. }
  17. );
  18. },
  19. function (reason) {
  20. Promise.resolve(callback()).then(
  21. function () {
  22. throw reason;
  23. }
  24. );
  25. }
  26. );
  27. }
  28. // 实况天气
  29. function getWeather(location) {
  30. return new Promise((resolve, reject) => {
  31. uni.request({
  32. url: weather+"/now",
  33. data: {
  34. location:location,
  35. key: QQ_MAP_KEY1
  36. },
  37. success: (res) => {
  38. resolve({result: res.data})
  39. },
  40. fail: (e) => {
  41. reject(e)
  42. },
  43. complete: (e) => {
  44. }
  45. })
  46. })
  47. }
  48. // 未来天气
  49. function getFuture(location) {
  50. return new Promise((resolve, reject) => {
  51. uni.request({
  52. // url: "/v/weather/index",
  53. url: weather+"/forecast",
  54. data: {
  55. // cityname:location,
  56. // key: QQ_MAP_KEY2
  57. location:location,
  58. key: QQ_MAP_KEY1
  59. },
  60. success: (res) => {
  61. resolve({result: res.data})
  62. },
  63. fail: (e) => {
  64. reject(e)
  65. },
  66. complete: (e) => {
  67. }
  68. })
  69. })
  70. }
  71. // 生活指数
  72. function getLift(location) {
  73. return new Promise((resolve, reject) => {
  74. uni.request({
  75. url: weather+"/lifestyle",
  76. data: {
  77. location:location,
  78. key: QQ_MAP_KEY1,
  79. },
  80. success: (res) => {
  81. resolve({result:res.data})
  82. },
  83. fail: (e) => {
  84. reject(e)
  85. }
  86. })
  87. })
  88. }
  89. // 空气质量---废弃
  90. function getAir(location) {
  91. return new Promise((resolve, reject) => {
  92. uni.request({
  93. url: air,
  94. data: {
  95. location:location,
  96. key: QQ_MAP_KEY1,
  97. },
  98. success: (res) => {
  99. resolve({result:res.data})
  100. },
  101. fail: (e) => {
  102. reject(e)
  103. }
  104. })
  105. })
  106. }
  107. // 經緯度逆解析
  108. function gelocation(obj) {
  109. return new Promise((resolve, reject) => {
  110. uni.request({
  111. url: "/api/geo/",
  112. data: {
  113. key: QQ_MAP_KEY3,
  114. lat:obj.lat,
  115. lng:obj.lng,
  116. type:1
  117. },
  118. success: (res) => {
  119. resolve({result:res.data})
  120. },
  121. fail: (e) => {
  122. reject(e)
  123. }
  124. })
  125. })
  126. }
  127. export {
  128. getWeather,
  129. getFuture,
  130. getLift,
  131. getAir,
  132. gelocation
  133. }
  134. // export const getWeather = (location) => {
  135. // return new Promise((resolve, reject) => {
  136. // wx.request({
  137. // url: 'https://free-api.heweather.net/s6/weather/now',
  138. // data: {
  139. // location:location,
  140. // key: QQ_MAP_KEY1
  141. // },
  142. // success: (res) => {
  143. // resolve({result: res.data})
  144. // },
  145. // fail: (e) => {
  146. // reject(e)
  147. // },
  148. // complete: (e) => {
  149. //
  150. // }
  151. // })
  152. // })
  153. // }
  154. // 未来七天天气
  155. // export const getFuture = (location) => {
  156. // return new Promise((resolve, reject) => {
  157. // wx.request({
  158. // url: 'http://v.juhe.cn/weather/index',
  159. // data: {
  160. // cityname:location,
  161. // key: QQ_MAP_KEY2
  162. // },
  163. // success: (res) => {
  164. // console.log(res)
  165. // resolve({result: res.data})
  166. // },
  167. // fail: (e) => {
  168. // console.log("haha chucuo 2 ")
  169. // reject(e)
  170. // }
  171. // })
  172. // })
  173. // }
  174. // 生活指数
  175. // export const getLift = (location) => {
  176. // return new Promise((resolve, reject) => {
  177. // wx.request({
  178. // url: 'https://free-api.heweather.net/s6/weather/lifestyle',
  179. // data: {
  180. // location:location,
  181. // key: QQ_MAP_KEY1,
  182. // },
  183. // success: (res) => {
  184. // resolve({result:res.data})
  185. // },
  186. // fail: (e) => {
  187. // reject(e)
  188. // }
  189. // })
  190. // })
  191. // }
  192. // 获取空气质量
  193. // export const getAir = (location) => {
  194. // return new Promise((resolve, reject) => {
  195. // wx.request({
  196. // url: 'https://free-api.heweather.net/s6/air/now',
  197. // data: {
  198. // location:location,
  199. // key: QQ_MAP_KEY1,
  200. // },
  201. // success: (res) => {
  202. // resolve({result:res.data})
  203. // },
  204. // fail: (e) => {
  205. // reject(e)
  206. // }
  207. // })
  208. // })
  209. // }
  210. // 经纬度逆向解析
  211. // export const gelocation = (obj) => {
  212. // return new Promise((resolve, reject) => {
  213. // wx.request({
  214. // url: 'http://apis.juhe.cn/geo/',
  215. // data: {
  216. // key: QQ_MAP_KEY3,
  217. // lat:obj.lat,
  218. // lng:obj.lng,
  219. // type:1
  220. // },
  221. // success: (res) => {
  222. // resolve({result:res.data})
  223. // },
  224. // fail: (e) => {
  225. // reject(e)
  226. // }
  227. // })
  228. // })
  229. // }