Browse Source

refactor: ♻️ 修改mallCode从url地址获取

pull/2/head
姜鑫 3 years ago
parent
commit
db1650206a
  1. 7
      src/App.vue
  2. 23
      src/components/ScrollList/ScrollList.vue
  3. 1
      src/components/ScrollListItem/ScrollListItem.vue
  4. 20
      src/components/WeatherAndTime/WeatherAndTime.vue
  5. 2
      src/composables/useAutoBack.ts
  6. 6
      src/composables/useConfig.ts
  7. 17
      src/composables/useWeather.ts
  8. 5
      src/http/api/base/index.ts

7
src/App.vue

@ -1,6 +1,6 @@
<template>
<WeatherAndTime />
<ScrollList ref="scrollList" />
<WeatherAndTime :mall-code="mallCode" />
<ScrollList ref="scrollList" :mall-code="mallCode" />
</template>
<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref } from 'vue'
@ -9,6 +9,7 @@ import WeatherAndTime from '@/components/WeatherAndTime/WeatherAndTime.vue'
import ScrollList from '@/components/ScrollList/ScrollList.vue'
const scrollList = ref<InstanceType<typeof ScrollList> | null>(null)
const mallCode = ref<string>('')
const { checkHandleScreen } = useAutoBack(_checkHandleScreen)
function _checkHandleScreen() {
@ -16,6 +17,8 @@ function _checkHandleScreen() {
}
onMounted(() => {
const href = window.location.href
mallCode.value = href.split('=')[1]
window.addEventListener('touchend', checkHandleScreen)
})
onBeforeUnmount(() => {

23
src/components/ScrollList/ScrollList.vue

@ -31,7 +31,7 @@
</template>
<script setup lang="ts">
import { ref, toRefs, onMounted } from 'vue'
import { ref, toRefs, watch } from 'vue'
import { useRootStore } from '@/store/root'
import { getCustomerList } from '@/http/api/base'
import { HTTP_CODE } from '@/enums'
@ -39,8 +39,23 @@ import ScrollView from '@/base/ScrollView/ScrollView.vue'
import ScrollListItem from '@/components/ScrollListItem/ScrollListItem.vue'
import Loading from '@/base/Loading/Loading.vue'
type Props = {
mallCode: string
}
const props = withDefaults(defineProps<Props>(), {
mallCode: ''
})
watch(
() => props.mallCode,
() => {
_getCustomerList()
}
)
const store = useRootStore()
const { config, device } = toRefs(store)
const { config } = toRefs(store)
const customerList = ref<Customer[]>([])
const pageIndex = ref(1)
@ -54,8 +69,6 @@ function scrollEnd() {
_getCustomerList()
}
onMounted(_getCustomerList)
function _getCustomerList() {
if (loading.value || loaded.value) {
return
@ -64,7 +77,7 @@ function _getCustomerList() {
const params = {
pageIndex: pageIndex.value,
pageSize: 10,
mallCode: device.value.mallCode
mallCode: props.mallCode
}
getCustomerList(config.value.smallUrl, params)
.then(({ code, data }) => {

1
src/components/ScrollListItem/ScrollListItem.vue

@ -52,6 +52,7 @@ const store = useRootStore()
const { config } = toRefs(store)
function formatDate(date: string) {
// eslint-disable-next-line newline-per-chained-call
return new Date(date).toLocaleString().replace(/\//g, '-').substring(0, 16)
}
</script>

20
src/components/WeatherAndTime/WeatherAndTime.vue

@ -23,13 +23,31 @@
</template>
<script setup lang="ts">
import { watch } from 'vue'
import { formatDay } from '@/utils/utils'
import { useWeather } from '@/composables/useWeather'
import { useTime } from '@/composables/useTime'
import { useDay } from '@/composables/useDay'
import Icon from '@/base/Icon/Icon.vue'
const { weather, icon } = useWeather()
type Props = {
mallCode: string
}
const props = withDefaults(defineProps<Props>(), {
mallCode: ''
})
const { weather, icon, getWeatherStatus } = useWeather()
const { currentTime } = useTime()
const { whichWeek } = useDay()
watch(
() => props.mallCode,
(newVal: string) => {
if (newVal.length) {
getWeatherStatus(props.mallCode)
}
}
)
</script>

2
src/composables/useAutoBack.ts

@ -3,7 +3,7 @@ import { ref } from 'vue'
export const useAutoBack = (callback: () => void) => {
const MIN_TIME = 0
const CHECK_TIME = 1000
const TO_INDEX_TIME = 10
const TO_INDEX_TIME = 60
const toIndexTime = ref(TO_INDEX_TIME) //回首页的时间
const toIndexInterval = ref()

6
src/composables/useConfig.ts

@ -1,13 +1,11 @@
import { getWeather, getConfig, getDevice } from '@/http/api/base'
import { getConfig } from '@/http/api/base'
import { useRootStore } from '@/store/root'
export const useConfig = async () => {
const store = useRootStore()
try {
const [_weather, _config, _device] = await Promise.all([getWeather(), getConfig(), getDevice()])
store.SET_WEATHER(_weather.data)
const [_config] = await Promise.all([getConfig()])
store.SET_CONFIG(_config.data)
store.SET_DEVICE(_device.data)
Promise.resolve()
} catch (error) {
console.log('error: ', error)

17
src/composables/useWeather.ts

@ -1,12 +1,19 @@
import { ref, toRefs } from 'vue'
import { ref, toRefs, computed } from 'vue'
import { useRootStore } from '@/store/root'
import { getWeather } from '@/http/api/base'
type IconMap = { type: ExtractIcons<'sunny' | 'cloudy' | 'rain' | 'snow' | 'shade'>; status: string }
export const useWeather = () => {
const store = useRootStore()
const { weather } = toRefs(store)
const icon = ref({} as IconMap)
const { config } = toRefs(store)
const weather = ref({} as Weather)
function getWeatherStatus(mallCode: string) {
getWeather(config.value.smallUrl, mallCode).then(({ data }) => {
weather.value = data ?? {}
})
}
const status: IconMap[] = [
{ type: 'sunny', status: '晴' },
@ -15,7 +22,7 @@ export const useWeather = () => {
{ type: 'snow', status: '雪' },
{ type: 'shade', status: '阴' }
]
icon.value = status.find(item => weather.value.status?.includes(item.status)) ?? status[0]
const icon = computed(() => status.find(item => weather.value.status?.includes(item.status)) ?? status[0])
return { weather, icon }
return { weather, icon, getWeatherStatus }
}

5
src/http/api/base/index.ts

@ -4,11 +4,8 @@ import { PREFIX } from '@/enums'
//获取配置项
export const getConfig = () => request<Config>({ url: `${PREFIX.STATIC_URL}/JSON/getConfig.json` })
//获取设备
export const getDevice = () => request<Device>({ url: `${PREFIX.STATIC_URL}/JSON/GetDevCoordinateByIP.json` })
//获取天气
export const getWeather = () => request<Weather>({ url: `${PREFIX.STATIC_URL}/JSON/GetWeathers.json` })
export const getWeather = (url: string, mallCode: string) => request<Weather>({ url: `${url}/Api/Cdn/GetWeathers?mallCode=${mallCode}` })
//获取心声列表
export const getCustomerList = (url: string, params: { pageIndex: number; pageSize: number; mallCode: string }) => {

Loading…
Cancel
Save