123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Transformer\User;
- use Vinkla\Hashids\Facades\Hashids;
- class UserTransformer
- {
- public function newBuildUserShelfBooks($data): array
- {
- return [
- 'list' => $this->newEachUserShelfBooks($data)
- ];
- }
- private function newEachUserShelfBooks($list): array
- {
- $result = [];
- if (empty($list)) return $result;
- foreach ($list as $item) {
- $result[] = [
- 'bid' => Hashids::encode(getProp($item, 'id')),
- 'book_name' => getProp($item, 'name'),
- 'cover' => addPrefix(getProp($item, 'cover')),
- 'read_time' => transDate(getProp($item, 'read_time')),
- ];
- }
- return $result;
- }
- public function newBuildEarnings($data): array
- {
- return [
- 'earnings' => $data['earnings'],
- 'earning_records' => $this->newEachEarningRecords($data['earning_records']),
- 'invite_records' => $this->newEachInviteRecords($data['invite_records']),
- ];
- }
- private function newEachEarningRecords($list): array
- {
- $result = [];
- if (empty($list)) return $result;
- foreach ($list as $item) {
- switch ($item['change_type']) {
- case '1':
- $info = $item['nickname'].' 充值'.$item['charge_price'].'元';
- $price = '+'.$item['change_amount'].'元';
- $price_type = 1;
- break;
- case '2':
- $info = '提现 '.$item['change_amount'].'元';
- $price = '-'.$item['change_amount'].'元';
- $price_type = 0;
- break;
- case '3':
- $info = '提现失败 '.$item['change_amount'].'元';
- $price = '+'.$item['change_amount'].'元';
- $price_type = 1;
- break;
- default:
- $info = '';
- $price = $item['change_amount'].'元';
- $price_type = 1;
- }
- $result[] = [
- 'info' => $info,
- 'change_amount' => $price,
- 'date' => transDate(getProp($item, 'created_at'), 'Y-m-d'),
- 'after_amount' => getProp($item, 'after_amount').'元',
- 'price_type' => $price_type
- ];
- }
- return $result;
- }
- private function newEachInviteRecords($list): array
- {
- $result = [];
- if (empty($list)) return $result;
- foreach ($list as $item) {
- $result[] = [
- 'nickname' => getProp($item, 'nickname'),
- 'date' => transDate(getProp($item, 'bind_time'), 'Y-m-d')
- ];
- }
- return $result;
- }
- // 当前阅读
- public function newBuildCurrentBook($data): array
- {
- if (empty($data)) {
- return [];
- }
- $data = (array)$data;
- $data['bid'] = Hashids::encode($data['bid']);
- $data['cover'] = addPrefix($data['cover']);
- return $data;
- }
- }
|