useRoleCityList.ts
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { computed, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { cityApi } from '@/api'
import { useAuthStore } from '@/stores/auth'
export type CityOption = {
id: number
name: string
[key: string]: any
}
export function useRoleCityList() {
const auth = useAuthStore()
const { isAdmin, user } = storeToRefs(auth)
const cityList = ref<CityOption[]>([])
const managedCityId = computed<number | undefined>(() => user.value?.cityId)
const managedCityName = computed(() => user.value?.cityName || '')
function buildManagedCityList() {
return managedCityId.value
? [{ id: managedCityId.value, name: managedCityName.value || `租户#${managedCityId.value}` }]
: []
}
async function loadCities() {
if (!isAdmin.value) {
cityList.value = buildManagedCityList()
return
}
const res: any = await cityApi.openList()
cityList.value = Array.isArray(res?.data) ? res.data : []
}
function getCityName(cityId?: number) {
const city = cityList.value.find(item => item.id === cityId)
return city?.name || (cityId ? `租户#${cityId}` : '-')
}
return {
isAdmin,
managedCityId,
managedCityName,
cityList,
loadCities,
getCityName,
}
}