Store.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Index.php
  4. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  5. * =========================================================
  6. * Copy right 2015-2025 杭州牛之云科技有限公司, 保留所有权利。
  7. * ----------------------------------------------
  8. * 官方网址: https://www.niushop.com
  9. * =========================================================
  10. * @author : niuteam
  11. * @date : 2022.8.8
  12. * @version : v5.0.0.1
  13. */
  14. namespace app\api\controller;
  15. use app\model\store\Store as StoreModel;
  16. use extend\api\HttpClient;
  17. use app\model\web\Config as ConfigModel;
  18. use app\model\goods\Goods;
  19. /**
  20. * 门店
  21. * @author Administrator
  22. *
  23. */
  24. class Store extends BaseApi
  25. {
  26. /**
  27. * 列表信息
  28. */
  29. public function page()
  30. {
  31. $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : null; // 纬度
  32. $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : null; // 经度
  33. $keyword = isset($this->params[ 'keyword' ]) ? $this->params[ 'keyword' ] : ''; // 搜索关键词
  34. $store_model = new StoreModel();
  35. $condition = [
  36. [ 'site_id', "=", $this->site_id ],
  37. [ 'status', '=', 1 ],
  38. [ 'is_frozen', '=', 0 ]
  39. ];
  40. if (!empty($keyword)) {
  41. $condition[] = [ 'store_name', 'like', '%' . $keyword . '%' ];
  42. }
  43. $latlng = array (
  44. 'lat' => $latitude,
  45. 'lng' => $longitude,
  46. );
  47. $field = '*';
  48. $list_result = $store_model->getLocationStoreList($condition, $field, $latlng);
  49. $list = $list_result[ 'data' ];
  50. if (!empty($longitude) && !empty($latitude) && !empty($list)) {
  51. foreach ($list as $k => $item) {
  52. if ($item[ 'longitude' ] && $item[ 'latitude' ]) {
  53. $distance = getDistance((float) $item[ 'longitude' ], (float) $item[ 'latitude' ], (float) $longitude, (float) $latitude);
  54. $list[ $k ][ 'distance' ] = $distance / 1000;
  55. } else {
  56. $list[ $k ][ 'distance' ] = 0;
  57. }
  58. }
  59. // 按距离就近排序
  60. array_multisort(array_column($list, 'distance'), SORT_ASC, $list);
  61. }
  62. $default_store_id = 0;
  63. if (!empty($list)) {
  64. $default_store_id = $list[ 0 ][ 'store_id' ];
  65. }
  66. return $this->response($this->success([ 'list' => $list, 'store_id' => $default_store_id ]));
  67. }
  68. /**
  69. * 获取离自己最近的一个门店
  70. */
  71. public function nearestStore()
  72. {
  73. $this->initStoreData();
  74. $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : null; // 纬度
  75. $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : null; // 经度
  76. $store_model = new StoreModel();
  77. $condition = [
  78. [ 'site_id', "=", $this->site_id ],
  79. [ 'status', '=', 1 ],
  80. [ 'is_frozen', '=', 0 ]
  81. ];
  82. if ($this->store_data[ 'config' ][ 'store_business' ] == 'shop') {
  83. // 平台运营模式,直接取默认门店
  84. if (!empty($this->store_data[ 'store_info' ])) {
  85. return $this->response($this->success($this->store_data[ 'store_info' ]));
  86. }
  87. } elseif ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  88. // 连锁门店运营模式,查询距离自己最近的门店
  89. $latlng = array (
  90. 'lat' => $latitude,
  91. 'lng' => $longitude,
  92. );
  93. $field = '*';
  94. $list = $store_model->getLocationStoreList($condition, $field, $latlng)[ 'data' ];
  95. if (!empty($longitude) && !empty($latitude) && !empty($list)) {
  96. foreach ($list as $k => $item) {
  97. if ($item[ 'longitude' ] && $item[ 'latitude' ]) {
  98. $distance = getDistance((float) $item[ 'longitude' ], (float) $item[ 'latitude' ], (float) $longitude, (float) $latitude);
  99. $list[ $k ][ 'distance' ] = $distance / 1000;
  100. } else {
  101. $list[ $k ][ 'distance' ] = 0;
  102. }
  103. }
  104. // 按距离就近排序
  105. array_multisort(array_column($list, 'distance'), SORT_ASC, $list);
  106. return $this->response($this->success($list[ 0 ]));
  107. }
  108. }
  109. return $this->response($this->error());
  110. }
  111. /**
  112. * 基础信息
  113. * @return false|string
  114. */
  115. public function info()
  116. {
  117. $store_id = isset($this->params[ 'store_id' ]) ? $this->params[ 'store_id' ] : 0;
  118. $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : null; // 纬度
  119. $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : null; // 经度
  120. if (empty($store_id)) {
  121. return $this->response($this->error('', 'REQUEST_STORE_ID'));
  122. }
  123. $condition = [
  124. [ 'store_id', "=", $store_id ],
  125. [ 'site_id', "=", $this->site_id ],
  126. [ 'status', '=', 1 ]
  127. ];
  128. $latlng = array (
  129. 'lat' => $latitude,
  130. 'lng' => $longitude,
  131. );
  132. $store_model = new StoreModel();
  133. $field = 'store_id,store_name,telphone,store_image,site_id,address,full_address,longitude,latitude,open_date,label_name,store_images,store_introduce,is_default,is_express,is_pickup,is_o2o';
  134. if (!empty($latlng[ 'lat' ]) && !empty($latlng[ 'lng' ])) {
  135. $field .= ',FORMAT(st_distance ( point ( ' . $latlng[ 'lng' ] . ', ' . $latlng[ 'lat' ] . ' ), point ( longitude, latitude ) ) * 111195 / 1000, 2) as distance';
  136. }
  137. $res = $store_model->getStoreInfo($condition, $field);
  138. if (!empty($res[ 'data' ])) {
  139. if (!empty($res[ 'data' ][ 'store_images' ])) {
  140. $res[ 'data' ][ 'store_images' ] = ( new Goods() )->getGoodsImage(explode(',', $res[ 'data' ][ 'store_images' ]), $this->site_id)[ 'data' ];
  141. }
  142. }
  143. return $this->response($res);
  144. }
  145. /**
  146. * 根据经纬度获取位置
  147. * 文档:https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
  148. * 示例:https://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&get_poi=1
  149. * @return false|string
  150. */
  151. public function getLocation()
  152. {
  153. $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : null; // 纬度
  154. $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : null; // 经度
  155. $post_url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=';
  156. $config_model = new ConfigModel();
  157. $config_result = $config_model->getMapConfig()[ 'data' ] ?? [];
  158. $config = $config_result[ 'value' ] ?? [];
  159. $tencent_map_key = $config[ 'tencent_map_key' ] ?? '';
  160. if (empty($tencent_map_key)) {
  161. return $this->response($this->error('未配置腾讯地图KEY'));
  162. }
  163. if (empty($latitude) && empty($longitude)) {
  164. return $this->response($this->error('缺少经纬度'));
  165. }
  166. $post_data = array (
  167. 'location' => $latitude . ',' . $longitude,
  168. 'key' => $tencent_map_key,
  169. 'get_poi' => 0,//是否返回周边POI列表:1.返回;0不返回(默认)
  170. );
  171. $httpClient = new HttpClient();
  172. $res = $httpClient->post($post_url, $post_data);
  173. $res = json_decode($res, true);
  174. if ($res[ 'status' ] == 0) {
  175. $result = $res[ 'result' ];
  176. return $this->response($this->success($result));
  177. } else {
  178. return $this->response($this->error('', $res[ 'message' ]));
  179. }
  180. }
  181. }