ChatBuy.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\controller;
  4. use app\model\api\ChatBalanceLog;
  5. use app\model\api\ChatOrder;
  6. use app\services\chat\ChatBalanceService;
  7. use library\Helper;
  8. use library\JwtAuth;
  9. use think\Response;
  10. /**
  11. * 聊天次数购买控制器
  12. */
  13. class ChatBuy extends JwtAuth
  14. {
  15. protected $noNeedLogin = [];
  16. /**
  17. * 获取购买配置信息
  18. * @return Response
  19. */
  20. public function config(): Response
  21. {
  22. $price = ChatBalanceService::getBuyPrice();
  23. $balance = ChatBalanceService::getBalance($this->userId);
  24. return $this->success('获取成功', [
  25. 'price' => $price, // 单次购买价格(积分)
  26. 'balance' => $balance, // 当前剩余次数
  27. ]);
  28. }
  29. /**
  30. * 创建购买订单
  31. * @return Response
  32. */
  33. public function createOrder(): Response
  34. {
  35. $data = $this->request->post();
  36. $chatNum = (int)($data['chat_num'] ?? 1);
  37. if ($chatNum <= 0) {
  38. return $this->fail('购买数量必须大于0');
  39. }
  40. $price = ChatBalanceService::getBuyPrice();
  41. if ($price <= 0) {
  42. return $this->fail('购买价格未设置');
  43. }
  44. $totalPrice = $price * $chatNum;
  45. // 检查用户积分是否足够
  46. $userScore = \app\model\api\UserScoreDetail::getUserScore($this->userId);
  47. if ($userScore < $totalPrice) {
  48. return $this->fail('积分不足,当前积分:' . $userScore);
  49. }
  50. // 创建订单
  51. $order = ChatOrder::createOrder($this->userId, $chatNum, $totalPrice);
  52. if (!$order) {
  53. return $this->fail('订单创建失败');
  54. }
  55. // 立即支付(积分支付,无需跳转)
  56. $result = ChatBalanceService::buyWithScore($this->userId, $chatNum, $order);
  57. if (!$result) {
  58. // 支付失败,取消订单
  59. ChatOrder::cancelOrder($order->id);
  60. return $this->fail('支付失败');
  61. }
  62. // 更新订单状态
  63. ChatOrder::paySuccess($order->id);
  64. $newBalance = ChatBalanceService::getBalance($this->userId);
  65. return $this->success('购买成功', [
  66. 'order_id' => $order->id,
  67. 'order_no' => $order->order_no,
  68. 'chat_num' => $chatNum,
  69. 'price' => $totalPrice,
  70. 'balance' => $newBalance,
  71. ]);
  72. }
  73. /**
  74. * 获取当前聊天次数余额
  75. * @return Response
  76. */
  77. public function balance(): Response
  78. {
  79. $balance = ChatBalanceService::getBalance($this->userId);
  80. return $this->success('获取成功', [
  81. 'balance' => $balance,
  82. ]);
  83. }
  84. /**
  85. * 获取购买记录列表
  86. * @return Response
  87. */
  88. public function orderList(): Response
  89. {
  90. $data = $this->request->get();
  91. $page = (int)($data['page'] ?? 1);
  92. $limit = (int)($data['limit'] ?? 10);
  93. $result = ChatOrder::getUserOrders($this->userId, $page, $limit);
  94. // 处理订单状态文本
  95. foreach ($result['list'] as &$item) {
  96. $item['status_text'] = ChatOrder::getStatusText($item['status']);
  97. }
  98. return $this->success('获取成功', $result);
  99. }
  100. /**
  101. * 获取变动明细列表
  102. * @return Response
  103. */
  104. public function balanceLogList(): Response
  105. {
  106. $data = $this->request->get();
  107. $page = (int)($data['page'] ?? 1);
  108. $limit = (int)($data['limit'] ?? 10);
  109. $result = ChatBalanceLog::getUserLogs($this->userId, $page, $limit);
  110. return $this->success('获取成功', $result);
  111. }
  112. }