仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 天前
1<?php
2if (!defined('IN_CRONLITE')) { exit; }
3$page_title = $page_title ?? '购买套餐';
4$plan = $plan ?? null; $methods = $methods ?? [];
5ob_start();
6?>
7<div class="layui-card">
8 <div class="layui-card-body" style="padding:28px;">
9 <div class="ly-msg" id="msg"></div>
10
11 <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:18px;">
12 <h1 style="font-size:20px;color:#222;margin:0;">购买:<?= htmlspecialchars($plan['name']) ?></h1>
13 <a class="layui-btn layui-btn-xs layui-btn-primary" href="<?= hosting_url('shop') ?>">返回套餐列表</a>
14 </div>
15
16 <ul class="hs-plan-spec" style="margin-bottom:20px;">
17 <li><span>网页空间</span><b><?= (int)$plan['spec_web'] ?> MB</b></li>
18 <li><span>数据库</span><b><?= (int)$plan['spec_sql'] ?> MB</b></li>
19 <li><span>流量</span><b><?= (int)$plan['spec_flow'] > 0 ? ((int)$plan['spec_flow'].' GB') : '不限' ?></b></li>
20 <li><span>域名绑定</span><b><?= (int)$plan['spec_domain'] ?> 个</b></li>
21 <li><span>开通节点</span><b><?= htmlspecialchars($plan['node'] ?: '—') ?></b></li>
22 </ul>
23
24 <form class="hs-order-form" id="orderForm">
25 <div class="layui-form-item">
26 <label class="layui-form-label">购买周期</label>
27 <div class="layui-input-block hs-form-choices" style="padding-top:8px;">
28 <?php
29 $enabled = hosting_plan_enabled_periods($plan);
30 foreach ($enabled as $p):
31 $cfg = hosting_periods()[$p];
32 $field = hosting_period_price_field($p);
33 $price = (int)($plan[$field] ?? 0);
34 ?>
35 <label class="hs-choice"><input type="radio" name="period" value="<?= htmlspecialchars($p, ENT_QUOTES) ?>"> <?= htmlspecialchars($cfg['label']) ?> ¥<?= hosting_format_cents($price) ?></label>
36 <?php endforeach; ?>
37 <?php if ($enabled === []): ?>
38 <span style="color:#999;">该套餐未设置购买周期</span>
39 <?php endif; ?>
40 </div>
41 </div>
42
43 <?php
44 $isFreePlan = true;
45 foreach ($enabled as $p) {
46 $field = hosting_period_price_field($p);
47 if ((int)($plan[$field] ?? 0) > 0) {
48 $isFreePlan = false;
49 break;
50 }
51 }
52 ?>
53 <?php if (!$isFreePlan): ?>
54 <?php if (!empty($methods)): ?>
55 <div class="layui-form-item">
56 <label class="layui-form-label">支付方式</label>
57 <div class="layui-input-block hs-form-choices" style="padding-top:6px;">
58 <?php foreach ($methods as $m): ?>
59 <label class="hs-choice"><input type="radio" name="type" value="<?= htmlspecialchars($m['plugin'].'__'.$m['method']) ?>" required> <?= htmlspecialchars($m['display_name'] ?: ($m['plugin'].' / '.$m['method'])) ?></label>
60 <?php endforeach; ?>
61 </div>
62 </div>
63 <?php else: ?>
64 <div class="layui-form-item"><div class="layui-input-block" style="color:#999;">暂无可用的支付方式</div></div>
65 <?php endif; ?>
66 <?php endif; ?>
67
68 <?php if ($isFreePlan || !empty($methods)): ?>
69 <div class="layui-form-item">
70 <div class="layui-input-block">
71 <button type="submit" class="layui-btn layui-btn-lg" id="submitBtn">确认购买</button>
72 </div>
73 </div>
74 <?php endif; ?>
75 </form>
76 </div>
77</div>
78
79<script>
80(function(){
81 // 确保单选框有默认选中项(layui 不渲染原生表单时也生效)
82 var periodRadios = document.querySelectorAll('input[name="period"]');
83 if (periodRadios.length && !document.querySelector('input[name="period"]:checked')) {
84 periodRadios[0].checked = true;
85 }
86 var typeRadios = document.querySelectorAll('input[name="type"]');
87 if (typeRadios.length && !document.querySelector('input[name="type"]:checked')) {
88 typeRadios[0].checked = true;
89 }
90
91 var form=document.getElementById('orderForm');if(!form)return;
92 var msg=document.getElementById('msg'),btn=document.getElementById('submitBtn');
93 function showMsg(text,type){msg.textContent=text;msg.className='ly-msg show '+(type==='success'?'ly-msg-success':'ly-msg-error');}
94 // 单选框选中态样式(兼容不支持 :has() 的浏览器)
95 function updateChoiceStyles(){
96 form.querySelectorAll('.hs-choice').forEach(function(l){l.classList.remove('active');});
97 form.querySelectorAll('input[type="radio"]:checked').forEach(function(r){
98 var p=r.closest('.hs-choice');if(p)p.classList.add('active');
99 });
100 }
101 form.addEventListener('change',updateChoiceStyles);
102 updateChoiceStyles();
103
104 form.addEventListener('submit',function(e){
105 e.preventDefault();if(!btn)return;btn.disabled=true;btn.textContent='正在创建订单...';msg.className='ly-msg';
106 var body=new URLSearchParams();
107 body.append('plan_id','<?=(int)$plan['id']?>');
108 var pc=form.querySelector('input[name="period"]:checked');
109 body.append('period',pc?pc.value:'');
110 var tc=form.querySelector('input[name="type"]:checked');
111 body.append('type',tc?tc.value:'');
112 fetch('<?=hosting_url('shop/api/create_order')?>',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:body.toString()})
113 .then(function(r){return r.json();})
114 .then(function(res){
115 if(res.html){document.open();document.write(res.html);document.close();}
116 else if(res.redirect){window.location.href=res.redirect;}
117 else{showMsg(res.code||'创建订单失败','error');btn.disabled=false;btn.textContent='确认购买';}
118 })
119 .catch(function(){showMsg('网络错误,请重试','error');btn.disabled=false;btn.textContent='确认购买';});
120 });
121})();
122</script>
123<?php
124$content = ob_get_clean();
125include __DIR__ . '/layout.php';