DetailTransformer.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. ];
  31. $funs = $this->getFunction($interestCircle);
  32. return array_merge($row, $funs);
  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['members'] = ['is_open' => 1, 'extra' => (string)$interestCircle['join_count']];
  49. $info['pictures'] = ['is_open' => 0, 'extra' => '0'];
  50. $info['chatroom'] = ['is_open' => 0, 'extra' => '0'];
  51. if (empty($functions)) {
  52. return $info;
  53. }
  54. foreach ($functions as &$func) {
  55. if ('pictures' == $func['function']) {
  56. if (1 == $func['is_open']) {
  57. $info['pictures'] = ['is_open' => 1, 'extra' => (string)$interestCircle['picture_count']];
  58. }
  59. }
  60. if ('chatroom' == $func['function']) {
  61. if (1 == $func['is_open']) {
  62. $info['chatroom'] = ['is_open' => 1, 'extra' => (string)$interestCircle['relate_id']];
  63. }
  64. }
  65. }
  66. return $info;
  67. }
  68. }