3 changed files with 46 additions and 66 deletions
@ -1,58 +1,50 @@ |
|||||
import { |
|
||||
axios |
|
||||
} from './libs' |
|
||||
let cities = [] |
|
||||
|
import { axios } from "./libs"; |
||||
|
let cities = []; |
||||
|
const cityIndexMap = { 广州市: "G", 南京市: "N" }; |
||||
const init = async () => { |
const init = async () => { |
||||
const db = wx.cloud.database() |
|
||||
const [{ |
|
||||
data: { |
|
||||
value: cityIndexMap |
|
||||
} |
|
||||
}, malls] = await Promise.all([db.collection('config').doc('cityIndexMap').get(), axios.get('/Api/Coupon/MallList')]) |
|
||||
cities = Object.values(malls.reduce((acc, nxt) => { |
|
||||
|
const [malls] = await Promise.all([axios.get("/Api/Coupon/MallList")]); |
||||
|
cities = Object.values( |
||||
|
malls.reduce((acc, nxt) => { |
||||
const mall = { |
const mall = { |
||||
name: nxt.name, |
name: nxt.name, |
||||
code: nxt.mallCode |
|
||||
} |
|
||||
|
code: nxt.mallCode, |
||||
|
}; |
||||
if (acc[nxt.areaName]) { |
if (acc[nxt.areaName]) { |
||||
acc[nxt.areaName].malls.push(mall) |
|
||||
} else acc[nxt.areaName] = { |
|
||||
|
acc[nxt.areaName].malls.push(mall); |
||||
|
} else |
||||
|
acc[nxt.areaName] = { |
||||
name: nxt.areaName, |
name: nxt.areaName, |
||||
malls: [mall], |
malls: [mall], |
||||
index: cityIndexMap[nxt.areaName] |
|
||||
} |
|
||||
return acc |
|
||||
}, {})) |
|
||||
cities.sort((a, b) => a.index.charCodeAt(0) - b.index.charCodeAt(0)) |
|
||||
|
|
||||
} |
|
||||
|
index: cityIndexMap[nxt.areaName], |
||||
|
}; |
||||
|
return acc; |
||||
|
}, {}) |
||||
|
); |
||||
|
cities.sort((a, b) => a.index.charCodeAt(0) - b.index.charCodeAt(0)); |
||||
|
}; |
||||
export const getCities = async () => { |
export const getCities = async () => { |
||||
if (!cities.length) { |
if (!cities.length) { |
||||
await init() |
|
||||
return cities |
|
||||
} else return cities |
|
||||
} |
|
||||
|
|
||||
|
await init(); |
||||
|
return cities; |
||||
|
} else return cities; |
||||
|
}; |
||||
|
|
||||
const toRadians = lonlat => lonlat / 180 * Math.PI; |
|
||||
const ToDigits = radian => radian / Math.PI * 180; |
|
||||
const R = 6371 |
|
||||
const r = 50 |
|
||||
const offset = Math.asin(r / 2 / R) * 2 |
|
||||
|
const toRadians = (lonlat) => (lonlat / 180) * Math.PI; |
||||
|
const ToDigits = (radian) => (radian / Math.PI) * 180; |
||||
|
const R = 6371; |
||||
|
const r = 50; |
||||
|
const offset = Math.asin(r / 2 / R) * 2; |
||||
const getRestrict = (lon, lat) => { |
const getRestrict = (lon, lat) => { |
||||
const lonR = toRadians(lon) |
|
||||
const latR = toRadians(lat) |
|
||||
return [lonR - offset, lonR + offset, latR - offset, latR + offset].map(ToDigits) |
|
||||
} |
|
||||
|
const lonR = toRadians(lon); |
||||
|
const latR = toRadians(lat); |
||||
|
return [lonR - offset, lonR + offset, latR - offset, latR + offset].map( |
||||
|
ToDigits |
||||
|
); |
||||
|
}; |
||||
const getMall = ([minLon, maxLon, minLat, maxLat]) => |
const getMall = ([minLon, maxLon, minLat, maxLat]) => |
||||
new Promise(resolve => { |
|
||||
|
|
||||
}) |
|
||||
|
new Promise((resolve) => {}); |
||||
|
|
||||
export const getNearMall = async ({ |
|
||||
longitude, |
|
||||
latitude |
|
||||
}) => { |
|
||||
const restrict = getRestrict(longitude, latitude) |
|
||||
return (await getCities())[0] |
|
||||
} |
|
||||
|
export const getNearMall = async ({ longitude, latitude }) => { |
||||
|
const restrict = getRestrict(longitude, latitude); |
||||
|
return (await getCities())[0]; |
||||
|
}; |
||||
|
|||||
Loading…
Reference in new issue