Community.php 3.9 KB

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