123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/6
- * Time: 14:08
- */
- namespace App\Transformers\Circle;
- use App\Models\InterestCircle;
- use App\Models\InterestCircleUser;
- use Illuminate\Support\Facades\Redis;
- use League\Fractal\TransformerAbstract;
- class DetailTransformer extends TransformerAbstract
- {
- public function __construct($uid)
- {
- $this->uid = $uid;
- }
- public function transform(InterestCircle $interestCircle)
- {
- $answer = $interestCircle['limit_condition'] - intval(Redis::get('circle_error_count_' . $this->uid));
- $row = [
- 'id' => $interestCircle['id'],
- 'name' => $interestCircle['name'],
- 'notice' => $interestCircle['notice'],
- 'image' => $interestCircle['image'],
- 'join_limit' => $interestCircle['join_limit'],
- 'is_join' => $this->isJoin($this->uid, $interestCircle['id']),
- 'answer_count' => $answer > 0 ? $answer : 0,
- 'is_black' => $this->isBlack($this->uid, $interestCircle['id'])
- ];
- $funs = $this->getFunction($interestCircle);
- return array_merge($row, $funs);
- }
- //判读当前用户是否圈子黑名单
- public function isBlack($uid, $circleId)
- {
- $info = InterestCircleUser::where([['circle_id', $circleId], ['uid', $uid], ['is_black', 1]])->first();
- return $info ? 1 : 0;
- }
- //判读当前用户是否加入圈子
- public function isJoin($uid, $circleId)
- {
- $info = InterestCircleUser::where([['circle_id', $circleId], ['uid', $uid]])->first();
- return $info ? 1 : 0;
- }
- /**
- * 查询圈子开放功能
- * @param InterestCircle $interestCircle
- * @return array
- */
- private function getFunction(InterestCircle $interestCircle)
- {
- $functions = json_decode($interestCircle['contains_function'], true);
- $info['members'] = ['is_open' => 1, 'extra' => (string)$interestCircle['join_count']];
- $info['pictures'] = ['is_open' => 0, 'extra' => '0'];
- $info['chatroom'] = ['is_open' => 0, 'extra' => '0'];
- if (empty($functions)) {
- return $info;
- }
- foreach ($functions as &$func) {
- if ('pictures' == $func['function']) {
- if (1 == $func['is_open']) {
- $info['pictures'] = ['is_open' => 1, 'extra' => (string)$interestCircle['picture_count']];
- }
- }
- if ('chatroom' == $func['function']) {
- if (1 == $func['is_open']) {
- $info['chatroom'] = ['is_open' => 1, 'extra' => (string)$interestCircle['relate_id']];
- }
- }
- }
- return $info;
- }
- }
|