DetailTransformer.php 2.9 KB

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