DetailTransformer.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. return [
  22. 'id' => $interestCircle['id'],
  23. 'name' => $interestCircle['name'],
  24. 'notice' => $interestCircle['notice'],
  25. 'image' => $interestCircle['image'],
  26. 'functions' => $this->getFunction($interestCircle),
  27. 'join_limit' => $interestCircle['join_limit'],
  28. //'limit_condition' => $interestCircle['limit_condition'],
  29. //'join_question' => (json_decode($interestCircle['join_question'], true)) ?? [],
  30. 'is_join' => $this->isJoin($this->uid, $interestCircle['id']),
  31. //'answer_error_count' => intval(Redis::get('circle_error_count_'.$this->uid))
  32. ];
  33. }
  34. //判读当前用户是否加入圈子
  35. public function isJoin($uid, $circleId)
  36. {
  37. $info = InterestCircleUser::where([['circle_id', $circleId], ['uid', $uid]])->first();
  38. return $info ? 1 : 0;
  39. }
  40. /**
  41. * 查询圈子开放功能
  42. * @param InterestCircle $interestCircle
  43. * @return array
  44. */
  45. private function getFunction(InterestCircle $interestCircle)
  46. {
  47. $functions = json_decode($interestCircle['contains_function'], true);
  48. $info[] = ['name' => 'members', 'is_open' => 1, 'extra' => (string)$interestCircle['join_count']];
  49. foreach ($functions as &$func) {
  50. if ('pictures' == $func['function']) {
  51. if (1 == $func['is_open']) {
  52. $info[] = ['name' => 'pictures', 'is_open' => 1, 'extra' => (string)$interestCircle['picture_count']];
  53. } else {
  54. $info[] = ['name' => 'pictures', 'is_open' => 0, 'extra' => '0'];
  55. }
  56. }
  57. if ('chatroom' == $func['function']) {
  58. if (1 == $func['is_open']) {
  59. $info[] = ['name' => 'chatroom', 'is_open' => 1, 'extra' => (string)$interestCircle['relate_id']];
  60. } else {
  61. $info[] = ['name' => 'chatroom', 'is_open' => 0, 'extra' => '0'];
  62. }
  63. }
  64. }
  65. return $info;
  66. }
  67. }