Skip to content

Commit

Permalink
refactor: 重构核心数据处理模块
Browse files Browse the repository at this point in the history
  • Loading branch information
TerryZ committed Feb 17, 2023
1 parent 84a5084 commit 10a0031
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
79 changes: 57 additions & 22 deletions src/utils/data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { reactive, toRaw } from 'vue'
import { CITY_KEY, AREA_KEY, TOWN_KEY } from '../constants'
import { CN } from '../language'
import { regionProvinces } from '../formatted'
import { regionToModel } from './parse'
import { loadCities, loadAreas, loadTowns } from './helper'
import { getCities, getAreas, getTowns } from './helper'

export const commonProps = {
city: { type: Boolean, default: true },
Expand All @@ -24,13 +25,7 @@ export function dataChange (emit, data) {
emit('change', data)
}

export function useData (props) {
const list = reactive({
provinces: regionProvinces,
cities: [],
areas: [],
towns: []
})
function useRegionData () {
const data = reactive({
province: {},
city: undefined,
Expand All @@ -39,59 +34,99 @@ export function useData (props) {
})

function reset () {
resetRegionList()
resetRegionData()
data.province = undefined
data.city = undefined
data.area = undefined
data.town = undefined
}
function resetRegionList () {
function setData (val) {
Object.assign(data, val)
}

return {
data,
reset,
setData
}
}

function useRegionList () {
const list = reactive({
provinces: regionProvinces,
cities: [],
areas: [],
towns: []
})

function reset () {
list.cities = []
list.areas = []
list.towns = []
}
function resetRegionData () {
data.province = undefined
data.city = undefined
data.area = undefined
data.town = undefined

function loadLevelList (level, parentData) {
switch (level) {
case CITY_KEY: return (list.cities = getCities(parentData))
case AREA_KEY: return (list.areas = getAreas(parentData))
case TOWN_KEY:
getTowns(parentData).then(resp => { list.towns = resp })
}
}

return {
list,
reset,
loadLevelList
}
}

export function useData (props) {
const data = useRegionData()
const list = useRegionList()

function reset () {
list.reset()
data.reset()
}
function setData (val) {
Object.assign(data, val)

list.reset()
}
function setProvince (val) {
data.province = val
data.city = undefined
data.area = undefined
data.town = undefined
list.cities = loadCities(val)
list.cities = getCities(val)
list.areas = []
list.towns = []
}
function setCity (val) {
data.city = val
data.area = undefined
data.town = undefined
list.areas = loadAreas(val)
list.areas = getAreas(val)
list.towns = []
}
function setArea (val) {
data.area = val
data.town = undefined
loadTowns(val).then(resp => {
getTowns(val).then(resp => {
list.towns = resp
})
}
function setTown (val) {
data.town = val
}
function getData () {
return toRaw(data)
return toRaw(data.data)
}

return {
list,
data,
reset,
resetRegionList,
resetRegionData,
setData,
setProvince,
setCity,
Expand Down
12 changes: 6 additions & 6 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function isCity (key) {
* @param {object} province - 省
* @returns {object[]} - 城市列表
*/
export function loadCities (province) {
export function getCities (province) {
if (!province || !Object.keys(province).length) return []

const list = regionCities.filter(val => {
Expand All @@ -45,7 +45,7 @@ export function loadCities (province) {
* @param {object} city - 城市
* @returns {object[]} 区/县列表
*/
export function loadAreas (city) {
export function getAreas (city) {
if (!city || !Object.keys(city).length) return []

const cityKey = Number.parseInt(city.key)
Expand All @@ -68,7 +68,7 @@ export function loadAreas (city) {
* @param {object} area - 区/县
* @returns {object[]} 乡/镇列表
*/
export async function loadTowns (area) {
export async function getTowns (area) {
if (!area || !Object.keys(area).length) return []

try {
Expand Down Expand Up @@ -97,9 +97,9 @@ export async function loadTowns (area) {
*/
export function getLoader (level) {
switch (level) {
case CITY_LEVEL: return loadCities
case AREA_LEVEL: return loadAreas
case TOWN_LEVEL: return loadTowns
case CITY_LEVEL: return getCities
case AREA_LEVEL: return getAreas
case TOWN_LEVEL: return getTowns
}
}

Expand Down

0 comments on commit 10a0031

Please sign in to comment.