Skip to content

Commit

Permalink
ip查询优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ljxi committed Nov 11, 2023
1 parent 744de25 commit 2905c78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
</div>
</el-header>
<el-main>
<MainUI :isVisible="isVisible" :IPinfo="IPinfo" />
<MainUI :isVisible="isVisible"/>
<br>
<IPinfoUI :isVisible="isVisible" :IPinfo="IPinfo" />
<IPinfoUI :isVisible="isVisible"/>
</el-main>
<div style="height: fit-content;padding-bottom: 10px;">
<div style="width: fit-content;margin-left: auto;margin-right: auto;">
Expand All @@ -49,7 +49,6 @@ import { ElMessage } from 'element-plus'
import { toClipboard } from '@soerenmartius/vue3-clipboard'
const isVisible = ref(true)
const IPinfo = reactive({ globalInfo: null, localInfo: null })
let GoToHomePage = () => {
//window.open("https://netart.cn/")
toClipboard('463481772')
Expand Down
59 changes: 38 additions & 21 deletions src/components/IPinfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@

<script lang="ts" setup>
const props = defineProps({
isVisible: Boolean,
IPinfo: Object
isVisible: Boolean
})
import CountryCode from "../assets/CountryCode.json"
import { reactive } from 'vue'
import { reactive,watchEffect } from 'vue'
import { ElMessage } from 'element-plus'
import { toClipboard } from '@soerenmartius/vue3-clipboard'
const info=reactive({
Expand All @@ -42,7 +41,10 @@ const info=reactive({
localLay:0,
globalLay:0,
})
const ip_cache=reactive(JSON.parse(localStorage.getItem("ip_cache")||"{}"))
watchEffect(()=>{
localStorage.setItem("ip_cache",JSON.stringify(ip_cache))
})
const copy=(ip:string)=>{
toClipboard(ip)
ElMessage.success({
Expand All @@ -58,27 +60,45 @@ const provinceMatch=(str:string)=>{
}
async function getLocalIp() {
try {
const response = await fetch('https://api.mir6.com/api/ip_json', { referrerPolicy: 'no-referrer' });
let resp = await response.json();
let localInfo:any={
ip:resp['data']['ip'],
isp:resp['data']['isp'],
isChinaMainland:provinceMatch(resp['data']['province'])?true:false,
province:provinceMatch(resp['data']['province']),
city:resp['data']['city'].replace(/$/, ""),
area:resp['data']['districts'],
}
return localInfo
} catch (error) {
throw "获取本地IP失败"
}
}
async function cacheCtr(ip_addr:string){
let ret=ip_cache[ip_addr]
if(!ret || new Date().getTime()/1000-ret['time']>60*60*24*30){
ret=await getLocalIp()
ret['time']=new Date().getTime()/1000
ip_cache[ip_addr]=ret
}
return ret
}
async function watchLocalIp() {
if(props.isVisible){
try {
const response = await fetch('https://pubstatic.b0.upaiyun.com/?_upnode', { referrerPolicy: 'no-referrer' });
const response = await fetch('https://forge.speedtest.cn/api/location/info', { referrerPolicy: 'no-referrer' });
let resp = await response.json();
let localInfo:any={
ip:resp['remote_addr'],
isp:resp['remote_addr_location']['isp'],
isChinaMainland:provinceMatch(resp['remote_addr_location']['province'])?true:false,
province:provinceMatch(resp['remote_addr_location']['province']),
city:resp['remote_addr_location']['city'].replace(/$/, ""),
area:''
}
let localInfo:any=await cacheCtr(resp['ip'])
info['localInfo']=localInfo
if(props.IPinfo){
props.IPinfo['localInfo']=localInfo
}
} catch (error) {
info['localInfo']=null
}
}
setTimeout(getLocalIp,info['localInfo']?5000:1000)
setTimeout(watchLocalIp,info['localInfo']?5000:1000)
}
async function getGlobalIp() {
if(props.isVisible){
Expand All @@ -91,16 +111,13 @@ async function getGlobalIp() {
country:CountryCode[resp['country_code'] as keyof typeof CountryCode],
}
info['globalInfo']=globalInfo
if(props.IPinfo){
props.IPinfo['globalInfo']=globalInfo
}
} catch (error) {
info['globalInfo']=null
}
}
setTimeout(getGlobalIp, info['globalInfo']?5000:1000)
}
getLocalIp()
watchLocalIp()
getGlobalIp()
async function get_lay(url:string,type:'localLay'|'globalLay') {
if(props.isVisible){
Expand Down
6 changes: 2 additions & 4 deletions src/components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ import type { EChartsType } from "echarts";
import iosSound from "../assets/ios.mp3";
import andoridSound from "../assets/android.mp3";
const props = defineProps({
isVisible: Boolean,
IPinfo: Object
isVisible: Boolean
})
import { ElMessage } from 'element-plus'
import nodesJson from "../assets/nodes.json"
Expand Down Expand Up @@ -409,8 +408,7 @@ async function uploadLog() {
url: runUrl.value,
threadNum: threadNum.value,
used: num,
time: time,
IPinfo: props.IPinfo
time: time
})
});
resp = await resp.json()
Expand Down

0 comments on commit 2905c78

Please sign in to comment.