|
|
@@ -15,7 +15,7 @@
|
|
|
v-for="item in province"
|
|
|
:key="item.cityId"
|
|
|
:label="item.name"
|
|
|
- :value="item.cityId">
|
|
|
+ :value="item.name">
|
|
|
</el-option>
|
|
|
</el-select>
|
|
|
</el-col>
|
|
|
@@ -25,7 +25,7 @@
|
|
|
v-for="item in city"
|
|
|
:key="item.cityId"
|
|
|
:label="item.name"
|
|
|
- :value="item.cityId">
|
|
|
+ :value="item.name">
|
|
|
</el-option>
|
|
|
</el-select>
|
|
|
</el-col>
|
|
|
@@ -35,7 +35,7 @@
|
|
|
v-for="item in district"
|
|
|
:key="item.cityId"
|
|
|
:label="item.name"
|
|
|
- :value="item.cityId">
|
|
|
+ :value="item.name">
|
|
|
</el-option>
|
|
|
</el-select>
|
|
|
</el-col>
|
|
|
@@ -58,16 +58,23 @@ export default {
|
|
|
name: "add",
|
|
|
data() {
|
|
|
return {
|
|
|
+ isEdit: false,
|
|
|
+ editAddressId: null,
|
|
|
citys:[],
|
|
|
province:[],
|
|
|
city:[],
|
|
|
district:[],
|
|
|
// 表单参数
|
|
|
form: {
|
|
|
+ userId: null,
|
|
|
+ realName: null,
|
|
|
+ phone: null,
|
|
|
+ detail: null,
|
|
|
province:null,
|
|
|
city:null,
|
|
|
district:null,
|
|
|
cityId:null,
|
|
|
+ isDefault: null,
|
|
|
},
|
|
|
// 表单校验
|
|
|
rules: {
|
|
|
@@ -95,26 +102,24 @@ export default {
|
|
|
},
|
|
|
methods: {
|
|
|
districtChange(val){
|
|
|
- const item = this.district.find(i => i.cityId === val)
|
|
|
- this.form.district=item.name;
|
|
|
+ const item = this.district.find(i => i.name === val)
|
|
|
},
|
|
|
cityChange(val){
|
|
|
- const item = this.city.find(i => i.cityId === val)
|
|
|
- this.district = item.children
|
|
|
+ const item = this.city.find(i => i.name === val)
|
|
|
+ this.district = item.children || []
|
|
|
this.form.district=null;
|
|
|
- this.form.city=item.name;
|
|
|
- this.form.cityId=val;
|
|
|
+ this.form.cityId=item.cityId;
|
|
|
},
|
|
|
provinceChange(val){
|
|
|
- const item = this.citys.find(i => i.cityId === val)
|
|
|
- this.city = item.children
|
|
|
+ const item = this.citys.find(i => i.name === val)
|
|
|
+ this.city = item.children || []
|
|
|
this.district=[];
|
|
|
this.form.city=null;
|
|
|
this.form.district=null;
|
|
|
- this.form.province=item.name;
|
|
|
+ this.form.cityId=null;
|
|
|
},
|
|
|
getCityList(){
|
|
|
- getCitys().then(res => {
|
|
|
+ return getCitys().then(res => {
|
|
|
this.loading = false;
|
|
|
this.citys = this.convertCityData(res.data)
|
|
|
this.province=this.citys.filter(item => item.parentId===0 )
|
|
|
@@ -130,18 +135,76 @@ export default {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- init(userId){
|
|
|
- this.form.userId=userId;
|
|
|
+ async init(userId, addressData){
|
|
|
+ this.form.userId = userId;
|
|
|
+ if (this.province.length === 0) {
|
|
|
+ await this.getCityList();
|
|
|
+ }
|
|
|
+ this.$nextTick(() => {
|
|
|
+ if (this.$refs["form"]) {
|
|
|
+ this.$refs["form"].clearValidate();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (addressData) {
|
|
|
+ this.isEdit = true;
|
|
|
+ this.editAddressId = addressData.addressId;
|
|
|
+ this.form.realName = addressData.realName;
|
|
|
+ this.form.phone = addressData.phone;
|
|
|
+ this.form.detail = addressData.detail;
|
|
|
+ this.form.isDefault = addressData.isDefault;
|
|
|
+ this.findAndSetCityIds(addressData);
|
|
|
+ } else {
|
|
|
+ this.isEdit = false;
|
|
|
+ this.editAddressId = null;
|
|
|
+ if (this.$refs["form"]) {
|
|
|
+ this.$refs["form"].resetFields();
|
|
|
+ }
|
|
|
+ this.form.realName = null;
|
|
|
+ this.form.phone = null;
|
|
|
+ this.form.detail = null;
|
|
|
+ this.form.province = null;
|
|
|
+ this.form.city = null;
|
|
|
+ this.form.district = null;
|
|
|
+ this.city = [];
|
|
|
+ this.district = [];
|
|
|
+ }
|
|
|
+ },
|
|
|
+ findAndSetCityIds(addressData) {
|
|
|
+ const provinceItem = this.province.find(p => p.name === addressData.province);
|
|
|
+ if (provinceItem) {
|
|
|
+ this.form.province = provinceItem.name;
|
|
|
+ this.city = provinceItem.children || [];
|
|
|
+ const cityItem = this.city.find(c => c.name === addressData.city);
|
|
|
+ if (cityItem) {
|
|
|
+ this.form.city = cityItem.name;
|
|
|
+ this.form.cityId = cityItem.cityId;
|
|
|
+ this.district = cityItem.children || [];
|
|
|
+ const districtItem = this.district.find(d => d.name === addressData.district);
|
|
|
+ if (districtItem) {
|
|
|
+ this.form.district = districtItem.name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
submitForm() {
|
|
|
this.$refs["form"].validate(valid => {
|
|
|
if (valid) {
|
|
|
- addUserAddress(this.form).then(response => {
|
|
|
- if (response.code === 200) {
|
|
|
- this.msgSuccess("新增成功");
|
|
|
- this.$emit("addUserAddress")
|
|
|
- }
|
|
|
- });
|
|
|
+ if (this.isEdit) {
|
|
|
+ this.form.addressId = this.editAddressId;
|
|
|
+ updateUserAddress(this.form).then(response => {
|
|
|
+ if (response.code === 200) {
|
|
|
+ this.msgSuccess("修改成功");
|
|
|
+ this.$emit("addUserAddress");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ addUserAddress(this.form).then(response => {
|
|
|
+ if (response.code === 200) {
|
|
|
+ this.msgSuccess("新增成功");
|
|
|
+ this.$emit("addUserAddress");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
},
|