PointsTransformer.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Transformer\Points;
  3. class PointsTransformer
  4. {
  5. const TYPE_LABELS = [
  6. 'video' => '视频生成',
  7. 'video_refund' => '视频生成退款',
  8. 'image' => '图片生成',
  9. 'image_refund' => '图片生成退款',
  10. 'chat' => 'AI对话',
  11. 'chat_refund' => 'AI对话退款',
  12. ];
  13. /**
  14. * 积分流水列表
  15. *
  16. * @param $data
  17. * @return array
  18. */
  19. public function newBuildPointsRecords($data): array
  20. {
  21. return [
  22. 'meta' => getMeta($data),
  23. 'list' => $this->newEachPointsRecord($data),
  24. ];
  25. }
  26. /**
  27. * 单条积分流水
  28. *
  29. * @param $list
  30. * @return array
  31. */
  32. public function newEachPointsRecord($list): array
  33. {
  34. $result = [];
  35. if (empty($list)) return $result;
  36. foreach ($list as $item) {
  37. $chargeInfo = getProp($item, 'charge_info');
  38. if (is_string($chargeInfo)) {
  39. $chargeInfo = json_decode($chargeInfo, true);
  40. }
  41. $chargeInfo = is_array($chargeInfo) ? $chargeInfo : [];
  42. $type = getProp($item, 'type', '');
  43. $result[] = [
  44. 'id' => (int)getProp($item, 'id'),
  45. 'type' => $type,
  46. 'type_label' => self::TYPE_LABELS[$type] ?? $type,
  47. 'api_type' => getProp($item, 'api_type'),
  48. 'task_id' => (int)getProp($item, 'task_id'),
  49. 'charge_info' => [
  50. 'model' => getProp($chargeInfo, 'model'),
  51. 'modality' => getProp($chargeInfo, 'modality'),
  52. 'video_resolution' => getProp($chargeInfo, 'video_resolution'),
  53. 'video_duration' => getProp($chargeInfo, 'video_duration'),
  54. 'ratio' => getProp($chargeInfo, 'ratio'),
  55. ],
  56. 'points_consumed' => (float)getProp($item, 'points_consumed', 0),
  57. 'points_before' => (float)getProp($item, 'points_before', 0),
  58. 'points_after' => (float)getProp($item, 'points_after', 0),
  59. 'tokens_consumed' => (int)getProp($item, 'tokens_consumed', 0),
  60. 'remark' => getProp($item, 'remark'),
  61. 'created_at' => getProp($item, 'created_at'),
  62. ];
  63. }
  64. return $result;
  65. }
  66. }