仰望星辰工作室

feat(hosting_shop): 支持多购买周期、免费套餐与周期可选配置

Y yang1145 提交于 2026-07-19 04:35 · c90e766 ·父提交 3877422
feat(hosting_shop): 支持多购买周期、免费套餐与周期可选配置

- 新增 6 种购买周期:月/季/半年/年/两年/三年

- 套餐表新增 quarter/half_year/two_year/three_year 价格字段与 enabled_periods

- 后台套餐编辑页可勾选启用周期并设置价格(0 元即免费)

- 用户端套餐页/下单页按 enabled_periods 动态显示

- 0 元订单直接开通,无需支付网关

- 自动 schema 升级,兼容旧数据
7 个文件变更 +258 -60 13172193298@163.com
•app_plugins/hosting_shop/bootstrap.php +34 -11
•app_plugins/hosting_shop/install.sql +5 -0
•app_plugins/hosting_shop/lib/hosting.php +128 -11
•app_plugins/hosting_shop/views/admin/plan_edit.php +34 -12
•app_plugins/hosting_shop/views/admin/plans.php +11 -2
•app_plugins/hosting_shop/views/order.php +35 -19
•app_plugins/hosting_shop/views/shop.php +11 -5
变更内容
diff --git a/app_plugins/hosting_shop/bootstrap.php b/app_plugins/hosting_shop/bootstrap.php
index 87a458a..9a0c200 100644
--- a/app_plugins/hosting_shop/bootstrap.php
+++ b/app_plugins/hosting_shop/bootstrap.php
@@ -16,6 +16,9 @@ if (!defined('IN_CRONLITE')) {
 
 require_once __DIR__ . '/lib/hosting.php';
 
+// 自动升级表结构(追加新周期价格字段等)
+hosting_upgrade_schema();
+
 mnbt_plugin_register('hosting_shop', [
 	'name' => '主机售卖',
 	'description' => '虚拟主机套餐售卖、自动开通、资产订单管理',
@@ -144,20 +147,28 @@ mnbt_register_route('POST', '/shop/api/create_order', function ($params, $ctx) {
 	if (!$plan || $plan['status'] !== 'active') {
 		hosting_json('套餐不存在或已下架');
 	}
-	if (!in_array($period, ['month', 'year'], true)) {
+	$periods = hosting_periods();
+	if (!isset($periods[$period])) {
 		hosting_json('请选择有效的购买周期');
 	}
-	$amount_cents = $period === 'year' ? (int)$plan['price_year_cents'] : (int)$plan['price_month_cents'];
-	if ($amount_cents <= 0) {
-		hosting_json('该套餐此周期不可购买');
+	$enabled = hosting_plan_enabled_periods($plan);
+	if (!in_array($period, $enabled, true)) {
+		hosting_json('该套餐不支持此购买周期');
+	}
+	$price_field = hosting_period_price_field($period);
+	$amount_cents = $price_field ? (int)($plan[$price_field] ?? 0) : 0;
+	if ($amount_cents < 0) {
+		hosting_json('该套餐此周期价格异常');
 	}
 	// 校验节点
 	if ($node === '' || !hosting_node_get($node)) {
 		hosting_json('请选择有效的开通节点');
 	}
-	// 校验支付方式
-	if ($type === '' || !function_exists('mnbt_pay_parse_type') || !mnbt_pay_parse_type($type)) {
-		hosting_json('请选择有效的支付方式');
+	// 非 0 元订单校验支付方式;0 元订单无需选择支付方式
+	if ($amount_cents > 0) {
+		if ($type === '' || !function_exists('mnbt_pay_parse_type') || !mnbt_pay_parse_type($type)) {
+			hosting_json('请选择有效的支付方式');
+		}
 	}
 
 	// 创建 hosting 订单
@@ -166,6 +177,17 @@ mnbt_register_route('POST', '/shop/api/create_order', function ($params, $ctx) {
 		hosting_json($create['msg'] ?? '创建订单失败');
 	}
 	$order_no = $create['order_no'];
+	$hosting_order_id = (int)$create['order_id'];
+
+	// 0 元免费套餐:直接标记 paid 并开通,无需创建 MN_dd 和调支付网关
+	if ($amount_cents === 0) {
+		hosting_order_set_status($hosting_order_id, 'paid', '免费套餐直接开通');
+		$open = hosting_open_host($hosting_order_id);
+		if (!$open['ok']) {
+			hosting_json('开通失败:' . ($open['msg'] ?? '未知错误'));
+		}
+		hosting_json('开通成功', ['redirect' => hosting_url('shop/assets')]);
+	}
 
 	// 创建 MN_dd 记录(支付系统订单)
 	$amount_yuan = (string)round($amount_cents / 100, 2);
@@ -176,7 +198,7 @@ mnbt_register_route('POST', '/shop/api/create_order', function ($params, $ctx) {
 		'node' => $node,
 		'amount' => $amount_cents,
 		'username' => $user['username'],
-		'order_id' => $create['order_id'],
+		'order_id' => $hosting_order_id,
 	], 256);
 	$ip = $_SERVER["REMOTE_ADDR"] ?? '127.0.0.1';
 
@@ -188,14 +210,15 @@ mnbt_register_route('POST', '/shop/api/create_order', function ($params, $ctx) {
 	);
 	if (!$ok) {
 		// 回滚 hosting 订单状态
-		hosting_order_set_status((int)$create['order_id'], 'cancelled', '支付订单创建失败');
+		hosting_order_set_status($hosting_order_id, 'cancelled', '支付订单创建失败');
 		hosting_json('支付订单创建失败,请稍后重试');
 	}
 
 	// 分发到支付插件
+	$period_label = $periods[$period]['label'];
 	$order_context = [
 		'out_trade_no' => $order_no,
-		'name' => '购买主机:' . $plan['name'] . '(' . ($period === 'year' ? '年付' : '月付') . ')',
+		'name' => '购买主机:' . $plan['name'] . '(' . $period_label . ')',
 		'money' => $amount_yuan,
 		'type' => $type,
 		'siteurl' => $siteurl,
@@ -204,7 +227,7 @@ mnbt_register_route('POST', '/shop/api/create_order', function ($params, $ctx) {
 
 	$html = mnbt_pay_dispatch_gateway($type, $order_context);
 	if ($html === false) {
-		hosting_order_set_status((int)$create['order_id'], 'cancelled', '支付方式不可用');
+		hosting_order_set_status($hosting_order_id, 'cancelled', '支付方式不可用');
 		hosting_json('支付方式不可用,请检查支付插件是否已启用');
 	}
 
diff --git a/app_plugins/hosting_shop/install.sql b/app_plugins/hosting_shop/install.sql
index 360381f..1219166 100644
--- a/app_plugins/hosting_shop/install.sql
+++ b/app_plugins/hosting_shop/install.sql
@@ -11,7 +11,12 @@ CREATE TABLE `MN_plugin_hosting_plan` (
   `spec_flow` int(11) NOT NULL DEFAULT '0' COMMENT '流量 GB(0=不限)',
   `spec_domain` int(11) NOT NULL DEFAULT '5' COMMENT '域名最大绑定数',
   `price_month_cents` int(11) NOT NULL DEFAULT '0' COMMENT '月付价格(分)',
+  `price_quarter_cents` int(11) NOT NULL DEFAULT '0' COMMENT '季付价格(分)',
+  `price_half_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '半年付价格(分)',
   `price_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '年付价格(分)',
+  `price_two_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '两年付价格(分)',
+  `price_three_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '三年付价格(分)',
+  `enabled_periods` varchar(255) NOT NULL DEFAULT '' COMMENT '允许的购买周期,逗号分隔(month,quarter,half_year,year,two_year,three_year)',
   `status` varchar(20) NOT NULL DEFAULT 'active' COMMENT 'active/inactive',
   `sort` int(11) NOT NULL DEFAULT '50' COMMENT '排序(小到大)',
   `created_at` varchar(50) NOT NULL DEFAULT '',
diff --git a/app_plugins/hosting_shop/lib/hosting.php b/app_plugins/hosting_shop/lib/hosting.php
index 81e4897..a61d07f 100644
--- a/app_plugins/hosting_shop/lib/hosting.php
+++ b/app_plugins/hosting_shop/lib/hosting.php
@@ -10,6 +10,95 @@ if (!defined('IN_CRONLITE')) {
 	exit;
 }
 
+/* ============================================================
+ *  周期 / schema 配置
+ * ============================================================ */
+
+/** 可用购买周期定义(months 用于计算到期时间)。 */
+function hosting_periods()
+{
+	return [
+		'month'       => ['label' => '月付', 'months' => 1],
+		'quarter'     => ['label' => '季付', 'months' => 3],
+		'half_year'   => ['label' => '半年付', 'months' => 6],
+		'year'        => ['label' => '年付', 'months' => 12],
+		'two_year'    => ['label' => '两年付', 'months' => 24],
+		'three_year'  => ['label' => '三年付', 'months' => 36],
+	];
+}
+
+/** 周期到数据库价格字段的映射。 */
+function hosting_period_price_field($period)
+{
+	$map = [
+		'month'      => 'price_month_cents',
+		'quarter'    => 'price_quarter_cents',
+		'half_year'  => 'price_half_year_cents',
+		'year'       => 'price_year_cents',
+		'two_year'   => 'price_two_year_cents',
+		'three_year' => 'price_three_year_cents',
+	];
+	return $map[$period] ?? '';
+}
+
+/** 获取套餐启用的购买周期列表。 */
+function hosting_plan_enabled_periods($plan)
+{
+	$periods = hosting_periods();
+	$enabled = [];
+	$raw = isset($plan['enabled_periods']) ? trim((string)$plan['enabled_periods']) : '';
+	if ($raw === '') {
+		// 兼容旧数据:只要对应价格 > 0 就认为启用
+		foreach (array_keys($periods) as $p) {
+			$field = hosting_period_price_field($p);
+			if ($field && (int)($plan[$field] ?? 0) > 0) {
+				$enabled[] = $p;
+			}
+		}
+		return $enabled;
+	}
+	foreach (explode(',', $raw) as $p) {
+		$p = trim($p);
+		if (isset($periods[$p])) {
+			$enabled[] = $p;
+		}
+	}
+	return $enabled;
+}
+
+/** 自动升级套餐表结构(对已经安装的插件追加新字段)。 */
+function hosting_upgrade_schema()
+{
+	global $DB;
+	if (!isset($DB)) {
+		return;
+	}
+	$cols = $DB->get_all_prepare("SHOW COLUMNS FROM MN_plugin_hosting_plan") ?: [];
+	$existing = [];
+	foreach ($cols as $c) {
+		$existing[] = $c['Field'];
+	}
+	$toAdd = [];
+	if (!in_array('price_quarter_cents', $existing, true)) {
+		$toAdd[] = "ADD COLUMN `price_quarter_cents` int(11) NOT NULL DEFAULT '0' COMMENT '季付价格(分)'";
+	}
+	if (!in_array('price_half_year_cents', $existing, true)) {
+		$toAdd[] = "ADD COLUMN `price_half_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '半年付价格(分)'";
+	}
+	if (!in_array('price_two_year_cents', $existing, true)) {
+		$toAdd[] = "ADD COLUMN `price_two_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '两年付价格(分)'";
+	}
+	if (!in_array('price_three_year_cents', $existing, true)) {
+		$toAdd[] = "ADD COLUMN `price_three_year_cents` int(11) NOT NULL DEFAULT '0' COMMENT '三年付价格(分)'";
+	}
+	if (!in_array('enabled_periods', $existing, true)) {
+		$toAdd[] = "ADD COLUMN `enabled_periods` varchar(255) NOT NULL DEFAULT '' COMMENT '允许的购买周期,逗号分隔'";
+	}
+	if (!empty($toAdd)) {
+		$DB->query("ALTER TABLE MN_plugin_hosting_plan " . implode(', ', $toAdd));
+	}
+}
+
 /* ============================================================
  *  URL / 渲染辅助
  * ============================================================ */
@@ -151,6 +240,16 @@ function hosting_plan_save($data)
 {
 	global $DB, $date;
 	$now = $date ?: date('Y-m-d H:i:s');
+
+	$periods = hosting_periods();
+	$enabled = [];
+	$rawEnabled = isset($data['enabled_periods']) && is_array($data['enabled_periods']) ? $data['enabled_periods'] : [];
+	foreach ($rawEnabled as $p) {
+		if (isset($periods[$p])) {
+			$enabled[] = $p;
+		}
+	}
+
 	$fields = [
 		'name' => trim((string)($data['name'] ?? '')),
 		'description' => trim((string)($data['description'] ?? '')),
@@ -160,7 +259,12 @@ function hosting_plan_save($data)
 		'spec_flow' => max(0, (int)($data['spec_flow'] ?? 0)),
 		'spec_domain' => max(0, (int)($data['spec_domain'] ?? 0)),
 		'price_month_cents' => max(0, (int)($data['price_month_cents'] ?? 0)),
+		'price_quarter_cents' => max(0, (int)($data['price_quarter_cents'] ?? 0)),
+		'price_half_year_cents' => max(0, (int)($data['price_half_year_cents'] ?? 0)),
 		'price_year_cents' => max(0, (int)($data['price_year_cents'] ?? 0)),
+		'price_two_year_cents' => max(0, (int)($data['price_two_year_cents'] ?? 0)),
+		'price_three_year_cents' => max(0, (int)($data['price_three_year_cents'] ?? 0)),
+		'enabled_periods' => implode(',', $enabled),
 		'status' => ($data['status'] ?? 'active') === 'inactive' ? 'inactive' : 'active',
 		'sort' => max(0, (int)($data['sort'] ?? 50)),
 		'updated_at' => $now,
@@ -168,18 +272,23 @@ function hosting_plan_save($data)
 	if ($fields['name'] === '') {
 		return '套餐名称不能为空';
 	}
+	// 至少启用一个有效周期
+	if ($enabled === []) {
+		return '请至少选择一个购买周期';
+	}
+
 	$id = (int)($data['id'] ?? 0);
 	if ($id > 0) {
 		$ok = $DB->query_prepare(
-			"UPDATE MN_plugin_hosting_plan SET name=?, description=?, spec_type=?, spec_web=?, spec_sql=?, spec_flow=?, spec_domain=?, price_month_cents=?, price_year_cents=?, status=?, sort=?, updated_at=? WHERE id=?",
-			[$fields['name'], $fields['description'], $fields['spec_type'], $fields['spec_web'], $fields['spec_sql'], $fields['spec_flow'], $fields['spec_domain'], $fields['price_month_cents'], $fields['price_year_cents'], $fields['status'], $fields['sort'], $fields['updated_at'], $id]
+			"UPDATE MN_plugin_hosting_plan SET name=?, description=?, spec_type=?, spec_web=?, spec_sql=?, spec_flow=?, spec_domain=?, price_month_cents=?, price_quarter_cents=?, price_half_year_cents=?, price_year_cents=?, price_two_year_cents=?, price_three_year_cents=?, enabled_periods=?, status=?, sort=?, updated_at=? WHERE id=?",
+			[$fields['name'], $fields['description'], $fields['spec_type'], $fields['spec_web'], $fields['spec_sql'], $fields['spec_flow'], $fields['spec_domain'], $fields['price_month_cents'], $fields['price_quarter_cents'], $fields['price_half_year_cents'], $fields['price_year_cents'], $fields['price_two_year_cents'], $fields['price_three_year_cents'], $fields['enabled_periods'], $fields['status'], $fields['sort'], $fields['updated_at'], $id]
 		);
 		return $ok ? true : '更新失败';
 	}
 	$fields['created_at'] = $now;
 	$ok = $DB->query_prepare(
-		"INSERT INTO MN_plugin_hosting_plan (name, description, spec_type, spec_web, spec_sql, spec_flow, spec_domain, price_month_cents, price_year_cents, status, sort, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
-		[$fields['name'], $fields['description'], $fields['spec_type'], $fields['spec_web'], $fields['spec_sql'], $fields['spec_flow'], $fields['spec_domain'], $fields['price_month_cents'], $fields['price_year_cents'], $fields['status'], $fields['sort'], $fields['created_at'], $fields['updated_at']]
+		"INSERT INTO MN_plugin_hosting_plan (name, description, spec_type, spec_web, spec_sql, spec_flow, spec_domain, price_month_cents, price_quarter_cents, price_half_year_cents, price_year_cents, price_two_year_cents, price_three_year_cents, enabled_periods, status, sort, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
+		[$fields['name'], $fields['description'], $fields['spec_type'], $fields['spec_web'], $fields['spec_sql'], $fields['spec_flow'], $fields['spec_domain'], $fields['price_month_cents'], $fields['price_quarter_cents'], $fields['price_half_year_cents'], $fields['price_year_cents'], $fields['price_two_year_cents'], $fields['price_three_year_cents'], $fields['enabled_periods'], $fields['status'], $fields['sort'], $fields['created_at'], $fields['updated_at']]
 	);
 	return $ok ? true : '新增失败';
 }
@@ -288,10 +397,18 @@ function hosting_order_list_all($page = 1, $per_page = 30, $filters = [])
 function hosting_order_create($user, $plan, $period, $node)
 {
 	global $DB, $date;
-	$period = $period === 'year' ? 'year' : 'month';
-	$amount_cents = $period === 'year' ? (int)$plan['price_year_cents'] : (int)$plan['price_month_cents'];
-	if ($amount_cents <= 0) {
-		return ['ok' => false, 'msg' => '该套餐此周期不可购买(价格为 0)'];
+	$periods = hosting_periods();
+	if (!isset($periods[$period])) {
+		return ['ok' => false, 'msg' => '无效的购买周期'];
+	}
+	$enabled = hosting_plan_enabled_periods($plan);
+	if (!in_array($period, $enabled, true)) {
+		return ['ok' => false, 'msg' => '该套餐不支持此购买周期'];
+	}
+	$field = hosting_period_price_field($period);
+	$amount_cents = $field ? (int)($plan[$field] ?? 0) : 0;
+	if ($amount_cents < 0) {
+		return ['ok' => false, 'msg' => '该套餐此周期价格异常'];
 	}
 	if (!hosting_node_get($node)) {
 		return ['ok' => false, 'msg' => '所选节点不存在'];
@@ -447,10 +564,10 @@ function hosting_open_host($order_id)
 	$mrwww = $node['btos'] == '1' ? $conf['hxu'] : $conf['hxu'];
 	$mrml = ($node['btos'] == '1' ? $conf['hxi'] : $conf['hxo']) . '/' . $btserw;
 
-	// 计算到期时间
+	// 计算到期时间(支持月/季/半年/年/两年/三年)
 	$now_ts = time();
-	$period = $order['period'] === 'year' ? 'year' : 'month';
-	$expire_ts = strtotime($period === 'year' ? '+1 year' : '+1 month', $now_ts);
+	$periodCfg = hosting_periods()[($order['period'] ?? 'month')] ?? hosting_periods()['month'];
+	$expire_ts = strtotime('+' . (int)$periodCfg['months'] . ' months', $now_ts);
 	$datae = date('Y-m-d', $expire_ts);
 
 	// 调用宝塔 API 开通
diff --git a/app_plugins/hosting_shop/views/admin/plan_edit.php b/app_plugins/hosting_shop/views/admin/plan_edit.php
index 8070722..2be0b18 100644
--- a/app_plugins/hosting_shop/views/admin/plan_edit.php
+++ b/app_plugins/hosting_shop/views/admin/plan_edit.php
@@ -20,11 +20,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['act'] ?? '') === 'save') {
 		'spec_sql' => (int)($_POST['spec_sql'] ?? 0),
 		'spec_flow' => (int)($_POST['spec_flow'] ?? 0),
 		'spec_domain' => (int)($_POST['spec_domain'] ?? 0),
-		'price_month_cents' => (int)round((float)($_POST['price_month'] ?? 0) * 100),
-		'price_year_cents' => (int)round((float)($_POST['price_year'] ?? 0) * 100),
+		'enabled_periods' => isset($_POST['enabled_periods']) && is_array($_POST['enabled_periods']) ? $_POST['enabled_periods'] : [],
 		'status' => $_POST['status'] ?? 'active',
 		'sort' => (int)($_POST['sort'] ?? 50),
 	];
+	foreach (hosting_periods() as $p => $cfg) {
+		$field = hosting_period_price_field($p);
+		$data[$field] = (int)round((float)($_POST['price'][$p] ?? 0) * 100);
+	}
 	$r = hosting_plan_save($data);
 	if ($r === true) {
 		header('Location: ' . hosting_admin_url('plans'));
@@ -91,20 +94,39 @@ mnbt_admin_include('head');
 						<input type="number" name="spec_domain" class="form-control" required min="0" value="<?= (int)($plan['spec_domain'] ?? 5) ?>">
 					</div>
 				</div>
-				<div class="form-row">
-					<div class="form-group col-md-4">
-						<label>月付价格 (元)</label>
-						<input type="number" name="price_month" class="form-control" step="0.01" min="0" value="<?= htmlspecialchars(isset($plan['price_month_cents']) ? hosting_format_cents($plan['price_month_cents']) : '0.00', ENT_QUOTES) ?>">
-					</div>
-					<div class="form-group col-md-4">
-						<label>年付价格 (元)</label>
-						<input type="number" name="price_year" class="form-control" step="0.01" min="0" value="<?= htmlspecialchars(isset($plan['price_year_cents']) ? hosting_format_cents($plan['price_year_cents']) : '0.00', ENT_QUOTES) ?>">
+				<div class="form-group row">
+					<label class="col-sm-3 col-form-label">购买周期与价格 (元)</label>
+					<div class="col-sm-9">
+						<div class="form-row">
+							<?php
+								$enabledPeriods = hosting_plan_enabled_periods($plan ?: []);
+								foreach (hosting_periods() as $p => $cfg):
+									$field = hosting_period_price_field($p);
+									$checked = in_array($p, $enabledPeriods, true) ? 'checked' : '';
+									$price = isset($plan[$field]) ? hosting_format_cents((int)$plan[$field]) : '0.00';
+							?>
+								<div class="form-group col-md-4">
+									<div class="input-group">
+										<div class="input-group-prepend">
+											<div class="input-group-text">
+												<input type="checkbox" name="enabled_periods[]" value="<?= htmlspecialchars($p, ENT_QUOTES) ?>" <?= $checked ?>>
+											</div>
+										</div>
+										<span class="input-group-text" style="min-width:60px;justify-content:center;"><?= htmlspecialchars($cfg['label']) ?></span>
+										<input type="number" name="price[<?= htmlspecialchars($p, ENT_QUOTES) ?>]" class="form-control" step="0.01" min="0" value="<?= htmlspecialchars($price, ENT_QUOTES) ?>">
+									</div>
+								</div>
+							<?php endforeach; ?>
+						</div>
+						<small class="form-text text-muted">勾选并填写价格即启用该周期;价格填 0 表示免费。</small>
 					</div>
-					<div class="form-group col-md-2">
+				</div>
+				<div class="form-row">
+					<div class="form-group col-md-6">
 						<label>排序</label>
 						<input type="number" name="sort" class="form-control" min="0" value="<?= (int)($plan['sort'] ?? 50) ?>">
 					</div>
-					<div class="form-group col-md-2">
+					<div class="form-group col-md-6">
 						<label>状态</label>
 						<select name="status" class="form-control">
 							<option value="active" <?= (($plan['status'] ?? 'active') === 'active') ? 'selected' : '' ?>>上架</option>
diff --git a/app_plugins/hosting_shop/views/admin/plans.php b/app_plugins/hosting_shop/views/admin/plans.php
index ae8bc57..92bd74a 100644
--- a/app_plugins/hosting_shop/views/admin/plans.php
+++ b/app_plugins/hosting_shop/views/admin/plans.php
@@ -47,7 +47,7 @@ mnbt_admin_include('head');
 								<th>类型</th>
 								<th>网页/数据库/流量</th>
 								<th>域名数</th>
-								<th>月付/年付</th>
+								<th>价格(启用周期)</th>
 								<th>状态</th>
 								<th>排序</th>
 								<th style="width:180px">操作</th>
@@ -55,13 +55,22 @@ mnbt_admin_include('head');
 						</thead>
 						<tbody>
 							<?php foreach ($plans as $p): ?>
+								<?php $enabled = hosting_plan_enabled_periods($p); ?>
 								<tr>
 									<td><?= (int)$p['id'] ?></td>
 									<td><?= htmlspecialchars($p['name'], ENT_QUOTES) ?></td>
 									<td><?= (int)$p['spec_type'] === 1 ? 'CDN' : '主机' ?></td>
 									<td><?= (int)$p['spec_web'] ?>MB / <?= (int)$p['spec_sql'] ?>MB / <?= (int)$p['spec_flow'] > 0 ? ((int)$p['spec_flow'] . 'GB') : '不限' ?></td>
 									<td><?= (int)$p['spec_domain'] ?></td>
-									<td>¥<?= htmlspecialchars(hosting_format_cents($p['price_month_cents'])) ?> / ¥<?= htmlspecialchars(hosting_format_cents($p['price_year_cents'])) ?></td>
+									<td>
+										<?php foreach ($enabled as $periodKey): ?>
+											<?php $cfg = hosting_periods()[$periodKey]; $field = hosting_period_price_field($periodKey); $price = (int)($p[$field] ?? 0); ?>
+											<span class="badge badge-light" style="margin-right:4px;"><?= htmlspecialchars($cfg['label']) ?> ¥<?= htmlspecialchars(hosting_format_cents($price)) ?></span>
+										<?php endforeach; ?>
+										<?php if ($enabled === []): ?>
+											<span class="text-muted">未启用周期</span>
+										<?php endif; ?>
+									</td>
 									<td>
 										<?php if ($p['status'] === 'active'): ?>
 											<span class="badge badge-success">上架</span>
diff --git a/app_plugins/hosting_shop/views/order.php b/app_plugins/hosting_shop/views/order.php
index dbbb3fa..49ed808 100644
--- a/app_plugins/hosting_shop/views/order.php
+++ b/app_plugins/hosting_shop/views/order.php
@@ -25,14 +25,17 @@ ob_start();
       <div class="layui-form-item">
         <label class="layui-form-label">购买周期</label>
         <div class="layui-input-block hs-form-choices" style="padding-top:8px;">
-          <?php if ((int)$plan['price_month_cents'] > 0): ?>
-            <label class="hs-choice"><input type="radio" name="period" value="month" <?= (int)$plan['price_year_cents']<=0?'checked':'' ?>> 月付 ¥<?= hosting_format_cents($plan['price_month_cents']) ?></label>
-          <?php endif; ?>
-          <?php if ((int)$plan['price_year_cents'] > 0): ?>
-            <label class="hs-choice"><input type="radio" name="period" value="year" <?= (int)$plan['price_month_cents']<=0?'checked':'' ?>> 年付 ¥<?= hosting_format_cents($plan['price_year_cents']) ?></label>
-          <?php endif; ?>
-          <?php if ((int)$plan['price_month_cents'] <= 0 && (int)$plan['price_year_cents'] <= 0): ?>
-            <span style="color:#999;">该套餐未设置价格</span>
+          <?php
+            $enabled = hosting_plan_enabled_periods($plan);
+            foreach ($enabled as $p):
+              $cfg = hosting_periods()[$p];
+              $field = hosting_period_price_field($p);
+              $price = (int)($plan[$field] ?? 0);
+          ?>
+            <label class="hs-choice"><input type="radio" name="period" value="<?= htmlspecialchars($p, ENT_QUOTES) ?>"> <?= htmlspecialchars($cfg['label']) ?> ¥<?= hosting_format_cents($price) ?></label>
+          <?php endforeach; ?>
+          <?php if ($enabled === []): ?>
+            <span style="color:#999;">该套餐未设置购买周期</span>
           <?php endif; ?>
         </div>
       </div>
@@ -53,20 +56,32 @@ ob_start();
         </div>
       </div>
 
-      <?php if (!empty($methods)): ?>
-        <div class="layui-form-item">
-          <label class="layui-form-label">支付方式</label>
-          <div class="layui-input-block hs-form-choices" style="padding-top:6px;">
-            <?php foreach ($methods as $m): ?>
-              <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>
-            <?php endforeach; ?>
+      <?php
+        $isFreePlan = true;
+        foreach ($enabled as $p) {
+          $field = hosting_period_price_field($p);
+          if ((int)($plan[$field] ?? 0) > 0) {
+            $isFreePlan = false;
+            break;
+          }
+        }
+      ?>
+      <?php if (!$isFreePlan): ?>
+        <?php if (!empty($methods)): ?>
+          <div class="layui-form-item">
+            <label class="layui-form-label">支付方式</label>
+            <div class="layui-input-block hs-form-choices" style="padding-top:6px;">
+              <?php foreach ($methods as $m): ?>
+                <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>
+              <?php endforeach; ?>
+            </div>
           </div>
-        </div>
-      <?php else: ?>
-        <div class="layui-form-item"><div class="layui-input-block" style="color:#999;">暂无可用的支付方式</div></div>
+        <?php else: ?>
+          <div class="layui-form-item"><div class="layui-input-block" style="color:#999;">暂无可用的支付方式</div></div>
+        <?php endif; ?>
       <?php endif; ?>
 
-      <?php if (!empty($nodes) && !empty($methods)): ?>
+      <?php if (!empty($nodes) && ($isFreePlan || !empty($methods))): ?>
         <div class="layui-form-item">
           <div class="layui-input-block">
             <button type="submit" class="layui-btn layui-btn-lg" id="submitBtn">确认购买</button>
@@ -115,6 +130,7 @@ ob_start();
       .then(function(r){return r.json();})
       .then(function(res){
         if(res.html){document.open();document.write(res.html);document.close();}
+        else if(res.redirect){window.location.href=res.redirect;}
         else{showMsg(res.code||'创建订单失败','error');btn.disabled=false;btn.textContent='确认购买';}
       })
       .catch(function(){showMsg('网络错误,请重试','error');btn.disabled=false;btn.textContent='确认购买';});
diff --git a/app_plugins/hosting_shop/views/shop.php b/app_plugins/hosting_shop/views/shop.php
index 80d7ba5..22701d0 100644
--- a/app_plugins/hosting_shop/views/shop.php
+++ b/app_plugins/hosting_shop/views/shop.php
@@ -28,11 +28,17 @@ ob_start();
           <li><span>域名绑定</span><b><?= (int)$plan['spec_domain'] ?> 个</b></li>
         </ul>
         <div class="hs-plan-price">
-          <?php if ((int)$plan['price_month_cents'] > 0): ?>
-            <div class="hs-price-item"><span class="hs-price-label">月付</span><span class="hs-price-value">¥<?= hosting_format_cents($plan['price_month_cents']) ?></span></div>
-          <?php endif; ?>
-          <?php if ((int)$plan['price_year_cents'] > 0): ?>
-            <div class="hs-price-item"><span class="hs-price-label">年付</span><span class="hs-price-value">¥<?= hosting_format_cents($plan['price_year_cents']) ?></span></div>
+          <?php
+            $enabled = hosting_plan_enabled_periods($plan);
+            foreach ($enabled as $p):
+              $cfg = hosting_periods()[$p];
+              $field = hosting_period_price_field($p);
+              $price = (int)($plan[$field] ?? 0);
+          ?>
+            <div class="hs-price-item"><span class="hs-price-label"><?= htmlspecialchars($cfg['label']) ?></span><span class="hs-price-value">¥<?= hosting_format_cents($price) ?></span></div>
+          <?php endforeach; ?>
+          <?php if ($enabled === []): ?>
+            <span style="color:#999;font-size:12px;">暂无可购买周期</span>
           <?php endif; ?>
         </div>
         <div class="hs-plan-buy"><a class="layui-btn" href="<?= hosting_url('shop/order/'.(int)$plan['id']) ?>">立即购买</a></div>