ipFileHooks_copy.js
4.48 KB
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
// serverConfig.json文件读写判断hook
import {
ref,
onMounted
} from 'vue'
export default function ipFileHooks() {
const serverIP = ref('https://store.keliuyun.com') // 默认值
const platType = ref('saas') // 默认平台
// 获取配置文件目录路径
const getConfigDir = () => plus.io.convertLocalFileSystemURL('_doc')
// 获取配置文件完整路径
const getConfigFilePath = () => `${getConfigDir()}/serverConfig.json`
// 检查配置文件是否存在
const checkConfigFile = () => {
return new Promise((resolve) => {
const filePath = getConfigFilePath()
plus.io.resolveLocalFileSystemURL(
filePath,
() => resolve(true), // 文件存在
() => resolve(false) // 文件不存在
)
})
}
// 读取配置文件内容
const readConfigFile = () => {
return new Promise((resolve, reject) => {
const filePath = getConfigFilePath()
plus.io.resolveLocalFileSystemURL(
filePath,
(entry) => {
entry.file((file) => {
const reader = new plus.io.FileReader()
reader.onloadend = (e) => {
try {
const config = JSON.parse(e.target.result)
// uni.setStorageSync(config.baseUrl) // 方便设置ip信息
resolve(config)
} catch (e) {
reject('配置文件解析失败')
}
}
reader.onerror = () => reject('文件读取失败')
reader.readAsText(file)
}, reject)
},
reject
)
})
}
// 写入配置文件
const writeConfigFile = (ip,platType,callback=()=>{}) => {
return new Promise((resolve, reject) => {
const config = {
baseUrl: ip,
platType
}
uni.setStorageSync('serverIp',ip)
uni.setStorageSync('platType',platType)
const content = JSON.stringify(config)
const filePath = getConfigFilePath()
// 先尝试直接写入
plus.io.resolveLocalFileSystemURL(
filePath,
(entry) => {
entry.createWriter(
(writer) => {
writer.onwrite = resolve
writer.onerror = reject
writer.write(content)
callback()
},
reject
)
},
// 文件不存在则创建
() => {
plus.io.resolveLocalFileSystemURL(
getConfigDir(),
(dirEntry) => {
dirEntry.getFile(
'serverConfig.json', {
create: true,
exclusive: false
},
(fileEntry) => {
fileEntry.createWriter(
(writer) => {
writer.onwrite = resolve
writer.onerror = reject
writer.write(content)
callback()
},
reject
)
},
reject
)
},
reject
)
}
)
})
}
// 初始化服务器配置
const initServerConfig = async () => {
try {
// 1. 检查配置文件是否存在
const fileExists = await checkConfigFile()
console.log(fileExists, '-s-s-s--s');
if (!fileExists) {
// 2. 如果不存在,创建文件并写入默认值
await writeConfigFile('https://store.keliuyun.com','saas')
uni.showToast({
title: '使用默认服务器地址',
icon: 'none'
})
} else {
// 3. 如果存在,读取文件内容并设置到输入框
const config = await readConfigFile()
if (config) {
serverIP.value = config.baseUrl
platType.value = config.platType
uni.setStorageSync('serverIp',config.baseUrl)
uni.setStorageSync('platType',config.platType)
}
}
} catch (error) {
console.error('初始化服务器配置失败:', error)
uni.showToast({
title: '配置初始化失败',
icon: 'none'
})
}
}
// 严格的IP验证
const validateIP = (ip) => {
const pattern =
/^(https?|ftp|ws|wss):\/\/(([a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[1-9][0-9]{0,4})?(\/.*)?$/;
return pattern.test(ip);
};
return {
serverIP,
platType,
validateIP,
writeConfigFile,
initServerConfig
}
}