本小节最后更新于 2023 年 11 月 07 日
231107 更新:移除JQ依赖库,轻微减少key的消耗,解决Pjax、ajax请求等问题;升级优化请求方案!
231006 更新:修复异步导致无法正常ajax请求的问题;无法正常获取定位的问题
230707 更新:删除新版内的旧版遗留CSS部分样式
2.0优化版 231107
本着性能缘由,以及某些封面,JQ却是存在某些问题,当然JQ还是较为能打的
但也不是说不可代替的,优化了下个性定位欢迎这个功能,为了更好的性能,还是决定剔除掉JQ
重要消息!重要消息!重要消息!
作为开发者之一,Solitude主题也已完美集成本功能,只需一步配置好key即可使用!感兴趣的喵友可以去看看喔~
Hexo-Theme-Solitude
一个优雅的Heo风格的Hexo主题,接近Heo,完整度高。
数据支持
注意:
如创建了多个应用,需在配额管理
- Key额度
里的 坐标转换 行政区划列表 行政区划子级查询 逆地址解析 IP定位 地点搜索 关键词输入提示 静态地图 进行分配额度
创建数据
- 创建
[blogRoot]/source/js/txmap.js
文件,并新增以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| function welcometxmap() { ipLoacation = window.saveToLocal.get('ipLocation'); if (ipLoacation) { } else { var script = document.createElement('script'); var url = `https://apis.map.qq.com/ws/location/v1/ip?key=${txkey}&output=jsonp`; script.src = url; window.QQmap = function (data) { ipLoacation = data; window.saveToLocal.set('ipLocation', ipLoacation, 1); document.body.removeChild(script); delete window.QQmap; }; document.body.appendChild(script); } showWelcome(); } function getDistance(e1, n1, e2, n2) { const R = 6371 const { sin, cos, asin, PI, hypot } = Math let getPoint = (e, n) => { e *= PI / 180 n *= PI / 180 return { x: cos(n) * cos(e), y: cos(n) * sin(e), z: sin(n) } }
let a = getPoint(e1, n1) let b = getPoint(e2, n2) let c = hypot(a.x - b.x, a.y - b.y, a.z - b.z) let r = asin(c / 2) * 2 * R return Math.round(r); }
function showWelcome() {
let dist = getDistance(longitude, Latitude, ipLoacation.result.location.lng, ipLoacation.result.location.lat); let pos = ipLoacation.result.ad_info.nation; let ip; let posdesc; switch (ipLoacation.result.ad_info.nation) { case "日本": posdesc = "よろしく,一起去看樱花吗"; break; case "美国": posdesc = "Let us live in peace!"; break; case "英国": posdesc = "想同你一起夜乘伦敦眼"; break; case "俄罗斯": posdesc = "干了这瓶伏特加!"; break; case "法国": posdesc = "C'est La Vie"; break; case "德国": posdesc = "Die Zeit verging im Fluge."; break; case "澳大利亚": posdesc = "一起去大堡礁吧!"; break; case "加拿大": posdesc = "拾起一片枫叶赠予你"; break; case "中国": pos = ipLoacation.result.ad_info.province + " " + ipLoacation.result.ad_info.city + " " + ipLoacation.result.ad_info.district; ip = ipLoacation.result.ip; switch (ipLoacation.result.ad_info.province) { case "北京市": posdesc = "北——京——欢迎你~~~"; break; case "天津市": posdesc = "讲段相声吧"; break; case "河北省": posdesc = "山势巍巍成壁垒,天下雄关铁马金戈由此向,无限江山"; break; case "山西省": posdesc = "展开坐具长三尺,已占山河五百余"; break; case "内蒙古自治区": posdesc = "天苍苍,野茫茫,风吹草低见牛羊"; break; case "辽宁省": posdesc = "我想吃烤鸡架!"; break; case "吉林省": posdesc = "状元阁就是东北烧烤之王"; break; case "黑龙江省": posdesc = "很喜欢哈尔滨大剧院"; break; case "上海市": posdesc = "众所周知,中国只有两个城市"; break; case "江苏省": switch (ipLoacation.result.ad_info.city) { case "南京市": posdesc = "这是我挺想去的城市啦"; break; case "苏州市": posdesc = "上有天堂,下有苏杭"; break; default: posdesc = "散装是必须要散装的"; break; } break; case "浙江省": posdesc = "东风渐绿西湖柳,雁已还人未南归"; break; case "河南省": switch (ipLoacation.result.ad_info.city) { case "郑州市": posdesc = "豫州之域,天地之中"; break; case "南阳市": posdesc = "臣本布衣,躬耕于南阳此南阳非彼南阳!"; break; case "驻马店市": posdesc = "峰峰有奇石,石石挟仙气嵖岈山的花很美哦!"; break; case "开封市": posdesc = "刚正不阿包青天"; break; case "洛阳市": posdesc = "洛阳牡丹甲天下"; break; default: posdesc = "可否带我品尝河南烩面啦?"; break; } break; case "安徽省": posdesc = "蚌埠住了,芜湖起飞"; break; case "福建省": posdesc = "井邑白云间,岩城远带山"; break; case "江西省": posdesc = "落霞与孤鹜齐飞,秋水共长天一色"; break; case "山东省": posdesc = "遥望齐州九点烟,一泓海水杯中泻"; break; case "湖北省": switch (ipLoacation.result.ad_info.city) { case "黄冈市": posdesc = "红安将军县!辈出将才!"; break; default: posdesc = "来碗热干面~"; break; } break; case "湖南省": posdesc = "74751,长沙斯塔克"; break; case "广东省": switch (ipLoacation.result.ad_info.city) { case "广州市": posdesc = "看小蛮腰,喝早茶了嘛~"; break; case "深圳市": switch (ipLoacation.result.ad_info.district) { case "坪山区": posdesc = "好巧!博主也在坪山区生活喔~"; break; default: posdesc = "今天你996了嘛~"; break; } break; case "阳江市": posdesc = "阳春合水!博主家乡~ 欢迎来玩~"; break; default: posdesc = "来两斤福建人~"; break; } break; case "广西壮族自治区": posdesc = "桂林山水甲天下"; break; case "海南省": posdesc = "朝观日出逐白浪,夕看云起收霞光"; break; case "四川省": posdesc = "康康川妹子"; break; case "贵州省": posdesc = "茅台,学生,再塞200"; break; case "云南省": posdesc = "玉龙飞舞云缠绕,万仞冰川直耸天"; break; case "西藏自治区": posdesc = "躺在茫茫草原上,仰望蓝天"; break; case "陕西省": posdesc = "来份臊子面加馍"; break; case "甘肃省": posdesc = "羌笛何须怨杨柳,春风不度玉门关"; break; case "青海省": posdesc = "牛肉干和老酸奶都好好吃"; break; case "宁夏回族自治区": posdesc = "大漠孤烟直,长河落日圆"; break; case "新疆维吾尔自治区": posdesc = "驼铃古道丝绸路,胡马犹闻唐汉风"; break; case "台湾省": posdesc = "我在这头,大陆在那头"; break; case "香港特别行政区": posdesc = "永定贼有残留地鬼嚎,迎击光非岁玉"; break; case "澳门特别行政区": posdesc = "性感荷官,在线发牌"; break; default: posdesc = "带我去你的城市逛逛吧!"; break; } break; default: posdesc = "带我去你的国家逛逛吧"; break; }
let timeChange; let date = new Date(); if (date.getHours() >= 5 && date.getHours() < 11) timeChange = "<span class='welcome-time'>🌤️ 早上好,一日之计在于晨</span>"; else if (date.getHours() >= 11 && date.getHours() < 13) timeChange = "<span class='welcome-time'>☀️ 中午好,记得午休喔~</span>"; else if (date.getHours() >= 13 && date.getHours() < 17) timeChange = "<span class='welcome-time'>🕞 下午好,饮茶先啦!</span>"; else if (date.getHours() >= 17 && date.getHours() < 19) timeChange = "<span class='welcome-time'>🚶♂️ 即将下班,记得按时吃饭~</span>"; else if (date.getHours() >= 19 && date.getHours() < 24) timeChange = "<span class='welcome-time'>🌙 晚上好,夜生活嗨起来!</span>"; else timeChange = "<span class='welcome-time'>夜深了,早点休息,少熬夜</span>";
try { document.getElementById("welcome-info").innerHTML = `<span>热烈欢迎来自~</span><br><span><span style="color: var(--icat-card-welcome);font-weight: bold;">${pos}</span> 的喵友</span><br><span class="welcome-message">${posdesc}</span><br>您当前位置距博主约 <b><span style="color: var(--icat-card-welcome);font-weight: bold;">${dist}</span></b> 公里!<br><span>您的IP地址为:${ip}</span><br>${timeChange}`; } catch (err) { } }
|
- 在
_config.butterfly.yml
主题配置文件中 inject
下的 bottom
引入 txmap.js
1 2 3 4 5 6 7 8 9
| ···
inject: head: - ··· bottom: - <script src="/js/txmap.js"></script>
···
|
- 在自建的CSS文件
[blogRoot]/source/css/icat.css
里新增以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #welcome-info { overflow: hidden; --icat-card-welcome: #49b1f5; } #welcome-info .welcome-time { width: 100%; margin: 12px 0 0; display: inline-flex; } #welcome-info .welcome-message { width: 100%; margin-bottom: 8px; display: inline-flex; } #welcome-info .welcome-region { background: var(--icat-background); border: var(--style-border); color: var(--icat-fontcolor); border-radius: 8px; display: inline-flex; align-items: center; padding: 6px 10px; margin: 6px 0; }
|
使用方式
- 修改
[blogRoot]/source/_data/widget.yml
文件,新增 name
icon
html
项的内容
我这使用的是自定义侧边栏功能,相关问题请看 Butterfly - 自定义侧边栏
1 2 3 4 5 6 7 8
| top: - class_name: id_name: name: 来访者 icon: iconfont icat-visitor html: <div id="welcome-info"></div><script data-pjax>var longitude="经度";var Latitude="纬度";var txkey="腾讯定位Key";var ipLoacation;window.onload = () => {welcometxmap()};document.addEventListener("pjax:complete", welcometxmap())</script>
···
|
- 将上方的
经度
、纬度
以及 腾讯定位Key
替换成你自己的
- 修改
[blogRoot]/theme/butterfly/latout/includes/widget/card_announcement.pug
页面内容
( + 号直接删除 即是正常缩进)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| if theme.aside.card_announcement.enable .card-widget.card-announcement + #welcome-info + script(data-pjax). + var longitude = '#{config.welcome.longitude}'; + var Latitude = '#{config.welcome.Latitude}'; + var txkey = '#{config.welcome.key}' + var ipLoacation; + window.onload = () => { + welcometxmap() + }; + document.addEventListener("pjax:complete", welcometxmap()) .item-headline i.fas.fa-bullhorn.fa-shake span= _p('aside.card_announcement') .announcement_content!= theme.aside.card_announcement.content
|
- 在你的Hexo配置文件
_config.yml
中新增以下内容,并填写 key
以及你对应的经纬度
1 2 3 4
| welcome: key: longitude: Latitude:
|
1.0旧版 231006
JQ依赖版本【自定义侧边栏】
数据支持
注意:
如创建了多个应用,需在配额管理
- Key额度
里的 坐标转换 行政区划列表 行政区划子级查询 逆地址解析 IP定位 地点搜索 关键词输入提示 静态地图 进行分配额度
- 在
_config.butterfly.yml
主题配置文件中 inject
下的 bottom
引入 jquery.min.js
1 2 3 4 5 6 7 8 9
| ···
inject: head: - ··· bottom: - <script type="text/javascript" src="https://unpkg.zhimg.com/jquery@latest/dist/jquery.min.js"></script>
···
|
创建数据
- 创建
[blogRoot]/source/js/txmap.js
文件,并新增以下内容
注意: 在第 6行
和第 32行
需分别填入自己对应的Key和经纬度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| $.ajax({ type: 'get', url: 'https://apis.map.qq.com/ws/location/v1/ip', data: { key: '你获取的key', output: 'jsonp', }, dataType: 'jsonp', success: function (res) { ipLoacation = res; } }) function getDistance(e1, n1, e2, n2) { const R = 6371 const { sin, cos, asin, PI, hypot } = Math let getPoint = (e, n) => { e *= PI / 180 n *= PI / 180 return { x: cos(n) * cos(e), y: cos(n) * sin(e), z: sin(n) } }
let a = getPoint(e1, n1) let b = getPoint(e2, n2) let c = hypot(a.x - b.x, a.y - b.y, a.z - b.z) let r = asin(c / 2) * 2 * R return Math.round(r); }
function showWelcome() {
let dist = getDistance(经度, 纬度, ipLoacation.result.location.lng, ipLoacation.result.location.lat); let pos = ipLoacation.result.ad_info.nation; let ip; let posdesc; switch (ipLoacation.result.ad_info.nation) { case "日本": posdesc = "よろしく,一起去看樱花吗"; break; case "美国": posdesc = "Let us live in peace!"; break; case "英国": posdesc = "想同你一起夜乘伦敦眼"; break; case "俄罗斯": posdesc = "干了这瓶伏特加!"; break; case "法国": posdesc = "C'est La Vie"; break; case "德国": posdesc = "Die Zeit verging im Fluge."; break; case "澳大利亚": posdesc = "一起去大堡礁吧!"; break; case "加拿大": posdesc = "拾起一片枫叶赠予你"; break; case "中国": pos = ipLoacation.result.ad_info.province + " " + ipLoacation.result.ad_info.city + " " + ipLoacation.result.ad_info.district; ip = ipLoacation.result.ip; switch (ipLoacation.result.ad_info.province) { case "北京市": posdesc = "北——京——欢迎你~~~"; break; case "天津市": posdesc = "讲段相声吧"; break; case "河北省": posdesc = "山势巍巍成壁垒,天下雄关铁马金戈由此向,无限江山"; break; case "山西省": posdesc = "展开坐具长三尺,已占山河五百余"; break; case "内蒙古自治区": posdesc = "天苍苍,野茫茫,风吹草低见牛羊"; break; case "辽宁省": posdesc = "我想吃烤鸡架!"; break; case "吉林省": posdesc = "状元阁就是东北烧烤之王"; break; case "黑龙江省": posdesc = "很喜欢哈尔滨大剧院"; break; case "上海市": posdesc = "众所周知,中国只有两个城市"; break; case "江苏省": switch (ipLoacation.result.ad_info.city) { case "南京市": posdesc = "这是我挺想去的城市啦"; break; case "苏州市": posdesc = "上有天堂,下有苏杭"; break; default: posdesc = "散装是必须要散装的"; break; } break; case "浙江省": posdesc = "东风渐绿西湖柳,雁已还人未南归"; break; case "河南省": switch (ipLoacation.result.ad_info.city) { case "郑州市": posdesc = "豫州之域,天地之中"; break; case "南阳市": posdesc = "臣本布衣,躬耕于南阳此南阳非彼南阳!"; break; case "驻马店市": posdesc = "峰峰有奇石,石石挟仙气嵖岈山的花很美哦!"; break; case "开封市": posdesc = "刚正不阿包青天"; break; case "洛阳市": posdesc = "洛阳牡丹甲天下"; break; default: posdesc = "可否带我品尝河南烩面啦?"; break; } break; case "安徽省": posdesc = "蚌埠住了,芜湖起飞"; break; case "福建省": posdesc = "井邑白云间,岩城远带山"; break; case "江西省": posdesc = "落霞与孤鹜齐飞,秋水共长天一色"; break; case "山东省": posdesc = "遥望齐州九点烟,一泓海水杯中泻"; break; case "湖北省": switch (ipLoacation.result.ad_info.city) { case "黄冈市": posdesc = "红安将军县!辈出将才!"; break; default: posdesc = "来碗热干面~"; break; } break; case "湖南省": posdesc = "74751,长沙斯塔克"; break; case "广东省": switch (ipLoacation.result.ad_info.city) { case "广州市": posdesc = "看小蛮腰,喝早茶了嘛~"; break; case "深圳市": switch (ipLoacation.result.ad_info.district) { case "坪山区": posdesc = "好巧!博主也在坪山区生活喔~"; break; default: posdesc = "今天你996了嘛~"; break; } break; case "阳江市": posdesc = "阳春合水!博主家乡~ 欢迎来玩~"; break; default: posdesc = "来两斤福建人~"; break; } break; case "广西壮族自治区": posdesc = "桂林山水甲天下"; break; case "海南省": posdesc = "朝观日出逐白浪,夕看云起收霞光"; break; case "四川省": posdesc = "康康川妹子"; break; case "贵州省": posdesc = "茅台,学生,再塞200"; break; case "云南省": posdesc = "玉龙飞舞云缠绕,万仞冰川直耸天"; break; case "西藏自治区": posdesc = "躺在茫茫草原上,仰望蓝天"; break; case "陕西省": posdesc = "来份臊子面加馍"; break; case "甘肃省": posdesc = "羌笛何须怨杨柳,春风不度玉门关"; break; case "青海省": posdesc = "牛肉干和老酸奶都好好吃"; break; case "宁夏回族自治区": posdesc = "大漠孤烟直,长河落日圆"; break; case "新疆维吾尔自治区": posdesc = "驼铃古道丝绸路,胡马犹闻唐汉风"; break; case "台湾省": posdesc = "我在这头,大陆在那头"; break; case "香港特别行政区": posdesc = "永定贼有残留地鬼嚎,迎击光非岁玉"; break; case "澳门特别行政区": posdesc = "性感荷官,在线发牌"; break; default: posdesc = "带我去你的城市逛逛吧!"; break; } break; default: posdesc = "带我去你的国家逛逛吧"; break; }
let timeChange; let date = new Date(); if (date.getHours() >= 5 && date.getHours() < 11) timeChange = "<span class='welcome-time'>🌤️ 早上好,一日之计在于晨</span>"; else if (date.getHours() >= 11 && date.getHours() < 13) timeChange = "<span class='welcome-time'>☀️ 中午好,记得午休喔~</span>"; else if (date.getHours() >= 13 && date.getHours() < 17) timeChange = "<span class='welcome-time'>🕞 下午好,饮茶先啦!</span>"; else if (date.getHours() >= 17 && date.getHours() < 19) timeChange = "<span class='welcome-time'>🚶♂️ 即将下班,记得按时吃饭~</span>"; else if (date.getHours() >= 19 && date.getHours() < 24) timeChange = "<span class='welcome-time'>🌙 晚上好,夜生活嗨起来!</span>"; else timeChange = "<span class='welcome-time'>夜深了,早点休息,少熬夜</span>";
try { document.getElementById("welcome-info").innerHTML = `<span>热烈欢迎来自~</span><br><span><span style="color: var(--icat-card-welcome);font-weight: bold;">${pos}</span> 的喵友</span><br><span class="welcome-message">${posdesc}</span><br>您当前位置距博主约 <b><span style="color: var(--icat-card-welcome);font-weight: bold;">${dist}</span></b> 公里!<br><span>您的IP地址为:${ip}</span><br>${timeChange}`; } catch (err) { } } window.onload = showWelcome;
document.addEventListener('pjax:complete', showWelcome);
|
- 在
_config.butterfly.yml
主题配置文件中 inject
下的 bottom
引入 txmap.js
1 2 3 4 5 6 7 8 9
| ···
inject: head: - ··· bottom: - <script defer data-pjax src="/js/txmap.js"></script>
···
|
- 修改
[blogRoot]/source/_data/widget.yml
文件,新增 name
icon
html
项的内容
我这使用的是自定义侧边栏功能,相关问题请看 Butterfly - 自定义侧边栏
1 2 3 4 5 6 7 8
| top: - class_name: id_name: name: 来访者 icon: iconfont icat-visitor html: <div id="welcome-info"></div>
···
|
- 在自建的CSS文件
[blogRoot]/source/css/icat.css
里新增以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #welcome-info { overflow: hidden; --icat-card-welcome: #49b1f5; } #welcome-info .welcome-time { width: 100%; margin: 12px 0 0; display: inline-flex; } #welcome-info .welcome-message { width: 100%; margin-bottom: 8px; display: inline-flex; } #welcome-info .welcome-region { background: var(--icat-background); border: var(--style-border); color: var(--icat-fontcolor); border-radius: 8px; display: inline-flex; align-items: center; padding: 6px 10px; margin: 6px 0; }
|