|
@@ -7,9 +7,13 @@ namespace App\Controllers\Admin;
|
|
use App\Controllers\BaseController;
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Product;
|
|
use App\Models\Product;
|
|
use App\Utils\Tools;
|
|
use App\Utils\Tools;
|
|
|
|
+use Exception;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
-use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
use Slim\Http\Response;
|
|
|
|
+use Slim\Http\ServerRequest;
|
|
|
|
+use function json_decode;
|
|
|
|
+use function json_encode;
|
|
|
|
+use function time;
|
|
|
|
|
|
final class ProductController extends BaseController
|
|
final class ProductController extends BaseController
|
|
{
|
|
{
|
|
@@ -45,7 +49,10 @@ final class ProductController extends BaseController
|
|
'node_group_required',
|
|
'node_group_required',
|
|
];
|
|
];
|
|
|
|
|
|
- public function index(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public function index(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
return $response->write(
|
|
return $response->write(
|
|
$this->view()
|
|
$this->view()
|
|
@@ -54,7 +61,10 @@ final class ProductController extends BaseController
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
- public function create(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public function create(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
return $response->write(
|
|
return $response->write(
|
|
$this->view()
|
|
$this->view()
|
|
@@ -63,234 +73,278 @@ final class ProductController extends BaseController
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
- public function add(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public function edit(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
|
|
+ {
|
|
|
|
+ $id = $args['id'];
|
|
|
|
+ $product = Product::find($id);
|
|
|
|
+ $content = json_decode($product->content);
|
|
|
|
+ $limit = json_decode($product->limit);
|
|
|
|
+
|
|
|
|
+ return $response->write(
|
|
|
|
+ $this->view()
|
|
|
|
+ ->assign('product', $product)
|
|
|
|
+ ->assign('content', $content)
|
|
|
|
+ ->assign('limit', $limit)
|
|
|
|
+ ->assign('update_field', self::$update_field)
|
|
|
|
+ ->fetch('admin/product/edit.tpl')
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function add(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
// base product
|
|
// base product
|
|
- $type = $request->getParam('type');
|
|
|
|
- $name = $request->getParam('name');
|
|
|
|
- $price = $request->getParam('price');
|
|
|
|
- $status = $request->getParam('status');
|
|
|
|
- $stock = $request->getParam('stock');
|
|
|
|
|
|
+ $type = $request->getParam('type') ?? '';
|
|
|
|
+ $name = $request->getParam('name') ?? '';
|
|
|
|
+ $price = $request->getParam('price') ?? 0;
|
|
|
|
+ $status = $request->getParam('status') ?? 1;
|
|
|
|
+ $stock = $request->getParam('stock') ?? -1;
|
|
// content
|
|
// content
|
|
- $time = $request->getParam('time');
|
|
|
|
- $bandwidth = $request->getParam('bandwidth');
|
|
|
|
- $class = $request->getParam('class');
|
|
|
|
- $class_time = $request->getParam('class_time');
|
|
|
|
- $node_group = $request->getParam('node_group');
|
|
|
|
- $speed_limit = $request->getParam('speed_limit');
|
|
|
|
- $ip_limit = $request->getParam('ip_limit');
|
|
|
|
|
|
+ $time = $request->getParam('time') ?? 0;
|
|
|
|
+ $bandwidth = $request->getParam('bandwidth') ?? 0;
|
|
|
|
+ $class = $request->getParam('class') ?? 0;
|
|
|
|
+ $class_time = $request->getParam('class_time') ?? 0;
|
|
|
|
+ $node_group = $request->getParam('node_group') ?? 0;
|
|
|
|
+ $speed_limit = $request->getParam('speed_limit') ?? 0;
|
|
|
|
+ $ip_limit = $request->getParam('ip_limit') ?? 0;
|
|
// limit
|
|
// limit
|
|
$class_required = $request->getParam('class_required') ?? '';
|
|
$class_required = $request->getParam('class_required') ?? '';
|
|
$node_group_required = $request->getParam('node_group_required') ?? '';
|
|
$node_group_required = $request->getParam('node_group_required') ?? '';
|
|
$new_user_required = $request->getParam('new_user_required') === 'true' ? 1 : 0;
|
|
$new_user_required = $request->getParam('new_user_required') === 'true' ? 1 : 0;
|
|
|
|
|
|
- try {
|
|
|
|
- $product = new Product();
|
|
|
|
|
|
+ $product = new Product();
|
|
|
|
+
|
|
|
|
+ if ($price < 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的商品价格',
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
|
|
- if ($name === '' || $name === null) {
|
|
|
|
- throw new \Exception('请填写商品名称');
|
|
|
|
|
|
+ if ($type === 'tabp') {
|
|
|
|
+ if ($time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的商品时长',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
- if ($price === '' || $price === null) {
|
|
|
|
- throw new \Exception('请填写商品售价');
|
|
|
|
|
|
+
|
|
|
|
+ if ($class_time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的等级时长',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
- if ($stock === '' || $stock === null) {
|
|
|
|
- throw new \Exception('请填写商品库存');
|
|
|
|
|
|
+
|
|
|
|
+ if ($bandwidth <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的套餐流量',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
|
|
|
|
- if ($type === 'tabp') {
|
|
|
|
- if ($time === '' || $time === null) {
|
|
|
|
- throw new \Exception('请填写套餐时长');
|
|
|
|
- }
|
|
|
|
- if ($bandwidth === '' || $bandwidth === null) {
|
|
|
|
- throw new \Exception('请填写套餐流量');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ($class === '') && $class = '0';
|
|
|
|
- ($class_time === '') && $class_time = $time;
|
|
|
|
- ($speed_limit === '') && $speed_limit = '0';
|
|
|
|
- ($ip_limit === '') && $ip_limit = '0';
|
|
|
|
-
|
|
|
|
- $content = [
|
|
|
|
- 'time' => $time,
|
|
|
|
- 'bandwidth' => $bandwidth,
|
|
|
|
- 'class' => $class,
|
|
|
|
- 'class_time' => $class_time,
|
|
|
|
- 'node_group' => $node_group,
|
|
|
|
- 'speed_limit' => $speed_limit,
|
|
|
|
- 'ip_limit' => $ip_limit,
|
|
|
|
- ];
|
|
|
|
- } elseif ($type === 'time') {
|
|
|
|
- if ($time === '' || $time === null) {
|
|
|
|
- throw new \Exception('请填写套餐时长');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $content = [
|
|
|
|
- 'time' => $time,
|
|
|
|
- ];
|
|
|
|
- } elseif ($type === 'bandwidth') {
|
|
|
|
- if ($bandwidth === '' || $bandwidth === null) {
|
|
|
|
- throw new \Exception('请填写套餐流量');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $content = [
|
|
|
|
- 'bandwidth' => $bandwidth,
|
|
|
|
- ];
|
|
|
|
- } else {
|
|
|
|
- throw new \Exception('商品类型错误');
|
|
|
|
|
|
+ $content = [
|
|
|
|
+ 'time' => $time,
|
|
|
|
+ 'bandwidth' => $bandwidth,
|
|
|
|
+ 'class' => $class,
|
|
|
|
+ 'class_time' => $class_time,
|
|
|
|
+ 'node_group' => $node_group,
|
|
|
|
+ 'speed_limit' => $speed_limit,
|
|
|
|
+ 'ip_limit' => $ip_limit,
|
|
|
|
+ ];
|
|
|
|
+ } elseif ($type === 'time') {
|
|
|
|
+ if ($time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的商品时长',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
|
|
|
|
- $limit = [
|
|
|
|
- 'class_required' => $class_required,
|
|
|
|
- 'node_group_required' => $node_group_required,
|
|
|
|
- 'new_user_required' => $new_user_required,
|
|
|
|
|
|
+ if ($class_time === '' || $class_time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的等级时长',
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $content = [
|
|
|
|
+ 'time' => $time,
|
|
|
|
+ 'class' => $class,
|
|
|
|
+ 'class_time' => $class_time,
|
|
|
|
+ 'node_group' => $node_group,
|
|
|
|
+ 'speed_limit' => $speed_limit,
|
|
|
|
+ 'ip_limit' => $ip_limit,
|
|
];
|
|
];
|
|
|
|
+ } elseif ($type === 'bandwidth') {
|
|
|
|
+ if ($bandwidth <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的套餐流量',
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
|
|
- $product->type = $type;
|
|
|
|
- $product->name = $name;
|
|
|
|
- $product->price = $price;
|
|
|
|
- $product->content = \json_encode($content);
|
|
|
|
- $product->limit = \json_encode($limit);
|
|
|
|
- $product->status = $status;
|
|
|
|
- $product->create_time = \time();
|
|
|
|
- $product->update_time = \time();
|
|
|
|
- $product->sale_count = 0;
|
|
|
|
- $product->stock = $stock;
|
|
|
|
- $product->save();
|
|
|
|
- } catch (\Exception $e) {
|
|
|
|
|
|
+ $content = [
|
|
|
|
+ 'bandwidth' => $bandwidth,
|
|
|
|
+ ];
|
|
|
|
+ } else {
|
|
return $response->withJson([
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'ret' => 0,
|
|
- 'msg' => $e->getMessage(),
|
|
|
|
|
|
+ 'msg' => '商品类型错误',
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ $limit = [
|
|
|
|
+ 'class_required' => $class_required,
|
|
|
|
+ 'node_group_required' => $node_group_required,
|
|
|
|
+ 'new_user_required' => $new_user_required,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ $product->type = $type;
|
|
|
|
+ $product->name = $name;
|
|
|
|
+ $product->price = $price;
|
|
|
|
+ $product->content = json_encode($content);
|
|
|
|
+ $product->limit = json_encode($limit);
|
|
|
|
+ $product->status = $status;
|
|
|
|
+ $product->create_time = time();
|
|
|
|
+ $product->update_time = time();
|
|
|
|
+ $product->sale_count = 0;
|
|
|
|
+ $product->stock = $stock;
|
|
|
|
+ $product->save();
|
|
|
|
+
|
|
return $response->withJson([
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'ret' => 1,
|
|
'msg' => '添加成功',
|
|
'msg' => '添加成功',
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
- public function edit(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
- {
|
|
|
|
- $id = $args['id'];
|
|
|
|
- $product = Product::find($id);
|
|
|
|
- $content = \json_decode($product->content, true);
|
|
|
|
- $limit = \json_decode($product->limit, true);
|
|
|
|
- return $response->write(
|
|
|
|
- $this->view()
|
|
|
|
- ->assign('product', $product)
|
|
|
|
- ->assign('content', $content)
|
|
|
|
- ->assign('limit', $limit)
|
|
|
|
- ->assign('update_field', self::$update_field)
|
|
|
|
- ->fetch('admin/product/edit.tpl')
|
|
|
|
- );
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public function update(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ public function update(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
$product_id = $args['id'];
|
|
$product_id = $args['id'];
|
|
// base product
|
|
// base product
|
|
- $type = $request->getParam('type');
|
|
|
|
- $name = $request->getParam('name');
|
|
|
|
- $price = $request->getParam('price');
|
|
|
|
- $status = $request->getParam('status');
|
|
|
|
- $stock = $request->getParam('stock');
|
|
|
|
|
|
+ $type = $request->getParam('type') ?? '';
|
|
|
|
+ $name = $request->getParam('name') ?? '';
|
|
|
|
+ $price = $request->getParam('price') ?? 0;
|
|
|
|
+ $status = $request->getParam('status') ?? 1;
|
|
|
|
+ $stock = $request->getParam('stock') ?? -1;
|
|
// content
|
|
// content
|
|
- $time = $request->getParam('time');
|
|
|
|
- $bandwidth = $request->getParam('bandwidth');
|
|
|
|
- $class = $request->getParam('class');
|
|
|
|
- $class_time = $request->getParam('class_time');
|
|
|
|
- $node_group = $request->getParam('node_group');
|
|
|
|
- $speed_limit = $request->getParam('speed_limit');
|
|
|
|
- $ip_limit = $request->getParam('ip_limit');
|
|
|
|
|
|
+ $time = $request->getParam('time') ?? 0;
|
|
|
|
+ $bandwidth = $request->getParam('bandwidth') ?? 0;
|
|
|
|
+ $class = $request->getParam('class') ?? 0;
|
|
|
|
+ $class_time = $request->getParam('class_time') ?? 0;
|
|
|
|
+ $node_group = $request->getParam('node_group') ?? 0;
|
|
|
|
+ $speed_limit = $request->getParam('speed_limit') ?? 0;
|
|
|
|
+ $ip_limit = $request->getParam('ip_limit') ?? 0;
|
|
// limit
|
|
// limit
|
|
$class_required = $request->getParam('class_required') ?? '';
|
|
$class_required = $request->getParam('class_required') ?? '';
|
|
$node_group_required = $request->getParam('node_group_required') ?? '';
|
|
$node_group_required = $request->getParam('node_group_required') ?? '';
|
|
$new_user_required = $request->getParam('new_user_required') === 'true' ? 1 : 0;
|
|
$new_user_required = $request->getParam('new_user_required') === 'true' ? 1 : 0;
|
|
|
|
|
|
- try {
|
|
|
|
- $product = Product::find($product_id);
|
|
|
|
|
|
+ $product = Product::find($product_id);
|
|
|
|
+
|
|
|
|
+ if ($price < 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的商品价格',
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ($type === 'tabp') {
|
|
|
|
+ if ($time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的商品时长',
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
|
|
- if ($name === '') {
|
|
|
|
- throw new \Exception('请填写商品名称');
|
|
|
|
|
|
+ if ($class_time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的等级时长',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
- if ($price === '') {
|
|
|
|
- throw new \Exception('请填写商品售价');
|
|
|
|
|
|
+
|
|
|
|
+ if ($bandwidth <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的套餐流量',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
- if ($stock === '') {
|
|
|
|
- throw new \Exception('请填写商品库存');
|
|
|
|
|
|
+
|
|
|
|
+ $content = [
|
|
|
|
+ 'time' => $time,
|
|
|
|
+ 'bandwidth' => $bandwidth,
|
|
|
|
+ 'class' => $class,
|
|
|
|
+ 'class_time' => $class_time,
|
|
|
|
+ 'node_group' => $node_group,
|
|
|
|
+ 'speed_limit' => $speed_limit,
|
|
|
|
+ 'ip_limit' => $ip_limit,
|
|
|
|
+ ];
|
|
|
|
+ } elseif ($type === 'time') {
|
|
|
|
+ if ($time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的商品时长',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
|
|
|
|
- if ($type === 'tabp') {
|
|
|
|
- if ($time === '') {
|
|
|
|
- throw new \Exception('请填写套餐时长');
|
|
|
|
- }
|
|
|
|
- if ($bandwidth === '') {
|
|
|
|
- throw new \Exception('请填写套餐流量');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ($class === '') && $class = '0';
|
|
|
|
- ($class_time === '') && $class_time = $time;
|
|
|
|
- ($speed_limit === '') && $speed_limit = '0';
|
|
|
|
- ($ip_limit === '') && $ip_limit = '0';
|
|
|
|
-
|
|
|
|
- $content = [
|
|
|
|
- 'time' => $time,
|
|
|
|
- 'bandwidth' => $bandwidth,
|
|
|
|
- 'class' => $class,
|
|
|
|
- 'class_time' => $class_time,
|
|
|
|
- 'node_group' => $node_group,
|
|
|
|
- 'speed_limit' => $speed_limit,
|
|
|
|
- 'ip_limit' => $ip_limit,
|
|
|
|
- ];
|
|
|
|
- } elseif ($type === 'time') {
|
|
|
|
- if ($time === '') {
|
|
|
|
- throw new \Exception('请填写套餐时长');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $content = [
|
|
|
|
- 'time' => $time,
|
|
|
|
- ];
|
|
|
|
- } elseif ($type === 'bandwidth') {
|
|
|
|
- if ($bandwidth === '') {
|
|
|
|
- throw new \Exception('请填写套餐流量');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $content = [
|
|
|
|
- 'bandwidth' => $bandwidth,
|
|
|
|
- ];
|
|
|
|
- } else {
|
|
|
|
- throw new \Exception('商品类型错误');
|
|
|
|
|
|
+ if ($class_time <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的等级时长',
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
|
|
|
|
- $limit = [
|
|
|
|
- 'class_required' => $class_required,
|
|
|
|
- 'node_group_required' => $node_group_required,
|
|
|
|
- 'new_user_required' => $new_user_required,
|
|
|
|
|
|
+ $content = [
|
|
|
|
+ 'time' => $time,
|
|
|
|
+ 'class' => $class,
|
|
|
|
+ 'class_time' => $class_time,
|
|
|
|
+ 'node_group' => $node_group,
|
|
|
|
+ 'speed_limit' => $speed_limit,
|
|
|
|
+ 'ip_limit' => $ip_limit,
|
|
];
|
|
];
|
|
|
|
+ } elseif ($type === 'bandwidth') {
|
|
|
|
+ if ($bandwidth <= 0) {
|
|
|
|
+ return $response->withJson([
|
|
|
|
+ 'ret' => 0,
|
|
|
|
+ 'msg' => '无效的套餐流量',
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
|
|
- $product->type = $type;
|
|
|
|
- $product->name = $name;
|
|
|
|
- $product->price = $price;
|
|
|
|
- $product->content = \json_encode($content);
|
|
|
|
- $product->limit = \json_encode($limit);
|
|
|
|
- $product->stock = $stock;
|
|
|
|
- $product->status = $status;
|
|
|
|
- $product->update_time = \time();
|
|
|
|
- $product->save();
|
|
|
|
- } catch (\Exception $e) {
|
|
|
|
|
|
+ $content = [
|
|
|
|
+ 'bandwidth' => $bandwidth,
|
|
|
|
+ ];
|
|
|
|
+ } else {
|
|
return $response->withJson([
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'ret' => 0,
|
|
- 'msg' => $e->getMessage(),
|
|
|
|
|
|
+ 'msg' => '商品类型错误',
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ $limit = [
|
|
|
|
+ 'class_required' => $class_required,
|
|
|
|
+ 'node_group_required' => $node_group_required,
|
|
|
|
+ 'new_user_required' => $new_user_required,
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ $product->type = $type;
|
|
|
|
+ $product->name = $name;
|
|
|
|
+ $product->price = $price;
|
|
|
|
+ $product->content = json_encode($content);
|
|
|
|
+ $product->limit = json_encode($limit);
|
|
|
|
+ $product->stock = $stock;
|
|
|
|
+ $product->status = $status;
|
|
|
|
+ $product->update_time = time();
|
|
|
|
+ $product->save();
|
|
|
|
+
|
|
return $response->withJson([
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'ret' => 1,
|
|
'msg' => '更新成功',
|
|
'msg' => '更新成功',
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
- public function delete(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ public function delete(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
$product_id = $args['id'];
|
|
$product_id = $args['id'];
|
|
Product::find($product_id)->delete();
|
|
Product::find($product_id)->delete();
|
|
@@ -301,28 +355,20 @@ final class ProductController extends BaseController
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
- public function copy(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ public function copy(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
- try {
|
|
|
|
- $old_product_id = $args['id'];
|
|
|
|
- $old_product = Product::find($old_product_id);
|
|
|
|
- $new_product = new Product();
|
|
|
|
- // https://laravel.com/docs/9.x/eloquent#replicating-models
|
|
|
|
- $new_product = $old_product->replicate([
|
|
|
|
- 'create_time',
|
|
|
|
- 'update_time',
|
|
|
|
- ]);
|
|
|
|
- $new_product->name .= ' (副本)';
|
|
|
|
- $new_product->create_time = \time();
|
|
|
|
- $new_product->update_time = \time();
|
|
|
|
- $new_product->sale_count = 0;
|
|
|
|
- $new_product->save();
|
|
|
|
- } catch (\Exception $e) {
|
|
|
|
- return $response->withJson([
|
|
|
|
- 'ret' => 0,
|
|
|
|
- 'msg' => $e->getMessage(),
|
|
|
|
- ]);
|
|
|
|
- }
|
|
|
|
|
|
+ $old_product_id = $args['id'];
|
|
|
|
+ $old_product = Product::find($old_product_id);
|
|
|
|
+
|
|
|
|
+ $new_product = $old_product->replicate([
|
|
|
|
+ 'create_time',
|
|
|
|
+ 'update_time',
|
|
|
|
+ ]);
|
|
|
|
+ $new_product->name .= ' (副本)';
|
|
|
|
+ $new_product->create_time = time();
|
|
|
|
+ $new_product->update_time = time();
|
|
|
|
+ $new_product->sale_count = 0;
|
|
|
|
+ $new_product->save();
|
|
|
|
|
|
return $response->withJson([
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'ret' => 1,
|
|
@@ -330,21 +376,21 @@ final class ProductController extends BaseController
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
- public function ajax(Request $request, Response $response, array $args): ResponseInterface
|
|
|
|
|
|
+ public function ajax(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
|
|
{
|
|
{
|
|
$products = Product::orderBy('id', 'desc')->get();
|
|
$products = Product::orderBy('id', 'desc')->get();
|
|
|
|
|
|
foreach ($products as $product) {
|
|
foreach ($products as $product) {
|
|
- $product->op = '<button type="button" class="btn btn-red" id="delete-product-' . $product->id . '"
|
|
|
|
- onclick="deleteProduct(' . $product->id . ')">删除</button>
|
|
|
|
- <button type="button" class="btn btn-orange" id="copy-product-' . $product->id . '"
|
|
|
|
- onclick="copyProduct(' . $product->id . ')">复制</button>
|
|
|
|
|
|
+ $product->op = '<button type="button" class="btn btn-red" id="delete-product-' . $product->id . '"
|
|
|
|
+ onclick="deleteProduct(' . $product->id . ')">删除</button>
|
|
|
|
+ <button type="button" class="btn btn-orange" id="copy-product-' . $product->id . '"
|
|
|
|
+ onclick="copyProduct(' . $product->id . ')">复制</button>
|
|
<a class="btn btn-blue" href="/admin/product/' . $product->id . '/edit">编辑</a>';
|
|
<a class="btn btn-blue" href="/admin/product/' . $product->id . '/edit">编辑</a>';
|
|
- $product->type = Tools::getProductType($product);
|
|
|
|
- $product->status = Tools::getProductStatus($product);
|
|
|
|
|
|
+ $product->type = $product->type();
|
|
|
|
+ $product->status = $product->status();
|
|
$product->create_time = Tools::toDateTime($product->create_time);
|
|
$product->create_time = Tools::toDateTime($product->create_time);
|
|
$product->update_time = Tools::toDateTime($product->update_time);
|
|
$product->update_time = Tools::toDateTime($product->update_time);
|
|
- $product->stock = Tools::getProductStock($product);
|
|
|
|
|
|
+ $product->stock = $product->stock();
|
|
}
|
|
}
|
|
|
|
|
|
return $response->withJson([
|
|
return $response->withJson([
|