ShopListForm.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: your name
  9. * @Date: 2021-03-02 09:50:20
  10. * @LastEditTime: 2021-04-27 17:34:18
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\models\v1\ShopListForm.php
  14. */
  15. namespace app\modules\client\models\v1;
  16. use app\cyy\ApiResponse;
  17. use app\models\Shop;
  18. use app\models\User;
  19. use yii\data\Pagination;
  20. use yii\helpers\ArrayHelper;
  21. use app\modules\client\models\ApiModel;
  22. class ShopListForm extends ApiModel
  23. {
  24. public $store_id;
  25. public $user;
  26. public $longitude;
  27. public $latitude;
  28. public $page;
  29. public $limit;
  30. public $keyword;
  31. public function rules()
  32. {
  33. return [
  34. [['longitude', 'latitude','keyword'], 'trim'],
  35. [['page'], 'integer'],
  36. [['page'], 'default', 'value' => 1],
  37. [['limit'], 'integer'],
  38. [['limit'], 'default', 'value' => 20],
  39. ];
  40. }
  41. public function search()
  42. {
  43. if (!$this->validate()) {
  44. return [
  45. 'code' => 1,
  46. 'msg' => $this->getErrorSummary(false)[0]
  47. ];
  48. }
  49. $shop_table_name = Shop::tableName();
  50. $store_id = get_store_id();
  51. $count_sql = 'SELECT count(1) AS total FROM '. $shop_table_name;
  52. $count_sql .= ' WHERE store_id = ' .$store_id. ' AND is_delete = 0 ';
  53. if ($this->keyword) {
  54. $count_sql .= ' AND name LIKE "%'.$this->keyword.'%"';
  55. }
  56. $count_sql = \Yii::$app->db->quoteSql($count_sql);
  57. $count = \Yii::$app->db->createCommand($count_sql, [
  58. ':store_id' => $this->store_id,
  59. ])->queryScalar();
  60. $latitude = $this->latitude ? $this->latitude : 0;
  61. $longitude = $this->longitude ? $this->longitude : 0;
  62. $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as
  63. distance FROM {$shop_table_name} WHERE ((`store_id`={$store_id}) AND (`is_delete`=0)) ";
  64. if ($this->keyword) {
  65. $sql .= " AND (`name` like '%{$this->keyword}%') ";
  66. }
  67. $sql .= ' ORDER BY distance ASC';
  68. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  69. $sql .= ' LIMIT ' . $pagination->offset . ',' . $pagination->limit;
  70. $sql = \Yii::$app->db->quoteSql($sql);
  71. $list = \Yii::$app->db->createCommand($sql, [
  72. ':latitude' => $latitude,
  73. ':longitude' => $longitude,
  74. ':store_id' => $store_id,
  75. ])->queryAll();
  76. foreach ($list as $index => $item) {
  77. $list[$index]['score'] = (int)$item['score'];
  78. $list[$index]['distance'] = self::distance($item['distance']);
  79. }
  80. return [
  81. 'code' => 0,
  82. 'msg' => 'success',
  83. 'data' => [
  84. 'list' => $list,
  85. 'page_count' => $pagination->pageCount,
  86. 'row_count' => $count
  87. ]
  88. ];
  89. }
  90. private static function distance($distance)
  91. {
  92. if ($distance == -1) {
  93. return -1;
  94. }
  95. if ($distance > 1000) {
  96. $distance = round($distance / 1000, 2) . 'km';
  97. } else {
  98. $distance = round($distance, 2);
  99. $distance .= 'm';
  100. }
  101. return $distance;
  102. }
  103. }