diff --git a/web/app/Controllers/Admin/ShopController.php b/web/app/Controllers/Admin/ShopController.php index 11141d01..8023d0da 100644 --- a/web/app/Controllers/Admin/ShopController.php +++ b/web/app/Controllers/Admin/ShopController.php @@ -3,6 +3,14 @@ namespace App\Controllers\Admin; use App\Controllers\AdminController; +use App\Models\CloudInstance; +use App\Models\CloudInvoice; +use App\Models\CloudNetworkLine; +use App\Models\CloudOffer; +use App\Models\CloudOrder; +use App\Models\CloudRegion; +use App\Models\CloudResourcePool; +use App\Models\CloudResourceReservation; use App\Models\Shop; use App\Services\AdminPermission; use App\Utils\DatatablesHelper; @@ -12,13 +20,19 @@ class ShopController extends AdminController { public function index($request, $response, $args) { - $columns = ['op' => '操作', 'id' => 'ID', 'name' => '套餐名称', 'host_id' => '宿主机 ID', - 'cpu_cores' => 'CPU 核数', 'memory_gb' => '内存 GB', 'bandwidth_mbps' => '带宽 Mbps', - 'disk_gib' => '磁盘 GB', 'validity_days' => '有效期(天)', 'traffic_tb' => '每期流量 TB', - 'price' => '每期价格(USD)', 'status' => '状态', 'stock' => '库存', 'sort' => '排序']; + $columns = [ + 'op' => '操作', 'id' => 'ID', 'name' => '基础套餐', + 'cpu_cores' => 'CPU 核数', 'memory_gb' => '内存 GB', + 'bandwidth_mbps' => '带宽 Mbps', 'disk_gib' => '磁盘 GB', + 'validity_days' => '每期天数', 'traffic_tb' => '每期流量 TB', + 'offer_count' => '销售方案', 'active_offer_count' => '已上架方案', + 'price_range' => '美元价格区间', 'status' => '套餐状态', 'sort' => '排序', + ]; return $this->view()->assign('table_config', [ - 'total_column' => $columns, 'default_show_column' => array_keys($columns), 'ajax_url' => 'shop/ajax', + 'total_column' => $columns, + 'default_show_column' => array_keys($columns), + 'ajax_url' => 'shop/ajax', ])->display('admin/shop/index.tpl'); } @@ -34,33 +48,136 @@ class ShopController extends AdminController public function edit($request, $response, $args) { - $shop = Shop::find((int) $args['id']); + $shop = Shop::find((int) ($args['id'] ?? 0)); if ($shop === null) { return $response->withStatus(404); } - return $this->view()->assign('shop', $shop)->display('admin/shop/edit.tpl'); + return $this->view()->assign([ + 'shop' => $shop, + 'offers' => $this->offerRows((int) $shop->id), + ])->display('admin/shop/edit.tpl'); } public function update($request, $response, $args) { - $shop = Shop::find((int) $args['id']); + $shop = Shop::find((int) ($args['id'] ?? 0)); if ($shop === null) { - return $response->withJson(['ret' => 0, 'msg' => '套餐不存在'], 404); + return $response->withJson(['ret' => 0, 'msg' => '基础套餐不存在'], 404); } return $this->savePlan($request, $response, $shop); } + public function offerCreate($request, $response, $args) + { + $shop = Shop::find((int) ($args['id'] ?? 0)); + if ($shop === null) { + return $response->withStatus(404); + } + + return $this->view()->assign([ + 'shop' => $shop, + 'offer' => null, + 'offer_pools' => $this->poolOptions(), + ])->display('admin/shop/offer_form.tpl'); + } + + public function offerAdd($request, $response, $args) + { + $shop = Shop::find((int) ($args['id'] ?? 0)); + if ($shop === null) { + return $response->withJson(['ret' => 0, 'msg' => '基础套餐不存在'], 404); + } + + return $this->saveOffer($request, $response, $shop, null); + } + + public function offerEdit($request, $response, $args) + { + $shop = Shop::find((int) ($args['id'] ?? 0)); + $offer = CloudOffer::where('plan_id', (int) ($args['id'] ?? 0)) + ->where('id', (int) ($args['offer_id'] ?? 0))->first(); + if ($shop === null || $offer === null) { + return $response->withStatus(404); + } + + return $this->view()->assign([ + 'shop' => $shop, + 'offer' => $offer, + 'offer_pools' => $this->poolOptions(), + ])->display('admin/shop/offer_form.tpl'); + } + + public function offerUpdate($request, $response, $args) + { + $shop = Shop::find((int) ($args['id'] ?? 0)); + $offer = CloudOffer::where('plan_id', (int) ($args['id'] ?? 0)) + ->where('id', (int) ($args['offer_id'] ?? 0))->first(); + if ($shop === null || $offer === null) { + return $response->withJson(['ret' => 0, 'msg' => '销售方案不存在'], 404); + } + + return $this->saveOffer($request, $response, $shop, $offer); + } + + public function offerDelete($request, $response, $args) + { + if (!AdminPermission::isSuperAdmin()) { + return $response->withJson(['ret' => 0, 'msg' => '只有超级管理员可以删除销售方案'], 403); + } + $planId = (int) ($args['id'] ?? 0); + $offerId = (int) ($args['offer_id'] ?? 0); + $offer = CloudOffer::where('plan_id', $planId)->where('id', $offerId)->first(); + if ($offer === null) { + return $response->withJson(['ret' => 0, 'msg' => '销售方案不存在'], 404); + } + + try { + $message = $offer->getConnection()->transaction(function () use ($planId, $offerId) { + $locked = CloudOffer::where('plan_id', $planId)->where('id', $offerId) + ->lockForUpdate()->first(); + if ($locked === null) { + throw new \InvalidArgumentException('销售方案不存在'); + } + $referenced = CloudOrder::where('offer_id', $offerId)->exists() + || CloudInvoice::where('offer_id', $offerId)->exists() + || CloudInstance::where('offer_id', $offerId)->exists() + || CloudResourceReservation::where('offer_id', $offerId)->exists(); + if ($referenced) { + $locked->status = 0; + $locked->updated_at = time(); + if (!$locked->save()) { + throw new \RuntimeException('销售方案下架失败'); + } + return '销售方案已被业务记录引用,已下架并保留历史数据'; + } + if (!$locked->delete()) { + throw new \RuntimeException('销售方案删除失败'); + } + return '未被引用的销售方案已删除'; + }); + } catch (\InvalidArgumentException $exception) { + return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 404); + } catch (\Throwable $exception) { + error_log('Offer delete failed: ' . get_class($exception)); + return $response->withJson(['ret' => 0, 'msg' => '销售方案删除失败'], 500); + } + + return $response->withJson(['ret' => 1, 'msg' => $message]); + } + private function savePlan($request, $response, Shop $shop) { if (!AdminPermission::isSuperAdmin()) { - return $response->withJson(['ret' => 0, 'msg' => '只有管理员可以修改套餐'], 403); + return $response->withJson(['ret' => 0, 'msg' => '只有超级管理员可以修改基础套餐'], 403); } try { $input = []; - foreach (['name', 'host_id', 'price', 'cpu_cores', 'memory_gb', 'bandwidth_mbps', 'disk_gib', - 'validity_days', 'traffic_tb', 'status', 'stock', 'sort'] as $field) { + foreach ([ + 'name', 'cpu_cores', 'memory_gb', 'bandwidth_mbps', 'disk_gib', + 'validity_days', 'traffic_tb', 'status', 'sort', + ] as $field) { $default = $shop->exists ? match ($field) { 'memory_gb' => $shop->memoryGB(), @@ -71,52 +188,262 @@ class ShopController extends AdminController $input[$field] = $request->getParam($field, $default); } $values = Shop::validatePlan($input); - if ($values['host_id'] !== null && !\App\Models\CloudHost::where('id', $values['host_id'])->where('enabled', 1)->exists()) { - throw new \InvalidArgumentException('宿主机不存在或已禁用'); - } + $now = time(); foreach ($values as $field => $value) { $shop->$field = $value; } + if (!$shop->exists) { + $shop->created_at = $now; + } + $shop->updated_at = $now; if (!$shop->save()) { - throw new \RuntimeException('套餐保存失败'); + throw new \RuntimeException('基础套餐保存失败'); } } catch (\InvalidArgumentException $exception) { return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422); } catch (\Throwable $exception) { error_log('Plan save failed: ' . get_class($exception)); - return $response->withJson(['ret' => 0, 'msg' => '套餐保存失败'], 500); + return $response->withJson(['ret' => 0, 'msg' => '基础套餐保存失败'], 500); + } + + return $response->withJson([ + 'ret' => 1, + 'msg' => '基础套餐已保存', + 'plan_id' => (int) $shop->id, + 'redirect' => '/admin/shop/' . (int) $shop->id . '/edit', + ]); + } + + private function saveOffer($request, $response, Shop $shop, ?CloudOffer $offer) + { + if (!AdminPermission::isSuperAdmin()) { + return $response->withJson(['ret' => 0, 'msg' => '只有超级管理员可以修改销售方案'], 403); + } + try { + $input = []; + foreach (['resource_pool_id', 'code', 'name', 'price', 'stock', 'status', 'sort'] as $field) { + $input[$field] = $request->getParam($field, $offer === null ? null : $offer->$field); + } + $expectedRevision = $offer === null ? 0 : filter_var( + $request->getParam('revision'), FILTER_VALIDATE_INT + ); + if ($offer !== null && ($expectedRevision === false || $expectedRevision < 1)) { + throw new \InvalidArgumentException('销售方案修订号无效,请刷新页面后重试'); + } + $values = CloudOffer::validateOffer($input); + if ((int) $values['status'] === 1 && (int) $shop->status !== 1) { + throw new \InvalidArgumentException('基础套餐已停用,不能上架销售方案'); + } + $currentPoolId = $offer === null ? null : (int) $offer->resource_pool_id; + + $saved = (new CloudOffer())->getConnection()->transaction(function () use ( + $shop, $offer, $values, $expectedRevision, $currentPoolId + ) { + $locked = $offer === null ? new CloudOffer() : CloudOffer::where('plan_id', (int) $shop->id) + ->where('id', (int) $offer->id)->lockForUpdate()->first(); + if ($locked === null) { + throw new \InvalidArgumentException('销售方案不存在'); + } + if ($locked->exists && (int) $locked->revision !== (int) $expectedRevision) { + throw new \DomainException('销售方案已被其他管理员修改,请刷新页面后重试'); + } + $this->lockAndAssertPoolAvailable( + (int) $values['resource_pool_id'], + $currentPoolId, + (int) $values['status'] + ); + if ($locked->exists + && (int) $locked->resource_pool_id !== (int) $values['resource_pool_id'] + && $this->offerReferenced((int) $locked->id)) { + throw new \DomainException( + '已被订单、账单、实例或资源预留引用的销售方案不能更换资源池,请新建方案' + ); + } + $duplicatePool = CloudOffer::where('plan_id', (int) $shop->id) + ->where('resource_pool_id', (int) $values['resource_pool_id']); + $duplicateCode = CloudOffer::where('code', (string) $values['code']); + if ($locked->exists) { + $duplicatePool->where('id', '<>', (int) $locked->id); + $duplicateCode->where('id', '<>', (int) $locked->id); + } + if ($duplicatePool->exists()) { + throw new \InvalidArgumentException('该基础套餐在所选资源池中已有销售方案'); + } + if ($duplicateCode->exists()) { + throw new \InvalidArgumentException('销售方案代码已被使用'); + } + + $priceChanged = $locked->exists && (string) $locked->price !== (string) $values['price']; + foreach ($values as $field => $value) { + $locked->$field = $value; + } + $locked->plan_id = (int) $shop->id; + if (!$locked->exists) { + $locked->price_version = 1; + $locked->revision = 1; + $locked->created_at = time(); + } elseif ($priceChanged) { + $locked->price_version = max(1, (int) $locked->price_version) + 1; + } + if ($locked->exists) { + $locked->revision = max(1, (int) $locked->revision) + 1; + } + $locked->updated_at = time(); + if (!$locked->save()) { + throw new \RuntimeException('销售方案保存失败'); + } + return $locked; + }); + } catch (\InvalidArgumentException $exception) { + return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422); + } catch (\DomainException $exception) { + return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 409); + } catch (\Throwable $exception) { + if ((string) $exception->getCode() === '23000') { + return $response->withJson([ + 'ret' => 0, + 'msg' => '销售方案代码或基础套餐与资源池的组合已存在', + ], 422); + } + error_log('Offer save failed: ' . get_class($exception)); + return $response->withJson(['ret' => 0, 'msg' => '销售方案保存失败'], 500); } - return $response->withJson(['ret' => 1, 'msg' => '套餐已保存']); + return $response->withJson([ + 'ret' => 1, + 'msg' => '销售方案已保存', + 'offer_id' => (int) $saved->id, + 'price_version' => (int) $saved->price_version, + 'revision' => (int) $saved->revision, + 'redirect' => '/admin/shop/' . (int) $shop->id . '/edit', + ]); + } + + private function lockAndAssertPoolAvailable(int $poolId, ?int $currentPoolId, + int $offerStatus): void + { + $poolRef = CloudResourcePool::find($poolId); + $lineRef = $poolRef === null ? null : CloudNetworkLine::find((int) $poolRef->line_id); + $regionId = $lineRef === null ? 0 : (int) $lineRef->region_id; + $region = $regionId < 1 ? null + : CloudRegion::where('id', $regionId)->lockForUpdate()->first(); + $line = $lineRef === null ? null + : CloudNetworkLine::where('id', (int) $lineRef->id)->lockForUpdate()->first(); + $pool = $poolRef === null ? null + : CloudResourcePool::where('id', (int) $poolRef->id)->lockForUpdate()->first(); + if ($pool === null || (int) $pool->id !== $poolId) { + throw new \InvalidArgumentException('资源池不存在'); + } + $active = (string) $pool->status === 'active' + && $line !== null && (int) $pool->line_id === (int) $line->id + && (string) $line->status === 'active' + && $region !== null && (int) $line->region_id === (int) $region->id + && (string) $region->status === 'active'; + if (!$active && !($currentPoolId !== null && $poolId === $currentPoolId && $offerStatus === 0)) { + throw new \InvalidArgumentException( + '只能在已启用的地区、线路和资源池中上架销售方案' + ); + } + } + + private function offerReferenced(int $offerId): bool + { + return CloudOrder::where('offer_id', $offerId)->exists() + || CloudInvoice::where('offer_id', $offerId)->exists() + || CloudInstance::where('offer_id', $offerId)->exists() + || CloudResourceReservation::where('offer_id', $offerId)->exists(); + } + + private function poolOptions(): array + { + return CloudResourcePool::query()->from('cloud_resource_pools as pool') + ->join('cloud_network_lines as line', 'line.id', '=', 'pool.line_id') + ->join('cloud_regions as region', 'region.id', '=', 'line.region_id') + ->orderByDesc('region.sort')->orderBy('region.id') + ->orderByDesc('line.sort')->orderBy('line.id') + ->orderByDesc('pool.sort')->orderBy('pool.id') + ->get([ + 'pool.id', 'pool.code', 'pool.name', 'pool.status', + 'line.name as line_name', 'line.status as line_status', + 'region.name as region_name', 'region.status as region_status', + ])->map(static function ($pool) { + return [ + 'id' => (int) $pool->id, + 'code' => (string) $pool->code, + 'label' => (string) $pool->region_name . ' / ' . (string) $pool->line_name + . ' / ' . (string) $pool->name, + 'active' => (string) $pool->status === 'active' + && (string) $pool->line_status === 'active' + && (string) $pool->region_status === 'active', + ]; + })->all(); + } + + private function offerRows(int $planId): array + { + return CloudOffer::query()->from('cloud_offers as offer') + ->leftJoin('cloud_resource_pools as pool', 'pool.id', '=', 'offer.resource_pool_id') + ->leftJoin('cloud_network_lines as line', 'line.id', '=', 'pool.line_id') + ->leftJoin('cloud_regions as region', 'region.id', '=', 'line.region_id') + ->where('offer.plan_id', $planId) + ->orderByDesc('offer.sort')->orderBy('offer.id') + ->get([ + 'offer.*', 'pool.name as pool_name', 'line.name as line_name', + 'region.name as region_name', + ])->map(static function ($offer) { + return [ + 'id' => (int) $offer->id, + 'code' => (string) $offer->code, + 'name' => (string) $offer->name, + 'pool' => trim(implode(' / ', array_filter([ + (string) $offer->region_name, + (string) $offer->line_name, + (string) $offer->pool_name, + ], static fn ($value) => $value !== ''))), + 'price' => '$' . number_format((float) $offer->price, 2, '.', ''), + 'price_version' => (int) $offer->price_version, + 'stock' => (int) $offer->stock, + 'status' => (bool) $offer->status, + 'sort' => (int) $offer->sort, + ]; + })->all(); } public function deleteGet($request, $response, $args) { if (!AdminPermission::isSuperAdmin()) { - return $response->withJson(['ret' => 0, 'msg' => '只有管理员可以下架套餐'], 403); + return $response->withJson(['ret' => 0, 'msg' => '只有超级管理员可以下架基础套餐'], 403); } $shop = Shop::find((int) $request->getParam('id', 0)); if ($shop === null) { - return $response->withJson(['ret' => 0, 'msg' => '套餐不存在'], 404); + return $response->withJson(['ret' => 0, 'msg' => '基础套餐不存在'], 404); } $shop->status = 0; - $shop->save(); + $shop->updated_at = time(); + if (!$shop->save()) { + return $response->withJson(['ret' => 0, 'msg' => '基础套餐下架失败'], 500); + } - return $response->withJson(['ret' => 1, 'msg' => '套餐已下架']); + return $response->withJson(['ret' => 1, 'msg' => '基础套餐已下架,所属销售方案将不再对用户展示']); } public function ajax_shop($request, $response, $args) { $table = new Datatables(new DatatablesHelper()); - $table->query('SELECT id AS op,id,name,host_id,cpu_cores,ROUND(memory_mib/1024,1) AS memory_gb,bandwidth_mbps,disk_gib,' - . 'validity_days,CASE WHEN traffic_quota_bytes=0 THEN 0 ELSE ROUND(traffic_quota_bytes/1099511627776,10) END AS traffic_tb,' - . 'price,status,stock,sort FROM shop'); + $table->query('SELECT shop.id AS op,shop.id,shop.name,shop.cpu_cores,' + . 'ROUND(shop.memory_mib/1024,1) AS memory_gb,shop.bandwidth_mbps,shop.disk_gib,' + . 'shop.validity_days,CASE WHEN shop.traffic_quota_bytes=0 THEN 0 ' + . 'ELSE ROUND(shop.traffic_quota_bytes/1099511627776,10) END AS traffic_tb,' + . '(SELECT COUNT(*) FROM cloud_offers offer WHERE offer.plan_id=shop.id) AS offer_count,' + . '(SELECT COUNT(*) FROM cloud_offers offer WHERE offer.plan_id=shop.id AND offer.status=1) AS active_offer_count,' + . "CASE WHEN EXISTS(SELECT 1 FROM cloud_offers offer WHERE offer.plan_id=shop.id) " + . "THEN CONCAT('$',FORMAT((SELECT MIN(offer.price) FROM cloud_offers offer WHERE offer.plan_id=shop.id),2)," + . "' - $',FORMAT((SELECT MAX(offer.price) FROM cloud_offers offer WHERE offer.plan_id=shop.id),2)) " + . "ELSE '未设置' END AS price_range,shop.status,shop.sort FROM shop"); $table->edit('op', static function ($data) { - if (!AdminPermission::isSuperAdmin()) { - return '只读'; - } $id = (int) $data['id']; - return '编辑'; + $label = AdminPermission::isSuperAdmin() ? '编辑' : '查看'; + return '' . $label . ''; }); $table->edit('name', static function ($data) { return htmlspecialchars((string) $data['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); @@ -131,14 +458,10 @@ class ShopController extends AdminController return rtrim(rtrim(number_format((float) $data['traffic_tb'], 10, '.', ''), '0'), '.'); }); $table->edit('status', static function ($data) { - return (int) $data['status'] === 1 ? '上架' : '下架'; - }); - $table->edit('price', static function ($data) { - return '$' . number_format((float) $data['price'], 2, '.', ''); + return (int) $data['status'] === 1 ? '已启用' : '已停用'; }); $response->getBody()->write($table->generate()); return $response; } - } diff --git a/web/app/Models/CloudOffer.php b/web/app/Models/CloudOffer.php index 515369c1..33098225 100644 --- a/web/app/Models/CloudOffer.php +++ b/web/app/Models/CloudOffer.php @@ -13,10 +13,65 @@ final class CloudOffer extends Model 'resource_pool_id' => 'int', 'price' => 'decimal:2', 'price_version' => 'int', + 'revision' => 'int', 'status' => 'boolean', 'stock' => 'int', 'sort' => 'int', 'created_at' => 'int', 'updated_at' => 'int', ]; + + public static function validateOffer(array $input): array + { + $poolId = filter_var($input['resource_pool_id'] ?? null, FILTER_VALIDATE_INT); + if ($poolId === false || $poolId < 1) { + throw new \InvalidArgumentException('请选择有效的资源池'); + } + + $code = isset($input['code']) && is_string($input['code']) + ? trim($input['code']) : ''; + if (preg_match('/^[a-z0-9][a-z0-9_-]{0,63}$/D', $code) !== 1) { + throw new \InvalidArgumentException('销售方案代码需为 1 至 64 位小写字母、数字、下划线或连字符'); + } + + $name = isset($input['name']) && is_string($input['name']) ? trim($input['name']) : ''; + if ($name === '' || mb_strlen($name) > 128) { + throw new \InvalidArgumentException('销售方案名称需为 1 至 128 个字符'); + } + + $price = $input['price'] ?? ''; + if ((!is_string($price) && !is_int($price)) + || preg_match('/^\d{1,10}(?:\.\d{1,2})?$/D', (string) $price) !== 1) { + throw new \InvalidArgumentException('价格需为非负美元金额,最多两位小数'); + } + $parts = explode('.', (string) $price); + $whole = ltrim($parts[0], '0'); + $normalizedPrice = ($whole === '' ? '0' : $whole) . '.' + . str_pad($parts[1] ?? '', 2, '0'); + + $data = [ + 'resource_pool_id' => (int) $poolId, + 'code' => $code, + 'name' => $name, + 'price' => $normalizedPrice, + 'currency' => 'USD', + ]; + foreach ([ + 'status' => [0, 1, 1, '销售状态'], + 'stock' => [-1, 2147483647, -1, '库存'], + 'sort' => [-1000000, 1000000, 0, '排序'], + ] as $field => [$minimum, $maximum, $default, $label]) { + $raw = $input[$field] ?? $default; + $value = is_string($raw) || is_int($raw) + ? filter_var($raw, FILTER_VALIDATE_INT) : false; + if ($value === false || $value < $minimum || $value > $maximum) { + throw new \InvalidArgumentException( + $label . ' 需为 ' . $minimum . ' 至 ' . $maximum . ' 的整数' + ); + } + $data[$field] = (int) $value; + } + + return $data; + } } diff --git a/web/app/Models/Shop.php b/web/app/Models/Shop.php index e3433460..d10272e6 100644 --- a/web/app/Models/Shop.php +++ b/web/app/Models/Shop.php @@ -11,11 +11,9 @@ class Shop extends Model protected $table = 'shop'; protected $casts = [ - 'host_id' => 'int', 'cpu_cores' => 'int', 'memory_mib' => 'int', 'bandwidth_mbps' => 'int', 'disk_gib' => 'int', 'validity_days' => 'int', 'traffic_quota_bytes' => 'int', - 'status' => 'int', 'stock' => 'int', 'sort' => 'int', - 'price' => 'decimal:2', + 'status' => 'int', 'sort' => 'int', 'created_at' => 'int', 'updated_at' => 'int', ]; @@ -53,23 +51,13 @@ class Shop extends Model throw new \InvalidArgumentException('套餐名称需为 1 至 128 个字符'); } $data = ['name' => $name]; - $host = $input['host_id'] ?? null; - if ($host === null || $host === '') { - $data['host_id'] = null; - } else { - $host = filter_var($host, FILTER_VALIDATE_INT); - if ($host === false || $host < 1) { - throw new \InvalidArgumentException('请选择有效的宿主机'); - } - $data['host_id'] = $host; - } foreach ([ 'cpu_cores' => [1, 64], 'bandwidth_mbps' => [1, 100000], 'disk_gib' => [1, 4096], 'validity_days' => [1, 3650], - 'status' => [0, 1], 'stock' => [-1, 2147483647], 'sort' => [-1000000, 1000000], + 'status' => [0, 1], 'sort' => [-1000000, 1000000], ] as $field => [$minimum, $maximum]) { - $raw = $input[$field] ?? (['status' => 1, 'stock' => -1, 'sort' => 0][$field] ?? null); + $raw = $input[$field] ?? (['status' => 1, 'sort' => 0][$field] ?? null); $value = is_string($raw) || is_int($raw) ? filter_var($raw, FILTER_VALIDATE_INT) : false; if ($value === false || $value < $minimum || $value > $maximum) { throw new \InvalidArgumentException($field . ' 需为 ' . $minimum . ' 至 ' . $maximum . ' 的整数'); @@ -89,16 +77,6 @@ class Shop extends Model throw new \InvalidArgumentException('每期流量额度的最小步进为 1 GiB(1/1024 TB)'); } $data['traffic_quota_bytes'] = (int) round($trafficGiB) * self::GIB; - $price = $input['price'] ?? ''; - if ((!is_string($price) && !is_int($price)) || preg_match('/^\d{1,10}(?:\.\d{1,2})?$/D', (string) $price) !== 1) { - throw new \InvalidArgumentException('价格需为非负金额,最多两位小数'); - } - $parts = explode('.', (string) $price); - $data['price'] = ltrim($parts[0], '0'); - if ($data['price'] === '') { - $data['price'] = '0'; - } - $data['price'] .= '.' . str_pad($parts[1] ?? '', 2, '0'); return $data; } diff --git a/web/config/routes.php b/web/config/routes.php index 2b0f3fdc..854acb0d 100644 --- a/web/config/routes.php +++ b/web/config/routes.php @@ -199,9 +199,14 @@ $app->group('/admin', function () { $this->get('/shop/create', 'App\Controllers\Admin\ShopController:create')->add(new SuperAdmin()); $this->post('/shop', 'App\Controllers\Admin\ShopController:add')->add(new SuperAdmin()); - $this->get('/shop/{id}/edit', 'App\Controllers\Admin\ShopController:edit')->add(new SuperAdmin()); + $this->get('/shop/{id}/edit', 'App\Controllers\Admin\ShopController:edit'); $this->put('/shop/{id}', 'App\Controllers\Admin\ShopController:update')->add(new SuperAdmin()); $this->delete('/shop', 'App\Controllers\Admin\ShopController:deleteGet')->add(new SuperAdmin()); + $this->get('/shop/{id}/offers/create', 'App\Controllers\Admin\ShopController:offerCreate')->add(new SuperAdmin()); + $this->post('/shop/{id}/offers', 'App\Controllers\Admin\ShopController:offerAdd')->add(new SuperAdmin()); + $this->get('/shop/{id}/offers/{offer_id}/edit', 'App\Controllers\Admin\ShopController:offerEdit'); + $this->put('/shop/{id}/offers/{offer_id}', 'App\Controllers\Admin\ShopController:offerUpdate')->add(new SuperAdmin()); + $this->delete('/shop/{id}/offers/{offer_id}', 'App\Controllers\Admin\ShopController:offerDelete')->add(new SuperAdmin()); // Ann Mange $this->get('/announcement', 'App\Controllers\Admin\AnnController:index'); diff --git a/web/resources/views/venix/admin/components/table_page.tpl b/web/resources/views/venix/admin/components/table_page.tpl index be14e173..f9261a21 100644 --- a/web/resources/views/venix/admin/components/table_page.tpl +++ b/web/resources/views/venix/admin/components/table_page.tpl @@ -255,10 +255,10 @@ window.__VENIX_ADMIN_TABLE__ = { function cellValue(column, item) { if (column.action === 'shopActions') { - var disabled = item.status === '下架'; - var editAction = canManage - ? '编辑' - : ''; + var disabled = item.status === '下架' || item.status === '已停用'; + var editAction = '' + + (canManage ? '编辑' : '查看') + ''; return '
基础套餐只定义 CPU、内存、磁盘、带宽、账期和流量;保存后再为不同资源池添加独立的美元价格与库存。
+