文件
fenleixinxi/uniapp/components/fc-area-picker/fc-area-picker.vue
MiAn cb12bf5fc8 feat: 新增地址选择插件及订阅消息等多项功能优化
- 新增地址选择插件,支持地图位置选择并返回地址和经纬度
- 新增微信订阅消息发送功能,支持审核结果、提现通知等场景
- 新增订单支付状态查询、提现收款人姓名保存等接口
- 新增服务区域管理,支持按省市区划分并同步到信息发布
- 新增资金流水和营收报表导出功能
- 新增可用优惠券查询、订单退款拆分处理等功能
- 优化后台角标显示、首页数据统计、UI配置下发等
- 修复订单支付并发问题、地区管理逻辑等
2026-10-22 17:14:56 +08:00

242 行
6.2 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<picker
class="fc-area-picker"
:class="'fc-area-picker--' + variant"
mode="multiSelector"
:range="columns"
range-key="label"
:value="index"
:disabled="disabled || !hasAreas"
@columnchange="onColumnChange"
@change="onChange"
>
<view class="fc-area-picker__inner">
<text class="fc-area-picker__text" :class="{ 'fc-area-picker__text--placeholder': !display }">
{{ display || placeholder }}
</text>
<text class="fc-area-picker__arrow">▾</text>
</view>
</picker>
</template>
<script>
import { groupAreaTree } from '@/common/utils/area.js'
/**
* 服务区域级联选择器(省 > 市 > 区县)
*
* 数据源为平铺的 /home/areas 列表,每条记录自带省 / 市 / 区县;
* 组件内部按省、市生成三列级联,选中项以区县(areaId)为准。
*/
export default {
name: 'FcAreaPicker',
props: {
// 平铺服务区域:[{ id, province, city, district }]
areas: { type: Array, default: () => [] },
// 当前选中的服务区域 ID
value: { type: [Number, String], default: 0 },
// 是否允许选择「全部区域」(列表筛选用)
allowAll: { type: Boolean, default: false },
placeholder: { type: String, default: '请选择区域' },
// field:表单行样式;chip:筛选栏胶囊样式
variant: { type: String, default: 'field' },
disabled: { type: Boolean, default: false },
// 是否展示「省 / 市 / 区」完整路径
showFull: { type: Boolean, default: false },
},
data() {
return {
tree: [],
columns: [[], [], []],
index: [0, 0, 0],
display: '',
}
},
computed: {
hasAreas() {
return Array.isArray(this.areas) && this.areas.length > 0
},
},
watch: {
areas: {
handler() {
this.rebuildTree()
},
deep: true,
},
value() {
this.syncFromValue()
},
allowAll() {
this.syncFromValue()
},
},
mounted() {
this.rebuildTree()
},
methods: {
/** 平铺列表 → 省市级联树 */
rebuildTree() {
this.tree = groupAreaTree(this.areas || [])
this.syncFromValue()
},
/** 按当前选中值回显三列索引 */
syncFromValue() {
const areaId = Number(this.value) || 0
const offset = this.allowAll ? 1 : 0
let pi = 0
let ci = 0
let di = 0
let matched = null
if (areaId > 0) {
for (let p = 0; p < this.tree.length && !matched; p++) {
const cities = this.tree[p].cities || []
for (let c = 0; c < cities.length && !matched; c++) {
const districts = cities[c].districts || []
for (let d = 0; d < districts.length; d++) {
if (Number(districts[d].id) === areaId) {
pi = p
ci = c
di = d
matched = { province: this.tree[p].name, city: cities[c].name, district: districts[d] }
break
}
}
}
}
}
this.index = [pi + offset, ci, di]
this.buildColumns()
this.display = matched ? this.formatDisplay(matched) : ''
},
/** 按当前索引重建三列 */
buildColumns() {
const offset = this.allowAll ? 1 : 0
const pi = Math.max(0, Number(this.index[0]) - offset)
const ci = Math.max(0, Number(this.index[1]) || 0)
const first = this.allowAll ? [{ label: '全部区域', type: 'all' }] : []
this.columns = [
first.concat(this.tree.map((p) => ({ label: p.name, type: 'province', province: p.name }))),
((this.tree[pi] && this.tree[pi].cities) || []).map((c) => ({
label: c.name,
type: 'city',
city: c.name,
})),
(((this.tree[pi] && this.tree[pi].cities[ci]) || {}).districts || []).map((d) => ({
label: d.name,
type: 'area',
area: d,
})),
]
},
/** 滚动某一列时联动后续列 */
onColumnChange(e) {
const column = Number(e.detail.column)
const value = Number(e.detail.value)
const next = [...this.index]
next[column] = value
if (column === 0) {
next[1] = 0
next[2] = 0
} else if (column === 1) {
next[2] = 0
}
this.index = next
this.buildColumns()
},
/** 点确定后向上抛出选中的区县 */
onChange(e) {
const values = e.detail.value || []
this.index = [Number(values[0] || 0), Number(values[1] || 0), Number(values[2] || 0)]
this.buildColumns()
const offset = this.allowAll ? 1 : 0
const pi = this.index[0] - offset
if (pi < 0) {
this.display = ''
this.$emit('change', null)
return
}
const province = this.tree[pi]
const city = province && province.cities ? province.cities[this.index[1]] : null
const district = city && city.districts ? city.districts[this.index[2]] : null
if (!province || !city || !district) {
this.display = ''
this.$emit('change', null)
return
}
const selected = { province: province.name, city: city.name, district }
this.display = this.formatDisplay(selected)
this.$emit('change', {
areaId: Number(district.id) || 0,
province: province.name,
city: city.name,
district: district.name,
})
},
formatDisplay(selected) {
if (!selected) return ''
const district = selected.district || {}
const districtName = typeof district === 'string' ? district : district.name
if (this.showFull) {
return `${selected.province} / ${selected.city} / ${districtName}`
}
return districtName || ''
},
},
}
</script>
<style lang="scss" scoped>
.fc-area-picker {
display: block;
}
.fc-area-picker__inner {
display: flex;
align-items: center;
justify-content: space-between;
}
.fc-area-picker--field .fc-area-picker__inner {
min-height: 88rpx;
padding: 0 24rpx;
background: #ffffff;
border: 2rpx solid $fc-border;
border-radius: $fc-radius;
}
.fc-area-picker--chip .fc-area-picker__inner {
justify-content: flex-start;
}
.fc-area-picker--plain .fc-area-picker__inner {
justify-content: flex-end;
}
.fc-area-picker--plain .fc-area-picker__text {
text-align: right;
font-size: $fc-font-sm;
color: $fc-text-secondary;
}
.fc-area-picker__text {
flex: 1;
font-size: $fc-font;
color: $fc-text;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
&--placeholder {
color: $fc-text-placeholder;
}
}
.fc-area-picker--chip .fc-area-picker__text {
flex: none;
font-size: $fc-font-sm;
color: $fc-text-regular;
}
.fc-area-picker__arrow {
margin-left: 8rpx;
font-size: 22rpx;
color: $fc-text-muted;
}
</style>