dingdong-mall/pages/my/edit-address.vue

124 lines
3.6 KiB
Vue
Raw Normal View History

2022-04-23 23:13:29 +08:00
<template>
<view>
<!-- 顶部操作条 -->
<cu-custom :bgColor="'bg-main-color'" :isBack="true">
<block slot="backText">返回</block>
<block slot="content">编辑地址</block>
</cu-custom>
<!-- 地址信息form -->
<view class="bg-white margin-top-sm">
<form @submit="submit">
<view class="cu-form-group">
<view class="title">联系人</view>
<input name="person2Contact" :value="formData.person2Contact"></input>
</view>
<view class="cu-form-group">
<view class="title">手机号码</view>
<input name="phone" :value="formData.phone"></input>
</view>
<!-- #ifndef H5 || APP-PLUS || MP-ALIPAY -->
<view class="cu-form-group">
2022-04-27 10:00:39 +08:00
<view class="title">地址选择</view>
<picker :mode="'multiSelector'" @change="regionChange" :value="multiIndex" :range-key="'name'" :range="areaList">
<view class="picker">
{{areaList[0][multiIndex[0]].name}}{{areaList[1][multiIndex[1]].name}}{{areaList[2][multiIndex[2]].name}}
</view>
2022-04-23 23:13:29 +08:00
</picker>
</view>
<!-- #endif -->
<view class="cu-form-group">
<view class="title">详细地址</view>
<input name="address" :value="formData.address"></input>
</view>
2022-04-27 10:00:39 +08:00
<view class="cu-form-group margin-top margin-bottom-with-bar">
2022-04-23 23:13:29 +08:00
<view class="title">默认地址</view>
<switch class="main-color radius" @change="isDefaultChange" :class="formData.isDefault?'checked':''"
:checked="formData.isDefault?true:false" name="isDefault" :value="formData.isDefault"></switch>
</view>
<!-- 底部操作栏 -->
<view class="cu-bar tabbar border shop fixed-bottom-bar">
<button class="cu-btn bg-main-color long-btn margin-lr-sm shadow-blur" form-type="submit">保存</button>
2022-04-23 23:13:29 +08:00
</view>
</form>
</view>
</view>
</template>
<script>
export default {
data() {
2022-04-27 10:00:39 +08:00
return {
areaList: [],
multiIndex: [0, 0, 0],
2022-04-23 23:13:29 +08:00
formData: {
},
mode: 1 // 1为新增0
2022-04-23 23:13:29 +08:00
}
},
2022-04-27 10:00:39 +08:00
onLoad(options) {
let params = JSON.parse(decodeURIComponent(options.params));
this.mode = params ? params.mode : this.mode;
console.log(this.mode)
if (this.mode === 0) {
// 修改
this.fillForm(params.addressInfo);
}
2022-04-27 10:00:39 +08:00
this.loadData();
2022-04-23 23:13:29 +08:00
},
2022-04-27 10:00:39 +08:00
methods: {
async loadData() {
this.areaList = await this.$api.data('areaList');
},
2022-04-23 23:13:29 +08:00
fillForm(addressInfo) {
2022-04-27 10:00:39 +08:00
this.formData = addressInfo && Object.keys(addressInfo).length > 0 ? addressInfo : this.formData;
2022-04-23 23:13:29 +08:00
},
2022-04-27 10:00:39 +08:00
regionChange(e) {
this.multiIndex = e.detail.value;
let chosenArea = [];
for(let i = 0; i < this.areaList.length; i++) {
chosenArea.push(this.areaList[i][this.multiIndex[i]]);
}
this.formData.area = chosenArea;
2022-04-23 23:13:29 +08:00
},
isDefaultChange(e) {
this.formData.isDefault = e.detail.value;
},
2022-04-27 10:00:39 +08:00
validateForm(addressInfo) {
let valid = Boolean(addressInfo.person2Contact) &&
Boolean(addressInfo.phone) &&
Boolean(addressInfo.address);
if (!valid) {
uni.showToast({
title: '请填写完整信息',
icon: 'none',
mask: true
})
} else if (!this.$validate.validContactNum(addressInfo.phone)) {
valid = false;
uni.showToast({
title: '联系号码格式错误',
icon: 'none',
mask: true
})
}
return valid;
2022-04-23 23:13:29 +08:00
},
submit(e) {
2022-04-27 10:00:39 +08:00
const confirmFormData = Object.assign({}, this.formData, e.detail.value)
2022-04-23 23:13:29 +08:00
let formValid = this.validateForm(confirmFormData);
if (formValid) {
uni.showToast({
title: '保存成功',
icon: 'success',
mask: true
})
}
}
}
}
</script>
<style>
</style>