Community.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\dynamic\Comment;
  4. use app\admin\model\dynamic\Dynamic;
  5. use app\admin\model\dynamic\Like;
  6. use app\admin\model\dynamic\Topic;
  7. use app\api\model\massager\Collect;
  8. use app\api\validate\BaseApiValidate;
  9. use app\common\controller\Api;
  10. use think\Request;
  11. /**
  12. * 评论
  13. */
  14. class Community extends Api
  15. {
  16. protected $noNeedLogin = ['*'];
  17. protected $massagerModel = null;
  18. public function __construct(Request $request = null)
  19. {
  20. parent::__construct($request);
  21. $this->massagerModel = new \app\api\model\massager\Massager();
  22. }
  23. public function fetch()
  24. {
  25. $params = (new BaseApiValidate([
  26. "lng" => "require|number",
  27. "lat" => "require|number",
  28. "page" => "require|number",
  29. "size" => "require|number",
  30. "hot" => "number",
  31. "topic_id" => "number",
  32. "local" => "number",
  33. "city_code" => "number",
  34. "collect" => "number",
  35. ]))->checkBody();
  36. $dynamicModel = new Dynamic();
  37. $where = [
  38. ['status', "=", \E_BASE_STATUS::Normal],
  39. ];
  40. $subQuery = $dynamicModel->field("*,round(st_distance_sphere (point({$params["lng"]},{$params["lat"]}), point ( `lng`, `lat` ))) distance");
  41. if (isset($params["hot"]) && $params["hot"] == 1) {
  42. $where[] = ["hot", "=", 1];
  43. }
  44. if (isset($params["topic_id"]) && $params["topic_id"] > 0) {
  45. $where[] = ["topic_id", "=", $params["topic_id"]];
  46. }
  47. if (isset($params["local"]) && $params["local"] == 1) {
  48. if (!isset($params["city_code"]))
  49. $this->error("定位错误!");
  50. $where[] = ["city_code", "=", $params["city_code"]];
  51. }
  52. if (isset($params["collect"]) && $params["collect"] == 1) {
  53. $user = $this->auth->getUser();
  54. $collects = (new Collect())->where("user_id", $user ? $user["id"] : -1)->select();
  55. $where[] = ["massager_id", "in", array_map(function ($item) {
  56. return $item["massager_id"];
  57. }, $collects)];
  58. }
  59. if (isset($params["search_text"]) && mb_strlen($params["search_text"]) > 0) {
  60. $where[] = ["massager.name", "like", "%{$params["search_text"]}%"];
  61. }
  62. foreach ($where as $item) {
  63. $subQuery->where($item[0], $item[1], $item[2]);
  64. }
  65. $query = $dynamicModel->table($subQuery->buildSql() . ' distance');
  66. foreach ($where as $item) {
  67. $query->where($item[0], $item[1], $item[2]);
  68. }
  69. $paginate = $query
  70. ->page($params["page"])
  71. ->paginate($params["size"]);
  72. $items = collection($paginate->items())->toArray();
  73. $massager_ids = array_map(function ($data) {
  74. return $data["massager_id"];
  75. }, $items);
  76. $massagers = $this->massagerModel->where("id", "in", $massager_ids)->select();
  77. $f_massager = array_reduce($massagers, function ($p, $cur) {
  78. $p[$cur["id"]] = $cur;
  79. return $p;
  80. }, []);
  81. $topicModel = new Topic();
  82. $likeModel = new Like();
  83. $commentModel = new Comment();
  84. foreach ($items as &$item) {
  85. $item["massager"] = $f_massager[$item["massager_id"]] ?? ["id" => null, "name" => null];
  86. $item["topic"] = $topicModel->get($item["topic_id"]);
  87. $item["like_count"] = $likeModel->where("dynamic_id", "=", $item["id"])->count();
  88. $item["comment_count"] = $commentModel->where("status", "normal")->where("dynamic_id", $item["id"])->count();
  89. }
  90. $this->success([
  91. $items,
  92. $paginate->total()
  93. ]);
  94. }
  95. public function getDynamic($id)
  96. {
  97. $res = (new Dynamic())
  98. ->where("id", $id)
  99. ->with(["comments", "likes", "comments.user"])
  100. ->find();
  101. $user = $this->auth->getUser();
  102. $res["is_can_like"] = true;
  103. if ($user) {
  104. $q = array_column($res["likes"], "user_id", "user_id");
  105. if (isset($q[$user["id"]]))
  106. $res["is_can_like"] = false;
  107. }
  108. $res ? $this->success($res) : $this->error("数据不存在!");
  109. }
  110. }