| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * @Author: your name
- * @Date: 2021-03-02 09:50:20
- * @LastEditTime: 2021-04-27 17:34:18
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: \admin_php\modules\client\models\v1\ShopListForm.php
- */
- namespace app\modules\client\models\v1;
- use app\cyy\ApiResponse;
- use app\models\Shop;
- use app\models\User;
- use yii\data\Pagination;
- use yii\helpers\ArrayHelper;
- use app\modules\client\models\ApiModel;
- class ShopListForm extends ApiModel
- {
- public $store_id;
- public $user;
- public $longitude;
- public $latitude;
- public $page;
- public $limit;
- public $keyword;
- public function rules()
- {
- return [
- [['longitude', 'latitude','keyword'], 'trim'],
- [['page'], 'integer'],
- [['page'], 'default', 'value' => 1],
- [['limit'], 'integer'],
- [['limit'], 'default', 'value' => 20],
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- $shop_table_name = Shop::tableName();
- $store_id = get_store_id();
- $count_sql = 'SELECT count(1) AS total FROM '. $shop_table_name;
- $count_sql .= ' WHERE store_id = ' .$store_id. ' AND is_delete = 0 ';
- if ($this->keyword) {
- $count_sql .= ' AND name LIKE "%'.$this->keyword.'%"';
- }
- $count_sql = \Yii::$app->db->quoteSql($count_sql);
- $count = \Yii::$app->db->createCommand($count_sql, [
- ':store_id' => $this->store_id,
- ])->queryScalar();
- $latitude = $this->latitude ? $this->latitude : 0;
- $longitude = $this->longitude ? $this->longitude : 0;
- $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
- distance FROM {$shop_table_name} WHERE ((`store_id`={$store_id}) AND (`is_delete`=0)) ";
- if ($this->keyword) {
- $sql .= " AND (`name` like '%{$this->keyword}%') ";
- }
- $sql .= ' ORDER BY distance ASC';
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
- $sql .= ' LIMIT ' . $pagination->offset . ',' . $pagination->limit;
- $sql = \Yii::$app->db->quoteSql($sql);
- $list = \Yii::$app->db->createCommand($sql, [
- ':latitude' => $latitude,
- ':longitude' => $longitude,
- ':store_id' => $store_id,
- ])->queryAll();
- foreach ($list as $index => $item) {
- $list[$index]['score'] = (int)$item['score'];
- $list[$index]['distance'] = self::distance($item['distance']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- 'page_count' => $pagination->pageCount,
- 'row_count' => $count
- ]
- ];
- }
- private static function distance($distance)
- {
- if ($distance == -1) {
- return -1;
- }
- if ($distance > 1000) {
- $distance = round($distance / 1000, 2) . 'km';
- } else {
- $distance = round($distance, 2);
- $distance .= 'm';
- }
- return $distance;
- }
- }
|