UserTransformer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Transformer\User;
  3. use Vinkla\Hashids\Facades\Hashids;
  4. class UserTransformer
  5. {
  6. public function newBuildUserShelfBooks($data): array
  7. {
  8. return [
  9. 'list' => $this->newEachUserShelfBooks($data)
  10. ];
  11. }
  12. private function newEachUserShelfBooks($list): array
  13. {
  14. $result = [];
  15. if (empty($list)) return $result;
  16. foreach ($list as $item) {
  17. $result[] = [
  18. 'bid' => Hashids::encode(getProp($item, 'id')),
  19. 'book_name' => getProp($item, 'name'),
  20. 'cover' => addPrefix(getProp($item, 'cover')),
  21. 'read_time' => transDate(getProp($item, 'read_time')),
  22. ];
  23. }
  24. return $result;
  25. }
  26. public function newBuildEarnings($data): array
  27. {
  28. return [
  29. 'earnings' => $data['earnings'],
  30. 'earning_records' => $this->newEachEarningRecords($data['earning_records']),
  31. 'invite_records' => $this->newEachInviteRecords($data['invite_records']),
  32. ];
  33. }
  34. private function newEachEarningRecords($list): array
  35. {
  36. $result = [];
  37. if (empty($list)) return $result;
  38. foreach ($list as $item) {
  39. switch ($item['change_type']) {
  40. case '1':
  41. $info = $item['nickname'].' 充值'.$item['charge_price'].'元';
  42. $price = '+'.$item['change_amount'].'元';
  43. $price_type = 1;
  44. break;
  45. case '2':
  46. $info = '提现 '.$item['change_amount'].'元';
  47. $price = '-'.$item['change_amount'].'元';
  48. $price_type = 0;
  49. break;
  50. case '3':
  51. $info = '提现失败 '.$item['change_amount'].'元';
  52. $price = '+'.$item['change_amount'].'元';
  53. $price_type = 1;
  54. break;
  55. default:
  56. $info = '';
  57. $price = $item['change_amount'].'元';
  58. $price_type = 1;
  59. }
  60. $result[] = [
  61. 'info' => $info,
  62. 'change_amount' => $price,
  63. 'date' => transDate(getProp($item, 'created_at'), 'Y-m-d'),
  64. 'after_amount' => getProp($item, 'after_amount').'元',
  65. 'price_type' => $price_type
  66. ];
  67. }
  68. return $result;
  69. }
  70. private function newEachInviteRecords($list): array
  71. {
  72. $result = [];
  73. if (empty($list)) return $result;
  74. foreach ($list as $item) {
  75. $result[] = [
  76. 'nickname' => getProp($item, 'nickname'),
  77. 'date' => transDate(getProp($item, 'bind_time'), 'Y-m-d')
  78. ];
  79. }
  80. return $result;
  81. }
  82. // 当前阅读
  83. public function newBuildCurrentBook($data): array
  84. {
  85. if (empty($data)) {
  86. return [];
  87. }
  88. $data = (array)$data;
  89. $data['bid'] = Hashids::encode($data['bid']);
  90. $data['cover'] = addPrefix($data['cover']);
  91. return $data;
  92. }
  93. }