仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 个月前
1<?php
2/**
3 * 用户端 - 主机升级(配置升级 / 产品升降级)
4 */
5if (!defined('IN_CRONLITE')) {
6 exit;
7}
8$page_title = $page_title ?? '主机升级';
9$host = $host ?? null;
10$kind = $kind ?? 'config';
11$options = $options ?? ['ok' => false, 'msg' => ''];
12$optionsData = !empty($options['ok']) && is_array($options['data'])
13 ? $options['data'] : [];
14
15function zjf_config_options($data)
16{
17 $opts = $data['configoptions'] ?? null;
18 if (!is_array($opts)) {
19 return [];
20 }
21 $out = [];
22 foreach ($opts as $o) {
23 if (!is_array($o)) {
24 continue;
25 }
26 $id = $o['id'] ?? $o['configid'] ?? 0;
27 $name = (string)($o['name'] ?? $o['optionname'] ?? ('选项 ' . $id));
28 $choices = [];
29 $raw = $o['options'] ?? $o['values'] ?? null;
30 if (is_array($raw)) {
31 foreach ($raw as $c) {
32 if (is_array($c)) {
33 $v = $c['value'] ?? $c['id'] ?? '';
34 $l = (string)($c['name'] ?? $c['label'] ?? $v);
35 } else {
36 $v = $c;
37 $l = (string)$c;
38 }
39 if ($v !== '') {
40 $choices[] = ['value' => $v, 'label' => $l];
41 }
42 }
43 }
44 if ($id && $choices) {
45 $out[] = ['id' => $id, 'name' => $name, 'choices' => $choices];
46 }
47 }
48 return $out;
49}
50
51function zjf_upgrade_products($data)
52{
53 $list = $data['list'] ?? $data['products'] ?? null;
54 if (!is_array($list) && isset($data[0]) && is_array($data[0])) {
55 $list = $data;
56 }
57 if (!is_array($list)) {
58 return [];
59 }
60 $out = [];
61 foreach ($list as $p) {
62 if (!is_array($p)) {
63 continue;
64 }
65 $id = (int)($p['id'] ?? 0);
66 if ($id <= 0) {
67 continue;
68 }
69 $cycles = [];
70 $raw = $p['cycles'] ?? null;
71 if (is_array($raw)) {
72 foreach ($raw as $c) {
73 if (!is_array($c)) {
74 continue;
75 }
76 $bc = (string)($c['billingcycle'] ?? $c['cycle'] ?? '');
77 if ($bc === '') {
78 continue;
79 }
80 $cycles[$bc] = (string)($c['name'] ?? $bc);
81 }
82 }
83 $out[] = ['id' => $id, 'name' => (string)($p['name'] ?? ''), 'cycles' => $cycles];
84 }
85 return $out;
86}
87
88$configOptions = zjf_config_options($optionsData);
89$productList = zjf_upgrade_products($optionsData);
90ob_start();
91?>
92<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:16px;">
93 <h1 style="font-size:20px;color:#222;margin:0;">升级:<?= htmlspecialchars($host['name']) ?></h1>
94 <a class="layui-btn layui-btn-xs layui-btn-primary"
95 href="<?= zjmf_url('reserve/hosts/' . (int)$host['id']) ?>">返回主机详情</a>
96</div>
97
98<div class="zj-msg" id="zjf-msg"></div>
99
100<div class="layui-btn-group" style="margin-bottom:16px;">
101 <a class="layui-btn <?= $kind === 'config' ? '' : 'layui-btn-primary' ?>"
102 href="<?= zjmf_url('reserve/hosts/' . (int)$host['id'] . '/upgrade?kind=config') ?>">配置升级</a>
103 <a class="layui-btn <?= $kind === 'product' ? '' : 'layui-btn-primary' ?>"
104 href="<?= zjmf_url('reserve/hosts/' . (int)$host['id'] . '/upgrade?kind=product') ?>">产品升级</a>
105</div>
106
107<?php if (empty($options['ok'])): ?>
108 <div class="layui-card">
109 <div class="layui-card-body" style="color:#999;">
110 获取升级选项失败:<?= htmlspecialchars($options['msg'] ?? '未知错误') ?>
111 </div>
112 </div>
113<?php elseif ($kind === 'config'): ?>
114 <div class="layui-card">
115 <div class="layui-card-header">选择配置项</div>
116 <div class="layui-card-body">
117 <?php if ($configOptions === []): ?>
118 <p class="zj-muted">
119 未解析到可配置项(上游返回结构需联调确认,见 PRD Q1)。请在上游后台直接调整配置。
120 </p>
121 <?php else: ?>
122 <form id="zjf-cfg-form">
123 <?php foreach ($configOptions as $opt): ?>
124 <div class="form-group" style="margin-bottom:14px;">
125 <label style="display:block;font-weight:600;margin-bottom:6px;">
126 <?= htmlspecialchars($opt['name']) ?>
127 </label>
128 <select class="form-control zjf-cfg-select"
129 data-id="<?= (int)$opt['id'] ?>"
130 style="max-width:360px;">
131 <?php foreach ($opt['choices'] as $c): ?>
132 <option value="<?= htmlspecialchars($c['value'], ENT_QUOTES) ?>">
133 <?= htmlspecialchars($c['label']) ?>
134 </option>
135 <?php endforeach; ?>
136 </select>
137 </div>
138 <?php endforeach; ?>
139 <div id="zjf-price" class="zj-muted" style="margin:10px 0;"></div>
140 <button type="button" class="layui-btn" id="zjf-cfg-preview">试算差额</button>
141 <button type="button" class="layui-btn layui-btn-danger" id="zjf-cfg-confirm">确认升级</button>
142 </form>
143 <?php endif; ?>
144 </div>
145 </div>
146<?php else: ?>
147 <div class="layui-card">
148 <div class="layui-card-header">选择目标产品</div>
149 <div class="layui-card-body">
150 <?php if ($productList === []): ?>
151 <p class="zj-muted">
152 未解析到可升级产品(上游返回结构需联调确认,见 PRD Q1)。
153 </p>
154 <?php else: ?>
155 <form id="zjf-prod-form">
156 <?php foreach ($productList as $p): ?>
157 <label class="zj-choice" style="display:flex;align-items:center;gap:8px;margin-bottom:8px;">
158 <input type="radio" name="newpid" value="<?= (int)$p['id'] ?>" class="zjf-prod-pid">
159 <?= htmlspecialchars($p['name']) ?>
160 <select class="form-control form-control-sm zjf-prod-cycle" style="width:140px;">
161 <?php foreach ($p['cycles'] as $bc => $cn): ?>
162 <option value="<?= htmlspecialchars($bc, ENT_QUOTES) ?>">
163 <?= htmlspecialchars($cn) ?>
164 </option>
165 <?php endforeach; ?>
166 </select>
167 </label>
168 <?php endforeach; ?>
169 <div id="zjf-price" class="zj-muted" style="margin:10px 0;"></div>
170 <button type="button" class="layui-btn" id="zjf-prod-preview">试算差额</button>
171 <button type="button" class="layui-btn layui-btn-danger" id="zjf-prod-confirm">确认升级</button>
172 </form>
173 <?php endif; ?>
174 </div>
175 </div>
176<?php endif; ?>
177
178<script>
179(function () {
180 var hostId = '<?= (int)$host['id'] ?>';
181 var kind = '<?= $kind === 'product' ? 'product' : 'config' ?>';
182 var msg = document.getElementById('zjf-msg');
183
184 function show(text, ok) {
185 msg.textContent = text;
186 msg.className = 'zj-msg zj-msg-show ' + (ok ? 'zj-msg-success' : 'zj-msg-error');
187 }
188 function buildBody(preview) {
189 var body = new URLSearchParams();
190 body.append('host_id', hostId);
191 body.append('kind', kind);
192 body.append('preview', preview);
193 if (kind === 'config') {
194 var cfg = {};
195 document.querySelectorAll('.zjf-cfg-select').forEach(function (s) {
196 cfg[s.getAttribute('data-id')] = s.value;
197 });
198 body.append('config_json', JSON.stringify(cfg));
199 } else {
200 var pid = document.querySelector('input[name="newpid"]:checked');
201 if (!pid) return null;
202 var sel = pid.closest('.zj-choice').querySelector('.zjf-prod-cycle');
203 body.append('newpid', pid.value);
204 body.append('billingcycle', sel.value);
205 }
206 return body;
207 }
208 function post(preview, confirmText) {
209 var body = buildBody(preview);
210 if (!body) {
211 show('请先选择升级目标', false);
212 return;
213 }
214 if (confirmText && !confirm(confirmText)) {
215 return;
216 }
217 fetch('<?= zjmf_url('reserve/api/upgrade') ?>', {
218 method: 'POST',
219 headers: {'Content-Type': 'application/x-www-form-urlencoded'},
220 body: body.toString()
221 }).then(function (r) { return r.json(); }).then(function (res) {
222 var ok = res.code === 'ok' || res.success;
223 if (preview && ok) {
224 document.getElementById('zjf-price').textContent =
225 '本次升级需支付:¥' + res.price;
226 return;
227 }
228 show(res.code || '完成', ok);
229 if (ok) setTimeout(function () {
230 window.location.href = '<?= zjmf_url('reserve/hosts/' . (int)$host['id']) ?>';
231 }, 800);
232 }).catch(function () {
233 show('网络错误,请重试', false);
234 });
235 }
236
237 var previewId = kind === 'config' ? 'zjf-cfg-preview' : 'zjf-prod-preview';
238 var confirmId = kind === 'config' ? 'zjf-cfg-confirm' : 'zjf-prod-confirm';
239 var pBtn = document.getElementById(previewId);
240 var cBtn = document.getElementById(confirmId);
241 if (pBtn) pBtn.addEventListener('click', function () { post('1', ''); });
242 if (cBtn) cBtn.addEventListener('click', function () {
243 post('0', '确认按试算差额扣除余额并执行升级吗?');
244 });
245})();
246</script>
247<?php
248$content = ob_get_clean();
249include __DIR__ . '/layout.php';