feat: 引入uView Plus组件库,新增部分静态页面
This commit is contained in:
@@ -19,17 +19,20 @@
|
||||
@pickstart="pickstart"
|
||||
@pickend="pickend"
|
||||
>
|
||||
<!-- 省级选择列 -->
|
||||
<picker-view-column>
|
||||
<view class="ui-column-item" v-for="province in provinceList" :key="province.id">
|
||||
<view :style="getSizeByNameLength(province.name)">{{ province.name }}</view>
|
||||
</view>
|
||||
</picker-view-column>
|
||||
<picker-view-column>
|
||||
<!-- 市级选择列 -->
|
||||
<picker-view-column v-if="props.level >= 2">
|
||||
<view class="ui-column-item" v-for="city in cityList" :key="city.id">
|
||||
<view :style="getSizeByNameLength(city.name)">{{ city.name }}</view>
|
||||
</view>
|
||||
</picker-view-column>
|
||||
<picker-view-column>
|
||||
<!-- 区级选择列 -->
|
||||
<picker-view-column v-if="props.level >= 3">
|
||||
<view class="ui-column-item" v-for="district in districtList" :key="district.id">
|
||||
<view :style="getSizeByNameLength(district.name)">{{ district.name }}</view>
|
||||
</view>
|
||||
@@ -56,6 +59,7 @@
|
||||
* @property {String Number} z-index 弹出时的z-index值(默认1075)
|
||||
* @property {Array} default-selector 数组形式,其中每一项表示选择了range对应项中的第几个
|
||||
* @property {String} range-key 当range参数的元素为对象时,指定Object中的哪个key的值作为选择器显示内容
|
||||
* @property {Number} level 选择器层级:1-只显示省,2-显示省市,3-显示省市区(默认3)
|
||||
* @event {Function} confirm 点击确定按钮,返回当前选择的值
|
||||
* @event {Function} cancel 点击取消按钮,返回当前选择的值
|
||||
*/
|
||||
@@ -85,6 +89,12 @@
|
||||
type: String,
|
||||
default: '确认',
|
||||
},
|
||||
// 选择器层级:1-只显示省,2-显示省市,3-显示省市区
|
||||
level: {
|
||||
type: Number,
|
||||
default: 3,
|
||||
validator: (value) => [1, 2, 3].includes(value),
|
||||
},
|
||||
});
|
||||
const areaData = uni.getStorageSync('areaData');
|
||||
|
||||
@@ -98,7 +108,7 @@
|
||||
}
|
||||
};
|
||||
const state = reactive({
|
||||
currentIndex: [0, 0, 0],
|
||||
currentIndex: Array(props.level).fill(0),
|
||||
moving: false, // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确
|
||||
});
|
||||
const emits = defineEmits(['confirm', 'cancel', 'change']);
|
||||
@@ -106,10 +116,12 @@
|
||||
const provinceList = areaData;
|
||||
|
||||
const cityList = computed(() => {
|
||||
return areaData[state.currentIndex[0]].children;
|
||||
const province = areaData?.[state.currentIndex[0]];
|
||||
return province?.children || [];
|
||||
});
|
||||
const districtList = computed(() => {
|
||||
return cityList.value[state.currentIndex[1]]?.children;
|
||||
const city = cityList.value?.[state.currentIndex[1]];
|
||||
return city?.children || [];
|
||||
});
|
||||
// 标识滑动开始,只有微信小程序才有这样的事件
|
||||
const pickstart = () => {
|
||||
@@ -132,21 +144,28 @@
|
||||
|
||||
// 用户更改picker的列选项
|
||||
const change = (e) => {
|
||||
if (
|
||||
state.currentIndex[0] === e.detail.value[0] &&
|
||||
state.currentIndex[1] === e.detail.value[1]
|
||||
) {
|
||||
// 不更改省市区列表
|
||||
state.currentIndex[2] = e.detail.value[2];
|
||||
return;
|
||||
} else {
|
||||
// 更改省市区列表
|
||||
if (state.currentIndex[0] !== e.detail.value[0]) {
|
||||
e.detail.value[1] = 0;
|
||||
const newIndex = [...e.detail.value];
|
||||
let shouldResetSubsequent = false;
|
||||
|
||||
// 检查每一列是否发生变化,如果上级发生变化,需要重置下级
|
||||
for (let i = 0; i < props.level; i++) {
|
||||
if (state.currentIndex[i] !== newIndex[i]) {
|
||||
shouldResetSubsequent = true;
|
||||
// 重置当前列之后的所有列
|
||||
for (let j = i + 1; j < props.level; j++) {
|
||||
newIndex[j] = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
e.detail.value[2] = 0;
|
||||
state.currentIndex = e.detail.value;
|
||||
}
|
||||
|
||||
if (shouldResetSubsequent) {
|
||||
state.currentIndex = newIndex;
|
||||
} else {
|
||||
// 只有最后一列变化,不需要重置
|
||||
state.currentIndex = newIndex;
|
||||
}
|
||||
|
||||
emits('change', state.currentIndex);
|
||||
};
|
||||
|
||||
@@ -156,18 +175,29 @@
|
||||
if (state.moving) return;
|
||||
// #endif
|
||||
let index = state.currentIndex;
|
||||
let province = provinceList[index[0]];
|
||||
let city = cityList.value[index[1]];
|
||||
let district = districtList.value[index[2]];
|
||||
let province = provinceList?.[index[0]];
|
||||
if (!province) return;
|
||||
let result = {
|
||||
province_name: province.name,
|
||||
province_id: province.id,
|
||||
city_name: city.name,
|
||||
city_id: city.id,
|
||||
district_name: district.name,
|
||||
district_id: district.id,
|
||||
};
|
||||
|
||||
if (props.level >= 2) {
|
||||
let city = cityList.value?.[index[1]];
|
||||
if (city?.name) {
|
||||
result.city_name = city.name;
|
||||
result.city_id = city.id;
|
||||
}
|
||||
}
|
||||
|
||||
if (props.level >= 3) {
|
||||
let district = districtList.value?.[index[2]];
|
||||
if (district?.name) {
|
||||
result.district_name = district.name;
|
||||
result.district_id = district.id;
|
||||
}
|
||||
}
|
||||
|
||||
if (event) emits(event, result);
|
||||
};
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user