Files
kangyuan-mobile/components/card_edit/card_edit.vue
T

395 lines
8.5 KiB
Vue

<!-- 收货地址的新增/编辑 -->
<template>
<view class="cardEdit" style="position: relative">
<view>
<view class="title" :class="props.theme == 2 ? 'c0' : ''">添加收货地址</view>
</view>
<view class="close" @click="onColse">
<image :src="$url + 'home/closee.svg'" mode="scaleToFill" />
</view>
<view>
<view class="flex-center w100 h_108 border-top p-x-28" v-for="(v, i) in arr" :key="i"
:class="{'ju-between': v.type == 'switch'}">
<view class="m-r-20 w_180" style="text-align: left;">{{ v.name }}</view>
<view @click="regionClick(v)" class="flex1 flex-center" :class="{'ju-flex-end': v.type == 'switch'}">
<uni-easyinput :type="v.typeName || 'text'" class="flex1 h100"
v-if="v.type == 'input'" v-model="state.model[v.value]"
:placeholder="v.placeholder" :inputBorder="false"
placeholderStyle="color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal"
:maxlength="v.max" :disabled="v.disabled"/>
<view v-if="v.type == 'select'" :style="{color: state.model[v.value]?'#333333':'#BBBBBB'}" style="font-size:30rpx;font-weight:400;line-height:normal;text-align: left;width: 100%">{{state.model[v.value] || v.placeholder}}</view>
<su-switch v-if="v.type == 'switch'" style="transform: scale(0.8)"
v-model="state.model.defaultStatus" />
<uni-icons v-if="v.value == 'areaName'" type="right" :color="props.theme == 2 ? '#ffffff' : ''" />
</view>
</view>
</view>
<view class="border-top p-x-28 c35" style="text-align: left;">{{messText}}</view>
<view class="btn p-x-28">
<button class="add-btn" :class="props.theme == 2 ? 'ai-button2' : ''" @tap="onSave">保存</button>
</view>
<!-- 省市区弹窗 -->
<su-region-picker :show="state.showRegion" @cancel="state.showRegion = false" @confirm="onRegionConfirm" />
</view>
</template>
<script setup>
import {
ref,
reactive,
unref
} from 'vue';
import sheep from '@/sheep';
import {
onLoad
} from '@dcloudio/uni-app';
import _ from 'lodash-es';
import {
mobile
} from '@/sheep/validate/form';
import AreaApi from '@/sheep/api/system/area';
import AddressApi from '@/sheep/api/member/address';
let emit = defineEmits(['close']);
let messText = ref('')
let props = defineProps({
theme: {
type: Number,
default: 2,
},
});
let arr = ref([{
name: '收货人',
value: 'name',
type: 'input',
typeName: 'text',
placeholder: '请输入收货人姓名',
},
{
name: '手机号',
value: 'mobile',
type: 'input',
max: 11,
typeName: 'number',
placeholder: '请输入手机号',
},
{
name: '省市区',
value: 'areaName',
type: 'select',
placeholder: '请选择省市区',
disabled: true
},
{
name: '详细地址',
value: 'detailAddress',
type: 'input',
typeName: 'text',
placeholder: '请输入详细地址',
},
{
name: '默认地址',
value: 'defaultStatus',
type: 'switch',
},
]);
const addressFormRef = ref(null);
const state = reactive({
showRegion: false,
model: {
name: '',
mobile: '',
detailAddress: '',
defaultStatus: false,
areaId: null,
areaName: '',
},
rules: {},
});
const rules = {
name: {
rules: [{
required: true,
errorMessage: '请输入收货人姓名',
}, ],
},
mobile: {
rules: [{
required: true,
errorMessage: '请输入手机号',
}, ],
},
areaId: {
rules: [{
required: true,
errorMessage: '请选择您的位置',
}, ],
},
detailAddress: {
rules: [{
required: true,
errorMessage: '请输入详细地址',
}, ],
},
};
// 确认选择地区
const onRegionConfirm = (e) => {
state.model.areaName = [e.province_name, e.city_name, e.district_name, e.street_name]
.filter(Boolean)
.join(' ');
state.model.areaId = e.street_id ?? e.district_id ?? e.city_id ?? e.province_id ?? null;
state.showRegion = false;
};
// 获得地区数据
const getAreaData = () => {
if (_.isEmpty(uni.getStorageSync('areaData'))) {
AreaApi.getAreaTree().then((res) => {
if (res.code === 0) {
uni.setStorageSync('areaData', res.data);
}
});
}
};
// 保存收货地址
const onSave = async () => {
// 参数校验
for (const key in rules) {
if (!state.model[key]) {
// messText.value = rules[key].rules[0].errorMessage
uni.showToast({
title: rules[key].rules[0].errorMessage,
icon: 'none',
});
return;
}
}
if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(state.model.mobile)) {
uni.showToast({
title: '请填写正确的手机号',
icon: 'none',
});
// messText.value = '请填写正确的手机号'
return;
}
// messText.value = ""
// 提交请求
const formData = {
...state.model,
};
const {
code
} =
state.model.id > 0 ?
await AddressApi.updateAddress(formData) :
await AddressApi.createAddress(formData);
if (code === 0) {
emit('close');
}
};
// 删除收货地址
const onDelete = () => {
uni.showModal({
title: '提示',
content: '确认删除此收货地址吗?',
confirmColor: '#FA7E31',
success: async function(res) {
if (!res.confirm) {
return;
}
const {
code
} = await AddressApi.deleteAddress(state.model.id);
if (code === 0) {
sheep.$router.back();
}
},
});
};
const onEdit = async (id) => {
let {
code,
data
} = await AddressApi.getAddress(id);
if (code !== 0) {
return;
}
state.model = data;
};
onLoad(async (options) => {
// 获得地区数据
getAreaData();
// 情况一:基于 id 获得收件地址
if (options.id) {
let {
code,
data
} = await AddressApi.getAddress(options.id);
if (code !== 0) {
return;
}
state.model = data;
}
// 情况二:微信导入
if (options.data) {
let data = JSON.parse(options.data);
const areaData = uni.getStorageSync('areaData');
const findAreaByName = (areas, name) => areas.find((item) => item.name === name);
let provinceObj = findAreaByName(areaData, data.province_name);
let cityObj = provinceObj ? findAreaByName(provinceObj.children, data.city_name) : undefined;
let districtObj = cityObj ? findAreaByName(cityObj.children, data.district_name) : undefined;
let areaId = (districtObj || cityObj || provinceObj).id;
state.model = {
...state.model,
areaId,
areaName: [data.province_name, data.city_name, data.district_name]
.filter(Boolean)
.join(' '),
defaultStatus: false,
detailAddress: data.address,
mobile: data.mobile,
name: data.consignee,
};
}
});
const onColse = () => {
emit('close');
};
defineExpose({
onEdit,
});
const regionClick = (e) => {
if (e.value == 'areaName') {
state.showRegion = true;
}
};
</script>
<style lang="scss" scoped>
.cardEdit {
// width: 90%;
}
:deep() {
.uni-forms-item__label .label-text {
font-size: 28rpx !important;
color: #333333;
line-height: normal !important;
}
.uni-easyinput__content-input {
font-size: 28rpx !important;
color: #333333;
line-height: normal !important;
padding-left: 0 !important;
}
.uni-easyinput__content-textarea {
font-size: 28rpx !important;
color: #333333;
line-height: normal !important;
margin-top: 8rpx !important;
}
.uni-icons {
font-size: 40rpx !important;
}
.is-textarea-icon {
margin-top: 22rpx;
}
.is-disabled {
color: #333333;
}
}
:deep(.form2) {
.border-top {
border-top: 1rpx solid #4c505b;
}
}
.default-box {
width: 100%;
box-sizing: border-box;
height: 100rpx;
.default-box-title {
font-size: 28rpx;
color: #333333;
line-height: normal;
}
}
.footer-box {
.save-btn {
width: 710rpx;
height: 80rpx;
border-radius: 40rpx;
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
color: $white;
}
.cancel-btn {
width: 710rpx;
height: 80rpx;
border-radius: 40rpx;
background: #F2F4F7;
}
}
:deep(.uni-forms-item--border) {
padding: 0;
border-top: 1px solid #4c505b;
}
.btn {
padding: 32rpx 0rpx;
box-align: border-box;
}
.add-btn {
background: linear-gradient(131deg, #f7772e 0%, #ee0a24 100%);
border-radius: 8rpx;
font-size: 32rpx;
// font-weight: 500;
line-height: 82rpx;
color: $white;
position: relative;
}
.title {
line-height: 92rpx;
color: #3d3d3d;
font-size: 32rpx;
}
.close {
width: 56rpx;
height: 56rpx;
position: absolute;
right: -14rpx;
top: -18rpx;
image {
width: 100%;
height: 100%;
}
}
:deep(.uni-forms-item__error) {
text-align: left;
}
:deep(.is-disabled){
background-color: #ffffff !important;
}
</style>