123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- const weather = 'https://free-api.heweather.net/s6/weather'
- const future = 'http://v.juhe.cn/weather/index'
- const air = 'https://free-api.heweather.net/s6/air/now'
- const locl = 'http://apis.juhe.cn/geo/'
- // 和风天气Api产品秘钥 自行去官网申请
- const QQ_MAP_KEY1 = '********************************'
- const QQ_MAP_KEY2 = '********************************'
- const QQ_MAP_KEY3 = '********************************'
- //添加finally事件
- Promise.prototype.finally = function (callback) {
- var Promise = this.constructor;
- return this.then(
- function (value) {
- Promise.resolve(callback()).then(
- function () {
- return value;
- }
- );
- },
- function (reason) {
- Promise.resolve(callback()).then(
- function () {
- throw reason;
- }
- );
- }
- );
- }
- // 实况天气
- function getWeather(location) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: weather+"/now",
- data: {
- location:location,
- key: QQ_MAP_KEY1
- },
- success: (res) => {
- resolve({result: res.data})
- },
- fail: (e) => {
- reject(e)
- },
- complete: (e) => {
-
- }
- })
- })
- }
- // 未来天气
- function getFuture(location) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: weather+"/forecast",
- data: {
- location:location,
- key: QQ_MAP_KEY1
- },
- success: (res) => {
- resolve({result: res.data})
- },
- fail: (e) => {
- reject(e)
- },
- complete: (e) => {
-
- }
- })
- })
- }
- // 生活指数
- function getLift(location) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: weather+"/lifestyle",
- data: {
- location:location,
- key: QQ_MAP_KEY1,
- },
- success: (res) => {
- resolve({result:res.data})
- },
- fail: (e) => {
- reject(e)
- }
- })
- })
- }
- // 空气质量---废弃
- function getAir(location) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: air,
- data: {
- location:location,
- key: QQ_MAP_KEY1,
- },
- success: (res) => {
- resolve({result:res.data})
- },
- fail: (e) => {
- reject(e)
- }
- })
- })
- }
- // 經緯度逆解析
- function gelocation(obj) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: "/api/geo/",
- data: {
- key: QQ_MAP_KEY3,
- lat:obj.lat,
- lng:obj.lng,
- type:1
- },
- success: (res) => {
- resolve({result:res.data})
- },
- fail: (e) => {
- reject(e)
- }
- })
- })
- }
- export {
- getWeather,
- getFuture,
- getLift,
- getAir,
- gelocation
- }
|