2022-07-22 17:32:45 +08:00
|
|
|
<template>
|
|
|
|
|
<el-form
|
2022-07-25 17:32:16 +08:00
|
|
|
ref="formRef"
|
|
|
|
|
:model="modelValue"
|
2022-07-22 17:32:45 +08:00
|
|
|
:rules="rules"
|
2022-07-26 14:35:21 +08:00
|
|
|
:label-width="`${labelWidth}px`"
|
2022-07-22 17:32:45 +08:00
|
|
|
>
|
|
|
|
|
<el-row>
|
|
|
|
|
<el-col :span="24">
|
|
|
|
|
<el-form-item label="所在地:" required>
|
|
|
|
|
<el-row type="flex" justify="space-between">
|
|
|
|
|
<el-col :span="7">
|
|
|
|
|
<el-form-item prop="province">
|
|
|
|
|
<el-select
|
2022-07-25 17:32:16 +08:00
|
|
|
v-model="modelValue.province"
|
2022-07-22 17:32:45 +08:00
|
|
|
clearable
|
2022-07-26 14:35:21 +08:00
|
|
|
filterable
|
2022-07-22 17:32:45 +08:00
|
|
|
placeholder="请选择"
|
2022-07-26 14:35:21 +08:00
|
|
|
:disabled="provinceSelectList.length === 0"
|
2022-07-25 17:32:16 +08:00
|
|
|
@change="provinceChanged"
|
2022-07-22 17:32:45 +08:00
|
|
|
>
|
|
|
|
|
<el-option
|
2022-07-25 17:32:16 +08:00
|
|
|
v-for="item in provinceSelectList"
|
|
|
|
|
:key="item.provinceCode"
|
|
|
|
|
:label="item.provinceName"
|
|
|
|
|
:value="item.provinceCode"
|
2022-07-22 17:32:45 +08:00
|
|
|
>
|
|
|
|
|
</el-option>
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-col>
|
|
|
|
|
<el-col :span="7">
|
|
|
|
|
<el-form-item prop="city">
|
|
|
|
|
<el-select
|
2022-07-25 17:32:16 +08:00
|
|
|
v-model="modelValue.city"
|
2022-07-22 17:32:45 +08:00
|
|
|
clearable
|
2022-07-26 14:35:21 +08:00
|
|
|
filterable
|
|
|
|
|
:disabled="citySelectList.length === 0"
|
2022-07-22 17:32:45 +08:00
|
|
|
placeholder="请选择"
|
2022-07-25 17:32:16 +08:00
|
|
|
@change="cityChanged"
|
2022-07-22 17:32:45 +08:00
|
|
|
>
|
|
|
|
|
<el-option
|
2022-07-25 17:32:16 +08:00
|
|
|
v-for="item in citySelectList"
|
|
|
|
|
:key="item.cityCode"
|
|
|
|
|
:label="item.cityName"
|
|
|
|
|
:value="item.cityCode"
|
2022-07-22 17:32:45 +08:00
|
|
|
>
|
|
|
|
|
</el-option>
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-col>
|
|
|
|
|
<el-col :span="7">
|
|
|
|
|
<el-form-item prop="district">
|
|
|
|
|
<el-select
|
2022-07-25 17:32:16 +08:00
|
|
|
v-model="modelValue.district"
|
2022-07-22 17:32:45 +08:00
|
|
|
clearable
|
2022-07-26 14:35:21 +08:00
|
|
|
filterable
|
|
|
|
|
:disabled="districtSelectList.length === 0"
|
2022-07-22 17:32:45 +08:00
|
|
|
placeholder="请选择"
|
|
|
|
|
>
|
|
|
|
|
<el-option
|
2022-07-25 17:32:16 +08:00
|
|
|
v-for="item in districtSelectList"
|
|
|
|
|
:key="item.areaCode"
|
|
|
|
|
:label="item.areaName"
|
|
|
|
|
:value="item.areaCode"
|
2022-07-22 17:32:45 +08:00
|
|
|
>
|
|
|
|
|
</el-option>
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-col>
|
|
|
|
|
</el-row>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-col>
|
|
|
|
|
</el-row>
|
|
|
|
|
</el-form>
|
|
|
|
|
</template>
|
2022-07-25 17:32:16 +08:00
|
|
|
<script setup name="CityOptions">
|
|
|
|
|
import { provinceList, cityList, districtList } from "@/api/config";
|
|
|
|
|
import { reactive, ref, toRefs } from "vue";
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
modelValue: Object,
|
|
|
|
|
labelWidth: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 120,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const { modelValue, labelWidth } = toRefs(props);
|
|
|
|
|
const formRef = ref(null);
|
|
|
|
|
const provinceSelectList = ref([]); // 省
|
|
|
|
|
const citySelectList = ref([]); // 市
|
|
|
|
|
const districtSelectList = ref([]); // 区\
|
|
|
|
|
const data = reactive({
|
|
|
|
|
rules: {
|
|
|
|
|
province: [
|
|
|
|
|
{ required: true, message: "请选择", trigger: ["change", "blue"] },
|
|
|
|
|
],
|
|
|
|
|
city: [{ required: true, message: "请选择", trigger: ["change", "blue"] }],
|
|
|
|
|
district: [
|
|
|
|
|
{ required: true, message: "请选择", trigger: ["change", "blue"] },
|
|
|
|
|
],
|
2022-07-22 17:32:45 +08:00
|
|
|
},
|
2022-07-25 17:32:16 +08:00
|
|
|
});
|
|
|
|
|
const { rules } = toRefs(data);
|
|
|
|
|
// 获取 省列表
|
|
|
|
|
const getProvinceList = async () => {
|
|
|
|
|
const resp = await provinceList();
|
2022-07-26 14:35:21 +08:00
|
|
|
provinceSelectList.value = resp.data.map((el) => {
|
|
|
|
|
return { ...el, provinceCode: el.provinceCode.toString() };
|
|
|
|
|
});
|
2022-07-25 17:32:16 +08:00
|
|
|
};
|
|
|
|
|
// 获取市列表
|
|
|
|
|
const getCityListByProvinceId = async (provinceId) => {
|
|
|
|
|
const { data } = await cityList(provinceId);
|
2022-07-26 14:35:21 +08:00
|
|
|
citySelectList.value = data.map((el) => {
|
|
|
|
|
return {
|
|
|
|
|
...el,
|
|
|
|
|
cityCode: el.cityCode.toString(),
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-07-25 17:32:16 +08:00
|
|
|
};
|
|
|
|
|
// 根据市id获取县区列表
|
|
|
|
|
const getAreaListByCityId = async (cityId) => {
|
|
|
|
|
const { data } = await districtList(cityId);
|
2022-07-26 14:35:21 +08:00
|
|
|
districtSelectList.value = data.map((el) => {
|
|
|
|
|
return {
|
|
|
|
|
...el,
|
|
|
|
|
areaCode: el.areaCode.toString(),
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-07-25 17:32:16 +08:00
|
|
|
};
|
|
|
|
|
// 当省改变时
|
|
|
|
|
const provinceChanged = () => {
|
|
|
|
|
// 清除市县代码列表
|
|
|
|
|
modelValue.value.city = undefined;
|
|
|
|
|
modelValue.value.district = undefined;
|
|
|
|
|
// 清除市县列表
|
|
|
|
|
citySelectList.value = [];
|
|
|
|
|
districtSelectList.value = [];
|
|
|
|
|
// 重新请求城市列表
|
|
|
|
|
modelValue.value.province &&
|
|
|
|
|
getCityListByProvinceId(modelValue.value.province);
|
|
|
|
|
};
|
|
|
|
|
// 当市改变时
|
|
|
|
|
const cityChanged = () => {
|
|
|
|
|
// 清除县区代码列表
|
|
|
|
|
modelValue.value.district = undefined;
|
|
|
|
|
districtSelectList.value = [];
|
|
|
|
|
modelValue.value.city && getAreaListByCityId(modelValue.value.city);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const validateForm = async () => {
|
2022-07-26 14:35:21 +08:00
|
|
|
try {
|
|
|
|
|
return await formRef.value.validate();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-07-25 17:32:16 +08:00
|
|
|
};
|
2022-07-26 14:35:21 +08:00
|
|
|
|
2022-07-25 17:32:16 +08:00
|
|
|
getProvinceList();
|
2022-10-31 17:45:39 +08:00
|
|
|
watch(
|
|
|
|
|
modelValue,
|
|
|
|
|
(val) => {
|
|
|
|
|
if (val.province) {
|
|
|
|
|
getCityListByProvinceId(val.province);
|
|
|
|
|
}
|
|
|
|
|
if (val.city) {
|
|
|
|
|
getAreaListByCityId(val.city);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ deep: true }
|
|
|
|
|
);
|
2022-07-25 17:32:16 +08:00
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
|
validateForm,
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- <script>
|
|
|
|
|
export default {
|
2022-07-22 17:32:45 +08:00
|
|
|
data() {
|
2022-07-25 17:32:16 +08:00
|
|
|
return {};
|
2022-07-22 17:32:45 +08:00
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
value(newOld) {
|
|
|
|
|
const data = Object.assign({}, newOld);
|
|
|
|
|
this.provinceCodeChange(data.province);
|
|
|
|
|
this.cityCodeChange(data.city);
|
|
|
|
|
newOld.city = data.city;
|
|
|
|
|
newOld.district = data.district;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
getProvinceByParent(id) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
areaList({ code: id })
|
|
|
|
|
.then(({ code, msg, data }) => {
|
|
|
|
|
if (code == 200) {
|
|
|
|
|
resolve(data);
|
|
|
|
|
} else {
|
|
|
|
|
this.$modal.msgError(msg);
|
|
|
|
|
reject({ msg, code });
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
reject(error);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
async provinceCodeChange(id) {
|
2022-07-25 17:32:16 +08:00
|
|
|
// delete this.modelValue.city;
|
|
|
|
|
// delete this.modelValue.district;
|
|
|
|
|
this.modelValue.city = undefined;
|
|
|
|
|
this.modelValue.district = undefined;
|
2022-07-22 17:32:45 +08:00
|
|
|
if (!id) {
|
|
|
|
|
this.citySelectList = [];
|
|
|
|
|
this.districtSelectList = [];
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.citySelectList = await this.getProvinceByParent(id);
|
|
|
|
|
},
|
|
|
|
|
async cityCodeChange(id) {
|
2022-07-25 17:32:16 +08:00
|
|
|
// delete this.modelValue.district;
|
|
|
|
|
this.modelValue.district = undefined;
|
2022-07-22 17:32:45 +08:00
|
|
|
if (!id) {
|
|
|
|
|
this.districtSelectList = [];
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.districtSelectList = await this.getProvinceByParent(id);
|
|
|
|
|
},
|
|
|
|
|
submitForm() {
|
|
|
|
|
let flag = false;
|
|
|
|
|
this.$refs["form"].validate((valid) => {
|
|
|
|
|
flag = valid;
|
|
|
|
|
});
|
|
|
|
|
return flag;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
created() {
|
|
|
|
|
areaList().then((res) => {
|
|
|
|
|
this.provinceSelectList = res.data;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
2022-07-25 17:32:16 +08:00
|
|
|
</script> -->
|