This commit is contained in:
zmj 2024-06-14 13:56:28 +08:00
parent 5afd6b8978
commit 87cc78ffcd
1 changed files with 167 additions and 115 deletions

View File

@ -1,133 +1,185 @@
<template> <template>
<view class=""> <view class="">
<view class="card"> <view class="card">
<up-form labelPosition="left" :model="formData" :rules="rules" ref="uForm" labelWidth="100"> <up-form labelPosition="left" :model="formData" :rules="rules" ref="uForm" labelWidth="100">
<up-form-item label="收货人" prop="real_name" borderBottom> <up-form-item label="收货人" prop="real_name" borderBottom>
<up-input v-model="formData.real_name" disabledColor="#ffffff" border="none" placeholder="请填写收货人姓名"></up-input> <up-input v-model="formData.real_name" disabledColor="#ffffff" border="none"
</up-form-item> placeholder="请填写收货人姓名"></up-input>
<up-form-item label="联系电话" prop="phone" borderBottom> </up-form-item>
<up-input v-model="formData.phone" disabledColor="#ffffff" type="number" placeholder="请填写联系电话" <up-form-item label="联系电话" prop="phone" borderBottom>
border="none"></up-input> <up-input v-model="formData.phone" disabledColor="#ffffff" type="number" placeholder="请填写联系电话"
</up-form-item> border="none"></up-input>
<up-form-item label="详细地址" prop="detail" borderBottom> </up-form-item>
<up-input v-model="formData.detail" disabledColor="#ffffff" placeholder="请填写详细地址" border="none"></up-input> <!-- <up-form-item label="所在位置" prop="address" borderBottom>
</up-form-item> <up-input v-model="formData.address" disabledColor="#ffffff" type="number"
</up-form> @click='showAddressPop=true' placeholder="点击选择位置" border="none"></up-input>
</view> <template #right>
<view class="card"> <up-icon name="arrow-right"></up-icon>
<view class="is-default"> </template>
<view>设置为默认地址</view> </up-form-item> -->
<up-switch v-model="formData.is_default" :activeValue="1" :inactiveValue="0" activeColor="#20B128"></up-switch> <up-form-item label="详细地址" prop="detail" borderBottom>
</view> <up-input v-model="formData.detail" disabledColor="#ffffff" placeholder="请填写详细地址"
</view> border="none"></up-input>
<view class="bottom-fixed"> </up-form-item>
<up-button color="#20B128" shape="circle" @click="submit">保存</up-button> </up-form>
</view> </view>
</view> <view class="card">
<view class="is-default">
<view>设置为默认地址</view>
<up-switch v-model="formData.is_default" :activeValue="1" :inactiveValue="0"
activeColor="#20B128"></up-switch>
</view>
</view>
<view class="bottom-fixed">
<up-button color="#20B128" shape="circle" @click="submit">保存</up-button>
</view>
<!-- 地址选择器 -->
<up-popup :show="showAddressPop" @close="showAddressPop=false" @open="showAddressPop=true" :round="10">
<view class='address-pop'>
<view class="title">
收货地址
</view>
</view>
</up-popup>
</view>
</template> </template>
<script setup> <script setup>
import { onLoad } from "@dcloudio/uni-app" import {
import { ref } from "vue" onLoad
import { addressCreateApi, addressEditApi, addressDetailApi } from "@/api/user.js" } from "@dcloudio/uni-app"
import {
ref
} from "vue"
import {
addressCreateApi,
addressEditApi,
addressDetailApi
} from "@/api/user.js"
const mode = ref('add'); const mode = ref('add');
const showAddressPop = ref(false)
const formData = ref({ const formData = ref({
real_name: '', real_name: '',
phone: '', phone: '',
detail: '', detail: '',
is_default: 0 is_default: 0,
}) address: ""
const rules = ref({ })
real_name: [{ required: true, message: '请输入姓名', trigger: ['blur'] }], const rules = ref({
phone: [{ required: true, message: '请输入手机号', trigger: ['blur'] },{ real_name: [{
required: true,
message: '请输入姓名',
trigger: ['blur']
}],
phone: [{
required: true,
message: '请输入手机号',
trigger: ['blur']
}, {
validator: (rule, value, callback) => { validator: (rule, value, callback) => {
// truefalse
// uni.$u.test.mobile()truefalse
return uni.$u.test.mobile(value); return uni.$u.test.mobile(value);
}, },
message: '手机号码不正确', message: '手机号码不正确',
trigger: ['change','blur'], trigger: ['change', 'blur'],
}], }],
detail: [{ required: true, message: '请输入地址', trigger: ['blur'] }] detail: [{
}) required: true,
const uForm = ref(null); message: '请输入地址',
const submit = () => { trigger: ['blur']
uForm.value.validate().then(() => { }]
console.log('验证通过'); })
if(mode.value=='add'){ const uForm = ref(null);
addressCreateApi(formData.value).then(res=>{ const submit = () => {
uni.$u.toast(res.msg); uForm.value.validate().then(() => {
uni.$u.sleep(800).then(res=>{ console.log('验证通过');
uni.navigateBack(); if (mode.value == 'add') {
}) addressCreateApi(formData.value).then(res => {
}) uni.$u.toast(res.msg);
} else { uni.$u.sleep(800).then(res => {
addressEditApi(formData.value).then(res=>{ uni.navigateBack();
uni.$u.toast(res.msg); })
uni.$u.sleep(800).then(res=>{ })
uni.navigateBack(); } else {
}) addressEditApi(formData.value).then(res => {
}) uni.$u.toast(res.msg);
} uni.$u.sleep(800).then(res => {
}).catch(() => { uni.navigateBack();
console.log('验证失败'); })
}) })
} }
}).catch(() => {
const getAddressDetil = (address_id)=>{ console.log('验证失败');
addressDetailApi({ })
address_id }
}).then(res=>{
formData.value = res.data;
})
}
onLoad((options) => { const getAddressDetil = (address_id) => {
if (options.mode == 'edit'&&options.address_id) { addressDetailApi({
mode.value = 'edit'; address_id
uni.setNavigationBarTitle({ }).then(res => {
title: '编辑地址' formData.value = res.data;
}) })
getAddressDetil(options.address_id) }
} else uni.setNavigationBarTitle({
title: '新增地址' onLoad((options) => {
}) if (options.mode == 'edit' && options.address_id) {
}) mode.value = 'edit';
uni.setNavigationBarTitle({
title: '编辑地址'
})
getAddressDetil(options.address_id)
} else uni.setNavigationBarTitle({
title: '新增地址'
})
})
</script> </script>
<style lang="scss"> <style lang="scss">
.card { .card {
margin: 20rpx; margin: 20rpx;
background-color: #fff; background-color: #fff;
border-radius: 14rpx; border-radius: 14rpx;
padding: 0 30rpx; padding: 0 30rpx;
.is-default { .is-default {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
padding: 20rpx 0; padding: 20rpx 0;
} }
} }
.bottom-fixed { .bottom-fixed {
position: fixed; position: fixed;
bottom: 0; bottom: 0;
left: 0; left: 0;
height: 120rpx; height: 120rpx;
height: calc(constant(safe-area-inset-bottom) + 120rpx); height: calc(constant(safe-area-inset-bottom) + 120rpx);
/* 适用于iOS设备 */ /* 适用于iOS设备 */
height: calc(env(safe-area-inset-bottom) + 120rpx); height: calc(env(safe-area-inset-bottom) + 120rpx);
/* 适用于Android设备 */ /* 适用于Android设备 */
width: 100%; width: 100%;
box-sizing: border-box; box-sizing: border-box;
background-color: #fff; background-color: #fff;
padding: 20rpx; padding: 20rpx;
padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx); padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
/* 适用于iOS设备 */ /* 适用于iOS设备 */
padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx); padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
/* 适用于Android设备 */ /* 适用于Android设备 */
} }
.address-pop {
height: 60vh;
padding: 20rpx;
overflow-y: auto;
.title {
font-size: 32rpx;
color: black;
text-align: center;
}
}
</style> </style>