martial-arts/pagesB/add-address/add-address.vue

222 lines
7.2 KiB
Vue

<template>
<view>
<status-container :titlet="addressText" returnc="#FFFFFF">
<view slot="content" style="margin-top: -20rpx;">
<view class="bacf pad-x30 pad-zy20">
<view class="pad20 disjbac bbot">
<view class="fon24 col26 flexs width140">姓名</view>
<input type="text" v-model="userInfo.name" class="width100 fon24" placeholder="收货人姓名" placeholder-style="color:#969696;">
</view>
<view class="pad20 disjbac bbot">
<view class="fon24 col26 flexs width140">电话</view>
<input type="number" v-model="userInfo.phone" maxlength="11" class="width100 fon24" placeholder="收货人手机号" placeholder-style="color:#969696;">
</view>
<view class="pad20 disjbac bbot">
<view class="fon24 col26 flexs width140">地区</view>
<input @tap="openAddress" type="text" v-model="region" class="width100 fon24" disabled placeholder="选择省/市/区" placeholder-style="color:#969696;">
</view>
<view class="pad20 disjbac bbot">
<view class="fon24 col26 flexs width140">详细地址</view>
<input type="text" v-model="userInfo.address" class="width100 fon24" placeholder="街道门牌、楼层房间号等信息" placeholder-style="color:#969696;">
</view>
<view class="pad20 disjbac bbot">
<view class="fon24 col26 flexs width140">邮政编码</view>
<input type="text" v-model="userInfo.postcode" class="width100 fon24" placeholder="邮政编码" placeholder-style="color:#969696;">
</view>
<view class="pad20 disjbac">
<view class="fon24 col26 flexs">设为默认收货地址</view>
<evan-switch v-model="moAddress" @change="setMoAddress" :size="28" inactive-color="#dedede" active-color="#f37717"></evan-switch>
</view>
</view>
<view class="disjcac fc fon28 pad-s50">
<!-- 保存 -->
<view @tap="saveAddress" class="disjcac radius34" style="width: 670rpx;height: 80rpx;background-color: #f37717;color: #FFFFFF;border: 2rpx solid #f37717;">保存</view>
<!-- 删除 -->
<view @tap="delAddress" class="disjcac radius34 mar-s20 borbot-df" style="width: 670rpx;height: 80rpx;background-color: #FFFFFF;">删除</view>
</view>
</view>
</status-container>
<addressOne @choseVal="choseValue" :lotusAddressData="lotusAddressData"></addressOne>
</view>
</template>
<script>
import evanSwitch from '@/components/evan-switch/evan-switch.vue';
import addressOne from '@/components/address-one/address-one.vue';
export default {
components:{
evanSwitch,
addressOne
},
data() {
return {
moAddress:false,
addressText:'地址新增',
addressId:'',
userInfo:{
name:'',
phone:'',
address:'',
postcode:''
},
addressObj:{
province:'',
city:'',
county:'',
province_str:'',
city_str:'',
county_str:'',
},
lotusAddressData:{
visible:false,
provinceName:'',
cityName:'',
townName:'',
},
region:'',
flag:true,
}
},
onLoad(op) {
if(op.id){
this.addressText = "编辑地址";
this.addressId = op.id;
// 调用查看地址详情
this.getAddressDetail();
} else {
this.getDistrict();
}
},
methods: {
// 是否设为默认地址
setMoAddress(status){
this.moAddress = status;
},
// 查看地址详情
getAddressDetail(){
this.$requst.post('/api/user/address-info',{id:this.addressId}).then(res=>{
if(res.code==0){
// 设置地址信息
let addressInfo = res.data;
// 设置用户信息
this.userInfo.name = addressInfo.name;
this.userInfo.phone = addressInfo.phone;
this.userInfo.address = addressInfo.address;
this.userInfo.postcode = addressInfo.postcode;
// 设置默认收货地址是否开启/关闭
this.moAddress = addressInfo.is_default?true:false;
// 设置地区
this.addressObj.province = addressInfo.province;
this.addressObj.city = addressInfo.city;
this.addressObj.county = addressInfo.county;
this.addressObj.province_str = addressInfo.province_str;
this.addressObj.city_str = addressInfo.city_str;
this.addressObj.county_str = addressInfo.county_str;
this.setAddress(this.addressObj);
}
})
},
// 保存或新增地址
saveAddress(){
if(this.flag && this.ifEmpty()){
this.flag = false;
let params = {
id:this.addressId,
...this.userInfo,
...this.addressObj,
is_default:this.moAddress?1:0
}
this.$requst.post('/api/user/address-save',params).then(res=>{
if(res.code==0){
this.$toolAll.tools.showToast(this.addressId?'已编辑':'已新增')
setTimeout(()=>{
uni.navigateBack({delta:1})
},500)
}
})
}
},
// 判空
ifEmpty(){
let result = false;
if(!this.userInfo.name){
this.$toolAll.tools.showToast('请输入姓名');
} else if(this.$toolAll.tools.isPhone(this.userInfo.phone)){
this.$toolAll.tools.showToast('请正确输入电话');
} else if(!this.region){
this.$toolAll.tools.showToast('请选择地区');
} else if(!this.userInfo.address){
this.$toolAll.tools.showToast('请填写详细地址');
} else {
result = true;
}
return result;
},
// 删除地址
delAddress(){
this.$requst.post('/api/user/address-del',{id:this.addressId}).then(res=>{
if(res.code==0){
this.$toolAll.tools.showToast('删除成功');
setTimeout(()=>{
uni.navigateBack({delta:1})
},500)
}
})
},
// 获取当前定位
getDistrict() {
uni.getLocation({
success:(res)=> {
uni.request({
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=B2ABZ-SIDKS-WD2O3-6CJ2U-CDZOT-U3FKF`,
header: {
'Content-Type':'application/json'
},
success:(res)=> {
console.log('地址数据:',res)
let info = res.data.result.address_component;
this.lotusAddressData.provinceName = info.province;//省
this.lotusAddressData.cityName = info.city;//市
this.lotusAddressData.townName = info.district;//区
}
})
},
})
},
//打开picker
openAddress() {
this.lotusAddressData.visible = true;
},
//回传已选的省市区的值
choseValue(res){
//res数据源包括已选省市区与省市区code
console.log(res);
this.lotusAddressData.visible = res.visible;//visible为显示与关闭组件标识true显示false隐藏
//res.isChose = 1省市区已选 res.isChose = 0;未选
if(res.isChose){
this.setAddress({
province:res.provinceCode,
city:res.cityCode,
county:res.townCode,
province_str:res.province,
city_str:res.city,
county_str:res.town,
})
}
},
// 设置省市区值
setAddress(obj){
this.addressObj = obj;
this.lotusAddressData.provinceName = obj.province_str;//省
this.lotusAddressData.cityName = obj.city_str;//市
this.lotusAddressData.townName = obj.county_str;//区
this.region = `${obj.province_str} ${obj.city_str} ${obj.county_str}`; //region为已选的省市区的值
}
}
}
</script>
<style>
.width140{width: 140rpx;}
</style>