1008 lines
30 KiB
Vue
1008 lines
30 KiB
Vue
<template>
|
|
<s-layout :bgStyle="{ color: '#FFF' }" title="选择自提门店">
|
|
<view class="store-page">
|
|
<view class="search-bar">
|
|
<view class="search-inner">
|
|
<image class="search-icon" src="/static/icon/search.svg" mode="aspectFit" />
|
|
<uni-easyinput
|
|
class="search-input"
|
|
v-model="state.keyword"
|
|
placeholder="搜索自提点..."
|
|
:inputBorder="false"
|
|
:clearable="true"
|
|
/>
|
|
</view>
|
|
</view>
|
|
<view class="map-card" style="width: 100%; height: 460rpx; min-height: 460rpx;">
|
|
<map
|
|
v-if="mapMounted"
|
|
id="storeSelectionMap"
|
|
class="map"
|
|
style="width: 100%; height: 460rpx; min-height: 460rpx; display: block;"
|
|
:latitude="state.mapLatitude"
|
|
:longitude="state.mapLongitude"
|
|
:scale="state.mapScale"
|
|
:markers="state.markers"
|
|
:show-location="false"
|
|
:setting="MAP_SETTING"
|
|
@markertap="onMarkerTap"
|
|
@ready="onMapReady"
|
|
@error="onMapError"
|
|
/>
|
|
</view>
|
|
<view class="list-hd">
|
|
<view class="list-title">附近自提点</view>
|
|
<view class="list-sub" v-if="filteredStoreList.length">共{{ filteredStoreList.length }}家</view>
|
|
</view>
|
|
<view class="store-list" ref="container">
|
|
<view
|
|
class="store-item"
|
|
v-for="(item, index) in filteredStoreList"
|
|
:key="item.id || index"
|
|
:class="{ 'store-item--active': Number(item.id) === Number(state.selectedStoreId) }"
|
|
@tap="onSelect(item)"
|
|
>
|
|
<view class="store-avatar-wrap">
|
|
<image
|
|
class="store-avatar"
|
|
:src="normalizeImageUrl(item?.coverImage || item?.image || item?.picUrl || item?.logo) || STORE_LIST_FALLBACK_AVATAR"
|
|
mode="aspectFill"
|
|
/>
|
|
<view class="store-badge">#{{ index + 1 }}</view>
|
|
</view>
|
|
<view class="store-item__bd">
|
|
<view class="store-item__hd">
|
|
<view class="store-name">{{ item.name }}</view>
|
|
<view
|
|
v-if="getStoreStatusText(item.status)"
|
|
class="store-status"
|
|
:class="`store-status--${getStoreStatusLevel(item.status)}`"
|
|
>
|
|
{{ getStoreStatusText(item.status) }}
|
|
</view>
|
|
<view class="store-distance" v-if="item.distance !== undefined && item.distance !== null">
|
|
{{ formatDistance(item.distance) }}
|
|
</view>
|
|
</view>
|
|
<view class="store-address line2">
|
|
{{ formatAddress(item) || '--' }}
|
|
</view>
|
|
<view class="store-item__ft">
|
|
<view class="store-hours">
|
|
<text v-if="Number(item.status) === 1 && formatBusinessHours(item)">
|
|
营业中 {{ formatBusinessHours(item) }}
|
|
</text>
|
|
</view>
|
|
<view class="store-actions">
|
|
<view class="btn btn--ghost" @tap.stop="showMaoLocation(item)">导航</view>
|
|
<view
|
|
v-if="Number(item.status) === 1"
|
|
class="btn"
|
|
@tap.stop="checked(item)"
|
|
>选择</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
|
|
<canvas
|
|
canvas-id="storeMarkerCanvas"
|
|
id="storeMarkerCanvas"
|
|
class="marker-canvas"
|
|
style="position: absolute; left: -9999px; top: -9999px; width: 420px; height: 200px;"
|
|
/>
|
|
|
|
</view>
|
|
</s-layout>
|
|
</template>
|
|
|
|
<script setup>
|
|
import StoreApi from '@/sheep/api/trade/store';
|
|
import { computed, getCurrentInstance, nextTick, onMounted, reactive, ref } from 'vue';
|
|
import { onLoad, onShow } from '@dcloudio/uni-app';
|
|
import sheep from '@/sheep';
|
|
const { proxy } = getCurrentInstance();
|
|
const LONGITUDE = 'user_longitude';
|
|
const LATITUDE = 'user_latitude';
|
|
const STORE_LIST_CACHE_KEY = 'goods_details_store_list_cache_v1';
|
|
const FALLBACK_LAT = 39.9042;
|
|
const FALLBACK_LON = 116.4074;
|
|
|
|
const isValidRealLocation = (lat, lon) => {
|
|
const la = Number(lat || 0);
|
|
const lo = Number(lon || 0);
|
|
if (!la || !lo || Number.isNaN(la) || Number.isNaN(lo)) return false;
|
|
if (Math.abs(la - FALLBACK_LAT) < 0.0001 && Math.abs(lo - FALLBACK_LON) < 0.0001) return false;
|
|
return true;
|
|
};
|
|
|
|
const readStoreListCache = () => {
|
|
try {
|
|
const raw = uni.getStorageSync(STORE_LIST_CACHE_KEY);
|
|
if (!raw) return [];
|
|
const arr = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
return Array.isArray(arr) ? arr : [];
|
|
} catch (e) { return []; }
|
|
};
|
|
const writeStoreListCache = (list) => {
|
|
try {
|
|
if (Array.isArray(list) && list.length) {
|
|
uni.setStorageSync(STORE_LIST_CACHE_KEY, JSON.stringify(list));
|
|
}
|
|
} catch (e) {}
|
|
};
|
|
|
|
const MARKER_CANVAS_ID = 'storeMarkerCanvas';
|
|
const MARKER_CANVAS_W = 420;
|
|
const MARKER_CANVAS_H = 200;
|
|
const MARKER_AVATAR_SIZE = 96;
|
|
const MARKER_AVATAR_BORDER_INACTIVE = 6;
|
|
const MARKER_AVATAR_BORDER_ACTIVE = 8;
|
|
const MARKER_BORDER_COLOR_INACTIVE = '#B8BECB';
|
|
const MARKER_BORDER_COLOR_ACTIVE = '#2556EC';
|
|
const MARKER_TEXT_COLOR_ACTIVE = '#2556EC';
|
|
const MARKER_TEXT_COLOR_INACTIVE = '#2A2D35';
|
|
const markerIconCache = reactive({});
|
|
|
|
const clearMarkerIconCache = () => {
|
|
Object.keys(markerIconCache).forEach((k) => { delete markerIconCache[k]; });
|
|
};
|
|
|
|
const getImagePath = (src) => new Promise((resolve) => {
|
|
if (!src) return resolve('');
|
|
if (/^https?:\/\//.test(String(src)) || /^\/static\//.test(String(src))) {
|
|
uni.getImageInfo({
|
|
src,
|
|
success: (res) => resolve(res.path || src),
|
|
fail: () => resolve(''),
|
|
});
|
|
return;
|
|
}
|
|
resolve(src);
|
|
});
|
|
|
|
const renderMarkerToCanvas = async (item) => {
|
|
const id = Number(item?.id || 0);
|
|
const active = id === Number(state.selectedStoreId);
|
|
const avatar = normalizeImageUrl(
|
|
item?.coverImage || item?.image || item?.picUrl || item?.logo || item?.avatar || STORE_LIST_FALLBACK_AVATAR,
|
|
) || STORE_LIST_FALLBACK_AVATAR;
|
|
const name = String(item?.name || item?.storeName || item?.shopName || '').trim() || '--';
|
|
const canvasKey = `${id}_${active ? 1 : 0}`;
|
|
if (markerIconCache[canvasKey]) return markerIconCache[canvasKey];
|
|
const avatarLocalPath = await getImagePath(avatar);
|
|
const borderW = active ? MARKER_AVATAR_BORDER_ACTIVE : MARKER_AVATAR_BORDER_INACTIVE;
|
|
const borderColor = active ? MARKER_BORDER_COLOR_ACTIVE : MARKER_BORDER_COLOR_INACTIVE;
|
|
const textColor = active ? MARKER_TEXT_COLOR_ACTIVE : MARKER_TEXT_COLOR_INACTIVE;
|
|
const avatarOuterSize = MARKER_AVATAR_SIZE + borderW * 2;
|
|
const totalW = Math.max(avatarOuterSize, 180);
|
|
const totalH = avatarOuterSize + 44;
|
|
const boxX = (MARKER_CANVAS_W - totalW) / 2;
|
|
const boxY = 0;
|
|
const avatarOuterX = (MARKER_CANVAS_W - avatarOuterSize) / 2;
|
|
const avatarOuterY = boxY + 8;
|
|
const ctx = uni.createCanvasContext(MARKER_CANVAS_ID, proxy || getCurrentInstance());
|
|
ctx.clearRect(0, 0, MARKER_CANVAS_W, MARKER_CANVAS_H);
|
|
ctx.save();
|
|
ctx.save();
|
|
ctx.setFillStyle(borderColor);
|
|
ctx.beginPath();
|
|
ctx.arc(avatarOuterX + avatarOuterSize / 2, avatarOuterY + avatarOuterSize / 2, avatarOuterSize / 2, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
const avatarInnerX = avatarOuterX + borderW;
|
|
const avatarInnerY = avatarOuterY + borderW;
|
|
ctx.beginPath();
|
|
ctx.arc(avatarInnerX + MARKER_AVATAR_SIZE / 2, avatarInnerY + MARKER_AVATAR_SIZE / 2, MARKER_AVATAR_SIZE / 2, 0, Math.PI * 2);
|
|
ctx.closePath();
|
|
ctx.clip();
|
|
if (avatarLocalPath) {
|
|
try {
|
|
ctx.drawImage(avatarLocalPath, avatarInnerX, avatarInnerY, MARKER_AVATAR_SIZE, MARKER_AVATAR_SIZE);
|
|
} catch (e) {
|
|
ctx.setFillStyle('#D9E0F0');
|
|
ctx.fillRect(avatarInnerX, avatarInnerY, MARKER_AVATAR_SIZE, MARKER_AVATAR_SIZE);
|
|
}
|
|
} else {
|
|
ctx.setFillStyle('#D9E0F0');
|
|
ctx.fillRect(avatarInnerX, avatarInnerY, MARKER_AVATAR_SIZE, MARKER_AVATAR_SIZE);
|
|
}
|
|
ctx.restore();
|
|
ctx.setFillStyle(textColor);
|
|
ctx.setFontSize(active ? 22 : 20);
|
|
ctx.setTextAlign('center');
|
|
ctx.setTextBaseline('top');
|
|
const textY = avatarOuterY + avatarOuterSize + 6;
|
|
const textMaxW = totalW - 8;
|
|
const chars = String(name).split('');
|
|
let shown = chars.join('');
|
|
if (chars.length > 6) {
|
|
shown = chars.slice(0, 6).join('') + '…';
|
|
}
|
|
ctx.fillText(shown, MARKER_CANVAS_W / 2, textY, textMaxW);
|
|
ctx.restore();
|
|
return new Promise((resolve) => {
|
|
ctx.draw(false, () => {
|
|
setTimeout(() => {
|
|
uni.canvasToTempFilePath(
|
|
{
|
|
canvasId: MARKER_CANVAS_ID,
|
|
x: boxX,
|
|
y: boxY,
|
|
width: totalW,
|
|
height: totalH,
|
|
destWidth: totalW * 2,
|
|
destHeight: totalH * 2,
|
|
fileType: 'png',
|
|
quality: 1,
|
|
success: (res) => {
|
|
markerIconCache[canvasKey] = res.tempFilePath;
|
|
resolve(res.tempFilePath);
|
|
},
|
|
fail: () => {
|
|
markerIconCache[canvasKey] = MARKER_PIN_PNG_WWW;
|
|
resolve(MARKER_PIN_PNG_WWW);
|
|
},
|
|
},
|
|
proxy || getCurrentInstance(),
|
|
);
|
|
}, 60);
|
|
});
|
|
});
|
|
};
|
|
|
|
const STORE_LIST_FALLBACK_AVATAR = '/static/index/shopIcon.png';
|
|
const MARKER_PIN_PNG_WWW = '/static/icon/map.png';
|
|
|
|
const MAP_SETTING = Object.freeze({
|
|
gestureZoom: true,
|
|
gestureScroll: true,
|
|
gestureRotate: false,
|
|
gestureTilt: false,
|
|
showCompass: false,
|
|
showScale: false,
|
|
});
|
|
|
|
const MAP_MOUNT_DELAY = 80;
|
|
|
|
const mapMounted = ref(false);
|
|
const lastValidStoreList = ref([]);
|
|
|
|
const state = reactive({
|
|
loading: false,
|
|
storeList: [],
|
|
system_store: {},
|
|
locationShow: false,
|
|
user_latitude: 0,
|
|
user_longitude: 0,
|
|
mapLatitude: 39.9042,
|
|
mapLongitude: 116.4074,
|
|
mapScale: 12,
|
|
selectedStoreId: 0,
|
|
keyword: '',
|
|
markers: [],
|
|
mapReady: false,
|
|
mapError: '',
|
|
});
|
|
|
|
const onMapReady = () => {
|
|
state.mapReady = true;
|
|
state.mapError = '';
|
|
};
|
|
|
|
const onMapError = (e) => {
|
|
state.mapError = JSON.stringify(e || {});
|
|
};
|
|
|
|
const selfLocation = () => {
|
|
let finished = false;
|
|
let fallbackTimer = null;
|
|
const prevLat = Number(state.user_latitude || 0);
|
|
const prevLon = Number(state.user_longitude || 0);
|
|
const done = (res) => {
|
|
if (finished) return;
|
|
finished = true;
|
|
if (fallbackTimer) {
|
|
clearTimeout(fallbackTimer);
|
|
fallbackTimer = null;
|
|
}
|
|
if (res && res.latitude && res.longitude) {
|
|
try {
|
|
const newLat = Number(res.latitude);
|
|
const newLon = Number(res.longitude);
|
|
state.user_latitude = newLat;
|
|
state.user_longitude = newLon;
|
|
state.mapLatitude = newLat;
|
|
state.mapLongitude = newLon;
|
|
if (isValidRealLocation(newLat, newLon)) {
|
|
uni.setStorageSync(LATITUDE, newLat);
|
|
uni.setStorageSync(LONGITUDE, newLon);
|
|
} else {
|
|
uni.removeStorageSync(LATITUDE);
|
|
uni.removeStorageSync(LONGITUDE);
|
|
}
|
|
const wasFake = !isValidRealLocation(prevLat, prevLon);
|
|
const moved = wasFake || !prevLat || !prevLon;
|
|
if (moved) {
|
|
getList();
|
|
}
|
|
} catch (e) { console.error(e); }
|
|
}
|
|
};
|
|
// #ifdef APP-PLUS
|
|
fallbackTimer = setTimeout(() => done(null), 4000);
|
|
// #endif
|
|
runGetLocation(done);
|
|
};
|
|
|
|
const showMaoLocation = (e) => runOpenLocation(e);
|
|
|
|
const checked = (addressInfo) => {
|
|
try {
|
|
if (!isStoreOpen(addressInfo)) {
|
|
uni.showToast({ title: '当前门店暂不支持自提', icon: 'none' });
|
|
return;
|
|
}
|
|
uni.$emit('SELECT_PICK_UP_INFO', {
|
|
addressInfo,
|
|
});
|
|
const routerBack = sheep?.$router?.back;
|
|
if (typeof routerBack === 'function') {
|
|
try {
|
|
routerBack.call(sheep.$router);
|
|
return;
|
|
} catch (e) {
|
|
console.error('[checked] sheep.$router.back err', e);
|
|
}
|
|
}
|
|
uni.navigateBack();
|
|
} catch (e) {
|
|
console.error('[checked] top exception', e);
|
|
}
|
|
};
|
|
|
|
const onSelect = (item) => {
|
|
if (!isStoreOpen(item)) {
|
|
uni.showToast({ title: '当前门店暂不支持自提', icon: 'none' });
|
|
return;
|
|
}
|
|
const storeId = Number(item?.id || 0);
|
|
state.selectedStoreId = storeId;
|
|
const lat = Number(item?.latitude);
|
|
const lon = Number(item?.longitude);
|
|
state.markers = [];
|
|
nextTick(() => {
|
|
if (lat && lon) {
|
|
moveMapToStore(lat, lon, 15);
|
|
}
|
|
buildMarkers();
|
|
});
|
|
};
|
|
|
|
const formatDistance = (km) => {
|
|
const d = Number(km);
|
|
if (!(d >= 0)) return '';
|
|
if (d >= 1) return `${d.toFixed(1)}km`;
|
|
return `${Math.round(d * 1000)}m`;
|
|
};
|
|
|
|
const formatAddress = (item) => {
|
|
const district = item?.district || '';
|
|
const description = item?.description || '';
|
|
const address = item?.address || '';
|
|
return `${district}${description ? ` ${description}` : ''}${address ? ` ${address}` : ''}`.trim();
|
|
};
|
|
|
|
const formatHm = (hm) => {
|
|
if (!Array.isArray(hm) || hm.length < 2) return '';
|
|
const h = String(hm[0]).padStart(2, '0');
|
|
const m = String(hm[1]).padStart(2, '0');
|
|
return `${h}:${m}`;
|
|
};
|
|
|
|
const formatBusinessHours = (item) => {
|
|
const start =
|
|
item?.openingTime || formatHm(item?.businessHoursStart) || item?.businessHoursStart || '';
|
|
const end = item?.closingTime || formatHm(item?.businessHoursEnd) || item?.businessHoursEnd || '';
|
|
if (!start || !end) return '';
|
|
return `${start}-${end}`;
|
|
};
|
|
|
|
const STORE_STATUS_TEXT = { 1: '营业中', 2: '休息中', 3: '停用' };
|
|
const STORE_STATUS_LEVEL = { 1: 'open', 2: 'rest', 3: 'closed' };
|
|
const getStoreStatusText = (status) => STORE_STATUS_TEXT[Number(status)] || '';
|
|
const getStoreStatusLevel = (status) => STORE_STATUS_LEVEL[Number(status)] || 'closed';
|
|
const isStoreOpen = (item) => Number(item?.status) === 1;
|
|
|
|
const wechatJsSdk = () => {
|
|
try {
|
|
const useProvider = sheep?.$platform?.useProvider;
|
|
const wx = typeof useProvider === 'function' ? useProvider.call(sheep.$platform, 'wechat').jsWxSdk : null;
|
|
return wx && typeof wx.isWechat === 'function' && wx.isWechat() ? wx : null;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const uniGetLocationWrap = (done) => {
|
|
uni.getLocation({
|
|
type: 'gcj02',
|
|
success: (res) => done(res),
|
|
fail: () => done(null),
|
|
});
|
|
};
|
|
|
|
const runGetLocation = (done) => {
|
|
// #ifdef H5
|
|
const wx = wechatJsSdk();
|
|
if (wx && typeof wx.getLocation === 'function') {
|
|
wx.getLocation(done);
|
|
return;
|
|
}
|
|
// #endif
|
|
uniGetLocationWrap(done);
|
|
};
|
|
|
|
const uniOpenLocationWrap = (e) => {
|
|
uni.openLocation({
|
|
latitude: Number(e.latitude),
|
|
longitude: Number(e.longitude),
|
|
name: e.name,
|
|
address: `${e.district}-${e.description}`,
|
|
});
|
|
};
|
|
|
|
const runOpenLocation = (e) => {
|
|
// #ifdef H5
|
|
const wx = wechatJsSdk();
|
|
if (wx && typeof wx.openLocation === 'function') {
|
|
wx.openLocation({
|
|
latitude: Number(e.latitude),
|
|
longitude: Number(e.longitude),
|
|
name: e.name,
|
|
address: `${e.district}-${e.description}`,
|
|
});
|
|
return;
|
|
}
|
|
// #endif
|
|
uniOpenLocationWrap(e);
|
|
};
|
|
|
|
const sortStoreList = (list = []) => {
|
|
const STATUS_WEIGHT = { 1: 0, 2: 1, 3: 2 };
|
|
return [...list].sort((a, b) => {
|
|
const wa = STATUS_WEIGHT[Number(a?.status)] ?? 99;
|
|
const wb = STATUS_WEIGHT[Number(b?.status)] ?? 99;
|
|
if (wa !== wb) return wa - wb;
|
|
const da = Number(a?.distance);
|
|
const db = Number(b?.distance);
|
|
if (da >= 0 && db >= 0) return da - db;
|
|
return 0;
|
|
});
|
|
};
|
|
|
|
const filteredStoreList = computed(() => {
|
|
const list = Array.isArray(state.storeList) ? state.storeList : [];
|
|
const kw = String(state.keyword || '').trim();
|
|
const raw = !kw
|
|
? list
|
|
: list.filter((item) => {
|
|
const s = `${item?.name || ''} ${formatAddress(item)}`.toLowerCase();
|
|
return s.includes(kw.toLowerCase());
|
|
});
|
|
return sortStoreList(raw);
|
|
});
|
|
|
|
const sanitizeUrl = (value) => String(value || '').replace(/[`"'\s]/g, '');
|
|
const normalizeImageUrl = (value) => {
|
|
const raw = sanitizeUrl(value);
|
|
if (!raw) return '';
|
|
if (/^https?:\/\//i.test(raw)) return raw;
|
|
const cdnFn = sheep?.$url?.cdn;
|
|
return typeof cdnFn === 'function' ? cdnFn(raw) : raw;
|
|
};
|
|
|
|
const sanitizeMarker = (rawList = []) => {
|
|
const list = Array.isArray(rawList) ? rawList : [];
|
|
const LAT_FIELDS = [
|
|
'latitude', 'lat', 'Latitude', 'LATITUDE', 'LAT',
|
|
'baidu_lat', 'gd_lat', 'gaode_lat', 'store_latitude',
|
|
'shopLatitude', 'shop_lat', 'storeLat',
|
|
'addrLat', 'address_lat',
|
|
];
|
|
const LON_FIELDS = [
|
|
'longitude', 'lng', 'lon', 'Longitude', 'LONGITUDE', 'LNG', 'LON',
|
|
'baidu_lng', 'baidu_lon', 'gd_lng', 'gd_lon', 'gaode_lng', 'gaode_lon',
|
|
'store_longitude', 'shopLongitude', 'shop_lng', 'shop_lon', 'storeLng', 'storeLon',
|
|
'addrLng', 'addrLon', 'address_lng', 'address_lon',
|
|
];
|
|
const picks = list
|
|
.map((item) => {
|
|
const id = Number(item?.id ?? item?.storeId ?? item?.shopId ?? item?.store_id ?? item?.shop_id ?? 0);
|
|
if (!(id > 0)) {
|
|
return null;
|
|
}
|
|
let latRaw = null;
|
|
let lonRaw = null;
|
|
const allKeys = item && typeof item === 'object' ? Object.keys(item) : [];
|
|
for (const k of LAT_FIELDS) {
|
|
const v = item?.[k];
|
|
if (typeof v !== 'undefined' && v !== null && v !== '' && !Number.isNaN(Number(v))) {
|
|
latRaw = v;
|
|
break;
|
|
}
|
|
}
|
|
if (latRaw === null && allKeys.length) {
|
|
for (const k of allKeys) {
|
|
const lowerK = String(k).toLowerCase();
|
|
if (/lat|latitude/.test(lowerK)) {
|
|
const v = item?.[k];
|
|
if (typeof v !== 'undefined' && v !== null && v !== '' && !Number.isNaN(Number(v))) {
|
|
latRaw = v;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (const k of LON_FIELDS) {
|
|
const v = item?.[k];
|
|
if (typeof v !== 'undefined' && v !== null && v !== '' && !Number.isNaN(Number(v))) {
|
|
lonRaw = v;
|
|
break;
|
|
}
|
|
}
|
|
if (lonRaw === null && allKeys.length) {
|
|
for (const k of allKeys) {
|
|
const lowerK = String(k).toLowerCase();
|
|
if (/lng|lon|longitude/.test(lowerK)) {
|
|
const v = item?.[k];
|
|
if (typeof v !== 'undefined' && v !== null && v !== '' && !Number.isNaN(Number(v))) {
|
|
lonRaw = v;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const lat = Number(latRaw);
|
|
const lon = Number(lonRaw);
|
|
const valid = lat !== 0 && !Number.isNaN(lat) && lon !== 0 && !Number.isNaN(lon);
|
|
if (!valid) {
|
|
return null;
|
|
}
|
|
return {
|
|
...item,
|
|
id,
|
|
latitude: lat,
|
|
longitude: lon,
|
|
};
|
|
})
|
|
.filter(Boolean);
|
|
return picks;
|
|
};
|
|
|
|
async function buildMarkers() {
|
|
const list = sanitizeMarker(filteredStoreList.value);
|
|
clearMarkerIconCache();
|
|
const iconPaths = [];
|
|
for (let i = 0; i < list.length; i++) {
|
|
try {
|
|
const p = await renderMarkerToCanvas(list[i]);
|
|
iconPaths.push(p || MARKER_PIN_PNG_WWW);
|
|
} catch (e) {
|
|
iconPaths.push(MARKER_PIN_PNG_WWW);
|
|
}
|
|
}
|
|
const newMarkers = list.map((item, idx) => {
|
|
const id = Number(item.id || 0);
|
|
const active = id === Number(state.selectedStoreId);
|
|
const icon = iconPaths[idx] || MARKER_PIN_PNG_WWW;
|
|
const w = active ? 84 : 78;
|
|
const h = active ? 102 : 94;
|
|
return {
|
|
id,
|
|
latitude: Number(item.latitude),
|
|
longitude: Number(item.longitude),
|
|
width: w,
|
|
height: h,
|
|
anchor: { x: 0.5, y: 1 },
|
|
zIndex: active ? 300 : 10,
|
|
iconPath: icon,
|
|
};
|
|
}).filter((m) => !!m.iconPath);
|
|
const serialized = JSON.parse(JSON.stringify(newMarkers));
|
|
state.markers = [];
|
|
nextTick(() => {
|
|
state.markers = serialized;
|
|
});
|
|
}
|
|
|
|
const moveMapToStore = (lat, lon, scale = 14) => {
|
|
if (!lat || !lon) return;
|
|
const numLat = Number(lat);
|
|
const numLon = Number(lon);
|
|
const numScale = Number(scale) || 12;
|
|
if (isNaN(numLat) || isNaN(numLon)) return;
|
|
const clampedScale = Math.max(3, Math.min(20, numScale));
|
|
const stepLat1 = Number((numLat + 0.0005).toFixed(7));
|
|
const stepLon1 = Number((numLon + 0.0005).toFixed(7));
|
|
const stepLat2 = Number((numLat - 0.0005).toFixed(7));
|
|
const stepLon2 = Number((numLon - 0.0005).toFixed(7));
|
|
state.mapScale = clampedScale + 0.0000001;
|
|
state.mapLatitude = stepLat1;
|
|
state.mapLongitude = stepLon1;
|
|
try {
|
|
const mapContext = uni.createMapContext('storeSelectionMap', proxy || getCurrentInstance());
|
|
if (mapContext && typeof mapContext.moveToLocation === 'function') {
|
|
mapContext.moveToLocation({ latitude: numLat, longitude: numLon });
|
|
}
|
|
} catch (e) {
|
|
console.warn('[moveMapToStore] moveToLocation 失败,已降级为state赋值', e);
|
|
}
|
|
setTimeout(() => {
|
|
state.mapScale = clampedScale - 0.0000001;
|
|
state.mapLatitude = stepLat2;
|
|
state.mapLongitude = stepLon2;
|
|
}, 60);
|
|
setTimeout(() => {
|
|
state.mapScale = clampedScale;
|
|
state.mapLatitude = numLat;
|
|
state.mapLongitude = numLon;
|
|
}, 160);
|
|
};
|
|
|
|
const ensureDefaultSelectedStore = (list = []) => {
|
|
const stores = Array.isArray(list) && list.length ? list : filteredStoreList.value;
|
|
if (!stores.length) return null;
|
|
const openStores = stores.filter((o) => isStoreOpen(o));
|
|
const candidates = openStores.length ? openStores : stores;
|
|
const preferred = candidates.find((o) => Number(o?.latitude) && Number(o?.longitude));
|
|
const target = preferred || candidates[0] || null;
|
|
if (target) {
|
|
state.selectedStoreId = isStoreOpen(target) ? Number(target?.id || 0) : 0;
|
|
const lat = Number(target?.latitude);
|
|
const lon = Number(target?.longitude);
|
|
if (lat && lon) {
|
|
moveMapToStore(lat, lon, 12);
|
|
}
|
|
}
|
|
setTimeout(() => {
|
|
buildMarkers();
|
|
}, 220);
|
|
return target;
|
|
};
|
|
|
|
const onMarkerTap = (e) => {
|
|
const markerId = Number(e?.markerId || 0);
|
|
if (!(markerId > 0)) return;
|
|
const target = (filteredStoreList.value || []).find((o) => Number(o?.id) === markerId);
|
|
if (target) {
|
|
onSelect(target);
|
|
}
|
|
};
|
|
|
|
const getList = async () => {
|
|
if (state.loading) {
|
|
return;
|
|
}
|
|
const useRealCoord = isValidRealLocation(state.user_latitude, state.user_longitude);
|
|
const apiLat = useRealCoord ? Number(state.user_latitude) : '';
|
|
const apiLon = useRealCoord ? Number(state.user_longitude) : '';
|
|
let listLoadSettled = false;
|
|
const loadTimeout = setTimeout(() => {
|
|
if (!listLoadSettled) {
|
|
console.warn('[getList] force clear loading after 10s timeout');
|
|
state.loading = false;
|
|
}
|
|
}, 10000);
|
|
state.loading = true;
|
|
try {
|
|
const memoryBackup = Array.isArray(lastValidStoreList.value) && lastValidStoreList.value.length
|
|
? lastValidStoreList.value
|
|
: [];
|
|
const persistentBackup = readStoreListCache();
|
|
const lastBackup = memoryBackup.length ? memoryBackup : persistentBackup;
|
|
const current = Array.isArray(state.storeList) && state.storeList.length ? state.storeList : [];
|
|
const previousList = current.length ? current : lastBackup;
|
|
const payload = {};
|
|
if (apiLat !== '' && apiLon !== '') {
|
|
payload.latitude = apiLat;
|
|
payload.longitude = apiLon;
|
|
}
|
|
const { data, code } = await StoreApi.getStoreListByLocation(payload).catch((e) => {
|
|
console.error('[store] getStoreListByLocation err', e);
|
|
return { code: -1, data: null };
|
|
});
|
|
if (code !== 0) {
|
|
if (!state.storeList.length && previousList.length) {
|
|
state.storeList = previousList;
|
|
}
|
|
if (state.storeList.length) {
|
|
ensureDefaultSelectedStore(state.storeList);
|
|
}
|
|
return;
|
|
}
|
|
const list = Array.isArray(data) ? data : [];
|
|
if (list.length) {
|
|
state.storeList = list.slice();
|
|
lastValidStoreList.value = list.slice();
|
|
writeStoreListCache(list);
|
|
} else if (previousList.length) {
|
|
state.storeList = previousList.slice();
|
|
}
|
|
|
|
ensureDefaultSelectedStore(state.storeList);
|
|
|
|
} catch (e) {
|
|
console.error('[store] getList exception', e);
|
|
} finally {
|
|
listLoadSettled = true;
|
|
clearTimeout(loadTimeout);
|
|
state.loading = false;
|
|
}
|
|
};
|
|
|
|
onLoad(() => {
|
|
try {
|
|
const cachedStoreList = readStoreListCache();
|
|
if (cachedStoreList.length) {
|
|
lastValidStoreList.value = cachedStoreList;
|
|
}
|
|
const cacheLat = Number(uni.getStorageSync(LATITUDE) || 0);
|
|
const cacheLon = Number(uni.getStorageSync(LONGITUDE) || 0);
|
|
const cacheValid = isValidRealLocation(cacheLat, cacheLon);
|
|
if (!cacheValid) {
|
|
uni.removeStorageSync(LATITUDE);
|
|
uni.removeStorageSync(LONGITUDE);
|
|
}
|
|
state.user_latitude = cacheValid ? cacheLat : FALLBACK_LAT;
|
|
state.user_longitude = cacheValid ? cacheLon : FALLBACK_LON;
|
|
const firstStore = Array.isArray(state.storeList) ? state.storeList[0] : null;
|
|
const firstLat = Number(firstStore?.latitude || firstStore?.lat || 0);
|
|
const firstLon = Number(firstStore?.longitude || firstStore?.lng || firstStore?.lon || 0);
|
|
let baseLat = 0;
|
|
let baseLon = 0;
|
|
if (firstLat && firstLon) {
|
|
baseLat = firstLat;
|
|
baseLon = firstLon;
|
|
} else if (isValidRealLocation(state.user_latitude, state.user_longitude)) {
|
|
baseLat = Number(state.user_latitude);
|
|
baseLon = Number(state.user_longitude);
|
|
}
|
|
if (!baseLat || !baseLon) {
|
|
baseLat = FALLBACK_LAT;
|
|
baseLon = FALLBACK_LON;
|
|
}
|
|
state.mapLatitude = baseLat;
|
|
state.mapLongitude = baseLon;
|
|
if (!state.mapScale || Number(state.mapScale) < 3) {
|
|
state.mapScale = 12;
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
});
|
|
|
|
onShow(() => {
|
|
state.loading = false;
|
|
state.storeList = [];
|
|
state.markers = [];
|
|
state.selectedStoreId = 0;
|
|
state.mapReady = false;
|
|
state.mapError = '';
|
|
getList();
|
|
const cachedCoordValid = isValidRealLocation(state.user_latitude, state.user_longitude);
|
|
if (!cachedCoordValid) {
|
|
selfLocation();
|
|
}
|
|
});
|
|
|
|
onMounted(() => {
|
|
nextTick(() => {
|
|
setTimeout(() => {
|
|
mapMounted.value = true;
|
|
}, MAP_MOUNT_DELAY);
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
.line2 {
|
|
display: -webkit-box;
|
|
line-clamp: 2;
|
|
-webkit-line-clamp: 2;
|
|
-webkit-box-orient: vertical;
|
|
overflow: hidden;
|
|
}
|
|
.geoPage {
|
|
position: fixed;
|
|
width: 100%;
|
|
height: 100%;
|
|
top: 0;
|
|
z-index: 10000;
|
|
}
|
|
.store-page {
|
|
background: #fff;
|
|
padding: 0 30rpx 30rpx;
|
|
}
|
|
.search-bar {
|
|
padding: 20rpx 0 10rpx;
|
|
.search-inner {
|
|
display: flex;
|
|
align-items: center;
|
|
height: 72rpx;
|
|
padding: 0 20rpx;
|
|
border-radius: 18rpx;
|
|
background: #f6f7f9;
|
|
box-sizing: border-box;
|
|
.search-icon {
|
|
width: 28rpx;
|
|
height: 28rpx;
|
|
margin-right: 12rpx;
|
|
opacity: 0.7;
|
|
}
|
|
}
|
|
}
|
|
.map-card {
|
|
position: relative;
|
|
border-radius: 20rpx;
|
|
overflow: hidden;
|
|
background: #f6f7f9;
|
|
width: 100%;
|
|
min-height: 460rpx;
|
|
height: 460rpx;
|
|
.map {
|
|
display: block;
|
|
width: 100%;
|
|
height: 460rpx;
|
|
min-height: 460rpx;
|
|
}
|
|
}
|
|
.list-hd {
|
|
display: flex;
|
|
align-items: baseline;
|
|
justify-content: space-between;
|
|
padding: 22rpx 0 10rpx;
|
|
.list-title {
|
|
font-size: 30rpx;
|
|
font-weight: 700;
|
|
color: #222;
|
|
}
|
|
.list-sub {
|
|
font-size: 24rpx;
|
|
color: #666;
|
|
}
|
|
}
|
|
.store-item {
|
|
position: relative;
|
|
display: flex;
|
|
align-items: flex-start;
|
|
padding: 22rpx 22rpx 20rpx;
|
|
border-radius: 18rpx;
|
|
background: #ffffff;
|
|
box-shadow: 0 10rpx 28rpx rgba(0, 0, 0, 0.06);
|
|
margin-top: 18rpx;
|
|
border: 2rpx solid transparent;
|
|
&.store-item--active {
|
|
border-color: rgba(37, 86, 236, 0.35);
|
|
box-shadow: 0 14rpx 34rpx rgba(37, 86, 236, 0.16);
|
|
}
|
|
.store-avatar-wrap {
|
|
position: relative;
|
|
width: 104rpx;
|
|
height: 104rpx;
|
|
flex-shrink: 0;
|
|
margin-right: 20rpx;
|
|
.store-avatar {
|
|
width: 104rpx;
|
|
height: 104rpx;
|
|
border-radius: 24rpx;
|
|
background: #f2f4f8;
|
|
display: block;
|
|
border: 2rpx solid rgba(0, 0, 0, 0.05);
|
|
}
|
|
.store-badge {
|
|
position: absolute;
|
|
right: -6rpx;
|
|
bottom: -6rpx;
|
|
height: 32rpx;
|
|
padding: 0 10rpx;
|
|
border-radius: 999rpx;
|
|
background: rgba(37, 86, 236, 1);
|
|
color: #ffffff;
|
|
font-size: 20rpx;
|
|
line-height: 32rpx;
|
|
box-shadow: 0 2rpx 8rpx rgba(37, 86, 236, 0.25);
|
|
z-index: 2;
|
|
}
|
|
}
|
|
.store-item__bd {
|
|
flex: 1;
|
|
min-width: 0;
|
|
.store-item__hd {
|
|
display: flex;
|
|
align-items: center;
|
|
.store-name {
|
|
flex: 1;
|
|
font-size: 30rpx;
|
|
font-weight: 700;
|
|
color: #222;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
.store-distance {
|
|
font-size: 24rpx;
|
|
color: #666;
|
|
margin-left: 10rpx;
|
|
}
|
|
.store-status {
|
|
height: 32rpx;
|
|
padding: 0 12rpx;
|
|
border-radius: 999rpx;
|
|
font-size: 20rpx;
|
|
line-height: 32rpx;
|
|
margin-left: 12rpx;
|
|
flex-shrink: 0;
|
|
&.store-status--open {
|
|
background: rgba(18, 180, 107, 0.12);
|
|
color: #12b46b;
|
|
}
|
|
&.store-status--rest {
|
|
background: rgba(255, 159, 67, 0.12);
|
|
color: #ff9f43;
|
|
}
|
|
&.store-status--closed {
|
|
background: rgba(153, 153, 153, 0.12);
|
|
color: #999;
|
|
}
|
|
}
|
|
}
|
|
.store-address {
|
|
margin-top: 10rpx;
|
|
font-size: 24rpx;
|
|
color: #666;
|
|
line-height: 34rpx;
|
|
}
|
|
.store-item__ft {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
margin-top: 14rpx;
|
|
.store-hours {
|
|
flex: 1;
|
|
min-width: 0;
|
|
height: 56rpx;
|
|
line-height: 56rpx;
|
|
font-size: 22rpx;
|
|
color: #12b46b;
|
|
margin-right: 14rpx;
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
}
|
|
.store-actions {
|
|
display: flex;
|
|
align-items: center;
|
|
flex-shrink: 0;
|
|
.btn {
|
|
height: 56rpx;
|
|
padding: 0 20rpx;
|
|
border-radius: 999rpx;
|
|
background: #2556ec;
|
|
color: #fff;
|
|
font-size: 24rpx;
|
|
line-height: 56rpx;
|
|
margin-left: 14rpx;
|
|
flex-shrink: 0;
|
|
&.btn--ghost {
|
|
background: rgba(37, 86, 236, 0.12);
|
|
color: #2556ec;
|
|
margin-left: 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</style>
|