'操作', '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' => '排序']; return $this->view()->assign('table_config', [ 'total_column' => $columns, 'default_show_column' => array_keys($columns), 'ajax_url' => 'shop/ajax', ])->display('admin/shop/index.tpl'); } public function create($request, $response, $args) { return $this->view()->display('admin/shop/create.tpl'); } public function add($request, $response, $args) { return $this->savePlan($request, $response, new Shop()); } public function edit($request, $response, $args) { $shop = Shop::find((int) $args['id']); if ($shop === null) { return $response->withStatus(404); } return $this->view()->assign('shop', $shop)->display('admin/shop/edit.tpl'); } public function update($request, $response, $args) { $shop = Shop::find((int) $args['id']); if ($shop === null) { return $response->withJson(['ret' => 0, 'msg' => '套餐不存在'], 404); } return $this->savePlan($request, $response, $shop); } private function savePlan($request, $response, Shop $shop) { if (!AdminPermission::isSuperAdmin()) { 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) { $default = $shop->exists ? match ($field) { 'memory_gb' => $shop->memoryGB(), 'traffic_tb' => $shop->trafficTB(), default => $shop->$field, } : null; $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('宿主机不存在或已禁用'); } foreach ($values as $field => $value) { $shop->$field = $value; } if (!$shop->save()) { 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' => 1, 'msg' => '套餐已保存']); } public function deleteGet($request, $response, $args) { if (!AdminPermission::isSuperAdmin()) { 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); } $shop->status = 0; $shop->save(); 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->edit('op', static function ($data) { if (!AdminPermission::isSuperAdmin()) { return '只读'; } $id = (int) $data['id']; return '编辑'; }); $table->edit('name', static function ($data) { return htmlspecialchars((string) $data['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }); $table->edit('memory_gb', static function ($data) { return rtrim(rtrim(number_format((float) $data['memory_gb'], 1, '.', ''), '0'), '.'); }); $table->edit('traffic_tb', static function ($data) { if ((float) $data['traffic_tb'] === 0.0) { return '不限'; } 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, '.', ''); }); $response->getBody()->write($table->generate()); return $response; } }