better-staridc-MNBT
1<?php
2/**
3 * 管理员端 - 资源主机管理
4 *
5 * 查看所有由资源池开通 / 归属资源池的主机,可按资源池筛选。
6 * URL: admin/plugin.php?p=resource_pool&page=hosts[&pool_id=N][&kw=xxx]
7 *
8 * 归属关系来自 MN_plugin_respool.host_users(MN_zj.user 的 JSON 数组),
9 * 不依赖主机表的任何新增字段。
10 */
11if (!defined('IN_CRONLITE')) {
12 exit;
13}
14
15rp_ensure_schema();
16
17$pool_id = (int)($_GET['pool_id'] ?? 0);
18$kw = trim((string)($_GET['kw'] ?? ''));
19
20$pools = rp_list(1, 200)['list'];
21$cur_pool = $pool_id > 0 ? rp_get($pool_id) : null;
22if ($pool_id > 0 && !$cur_pool) {
23 $pool_id = 0;
24}
25
26$hosts = rp_all_pool_hosts($pool_id, $kw);
27$unbound = rp_unbound_hosts(500);
28$total_all = count(rp_host_user_map());
29
30$title = '资源主机管理';
31mnbt_admin_include('head');
32?>
33<link rel="stylesheet" href="<?= mnbt_asset_url('js/jquery-confirm/jquery-confirm.min.css') ?>">
34<script src="<?= mnbt_asset_url('js/jquery-confirm/jquery-confirm.min.js') ?>"></script>
35
36<div class="container-fluid p-t-15">
37 <div class="card">
38 <header class="card-header">
39 <div class="card-title" style="display:inline-block">
40 资源主机管理<?= $cur_pool ? ' - ' . htmlspecialchars($cur_pool['name'], ENT_QUOTES, 'UTF-8') : '' ?>
41 </div>
42 <small class="text-muted ml-2">
43 当前 <?= count($hosts) ?> 台<?= $pool_id > 0 ? '(全部资源池共 ' . (int)$total_all . ' 台)' : '' ?>
44 </small>
45 </header>
46 <div class="card-body">
47
48 <div class="callout callout-info">
49 <p class="small">
50 本页只列出<b>归属资源池</b>的主机。主机的开通 / 暂停 / 删除仍在「主机管理 → 主机列表」操作;
51 这里管理归属关系与查看各资源池的主机分布。
52 </p>
53 </div>
54
55 <form method="get" class="form-inline mb-3">
56 <input type="hidden" name="p" value="resource_pool">
57 <input type="hidden" name="page" value="hosts">
58 <label class="mr-2">资源池</label>
59 <select name="pool_id" class="form-control form-control-sm mr-2">
60 <option value="0">全部资源池</option>
61 <?php foreach ($pools as $p): ?>
62 <option value="<?= (int)$p['id'] ?>" <?= $pool_id === (int)$p['id'] ? 'selected' : '' ?>>
63 <?= htmlspecialchars($p['name'], ENT_QUOTES, 'UTF-8') ?>
64 </option>
65 <?php endforeach; ?>
66 </select>
67 <input type="text" name="kw" class="form-control form-control-sm mr-2" placeholder="主机账号 / 网站名 / 宝塔"
68 value="<?= htmlspecialchars($kw, ENT_QUOTES, 'UTF-8') ?>">
69 <button type="submit" class="btn btn-sm btn-primary">筛选</button>
70 <a href="plugin.php?p=resource_pool&page=hosts" class="btn btn-sm btn-outline-secondary ml-2">重置</a>
71 <button type="button" class="btn btn-sm btn-success ml-2" onclick="rpBindDialog()">
72 <i class="mdi mdi-link-variant"></i> 绑定已有主机
73 </button>
74 <button type="button" class="btn btn-sm btn-outline-warning ml-2" onclick="rpPrune()">
75 清理失效归属
76 </button>
77 <a href="plugin.php?p=resource_pool&page=list" class="btn btn-sm btn-outline-secondary ml-2">资源池列表</a>
78 </form>
79
80 <div class="table-responsive">
81 <table class="table table-bordered table-hover table-striped">
82 <thead>
83 <tr>
84 <th style="width:60px">ID</th>
85 <th>资源池</th>
86 <th>所属宝塔</th>
87 <th>网站名</th>
88 <th>主机账号</th>
89 <th>网页空间</th>
90 <th>数据库空间</th>
91 <th>流量</th>
92 <th>创建时间</th>
93 <th>到期时间</th>
94 <th>状态</th>
95 <th style="width:100px">操作</th>
96 </tr>
97 </thead>
98 <tbody>
99 <?php if (empty($hosts)): ?>
100 <tr><td colspan="12" class="text-center text-muted">
101 <?php if ($kw !== ''): ?>
102 没有匹配「<?= htmlspecialchars($kw, ENT_QUOTES, 'UTF-8') ?>」的资源池主机
103 <?php elseif ($pool_id > 0): ?>
104 该资源池还没有主机,可在「资源池列表」点「开通主机」,或用上方「绑定已有主机」
105 <?php else: ?>
106 还没有任何归属资源池的主机
107 <?php endif; ?>
108 </td></tr>
109 <?php else: ?>
110 <?php foreach ($hosts as $h):
111 $hu = (string)$h['user'];
112 $hpid = (int)($h['pool_id'] ?? 0);
113 ?>
114 <tr>
115 <td><?= (int)$h['id'] ?></td>
116 <td>
117 <?php if ($hpid > 0): ?>
118 <a href="plugin.php?p=resource_pool&page=hosts&pool_id=<?= $hpid ?>">
119 <span class="badge badge-info"><?= htmlspecialchars((string)$h['pool_name'], ENT_QUOTES, 'UTF-8') ?></span>
120 </a>
121 <?php else: ?>
122 <span class="text-muted">-</span>
123 <?php endif; ?>
124 </td>
125 <td><?= htmlspecialchars((string)$h['ssbt'], ENT_QUOTES, 'UTF-8') ?></td>
126 <td><?= htmlspecialchars((string)$h['sqldz'], ENT_QUOTES, 'UTF-8') ?></td>
127 <td><?= htmlspecialchars($hu, ENT_QUOTES, 'UTF-8') ?></td>
128 <td><?= (int)rp_json_max($h['hxa'] ?? '') ?>MB</td>
129 <td><?= (int)rp_json_max($h['hxb'] ?? '') ?>MB</td>
130 <td><?= (int)rp_json_max($h['llmax'] ?? '') ?>G</td>
131 <td class="small text-muted"><?= htmlspecialchars((string)$h['data'], ENT_QUOTES, 'UTF-8') ?></td>
132 <td><?= htmlspecialchars((string)$h['datae'], ENT_QUOTES, 'UTF-8') ?></td>
133 <td>
134 <?php if ((string)$h['qk'] === 'true'): ?>
135 <span class="badge badge-success">正常</span>
136 <?php else: ?>
137 <span class="badge badge-danger">暂停</span>
138 <?php endif; ?>
139 </td>
140 <td>
141 <button type="button" class="btn btn-xs btn-outline-danger"
142 onclick="rpUnbind('<?= htmlspecialchars($hu, ENT_QUOTES, 'UTF-8') ?>')"
143 title="解除归属,不删除主机">解除归属</button>
144 </td>
145 </tr>
146 <?php endforeach; ?>
147 <?php endif; ?>
148 </tbody>
149 </table>
150 </div>
151
152 <div class="panel-footer">
153 <span class="mdi mdi-information-outline"></span>
154 归属关系存在资源池表的 <code>host_users</code> 字段(按主机账号匹配),未改动主机表结构。
155 主机在「主机列表」被删除后,归属会自动失效不再统计,可点「清理失效归属」清掉残留记录。
156 </div>
157 </div>
158 </div>
159</div>
160
161<script type="text/javascript" src="<?= mnbt_asset_url('js/main.min.js') ?>"></script>
162<script type="text/javascript">
163var RP_POOLS = <?= json_encode(array_map(function ($p) {
164 return ['id' => (int)$p['id'], 'name' => (string)$p['name'], 'status' => (string)$p['status']];
165}, $pools), JSON_UNESCAPED_UNICODE) ?: '[]' ?>;
166
167var RP_UNBOUND = <?= json_encode(array_map(function ($h) {
168 return ['user' => (string)$h['user'], 'sqldz' => (string)$h['sqldz'], 'ssbt' => (string)$h['ssbt']];
169}, $unbound), JSON_UNESCAPED_UNICODE) ?: '[]' ?>;
170
171function rpEsc(s) { return $('<div>').text(s == null ? '' : s).html(); }
172function rpResp(resp) {
173 try { return typeof resp === 'string' ? JSON.parse(resp) : resp; } catch (e) { return {}; }
174}
175function rpOk(j) { return j && (j.qk === 1 || j.qk === '1'); }
176function rpMsg(j) { return (j && (j.msg || j.code)) || '未知返回'; }
177
178function rpUnbind(hostUser) {
179 $.confirm({
180 title: '解除资源池归属',
181 content: '确认解除主机「' + rpEsc(hostUser) + '」的资源池归属?<br/>主机<b>不会</b>被删除,其占用的配额会从资源池释放。',
182 type: 'orange',
183 buttons: {
184 confirm: {
185 text: '确认解除', btnClass: 'btn-warning',
186 action: function () {
187 msloading('正在处理...');
188 $.post('ajax.php', { gn: 'p_respool_unbind_host', host_user: hostUser }, function (resp) {
189 var j = rpResp(resp);
190 msloadingde();
191 if (rpOk(j)) { msalert(1, rpMsg(j), 1500); setTimeout(function () { location.reload(); }, 600); }
192 else { msalert(4, rpMsg(j), 4000); }
193 }).fail(function () { msloadingde(); msalert(4, '网络错误', 3000); });
194 }
195 },
196 cancel: { text: '取消' }
197 }
198 });
199}
200
201function rpBindDialog() {
202 if (!RP_POOLS.length) {
203 $.alert({ title: '没有资源池', content: '请先到「添加资源池」创建资源池。', type: 'red' });
204 return;
205 }
206 if (!RP_UNBOUND.length) {
207 $.alert({ title: '没有可绑定的主机', content: '所有主机都已归属资源池,或系统内还没有主机。', type: 'orange' });
208 return;
209 }
210 var pOpts = '';
211 for (var i = 0; i < RP_POOLS.length; i++) {
212 var dis = RP_POOLS[i].status === 'enabled' ? '' : '(已禁用)';
213 pOpts += '<option value="' + RP_POOLS[i].id + '">' + rpEsc(RP_POOLS[i].name) + dis + '</option>';
214 }
215 var hOpts = '';
216 for (var k = 0; k < RP_UNBOUND.length; k++) {
217 var h = RP_UNBOUND[k];
218 hOpts += '<option value="' + rpEsc(h.user) + '">' + rpEsc(h.user) + ' — ' + rpEsc(h.sqldz) + ' (' + rpEsc(h.ssbt) + ')</option>';
219 }
220 $.confirm({
221 title: '绑定已有主机到资源池',
222 columnClass: 'col-md-8 col-md-offset-2',
223 content: '<div class="form-group"><label>目标资源池</label>' +
224 '<select class="form-control" id="rp-bd-pool">' + pOpts + '</select></div>' +
225 '<div class="form-group"><label>选择主机(仅列出未归属任何资源池的主机)</label>' +
226 '<select class="form-control" id="rp-bd-host">' + hOpts + '</select></div>' +
227 '<p class="small text-muted">绑定后该主机的空间 / 流量会计入所选资源池的已用配额。</p>',
228 buttons: {
229 confirm: {
230 text: '确认绑定', btnClass: 'btn-success',
231 action: function () {
232 msloading('正在绑定...');
233 $.post('ajax.php', {
234 gn: 'p_respool_bind_host',
235 pool_id: $('#rp-bd-pool').val(),
236 host_user: $('#rp-bd-host').val()
237 }, function (resp) {
238 var j = rpResp(resp);
239 msloadingde();
240 if (rpOk(j)) {
241 msalert(1, rpMsg(j), 1500);
242 setTimeout(function () { location.reload(); }, 600);
243 } else {
244 $.alert({ title: '绑定失败', content: rpEsc(rpMsg(j)), type: 'red' });
245 }
246 }).fail(function () { msloadingde(); msalert(4, '网络错误', 3000); });
247 return true;
248 }
249 },
250 cancel: { text: '取消' }
251 }
252 });
253}
254
255function rpPrune() {
256 $.confirm({
257 title: '清理失效归属',
258 content: '把已删除主机的账号从资源池的归属列表里移除。<br/>不会影响任何现存主机。',
259 buttons: {
260 confirm: {
261 text: '开始清理', btnClass: 'btn-warning',
262 action: function () {
263 msloading('正在清理...');
264 $.post('ajax.php', { gn: 'p_respool_prune' }, function (resp) {
265 var j = rpResp(resp);
266 msloadingde();
267 if (rpOk(j)) { msalert(1, rpMsg(j), 2000); setTimeout(function () { location.reload(); }, 900); }
268 else { msalert(4, rpMsg(j), 4000); }
269 }).fail(function () { msloadingde(); msalert(4, '网络错误', 3000); });
270 }
271 },
272 cancel: { text: '取消' }
273 }
274 });
275}
276</script>