forked from jason5ng32/MyIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
161 lines (155 loc) · 5.06 KB
/
store.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
// store.js
import { defineStore } from 'pinia';
export const useMainStore = defineStore('main', {
state: () => ({
lang: 'en',
currentPath: {},
mountingStatus: {
ipcheck: false,
connectivity: false,
webrtc: false,
dnsleaktest: false,
speedtest: false,
advancedtools: false,
},
shell: {
ipv4Domain: import.meta.env.VITE_CURL_IPV4_DOMAIN,
ipv6Domain: import.meta.env.VITE_CURL_IPV6_DOMAIN,
ipv64Domain: import.meta.env.VITE_CURL_IPV64_DOMAIN,
},
loadingStatus: {
ipcheck: false,
connectivity: false,
webrtc: false,
dnsleaktest: false,
},
isDarkMode: false,
isMobile: false,
shouldRefreshEveryThing: false,
allIPs: [],
configs: {},
userPreferences: {},
alert: {
alertToShow: false,
alertStyle: "",
alertMessage: "",
alertTitle: "",
},
ipDBs: [
{ id: 0, text: 'IPCheck.ing', url: '/api/ipchecking?ip={{ip}}&lang={{lang}}', enabled: true },
{ id: 1, text: 'IPinfo.io', url: '/api/ipinfo?ip={{ip}}', enabled: true },
{ id: 2, text: 'IP-API.com', url: '/api/ipapicom?ip={{ip}}&lang={{lang}}', enabled: true },
{ id: 3, text: 'IPAPI.co', url: 'https://ipapi.co/{{ip}}/json/', enabled: true },
{ id: 4, text: 'KeyCDN', url: '/api/keycdn?ip={{ip}}', enabled: true },
{ id: 5, text: 'IP.SB', url: '/api/ipsb?ip={{ip}}', enabled: true },
{ id: 6, text: 'IPAPI.is', url: '/api/ipapiis?ip={{ip}}', enabled: true },
{ id: 7, text: 'MaxMind', url: '/api/maxmind?ip={{ip}}&lang={{lang}}', enabled: true },
],
}),
getters: {
activeSources: (state) => state.ipDBs.filter(db => db.enabled),
allHasLoaded: (state) => {
return Object.values(state.loadingStatus).every(status => status);
},
curlDomainsHadSet: (state) => {
return state.shell.ipv4Domain && state.shell.ipv6Domain && state.shell.ipv64Domain;
}
},
actions: {
// 设置当前 route 路径
setCurrentPath(path, id) {
this.currentPath = { path: path, id: id };
},
// 获取数据库的URL
getDbUrl(id, ip, lang) {
const db = this.ipDBs.find(d => d.id === id);
if (!db) return null;
return db.url.replace('{{ip}}', ip).replace('{{lang}}', lang || 'en');
},
// 从每个组件返回启动状态
setMountingStatus(key, value) {
this.mountingStatus[key] = value;
},
// 从每个组件返回加载状态
setLoadingStatus(key, value) {
this.loadingStatus[key] = value;
},
// 设置 Toast
setAlert(alertToShow, alertStyle, alertMessage, alertTitle) {
this.alert = { alertToShow, alertStyle, alertMessage, alertTitle };
},
// 从不同的组件收集合并 IP 数据
updateAllIPs(payload) {
const uniqueIPs = new Set([...this.allIPs, ...payload]);
this.allIPs = Array.from(uniqueIPs);
},
// 设置移动模式
setIsMobile(payload) {
this.isMobile = payload;
},
// App.vue 和 Nav.vue 的通信辅助函数
setRefreshEveryThing(payload) {
this.shouldRefreshEveryThing = payload;
},
// 设置黑暗模式
setDarkMode(value) {
this.isDarkMode = value;
},
// 设置 IP 数据库的使能状态
updateIPDBs({ id, enabled }) {
const index = this.ipDBs.findIndex(db => db.id === id);
if (index !== -1) {
this.ipDBs[index].enabled = enabled;
}
},
// 用户偏好设置
setPreferences(userPreferences) {
this.userPreferences = userPreferences;
localStorage.setItem('userPreferences', JSON.stringify(userPreferences));
},
// 更新用户偏好设置
updatePreference(key, value) {
this.userPreferences[key] = value;
localStorage.setItem('userPreferences', JSON.stringify(this.userPreferences));
},
// 从本地存储加载用户偏好设置
loadPreferences() {
const defaultPreferences = {
theme: 'auto', // auto, light, dark
connectivityAutoRefresh: false,
showMap: false,
simpleMode: false,
autoStart: true,
hideUnavailableIPStack: false,
popupConnectivityNotifications: true,
ipCardsToShow: 6,
ipGeoSource: 0,
lang: 'auto',
};
const storedPreferences = localStorage.getItem('userPreferences');
let preferencesToStore;
if (storedPreferences) {
const currentPreferences = JSON.parse(storedPreferences);
preferencesToStore = { ...defaultPreferences, ...currentPreferences };
} else {
preferencesToStore = defaultPreferences;
}
localStorage.setItem('userPreferences', JSON.stringify(preferencesToStore));
this.setPreferences(preferencesToStore);
},
// 从服务器获取配置
fetchConfigs() {
fetch('/api/configs')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
this.configs = data;
})
.catch(error => console.error('Fetching configs failed: ', error));
},
}
});