Files
dvcp_v2_wxcp_app/src/components/AiAreaPicker.vue

148 lines
3.3 KiB
Vue
Raw Normal View History

2021-12-10 16:47:42 +08:00
<template>
<section class="AiAreaPicker">
<div @tap="handleJump">
<slot v-if="$slots.default"/>
<div v-else-if="isForm">
<ai-more v-model="areaName"/>
2021-12-10 16:47:42 +08:00
</div>
<div v-else class="areaBtn">
<image :src="locationIcon" class="location"/>
<div v-text="areaName"/>
2021-12-29 11:50:04 +08:00
</div>
</div>
2021-12-10 16:47:42 +08:00
</section>
</template>
<script>
2021-12-15 14:37:20 +08:00
import {mapState} from 'vuex'
2022-03-15 15:36:19 +08:00
import AiMore from "./AiMore";
import qs from "query-string"
2021-12-10 16:47:42 +08:00
export default {
name: 'AiAreaPicker',
components: {AiMore},
2022-03-17 15:40:36 +08:00
model: {
prop: "value",
event: "select"
},
2021-12-10 16:47:42 +08:00
props: {
2021-12-15 14:37:20 +08:00
areaId: {default: ''},
name: {default: ''},
2022-01-13 16:47:34 +08:00
value: String,
2021-12-10 16:47:42 +08:00
all: Boolean,
2022-03-14 20:35:00 +08:00
icon: {default: "location.svg"},
isForm: Boolean,
valueLevel: {default: 5},
2022-03-16 20:10:17 +08:00
fullName: {default: ''},
disabled: Boolean,
selectRoot: Boolean,
2021-12-10 16:47:42 +08:00
},
computed: {
...mapState(['user']),
dataRange() {
let rules = [10, 8, 6, 3, 0],
2021-12-15 14:37:20 +08:00
level = 0
2022-03-17 17:18:42 +08:00
if (this.all || this.disabled) return (level = 0)
2021-12-10 16:47:42 +08:00
rules.some((e, i) => {
let reg = new RegExp(`0{${e}}`, 'g')
2022-03-16 20:10:17 +08:00
if (reg.test(this.root)) {
2021-12-10 16:47:42 +08:00
return (level = i)
}
})
return level
},
2022-03-16 20:10:17 +08:00
root() {
return this.areaId || this.user.areaId || this.$areaId
2021-12-10 16:47:42 +08:00
},
2021-12-20 17:29:52 +08:00
locationIcon() {
return this.$cdn + this.icon
2021-12-10 16:47:42 +08:00
},
},
data() {
return {
2021-12-29 11:50:04 +08:00
fullArea: [],
2022-03-17 15:40:36 +08:00
areaName: ""
2021-12-10 16:47:42 +08:00
}
},
watch: {
2022-03-17 15:40:36 +08:00
root(v) {
2022-03-16 20:10:17 +08:00
v && (this.getFullArea(v))
2022-01-13 18:48:39 +08:00
},
2022-03-14 20:35:00 +08:00
value(v) {
2022-03-17 15:40:36 +08:00
v && !this.areaName && this.getAreaName(this.value)
2022-01-18 17:16:18 +08:00
},
fullArea(v) {
v && this.$emit('update:fullName', v?.map(e => e.name)?.join("") || "")
},
areaName(v) {
v && this.$emit('update:name', v)
2021-12-30 17:36:27 +08:00
}
2021-12-10 16:47:42 +08:00
},
2022-03-17 15:40:36 +08:00
created() {
2022-03-17 15:53:29 +08:00
this.value && this.getAreaName(this.value)
2022-01-13 16:47:34 +08:00
},
2021-12-10 16:47:42 +08:00
methods: {
2022-03-17 15:40:36 +08:00
getInfo(areaId) {
2022-03-16 20:10:17 +08:00
return areaId && this.$http.post('/admin/area/getAllParentAreaId', null, {
2021-12-15 14:37:20 +08:00
withoutToken: true,
params: {areaId},
2022-03-17 15:40:36 +08:00
}).then(res => {
2021-12-15 14:37:20 +08:00
if (res?.data) {
2022-03-17 15:40:36 +08:00
return res.data.reverse()
2021-12-15 14:37:20 +08:00
}
2022-01-13 16:47:34 +08:00
})
2021-12-10 16:47:42 +08:00
},
2022-03-17 15:40:36 +08:00
getFullArea(areaId) {
return this.fullArea?.length > 0 ? Promise.resolve(this.fullArea) : this.getInfo(areaId).then(meta => {
if (meta.length > 1) {
this.fullArea = meta.slice(this.dataRange)
} else {
this.fullArea = meta
}
return this.fullArea
})
},
handleJump() {
2022-03-16 20:10:17 +08:00
if (!this.disabled) {
uni.$once('selectArea', data => {
2022-03-21 19:50:58 +08:00
if (data?.id) {
this.$emit("select", data.id)
this.areaName = data.name
this.fullArea = data.fullArea
2022-03-21 19:52:13 +08:00
this.$forceUpdate()
2022-03-21 19:50:58 +08:00
}
})
let {value, all, valueLevel, selectRoot, areaId} = this.$props
let url = qs.stringifyUrl({
url: "/components/pages/selectArea", query: {...this.$attrs, value, all, valueLevel, selectRoot, areaId}
})
uni.navigateTo({url})
2022-01-13 16:47:34 +08:00
}
2021-12-10 16:47:42 +08:00
},
2022-03-17 15:40:36 +08:00
getAreaName(area, name) {
name ? this.areaName = name : this.getFullArea(area).then(list => {
let arr = JSON.parse(JSON.stringify(list))
this.areaName = arr?.reverse()?.[0]?.name
})
},
2021-12-29 11:50:04 +08:00
}
2021-12-10 16:47:42 +08:00
}
</script>
<style lang="scss" scoped>
.AiAreaPicker {
.areaBtn {
2021-12-29 11:50:04 +08:00
display: flex;
2021-12-31 10:16:43 +08:00
align-items: center;
2021-12-29 11:50:04 +08:00
}
2021-12-10 16:47:42 +08:00
.location {
width: 28px;
height: 80px;
margin-right: 12px;
}
}
</style>