Community.php 3.8 KB

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