From d35f1ebb8f3e365af58c193518ea7902b1c1af3e Mon Sep 17 00:00:00 2001 From: Eason010212 <1371033826@qq.com> Date: Mon, 28 Jul 2025 19:53:59 +0800 Subject: [PATCH] tencent-map-weather --- README.md | 5 +- mixio.js | 292 +++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 214 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index 8ce192f..6bd13f3 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,9 @@ https://gitee.com/bnu_mixly/mixio-linux-x86-dist/blob/darwin/mixio 13. ALLOW_REGISTER - bool,是否允许自主注册,默认为true 14. ALLOW_HOOK - bool,是否允许离线存储消息,默认为true 15. OFFLINE_MODE - bool,是否禁用天气/地图数据,默认为true -16. BAIDU_MAP_AK - string,百度地图客户端应用AK(OFFLINE_MODE=false时必须配置),默认为"" -17. BAIDU_MAP_SERVER_AK - string,百度地图服务端应用AK(OFFLINE_MODE=false时必须配置),默认为"" +16. BAIDU_MAP_AK - string,百度地图客户端应用AK(OFFLINE_MODE=false时可配置),默认为"" +17. BAIDU_MAP_SERVER_AK - string,百度地图服务端应用AK(OFFLINE_MODE=false时可配置),默认为"" +18. TENCENT_MAP_KEY - string, 腾讯地图key(OFFLINE_MODE=false时可配置),默认为"",和百度地图二选一进行配置即可,同时配置时优先启用百度地图 18. BAIDU_STAT_LINK - string, 百度统计链接,通常以"https://hm.baidu.com/hm.js?"开头 19. ADMIN_USERNAME - string,管理后台用户名,默认为"admin" 20. ADMIN_PASSWORD - string,管理后台密码,默认为"public" diff --git a/mixio.js b/mixio.js index 8390bf7..b180801 100644 --- a/mixio.js +++ b/mixio.js @@ -116,33 +116,34 @@ function init(cb) { } if (!fs.existsSync("config")) { fs.mkdirSync("config") - var defaultConfig = `{ - "MIXIO_HTTP_PORT": 8080, - "MIXIO_HTTPS_PORT": 8443, - "MIXIO_MQTT_PORT": 1883, - "MIXIO_WS_PORT": 8083, - "MIXIO_WSS_PORT": 8084, - "HTTPS_CRT_FILE": "config/certs/file.crt", - "HTTPS_PRIVATE_PEM": "config/certs/private.pem", - "MAX_PROJECT_NUM_PER_USER": 20, - "MAX_MESSAGE_PER_USER": 1000, - "MAX_MESSAGE_PER_SECOND": 5, - "ALLOW_REGISTER": true, - "ALLOW_HOOK": true, - "OFFLINE_MODE": true, - "BAIDU_MAP_AK": "", - "BAIDU_MAP_SERVER_AK": "", - "BAIDU_STAT_LINK": "", - "ADMIN_USERNAME":"admin", - "ADMIN_PASSWORD":"public", - "STORAGE_ENGINE":"sqlite", - "MYSQL_HOST":"localhost", - "MYSQL_PORT":3306, - "MYSQL_USER":"", - "MYSQL_PASS":"", - "MYSQL_DB":"mixio", - "FOOTER":"" - }` +var defaultConfig = `{ + "MIXIO_HTTP_PORT": 8080, + "MIXIO_HTTPS_PORT": 8443, + "MIXIO_MQTT_PORT": 1883, + "MIXIO_WS_PORT": 8083, + "MIXIO_WSS_PORT": 8084, + "HTTPS_CRT_FILE": "config/certs/file.crt", + "HTTPS_PRIVATE_PEM": "config/certs/private.pem", + "MAX_PROJECT_NUM_PER_USER": 20, + "MAX_MESSAGE_PER_USER": 1000, + "MAX_MESSAGE_PER_SECOND": 5, + "ALLOW_REGISTER": true, + "ALLOW_HOOK": true, + "OFFLINE_MODE": true, + "BAIDU_MAP_AK": "", + "BAIDU_MAP_SERVER_AK": "", + "TENCENT_MAP_KEY": "", + "BAIDU_STAT_LINK": "", + "ADMIN_USERNAME":"admin", + "ADMIN_PASSWORD":"public", + "STORAGE_ENGINE":"sqlite", + "MYSQL_HOST":"localhost", + "MYSQL_PORT":3306, + "MYSQL_USER":"", + "MYSQL_PASS":"", + "MYSQL_DB":"mixio", + "FOOTER":"" +}` fs.writeFileSync("config/config.json", defaultConfig) fs.mkdirSync("config/certs") @@ -2010,22 +2011,65 @@ var mixioServer = async function() { res.send(globalWeather[req.query.dsc_code].data) } else { try { - http.get('http://api.map.baidu.com/weather/v1/?district_id=' + req.query.dsc_code + '&data_type=now&ak=' + configs["BAIDU_MAP_SERVER_AK"], function(req2, res2) { - var html = '' - req2.on('data', function(data) { - html += data; - }); - req2.on('end', function() { - globalWeather[req.query.dsc_code] = { - time: new Date().getTime(), - data: html - } - res.send(html) - }); - }).on('error', function(e) { - res.send('-1') - }) + if(configs["BAIDU_MAP_SERVER_AK"]) + { + http.get('http://api.map.baidu.com/weather/v1/?district_id=' + req.query.dsc_code + '&data_type=now&ak=' + configs["BAIDU_MAP_SERVER_AK"], function(req2, res2) { + var html = '' + req2.on('data', function(data) { + html += data; + }); + req2.on('end', function() { + globalWeather[req.query.dsc_code] = { + time: new Date().getTime(), + data: html + } + res.send(html) + }); + }).on('error', function(e) { + res.send('-1') + }) + } + else if(configs["TENCENT_MAP_KEY"]) + { + http.get('http://apis.map.qq.com/ws/weather/v1/?adcode=' + req.query.dsc_code + '&type=now&key=' + configs["TENCENT_MAP_KEY"], function(req2, res2) { + var html = '' + req2.on('data', function(data) { + html += data; + }); + req2.on('end', function() { + try{ + var newhtml = JSON.parse(html)['result']['realtime'][0] + html = JSON.stringify({ + 'status': 0, + 'result':{ + 'location':{ + 'name': newhtml['province'] + newhtml['city'] + newhtml['district'] + }, + 'now':{ + 'temp': newhtml['infos']['temperature'], + 'text': newhtml['infos']['weather'], + 'rh': newhtml['infos']['humidity'], + 'wind_class': newhtml['infos']['wind_power'], + 'wind_dir': newhtml['infos']['wind_direction'] + } + } + }) + globalWeather[req.query.dsc_code] = { + time: new Date().getTime(), + data: html + } + res.send(html) + } + catch(e){ + res.send('-1') + } + }); + }).on('error', function(e) { + res.send('-1') + }) + } } catch (e) { + console.log(e) res.send('-1') } } @@ -2039,22 +2083,65 @@ var mixioServer = async function() { res.send(globalWeather[req.query.dsc_code].data) } else { try { - http.get('http://api.map.baidu.com/weather/v1/?district_id=' + req.query.dsc_code + '&data_type=now&ak=' + configs["BAIDU_MAP_SERVER_AK"], function(req2, res2) { - var html = '' - req2.on('data', function(data) { - html += data; - }); - req2.on('end', function() { - globalWeather[req.query.dsc_code] = { - time: new Date().getTime(), - data: html - } - res.send(html) - }); - }).on('error', function(e) { - res.send('-1') - }) + if(configs["BAIDU_MAP_SERVER_AK"]) + { + http.get('http://api.map.baidu.com/weather/v1/?district_id=' + req.query.dsc_code + '&data_type=now&ak=' + configs["BAIDU_MAP_SERVER_AK"], function(req2, res2) { + var html = '' + req2.on('data', function(data) { + html += data; + }); + req2.on('end', function() { + globalWeather[req.query.dsc_code] = { + time: new Date().getTime(), + data: html + } + res.send(html) + }); + }).on('error', function(e) { + res.send('-1') + }) + } + else if(configs["TENCENT_MAP_KEY"]) + { + http.get('http://apis.map.qq.com/ws/weather/v1/?adcode=' + req.query.dsc_code + '&type=now&key=' + configs["TENCENT_MAP_KEY"], function(req2, res2) { + var html = '' + req2.on('data', function(data) { + html += data; + }); + req2.on('end', function() { + try{ + var newhtml = JSON.parse(html)['result']['realtime'][0] + html = JSON.stringify({ + 'status': 0, + 'result':{ + 'location':{ + 'name': newhtml['province'] + newhtml['city'] + newhtml['district'] + }, + 'now':{ + 'temp': newhtml['infos']['temperature'], + 'text': newhtml['infos']['weather'], + 'rh': newhtml['infos']['humidity'], + 'wind_class': newhtml['infos']['wind_power'], + 'wind_dir': newhtml['infos']['wind_direction'] + } + } + }) + globalWeather[req.query.dsc_code] = { + time: new Date().getTime(), + data: html + } + res.send(html) + } + catch(e){ + res.send('-1') + } + }); + }).on('error', function(e) { + res.send('-1') + }) + } } catch (e) { + console.log(e) res.send('-1') } } @@ -3566,30 +3653,73 @@ var MixIOclosure = function(userName, projectName, projectPass, dataStorage, dom title.parent().parent().attr('user-content', [title.parent().parent().attr('user-content').split(',')[0], district, weather_type, temperature, humidity, wind_dir, wind_class].join(',')) itemdiv.trigger(MixIO.eventTags.WEATHER_SYNCED, [district, weather_type, temperature, humidity, wind_dir, wind_class]) } else { - http.get('http://api.map.baidu.com/weather/v1/?district_id=' + dsc_code + '&data_type=now&ak=' + configs["BAIDU_MAP_SERVER_AK"], function(req2, res2) { - var html = '' - req2.on('data', function(data) { - html += data; - }); - req2.on('end', function() { - globalWeather[dsc_code] = { - time: new Date().getTime(), - data: html - } - var result = html - var resJSON = JSON.parse(result) - if (resJSON.result && resJSON.result.now) { - weather_type = resJSON.result.now.text - temperature = resJSON.result.now.temp - humidity = resJSON.result.now.rh - wind_dir = resJSON.result.now.wind_dir - wind_class = resJSON.result.now.wind_class - district = resJSON.result.location.name - title.parent().parent().attr('user-content', [title.parent().parent().attr('user-content').split(',')[0], district, weather_type, temperature, humidity, wind_dir, wind_class].join(',')) - itemdiv.trigger(MixIO.eventTags.WEATHER_SYNCED, [district, weather_type, temperature, humidity, wind_dir, wind_class]) - } - }); - }) + if(configs["BAIDU_MAP_SERVER_AK"]) + { + http.get('http://api.map.baidu.com/weather/v1/?district_id=' + dsc_code + '&data_type=now&ak=' + configs["BAIDU_MAP_SERVER_AK"], function(req2, res2) { + var html = '' + req2.on('data', function(data) { + html += data; + }); + req2.on('end', function() { + globalWeather[dsc_code] = { + time: new Date().getTime(), + data: html + } + var result = html + var resJSON = JSON.parse(result) + if (resJSON.result && resJSON.result.now) { + weather_type = resJSON.result.now.text + temperature = resJSON.result.now.temp + humidity = resJSON.result.now.rh + wind_dir = resJSON.result.now.wind_dir + wind_class = resJSON.result.now.wind_class + district = resJSON.result.location.name + title.parent().parent().attr('user-content', [title.parent().parent().attr('user-content').split(',')[0], district, weather_type, temperature, humidity, wind_dir, wind_class].join(',')) + itemdiv.trigger(MixIO.eventTags.WEATHER_SYNCED, [district, weather_type, temperature, humidity, wind_dir, wind_class]) + } + }); + }) + } + else if(configs["TENCENT_MAP_KEY"]) + { + http.get('http://apis.map.qq.com/ws/weather/v1/?adcode=' + req.query.dsc_code + '&type=now&key=' + configs["TENCENT_MAP_KEY"], function(req2, res2) { + var html = '' + req2.on('data', function(data) { + html += data; + }); + req2.on('end', function() { + try{ + var newhtml = JSON.parse(html)['result']['realtime'][0] + html = JSON.stringify({ + 'status': 0, + 'result':{ + 'location':{ + 'name': newhtml['province'] + newhtml['city'] + newhtml['district'] + }, + 'now':{ + 'temp': newhtml['infos']['temperature'], + 'text': newhtml['infos']['weather'], + 'rh': newhtml['infos']['humidity'], + 'wind_class': newhtml['infos']['wind_power'], + 'wind_dir': newhtml['infos']['wind_direction'] + } + } + }) + globalWeather[req.query.dsc_code] = { + time: new Date().getTime(), + data: html + } + title.parent().parent().attr('user-content', [title.parent().parent().attr('user-content').split(',')[0], newhtml['province'] + newhtml['city'] + newhtml['district'], newhtml['infos']['weather'], newhtml['infos']['temperature'], newhtml['infos']['humidity'], newhtml['infos']['wind_direction'], newhtml['infos']['wind_power']].join(',')) + itemdiv.trigger(MixIO.eventTags.WEATHER_SYNCED, [newhtml['province'] + newhtml['city'] + newhtml['district'], newhtml['infos']['weather'], newhtml['infos']['temperature'], newhtml['infos']['humidity'], newhtml['infos']['wind_direction'], newhtml['infos']['wind_power']]) + } + catch(e){ + res.send('-1') + } + }); + }).on('error', function(e) { + res.send('-1') + }) + } } } itemdiv.bind(MixIO.actionTags.WEATHER_SYNC, function() {