index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div class="shop-distribution-page">
  3. <!-- 页面头部 -->
  4. <div class="page-header">
  5. <div class="header-left">
  6. <h2 class="page-title">🗺️ 门店分布地图</h2>
  7. <el-tag type="success" effect="dark" size="large">
  8. 已显示 {{ shopList.length }} 个门店
  9. </el-tag>
  10. </div>
  11. <div class="header-right">
  12. <el-input
  13. v-model="searchAddress"
  14. placeholder="请输入地址搜索"
  15. clearable
  16. style="width: 300px"
  17. @keyup.enter="handleAddressSearch"
  18. >
  19. <template #append>
  20. <el-button @click="handleAddressSearch">
  21. <el-icon><Search /></el-icon>
  22. 搜索
  23. </el-button>
  24. </template>
  25. </el-input>
  26. </div>
  27. </div>
  28. <!-- 地图容器 - 占满剩余空间 -->
  29. <div class="map-container">
  30. <AmapWithSearch
  31. ref="mapComponentRef"
  32. :longitude="centerLongitude"
  33. :latitude="centerLatitude"
  34. height="calc(100vh - 200px)"
  35. :zoom="12"
  36. :enable-click-mark="false"
  37. :show-search="false"
  38. />
  39. <!-- 门店信息浮层 -->
  40. <div v-if="selectedShop" class="shop-info-panel" :style="infoPanelStyle">
  41. <div class="panel-header">
  42. <h3>{{ selectedShop.name }}</h3>
  43. <el-button type="info" size="small" circle @click="closeShopInfo">✕</el-button>
  44. </div>
  45. <div class="panel-content">
  46. <p><strong>📍 地址:</strong>{{ selectedShop.province }}{{ selectedShop.city }}{{ selectedShop.district }}{{ selectedShop.address }}</p>
  47. <p><strong>🔢 坐标:</strong>经度 {{ selectedShop.longitude }}, 纬度 {{ selectedShop.latitude }}</p>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. </template>
  53. <script setup lang="ts">
  54. import { ref, computed, onMounted, nextTick } from 'vue';
  55. import { AmapWithSearch } from '@/components/AmapWithSearch';
  56. import { getShopList } from '@/api/shop';
  57. import { Search } from '@element-plus/icons-vue';
  58. // 高德地图全局对象
  59. let AMap: any = null;
  60. // 地图组件引用
  61. const mapComponentRef = ref<any>(null);
  62. // 门店数据
  63. const shopList = ref<any[]>([]);
  64. const loading = ref(false);
  65. const searchAddress = ref('');
  66. // 地图中心点(默认深圳)
  67. const centerLongitude = ref(114.057868);
  68. const centerLatitude = ref(22.677079);
  69. // 当前选中的门店
  70. const selectedShop = ref<any>(null);
  71. // 标记点位置(用于计算信息面板位置)
  72. const markerPosition = ref<{ lng: number; lat: number } | null>(null);
  73. // 加载门店列表并在地图上标记
  74. const loadShopList = async () => {
  75. loading.value = true;
  76. try {
  77. const res = await getShopList({ page: 1, pageSize: 100 });
  78. console.log('门店列表 API 响应:', JSON.stringify(res));
  79. if (res.code === 200 || res.code === 0) {
  80. shopList.value = res.data?.list || [];
  81. console.log('加载门店列表成功,数量:', shopList.value.length);
  82. // 不立即添加标记,等待用户手动触发或地图组件准备好后再添加
  83. if (shopList.value.length > 0 && shopList.value[0].longitude && shopList.value[0].latitude) {
  84. centerLongitude.value = Number(shopList.value[0].longitude);
  85. centerLatitude.value = Number(shopList.value[0].latitude);
  86. }
  87. } else {
  88. console.warn('门店列表 API 返回异常 code:', res.code, 'message:', res.message);
  89. }
  90. } catch (error) {
  91. console.error('加载门店列表失败:', error);
  92. } finally {
  93. loading.value = false;
  94. }
  95. };
  96. // 获取 AMap 实例(从全局 window 对象)
  97. const getAMapInstance = () => {
  98. if (!AMap && (window as any).AMap) {
  99. AMap = (window as any).AMap;
  100. }
  101. return AMap;
  102. };
  103. // 在地图上添加所有门店的标记
  104. const addShopMarkers = async () => {
  105. if (!mapComponentRef.value) {
  106. console.error('地图组件未初始化');
  107. return;
  108. }
  109. // 等待地图实例初始化
  110. await nextTick();
  111. console.log('[addShopMarkers] 开始添加门店标记...');
  112. const mapInstance = mapComponentRef.value;
  113. shopList.value.forEach((shop, index) => {
  114. if (shop.longitude && shop.latitude) {
  115. const lng = Number(shop.longitude);
  116. const lat = Number(shop.latitude);
  117. console.log(`添加门店标记 ${index + 1}:`, shop.name, lng, lat);
  118. // 使用组件暴露的 addCustomMarker 方法添加带点击事件的红色标记
  119. if (typeof mapInstance.addCustomMarker === 'function') {
  120. const result = mapInstance.addCustomMarker(lng, lat, shop.name, () => {
  121. console.log('点击门店标记:', shop.name);
  122. // 设置选中的门店并计算面板位置
  123. selectedShop.value = shop;
  124. markerPosition.value = { lng, lat };
  125. }, 'red'); // 使用红色标记
  126. if (result) {
  127. console.log(`[addShopMarkers] 标记点 ${shop.name} 添加成功`);
  128. } else {
  129. console.error(`[addShopMarkers] 标记点 ${shop.name} 添加失败`);
  130. }
  131. } else {
  132. console.error('[addShopMarkers] addCustomMarker 方法不存在');
  133. }
  134. } else {
  135. console.warn(`门店 ${shop.name} 没有有效的经纬度`);
  136. }
  137. });
  138. console.log('[addShopMarkers] 门店标记添加完成');
  139. };
  140. // 关闭门店信息面板
  141. const closeShopInfo = () => {
  142. selectedShop.value = null;
  143. markerPosition.value = null;
  144. };
  145. // 处理地址搜索
  146. const handleAddressSearch = async () => {
  147. if (!searchAddress.value.trim()) {
  148. return;
  149. }
  150. if (mapComponentRef.value && typeof mapComponentRef.value.searchAddress === 'function') {
  151. await mapComponentRef.value.searchAddress(searchAddress.value);
  152. }
  153. };
  154. // 计算信息面板样式(显示在标记点右上方)
  155. const infoPanelStyle = computed(() => {
  156. // 简单方案:始终显示在地图容器的右上角
  157. // 这样更稳定,不受地图缩放和移动影响
  158. return {
  159. position: 'absolute' as const,
  160. top: '20px',
  161. right: '20px',
  162. maxWidth: '320px',
  163. zIndex: 1000
  164. };
  165. });
  166. // 初始化加载门店列表
  167. onMounted(() => {
  168. loadShopList();
  169. // 等待地图组件初始化完成后再添加标记
  170. setTimeout(() => {
  171. addShopMarkers();
  172. }, 1000); // 延迟 1 秒确保地图已初始化
  173. });
  174. </script>
  175. <style scoped>
  176. .shop-distribution-page {
  177. display: flex;
  178. flex-direction: column;
  179. height: 100vh; /* 占满整个视口高度 */
  180. overflow: hidden;
  181. }
  182. .page-header {
  183. display: flex;
  184. justify-content: space-between;
  185. align-items: center;
  186. padding: 16px 20px;
  187. background: white;
  188. border-bottom: 1px solid #e4e7ed;
  189. flex-shrink: 0; /* 不压缩头部 */
  190. }
  191. .header-left {
  192. display: flex;
  193. align-items: center;
  194. gap: 16px;
  195. }
  196. .page-title {
  197. margin: 0;
  198. font-size: 20px;
  199. color: #303133;
  200. font-weight: 600;
  201. }
  202. .map-container {
  203. flex: 1; /* 占满剩余空间 */
  204. position: relative;
  205. background: white;
  206. overflow: hidden;
  207. min-height: 500px; /* 确保最小高度 */
  208. }
  209. .shop-info-panel {
  210. position: absolute;
  211. top: 20px;
  212. right: 20px;
  213. background: white;
  214. border-radius: 8px;
  215. box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  216. z-index: 1000;
  217. min-width: 300px;
  218. max-width: 400px;
  219. border: 1px solid #e4e7ed;
  220. }
  221. .panel-header {
  222. display: flex;
  223. justify-content: space-between;
  224. align-items: center;
  225. padding: 12px 16px;
  226. border-bottom: 1px solid #e4e7ed;
  227. background: #f5f7fa;
  228. border-radius: 8px 8px 0 0;
  229. }
  230. .panel-header h3 {
  231. margin: 0;
  232. font-size: 16px;
  233. color: #303133;
  234. font-weight: 600;
  235. }
  236. .panel-content {
  237. padding: 16px;
  238. }
  239. .panel-content p {
  240. margin: 8px 0;
  241. font-size: 13px;
  242. color: #606266;
  243. line-height: 1.6;
  244. }
  245. .panel-content strong {
  246. color: #303133;
  247. font-weight: 600;
  248. }
  249. </style>