Community.php 3.8 KB

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