| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- /**
- * 话题分页返回封装.
- *
- * @author nj <nj@gmail.com>
- * @date 2020-01-28 20:46:56
- */
- declare(strict_types=1);
- namespace Modules\Api\Responses;
- use App\Contracts\Responsable;
- class TopicPagesResponse implements Responsable
- {
- /**
- * 录入的数据.
- *
- * @var mixed
- */
- private $data = null;
- /**
- * 附加条件参数.
- *
- * @var array
- */
- private $params = [];
- public function __construct($data = null, $params = [])
- {
- $this->data = $data;
- $this->params = $params;
- }
- public function toResponse(
- $request = null
- ) {
- $data = [];
- $result = $this->data->toArray();
- if (!empty($result['data'])) {
- foreach ($result['data'] as $k => $v) {
- $user = !empty($v['user']) ? collect($v['user'])->only([
- 'nick', 'avatar', 'code',
- ]) : [];
- $category = !empty($v['category']) ? collect($v['category'])->only([
- 'id', 'name', 'cover_pic', ' total_topic',
- ]) : [];
- $data[$k] = collect($v)->only([
- 'id', 'created_at', 'user_id', 'category_id', 'content', 'ct_type', 'cover_pic', 'video_id',
- 'total_comment', 'total_share', 'total_support', 'is_top', 'is_digest', 'sticked_at',
- ]);
- if (empty($v['sdata'])) {
- $jdata = [];
- } else {
- $jdata = json_decode($v['sdata'], true);
- }
- if (empty($jdata)) {
- // 当数据是空的时候,为了前端识别固定类型,强制转空对象.
- $jdata = new \stdClass();
- }
- $data[$k]['user'] = $user;
- $data[$k]['category'] = $category;
- $data[$k]['jdata'] = $jdata;
- }
- }
- $result['data'] = $data;
- unset($v, $user, $category, $jdata);
- return json_success($result);
- }
- }
|