GiftCardController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\Admin;
  4. use App\Controllers\BaseController;
  5. use App\Models\GiftCard;
  6. use App\Utils\Tools;
  7. use Exception;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Slim\Http\Response;
  10. use Slim\Http\ServerRequest;
  11. use function time;
  12. use const PHP_EOL;
  13. final class GiftCardController extends BaseController
  14. {
  15. private static array $details = [
  16. 'field' => [
  17. 'op' => '操作',
  18. 'id' => '礼品卡ID',
  19. 'card' => '卡号',
  20. 'balance' => '面值',
  21. 'create_time' => '创建时间',
  22. 'status' => '使用状态',
  23. 'use_time' => '使用时间',
  24. 'use_user' => '使用用户',
  25. ],
  26. 'create_dialog' => [
  27. [
  28. 'id' => 'card_number',
  29. 'info' => '创建数量',
  30. 'type' => 'input',
  31. 'placeholder' => '',
  32. ],
  33. [
  34. 'id' => 'card_value',
  35. 'info' => '礼品卡面值',
  36. 'type' => 'input',
  37. 'placeholder' => '',
  38. ],
  39. [
  40. 'id' => 'card_length',
  41. 'info' => '礼品卡长度',
  42. 'type' => 'select',
  43. 'select' => [
  44. '12' => '12位',
  45. '18' => '18位',
  46. '24' => '24位',
  47. '30' => '30位',
  48. '36' => '36位',
  49. ],
  50. ],
  51. ],
  52. ];
  53. /**
  54. * @throws Exception
  55. */
  56. public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
  57. {
  58. return $response->write(
  59. $this->view()
  60. ->assign('details', self::$details)
  61. ->fetch('admin/giftcard.tpl')
  62. );
  63. }
  64. public function add(ServerRequest $request, Response $response, array $args): ResponseInterface
  65. {
  66. $card_number = $request->getParam('card_number') ?? 0;
  67. $card_value = $request->getParam('card_value') ?? 0;
  68. $card_length = $request->getParam('card_length') ?? 0;
  69. $card_added = '';
  70. if ($card_number === '' || $card_number <= 0) {
  71. return $response->withJson([
  72. 'ret' => 0,
  73. 'msg' => '生成数量不能为空或小于0',
  74. ]);
  75. }
  76. if ($card_value === '' || $card_value <= 0) {
  77. return $response->withJson([
  78. 'ret' => 0,
  79. 'msg' => '礼品卡面值不能为空或小于0',
  80. ]);
  81. }
  82. if ($card_length === '' || $card_length <= 0) {
  83. return $response->withJson([
  84. 'ret' => 0,
  85. 'msg' => '礼品卡长度不能为空或小于0',
  86. ]);
  87. }
  88. for ($i = 0; $i < $card_number; $i++) {
  89. $card = Tools::genRandomChar((int) $card_length);
  90. // save to database
  91. $giftcard = new GiftCard();
  92. $giftcard->card = $card;
  93. $giftcard->balance = $card_value;
  94. $giftcard->create_time = time();
  95. $giftcard->status = 0;
  96. $giftcard->use_time = 0;
  97. $giftcard->use_user = 0;
  98. $giftcard->save();
  99. $card_added .= $card . PHP_EOL;
  100. }
  101. return $response->withJson([
  102. 'ret' => 1,
  103. 'msg' => '添加成功' . PHP_EOL . $card_added,
  104. ]);
  105. }
  106. public function delete(ServerRequest $request, Response $response, array $args): ResponseInterface
  107. {
  108. $card_id = $args['id'];
  109. (new GiftCard())->find($card_id)->delete();
  110. return $response->withJson([
  111. 'ret' => 1,
  112. 'msg' => '删除成功',
  113. ]);
  114. }
  115. public function ajax(ServerRequest $request, Response $response, array $args): ResponseInterface
  116. {
  117. $giftcards = (new GiftCard())->orderBy('id', 'desc')->get();
  118. foreach ($giftcards as $giftcard) {
  119. $giftcard->op = '<button type="button" class="btn btn-red" id="delete-gift-card-' . $giftcard->id . '"
  120. onclick="deleteGiftCard(' . $giftcard->id . ')">删除</button>';
  121. $giftcard->status = $giftcard->status();
  122. $giftcard->create_time = Tools::toDateTime((int) $giftcard->create_time);
  123. $giftcard->use_time = Tools::toDateTime((int) $giftcard->use_time);
  124. }
  125. return $response->withJson([
  126. 'giftcards' => $giftcards,
  127. ]);
  128. }
  129. }