12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?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\Log;
- 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)
- {
- if ($interestCircle['limit_condition']) {
- $answer = $interestCircle['limit_condition'] - intval(Redis::get('circle_error_count_' . $this->uid));
- } else {
- $answer = -1;
- }
- $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,
- '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();
- Log::debug('blacklist:'.json_encode($info));
- 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' => intval($interestCircle['join_count'])];
- $info['pictures'] = ['is_open' => 0, 'extra' => 0];
- $info['chatroom'] = ['is_open' => 0, 'extra' => ''];
- if (empty($functions)) {
- return $info;
- }
- foreach ($functions as &$func) {
- if ('pictures' == $func['function']) {
- if (1 == $func['is_open']) {
- $info['pictures'] = ['is_open' => 1, 'extra' => intval($interestCircle['picture_count'])];
- }
- }
- if ('chatroom' == $func['function']) {
- if (1 == $func['is_open']) {
- $info['chatroom'] = ['is_open' => 1, 'extra' => (string)$func['relate_id']];
- }
- }
- }
- return $info;
- }
- }
|