| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import { defineStore } from 'pinia'
- interface SysState {
- ScrollDown: boolean
- /**
- * 分类顶部激活id
- */
- topNavActive: string
- /**
- * 二级分类激活id
- */
- leftActive: string
- searchList: string[]
- /**
- * 回到顶部
- */
- backTop: boolean
- /**
- * 店铺名称
- */
- SelectShopInfo: Api.xsbShopList
- /**
- * 店铺列表
- */
- xsbShopList: Api.xsbShopList[]
- }
- export const useSysXsbStore = defineStore('system-xsb', {
- state: (): SysState => ({
- ScrollDown: false,
- topNavActive: '',
- leftActive: '',
- searchList: [],
- backTop: false,
- SelectShopInfo: { shopId: 0 },
- xsbShopList: [],
- }),
- actions: {
- getTabbarItemValue(name: string) {
- if (name === 'xsb-cart') {
- const { getTotalNum } = storeToRefs(useSmqjhCartStore())
- return getTotalNum.value
- }
- },
- async getAllShopList() {
- return new Promise((resolve, reject) => {
- Apis.xsb.shopList({}).then((res) => {
- this.xsbShopList = res.data
- const { Location } = storeToRefs(useAddressStore())
- if (Location.value.latitude == null || Location.value.longitude == null) {
- useAddressStore().getLocation()
- return
- }
- let minDistance = Infinity
- for (const shop of res.data) {
- if (shop.shopLat && shop.shopLng) {
- const distance = this.getDistance(
- Location.value.latitude,
- Location.value.longitude,
- Number(shop.shopLat),
- Number(shop.shopLng),
- )
- if (distance < minDistance) {
- minDistance = distance
- if (!this.SelectShopInfo) {
- this.SelectShopInfo = { ...shop, distance: Number(distance.toFixed(2)) }
- }
- }
- }
- }
- resolve(res)
- }).catch((err) => { reject(err) })
- })
- },
- /**
- * 计算距离
- * @param lat1
- * @param lng1
- * @param lat2
- * @param lng2
- * @returns 计算距离
- */
- getDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
- const R = 6371 // 地球半径(公里)
- const dLat = (lat2 - lat1) * Math.PI / 180
- const dLng = (lng2 - lng1) * Math.PI / 180
- const a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
- + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180)
- * Math.sin(dLng / 2) * Math.sin(dLng / 2)
- const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
- return R * c // 距离(公里)
- },
- },
- })
|