You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/Admin/ShopController.php

186 lines
7.8 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Models\Bought;
use App\Models\Shop;
use App\Services\AdminDataMasker;
use App\Services\AdminPermission;
use App\Utils\DatatablesHelper;
use App\Utils\SafeDatatables as Datatables;
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' => '排序'];
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 '<span class="text-muted">只读</span>';
}
$id = (int) $data['id'];
return '<a class="btn btn-brand" href="/admin/shop/' . $id . '/edit">编辑</a>';
});
$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;
}
public function bought($request, $response, $args)
{
$columns = ['op' => '操作', 'id' => 'ID', 'content' => '套餐',
'price' => '成交金额(USD)', 'user_id' => '用户 ID', 'user_name' => '用户名', 'datetime' => '购买时间'];
return $this->view()->assign('table_config', [
'total_column' => $columns, 'default_show_column' => array_keys($columns), 'ajax_url' => 'bought/ajax',
])->display('admin/shop/bought.tpl');
}
public function deleteBoughtGet($request, $response, $args)
{
return $response->withJson(['ret' => 0, 'msg' => '虚拟化服务尚未接入,暂不能变更实例订单'], 409);
}
public function ajax_bought($request, $response, $args)
{
$table = new Datatables(new DatatablesHelper());
$table->query('SELECT bought.id AS op,bought.id,shop.name AS content,bought.price,user.id AS user_id,user.user_name,bought.datetime FROM bought LEFT JOIN user ON bought.userid=user.id LEFT JOIN shop ON bought.shopid=shop.id');
$table->edit('op', static function ($data) {
return '<span class="text-muted">历史记录</span>';
});
$table->edit('user_name', static function ($data) {
return AdminDataMasker::account($data['user_name'] ?? '');
});
$table->edit('content', static function ($data) {
return htmlspecialchars((string) ($data['content'] ?? '套餐已删除'), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
});
$table->edit('price', static function ($data) {
$amount = (float) ($data['price'] ?? 0);
return ($amount < 0 ? '-$' : '$') . number_format(abs($amount), 2, '.', '');
});
$table->edit('datetime', static function ($data) {
return date('Y-m-d H:i:s', (int) $data['datetime']);
});
$response->getBody()->write($table->generate());
return $response;
}
}