setting.html
4.75 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<script src="./js/common.js"></script>
<script src="./js/backbutton.js"></script>
<style>
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
color: #666;
font-size: 14px;
}
header {
background-color: #f2f2f2;
padding: 15px 10px;
font-weight: bold;
font-size: 16px;
}
main {
margin: 10px;
}
h3 {
font-size: 14px;
margin: 20px 0 5px 0;
font-weight: normal;
}
select,
input {
border: none;
border-bottom: 1px solid #eee;
width: 100%;
outline: none;
padding: 10px 0;
transition: all 0.3s;
color: #666;
border-radius: 0;
}
input:focus {
border-bottom: 1px solid #aaa;
}
button {
background-color: #f2f2f2;
color: #1d498d;
border: none;
width: 100%;
padding: 8px;
border-radius: 8px;
outline: none;
transition: all 0.3s;
}
button:active {
background-color: #d9dede;
}
.ControlGroup {
display: flex;
}
.ControlGroup>*:not(:last-child) {
margin-right: 10px;
}
.BottomFixed {
position: fixed;
bottom: 10px;
left: 10px;
right: 10px;
}
</style>
<script type="text/javascript">
let settingData;
document.addEventListener('plusready', function() {
settingData = getUrlData() || getDefaultUrlData();
updateDisplay();
addListeners();
});
function updateDisplay() {
const data = settingData;
document.getElementById('stProtocol').value = data.protocol;
document.getElementById('tbIp').value = data.url;
document.getElementById('tbPort').value = data.port;
}
function close() {
plus.webview.currentWebview().close();
}
function addListeners() {
document.getElementById('stProtocol').addEventListener('change', (event) => {
settingData.protocol = event.target.value;
});
document.getElementById('tbIp').addEventListener('change', (event) => {
settingData.url = event.target.value;
});
document.getElementById('tbPort').addEventListener('change', (event) => {
settingData.port = event.target.value;
});
// document.getElementById('btnSave').addEventListener('click', () => {
// let error = '';
// if (!settingData.url) {
// error = '请填写url';
// } else if (!settingData.port) {
// error = '请填写端口号';
// } else if (isNaN(settingData.port)) {
// error = '请输入正确的端口号'
// }
// if (error) {
// plus.nativeUI.toast(error);
// return;
// }
// setUrlData(settingData.protocol, settingData.url, settingData.port);
// plus.runtime.restart();
// });
document.getElementById('btnSave').addEventListener('click', () => {
let error = '';
if (!settingData.url) {
error = '请填写url';
} else if (!settingData.port) {
error = '请填写端口号';
} else if (isNaN(settingData.port)) {
error = '请输入正确的端口号';
}
if (error) {
plus.nativeUI.toast(error);
return;
}
const testUrl = `${settingData.protocol}://${settingData.url}:${settingData.port}/`;
plus.nativeUI.showWaiting('正在连接服务器...');
const xhr = new plus.net.XMLHttpRequest();
xhr.open('GET', testUrl, true);
xhr.timeout = 5000; // 5秒超时
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
plus.nativeUI.closeWaiting();
if (xhr.status === 0) {
// 网络层失败:DNS错误、连接拒绝、超时等
plus.nativeUI.toast('无法连接到服务器,请检查地址和网络');
} else {
// 成功收到 HTTP 响应(200/404/500 都算通)
setUrlData(settingData.protocol, settingData.url, settingData.port);
plus.nativeUI.toast('连接成功,正在重启应用...');
setTimeout(() => {
plus.runtime.restart();
}, 800);
}
}
};
// 注意:不再单独监听 onerror/ontimeout,因为 onreadystatechange 已覆盖
xhr.send();
});
document.getElementById('btnCancel').addEventListener('click', (event) => {
plus.runtime.restart();
});
}
</script>
</head>
<body>
<header>服务器设置</header>
<main>
<h3>协议</h3>
<select id='stProtocol'>
<option value="http">http</option>
<option value="https">https</option>
</select>
<h3>IP地址</h3>
<input id="tbIp" />
<h3>端口号</h3>
<input id="tbPort" />
<div class="ControlGroup BottomFixed">
<button id='btnCancel'>取消</button>
<button id='btnSave'>保存</button>
</div>
</main>
</body>
</html>