DetailTransformer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/6
  6. * Time: 14:08
  7. */
  8. namespace App\Transformers\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleUser;
  11. use Illuminate\Support\Facades\Redis;
  12. use League\Fractal\TransformerAbstract;
  13. class DetailTransformer extends TransformerAbstract
  14. {
  15. public function __construct($uid)
  16. {
  17. $this->uid = $uid;
  18. }
  19. public function transform(InterestCircle $interestCircle)
  20. {
  21. $answer = $interestCircle['limit_condition'] - intval(Redis::get('circle_error_count_' . $this->uid));
  22. $row = [
  23. 'id' => $interestCircle['id'],
  24. 'name' => $interestCircle['name'],
  25. 'notice' => $interestCircle['notice'],
  26. 'image' => $interestCircle['image'],
  27. 'join_limit' => $interestCircle['join_limit'],
  28. 'is_join' => $this->isJoin($this->uid, $interestCircle['id']),
  29. 'answer_count' => $answer > 0 ? $answer : 0,
  30. 'is_black' => $this->isBlack($this->uid, $interestCircle['id'])
  31. ];
  32. $funs = $this->getFunction($interestCircle);
  33. return array_merge($row, $funs);
  34. }
  35. //判读当前用户是否圈子黑名单
  36. public function isBlack($uid, $circleId)
  37. {
  38. $info = InterestCircleUser::where([['circle_id', $circleId], ['uid', $uid], ['is_black', 1]])->first();
  39. return $info ? 1 : 0;
  40. }
  41. //判读当前用户是否加入圈子
  42. public function isJoin($uid, $circleId)
  43. {
  44. $info = InterestCircleUser::where([['circle_id', $circleId], ['uid', $uid]])->first();
  45. return $info ? 1 : 0;
  46. }
  47. /**
  48. * 查询圈子开放功能
  49. * @param InterestCircle $interestCircle
  50. * @return array
  51. */
  52. private function getFunction(InterestCircle $interestCircle)
  53. {
  54. $functions = json_decode($interestCircle['contains_function'], true);
  55. $info['members'] = ['is_open' => 1, 'extra' => (string)$interestCircle['join_count']];
  56. $info['pictures'] = ['is_open' => 0, 'extra' => '0'];
  57. $info['chatroom'] = ['is_open' => 0, 'extra' => '0'];
  58. if (empty($functions)) {
  59. return $info;
  60. }
  61. foreach ($functions as &$func) {
  62. if ('pictures' == $func['function']) {
  63. if (1 == $func['is_open']) {
  64. $info['pictures'] = ['is_open' => 1, 'extra' => (string)$interestCircle['picture_count']];
  65. }
  66. }
  67. if ('chatroom' == $func['function']) {
  68. if (1 == $func['is_open']) {
  69. $info['chatroom'] = ['is_open' => 1, 'extra' => (string)$interestCircle['relate_id']];
  70. }
  71. }
  72. }
  73. return $info;
  74. }
  75. }