仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 个月前
1<?php
2/**
3 * 连接配置页视图。
4 *
5 * @package MnbtWp
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10}
11
12$mnbtwp_nonce = wp_create_nonce( 'mnbtwp_admin' );
13?>
14<div class="wrap">
15 <h1><?php esc_html_e( 'MNBT 连接配置', 'wp-seller-plugin' ); ?></h1>
16 <p class="description">
17 <?php esc_html_e( '配置 MNBT(梦奈宝塔)节点连接信息。密钥在「系统设置 → API 密钥」与「宝塔列表 → ktmy」处获取。', 'wp-seller-plugin' ); ?>
18 </p>
19
20 <div style="display:flex;gap:24px;align-items:flex-start;flex-wrap:wrap;">
21 <!-- 左侧:表单 -->
22 <div style="min-width:380px;max-width:520px;flex:1;">
23 <form id="mnbtwp-provider-form" autocomplete="off">
24 <input type="hidden" id="mnbtwp-pid" value="0">
25 <table class="form-table" role="presentation">
26 <tr>
27 <th scope="row"><label for="mnbtwp-name"><?php esc_html_e( '名称', 'wp-seller-plugin' ); ?></label></th>
28 <td>
29 <input type="text" id="mnbtwp-name" class="regular-text" placeholder="<?php esc_attr_e( '如:主节点', 'wp-seller-plugin' ); ?>">
30 </td>
31 </tr>
32 <tr>
33 <th scope="row"><label for="mnbtwp-api-url"><?php esc_html_e( 'MNBT 站点地址', 'wp-seller-plugin' ); ?></label></th>
34 <td>
35 <input type="text" id="mnbtwp-api-url" class="regular-text" placeholder="https://mnbt.example.com">
36 <p class="description"><?php esc_html_e( 'MNBT 站点根地址,接口为 地址/api/api.php', 'wp-seller-plugin' ); ?></p>
37 </td>
38 </tr>
39 <tr>
40 <th scope="row"><label for="mnbtwp-btdh"><?php esc_html_e( '宝塔节点代号 (btdh)', 'wp-seller-plugin' ); ?></label></th>
41 <td>
42 <input type="text" id="mnbtwp-btdh" class="regular-text" placeholder="如 mn123456f">
43 <p class="description"><?php esc_html_e( 'MNBT 后台「宝塔列表」中的宝塔编号', 'wp-seller-plugin' ); ?></p>
44 </td>
45 </tr>
46 <tr>
47 <th scope="row"><label for="mnbtwp-mn-key"><?php esc_html_e( '系统 API 密钥 (mn_key)', 'wp-seller-plugin' ); ?></label></th>
48 <td>
49 <input type="password" id="mnbtwp-mn-key" class="regular-text" autocomplete="new-password">
50 <p class="description"><?php esc_html_e( 'MNBT 后台「系统设置 → API 密钥」', 'wp-seller-plugin' ); ?></p>
51 </td>
52 </tr>
53 <tr>
54 <th scope="row"><label for="mnbtwp-mn-keye"><?php esc_html_e( '节点调用密钥 (mn_keye)', 'wp-seller-plugin' ); ?></label></th>
55 <td>
56 <input type="password" id="mnbtwp-mn-keye" class="regular-text" autocomplete="new-password">
57 <p class="description"><?php esc_html_e( 'MNBT 后台「宝塔列表」ktmy 列 👁 显示的值(md5(ktmy.qmk))', 'wp-seller-plugin' ); ?></p>
58 </td>
59 </tr>
60 <tr>
61 <th scope="row"><label for="mnbtwp-mn-vs"><?php esc_html_e( 'API 版本号 (mn_vs)', 'wp-seller-plugin' ); ?></label></th>
62 <td>
63 <input type="number" id="mnbtwp-mn-vs" class="small-text" value="15" min="15">
64 <p class="description"><?php esc_html_e( '默认 15,MNBT 要求 ≥ 15', 'wp-seller-plugin' ); ?></p>
65 </td>
66 </tr>
67 <tr>
68 <th scope="row"><?php esc_html_e( '启用', 'wp-seller-plugin' ); ?></th>
69 <td>
70 <label>
71 <input type="checkbox" id="mnbtwp-qk" checked>
72 <?php esc_html_e( '启用该节点', 'wp-seller-plugin' ); ?>
73 </label>
74 </td>
75 </tr>
76 </table>
77 <p class="submit">
78 <button type="button" class="button button-primary" onclick="mnbtwpSaveProvider()"><?php esc_html_e( '保存', 'wp-seller-plugin' ); ?></button>
79 <button type="button" class="button" onclick="mnbtwpResetForm()"><?php esc_html_e( '重置', 'wp-seller-plugin' ); ?></button>
80 </p>
81 </form>
82 </div>
83
84 <!-- 右侧:节点列表 -->
85 <div style="min-width:520px;flex:2;">
86 <h2><?php esc_html_e( '已配置节点', 'wp-seller-plugin' ); ?></h2>
87 <?php if ( empty( $providers ) ) : ?>
88 <p class="description"><?php esc_html_e( '暂无配置,请在左侧添加。', 'wp-seller-plugin' ); ?></p>
89 <?php else : ?>
90 <table class="widefat striped">
91 <thead>
92 <tr>
93 <th><?php esc_html_e( 'ID', 'wp-seller-plugin' ); ?></th>
94 <th><?php esc_html_e( '名称', 'wp-seller-plugin' ); ?></th>
95 <th><?php esc_html_e( 'MNBT 地址', 'wp-seller-plugin' ); ?></th>
96 <th><?php esc_html_e( '节点代号', 'wp-seller-plugin' ); ?></th>
97 <th><?php esc_html_e( '状态', 'wp-seller-plugin' ); ?></th>
98 <th><?php esc_html_e( '操作', 'wp-seller-plugin' ); ?></th>
99 </tr>
100 </thead>
101 <tbody>
102 <?php foreach ( $providers as $mnbtwp_p ) : ?>
103 <tr>
104 <td><?php echo (int) $mnbtwp_p['id']; ?></td>
105 <td><?php echo esc_html( $mnbtwp_p['name'] ); ?></td>
106 <td><?php echo esc_html( $mnbtwp_p['api_url'] ); ?></td>
107 <td><code><?php echo esc_html( $mnbtwp_p['btdh'] ); ?></code></td>
108 <td>
109 <?php if ( 'true' === $mnbtwp_p['qk'] ) : ?>
110 <span class="dashicons dashicons-yes" style="color:#00a32a;" title="<?php esc_attr_e( '启用', 'wp-seller-plugin' ); ?>"></span>
111 <?php else : ?>
112 <span class="dashicons dashicons-no" style="color:#d63638;" title="<?php esc_attr_e( '禁用', 'wp-seller-plugin' ); ?>"></span>
113 <?php endif; ?>
114 </td>
115 <td>
116 <button type="button" class="button button-small" onclick="mnbtwpEditProvider(<?php echo (int) $mnbtwp_p['id']; ?>)"><?php esc_html_e( '编辑', 'wp-seller-plugin' ); ?></button>
117 <button type="button" class="button button-small" onclick="mnbtwpTestProvider(<?php echo (int) $mnbtwp_p['id']; ?>)"><?php esc_html_e( '测试连接', 'wp-seller-plugin' ); ?></button>
118 <button type="button" class="button button-small button-link-delete" onclick="mnbtwpDeleteProvider(<?php echo (int) $mnbtwp_p['id']; ?>)"><?php esc_html_e( '删除', 'wp-seller-plugin' ); ?></button>
119 </td>
120 </tr>
121 <?php endforeach; ?>
122 </tbody>
123 </table>
124 <?php endif; ?>
125 </div>
126 </div>
127</div>
128
129<script type="text/javascript">
130 var mnbtwpNonce = '<?php echo esc_js( $mnbtwp_nonce ); ?>';
131
132 function mnbtwpResetForm() {
133 document.getElementById('mnbtwp-pid').value = '0';
134 document.getElementById('mnbtwp-name').value = '';
135 document.getElementById('mnbtwp-api-url').value = '';
136 document.getElementById('mnbtwp-btdh').value = '';
137 document.getElementById('mnbtwp-mn-key').value = '';
138 document.getElementById('mnbtwp-mn-keye').value = '';
139 document.getElementById('mnbtwp-mn-vs').value = '15';
140 document.getElementById('mnbtwp-qk').checked = true;
141 }
142
143 function mnbtwpSaveProvider() {
144 var data = {
145 action: 'mnbtwp_save_provider',
146 nonce: mnbtwpNonce,
147 id: document.getElementById('mnbtwp-pid').value,
148 name: document.getElementById('mnbtwp-name').value,
149 api_url: document.getElementById('mnbtwp-api-url').value,
150 btdh: document.getElementById('mnbtwp-btdh').value,
151 mn_key: document.getElementById('mnbtwp-mn-key').value,
152 mn_keye: document.getElementById('mnbtwp-mn-keye').value,
153 mn_vs: document.getElementById('mnbtwp-mn-vs').value,
154 qk: document.getElementById('mnbtwp-qk').checked ? '1' : '0'
155 };
156 jQuery.post(ajaxurl, data, function (r) {
157 if (r && r.success) {
158 alert(r.data);
159 location.reload();
160 } else {
161 alert((r && r.data) ? r.data : '<?php echo esc_js( __( '保存失败', 'wp-seller-plugin' ) ); ?>');
162 }
163 }).fail(function () {
164 alert('<?php echo esc_js( __( '网络错误', 'wp-seller-plugin' ) ); ?>');
165 });
166 }
167
168 function mnbtwpEditProvider(id) {
169 jQuery.post(ajaxurl, { action: 'mnbtwp_get_provider', nonce: mnbtwpNonce, id: id }, function (r) {
170 if (!r || !r.success) {
171 alert((r && r.data) ? r.data : '<?php echo esc_js( __( '获取失败', 'wp-seller-plugin' ) ); ?>');
172 return;
173 }
174 var d = r.data;
175 document.getElementById('mnbtwp-pid').value = d.id;
176 document.getElementById('mnbtwp-name').value = d.name;
177 document.getElementById('mnbtwp-api-url').value = d.api_url;
178 document.getElementById('mnbtwp-btdh').value = d.btdh;
179 document.getElementById('mnbtwp-mn-key').value = d.mn_key;
180 document.getElementById('mnbtwp-mn-keye').value = d.mn_keye;
181 document.getElementById('mnbtwp-mn-vs').value = d.mn_vs;
182 document.getElementById('mnbtwp-qk').checked = (d.qk === 'true');
183 window.scrollTo({ top: 0, behavior: 'smooth' });
184 });
185 }
186
187 function mnbtwpTestProvider(id) {
188 jQuery.post(ajaxurl, { action: 'mnbtwp_test_provider', nonce: mnbtwpNonce, id: id }, function (r) {
189 if (r && r.success) {
190 alert(r.data);
191 } else {
192 alert((r && r.data) ? r.data : '<?php echo esc_js( __( '连接失败', 'wp-seller-plugin' ) ); ?>');
193 }
194 });
195 }
196
197 function mnbtwpDeleteProvider(id) {
198 if (!confirm('<?php echo esc_js( __( '确定删除该节点配置?不影响 MNBT 中已有主机。', 'wp-seller-plugin' ) ); ?>')) return;
199 jQuery.post(ajaxurl, { action: 'mnbtwp_delete_provider', nonce: mnbtwpNonce, id: id }, function (r) {
200 if (r && r.success) {
201 location.reload();
202 } else {
203 alert((r && r.data) ? r.data : '<?php echo esc_js( __( '删除失败', 'wp-seller-plugin' ) ); ?>');
204 }
205 });
206 }
207</script>