Files
dvcp_v2_wxcp_app/components/AiAreaPicker.vue

181 lines
4.4 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: {
2022-05-31 15:34:34 +08:00
/**
* 用于标定最大地区范围地区编码
*/
2022-04-29 19:26:41 +08:00
areaId: String,
2021-12-15 14:37:20 +08:00
name: {default: ''},
2022-05-31 15:41:11 +08:00
value: {default: ''},
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,
2022-05-31 15:34:34 +08:00
multiple: 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() {
2022-04-29 19:26:41 +08:00
return this.areaId || this.rootArea
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
},
2022-05-31 15:34:34 +08:00
hasValue() {
return this.multiple ? this.value?.length > 0 : !!this.value
}
2021-12-10 16:47:42 +08:00
},
data() {
return {
2021-12-29 11:50:04 +08:00
fullArea: [],
2022-04-29 19:26:41 +08:00
areaName: "",
rootArea: ""
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-08-17 15:18:06 +08:00
value: {
immediate: true,
handler(v) {
2022-08-17 17:13:57 +08:00
v && this.getAreaName(this.value)
2022-08-17 15:18:06 +08:00
}
2022-01-18 17:16:18 +08:00
},
fullArea(v) {
2022-05-31 15:34:34 +08:00
v && this.value && !this.multiple && 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-04-29 19:26:41 +08:00
this.getRootArea()
2022-01-13 16:47:34 +08:00
},
2021-12-10 16:47:42 +08:00
methods: {
2022-04-29 19:26:41 +08:00
getRootArea() {
if (uni.getStorageSync("rootArea")) {
this.rootArea = uni.getStorageSync("rootArea")
} else this.$http.post("/app/appdvcpconfig/getCorpArea", null, {}).then(res => {
if (res?.data) {
this.rootArea = res.data
uni.setStorageSync("rootArea", res.data)
}
})
},
2022-03-17 15:40:36 +08:00
getInfo(areaId) {
2022-08-17 17:13:57 +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-08-17 17:13:57 +08:00
}) : Promise.reject()
2021-12-10 16:47:42 +08:00
},
2022-03-17 15:40:36 +08:00
getFullArea(areaId) {
2022-08-17 17:13:57 +08:00
const current = this.fullArea.slice(-1)?.[0]?.id
return current && current == areaId ? Promise.resolve(this.fullArea) : this.getInfo(areaId).then(meta => {
2022-03-17 15:40:36 +08:00
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-05-31 15:34:34 +08:00
} else if (data.length > -1) {
this.$emit("select", data)
this.areaName = data.length == 0 ? '' : `已选择${data.length}`
this.$forceUpdate()
2022-03-21 19:50:58 +08:00
}
})
2022-05-31 15:34:34 +08:00
let {value, all, valueLevel, selectRoot, areaId = this.root} = this.$props,
action = "/components/pages/selectArea"
if (this.multiple) {
action = "/components/pages/multiplyArea"
}
let url = qs.stringifyUrl({
2022-05-31 15:34:34 +08:00
url: action, 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-08-17 17:13:57 +08:00
getAreaName(area) {
if (this.multiple) {
this.areaName = `已选择${[area].flat().filter(Boolean)?.length}`.replace('已选择0个', '')
} else {
this.getFullArea(area).then(list => {
let arr = JSON.parse(JSON.stringify(list))
this.areaName = arr?.reverse()?.[0]?.name || ""
}).catch(() => this.areaName = '')
}
2022-03-17 15:40:36 +08:00
},
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>