修复选择地址回显的问题

This commit is contained in:
Gao xiaosong
2021-11-07 23:51:59 +08:00
parent 0a13a11ec6
commit 5e8f7befd9
3 changed files with 201 additions and 164 deletions

View File

@ -13,7 +13,7 @@
<view class="name">所在地区</view>
<view class="picker acea-row row-between-wrapper select-value form-control">
<view class="address">
<CitySelect ref="cityselect" :defaultValue="addressText" @callback="result" :items="district"></CitySelect>
<CitySelect ref="cityselect" :defaultValue="defaultAddress" @callback="result" :items="district"></CitySelect>
</view>
<view class="iconfont icon-dizhi font-color-red"></view>
</view>
@ -28,7 +28,7 @@
<view class="checkbox-wrapper">
<checkbox-group @change="ChangeIsDefault">
<label class="well-check">
<checkbox :value="userAddress.isDefault==1?'checked':''" :checked="userAddress.isDefault ? true : false"></checkbox>
<checkbox :value="userAddress.isDefault == 1 ? 'checked' : ''" :checked="userAddress.isDefault ? true : false"></checkbox>
<text class="def">设置为默认地址</text>
</label>
</checkbox-group>
@ -42,17 +42,17 @@
</template>
<script type="text/babel">
import CitySelect from "@/components/CitySelect";
import { getAddress, postAddress, getCity } from "@/api/user";
import attrs, { required, chs_phone } from "@/utils/validate";
import { validatorDefaultCatch } from "@/utils/dialog";
import CitySelect from '@/components/CitySelect'
import { getAddress, postAddress, getCity } from '@/api/user'
import attrs, { required, chs_phone } from '@/utils/validate'
import { validatorDefaultCatch } from '@/utils/dialog'
// import { openAddress } from "@/libs/wechat";
import { isWeixin } from "@/utils";
import { isWeixin } from '@/utils'
export default {
name: "AddAddress",
name: 'AddAddress',
components: {
CitySelect
CitySelect,
},
data() {
return {
@ -61,73 +61,75 @@ export default {
userAddress: { isDefault: 0 },
address: {},
isWechat: isWeixin(),
addressText: ""
};
defaultAddress: {},
addressText: '',
}
},
mounted: function() {
let id = this.$yroute.query.id;
this.id = id;
this.getUserAddress();
this.getCityList();
let id = this.$yroute.query.id
this.id = id
this.getUserAddress()
this.getCityList()
},
watch: {
addressText(nextModel2) {
console.log(nextModel2, 8585858585);
}
console.log(nextModel2, 8585858585)
},
},
methods: {
getCityList: function() {
let that = this;
let that = this
getCity()
.then(res => {
that.district = res.data;
that.ready = true;
that.district = res.data
that.ready = true
})
.catch(err => {
uni.showToast({
title: err.msg,
icon: "none",
duration: 2000,
});
});
title: err.msg,
icon: 'none',
duration: 2000,
})
})
},
getUserAddress: function() {
if (!this.id) return false;
let that = this;
if (!this.id) return false
let that = this
getAddress(that.id).then(res => {
that.userAddress = res.data;
that.addressText =
res.data.province + " " + res.data.city + " " + res.data.district;
that.address.province = res.data.province;
that.address.city = res.data.city;
that.address.district = res.data.district;
});
that.userAddress = res.data
that.defaultAddress = {
province: {
n: res.data.province,
},
city: {
n: res.data.city,
},
district: {
n: res.data.district,
},
}
that.addressText = res.data.province + ' ' + res.data.city + ' ' + res.data.district
that.address.province = res.data.province
that.address.city = res.data.city
that.address.district = res.data.district
})
},
getAddress() {},
async submit() {
console.log(this);
console.log(this.address);
console.log(this.addressText);
let name = this.userAddress.realName,
phone = this.userAddress.phone,
addressText = this.addressText,
detail = this.userAddress.detail,
isDefault = this.userAddress.isDefault;
isDefault = this.userAddress.isDefault
try {
await this.$validator({
name: [
required(required.message("姓名")),
attrs.range([2, 16], attrs.range.message("姓名"))
],
phone: [
required(required.message("联系电话")),
chs_phone(chs_phone.message())
],
addressText: [required("请选择地址")],
detail: [required(required.message("具体地址"))]
}).validate({ name, phone, addressText, detail });
name: [required(required.message('姓名')), attrs.range([2, 16], attrs.range.message('姓名'))],
phone: [required(required.message('联系电话')), chs_phone(chs_phone.message())],
addressText: [required('请选择地址')],
detail: [required(required.message('具体地址'))],
}).validate({ name, phone, addressText, detail })
} catch (e) {
return validatorDefaultCatch(e);
return validatorDefaultCatch(e)
}
try {
let that = this,
@ -138,21 +140,21 @@ export default {
address: this.address,
detail: detail,
is_default: isDefault ? true : false,
post_code: ""
};
post_code: '',
}
postAddress(data).then(function() {
if (that.id) {
uni.showToast({
title: "修改成功",
icon: "none",
duration: 2000
});
title: '修改成功',
icon: 'none',
duration: 2000,
})
} else {
uni.showToast({
title: "操作成功",
icon: "none",
duration: 2000
});
title: '操作成功',
icon: 'none',
duration: 2000,
})
// uni.showToast({
// title: "已取消绑定",
// icon: "none",
@ -162,41 +164,41 @@ export default {
// path: "/pages/user/PersonalData/index"
// });
}
that.$yrouter.back();
});
that.$yrouter.back()
})
} catch (err) {
uni.showToast({
title: err.msg || err.response.data.msg || err.response.data.message,
icon: "none",
duration: 2000
});
icon: 'none',
duration: 2000,
})
}
},
ChangeIsDefault: function() {
this.userAddress.isDefault = !this.userAddress.isDefault;
this.userAddress.isDefault = !this.userAddress.isDefault
},
result(values) {
console.log(this);
console.log(values);
console.log(this)
console.log(values)
this.address = {
province: values.province.name || "",
city: values.city.name || "",
district: values.district.name || "",
city_id: values.city.id
};
this.addressText = `${this.address.province}${this.address.city}${this.address.district}`;
province: values.province.name || '',
city: values.city.name || '',
district: values.district.name || '',
city_id: values.city.id,
}
this.addressText = `${this.address.province}${this.address.city}${this.address.district}`
// this.addressText =
// this.address.province + this.address.city + this.address.district;
}
}
};
},
},
}
</script>
<style lang="less">
.address {
text {
width: 100%;
display: block;
}
.address {
text {
width: 100%;
display: block;
}
}
</style>