文件
yjyf/yjyf-app/pages/address/list.vue

733 行
19 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="page">
<YjyfNavBar theme="orange" :title="selectMode ? '选择服务地址' : '地址管理'" />
<view class="list" v-if="list.length">
<view
class="item"
:class="{ selectable: selectMode }"
v-for="a in list"
:key="a.id"
@tap="onCardTap(a)"
>
<view class="item-top">
<text class="contact">{{ a.contact }}</text>
<text class="mobile">{{ a.mobile }}</text>
<text class="badge" v-if="Number(a.is_default)">默认</text>
</view>
<text class="addr">{{ formatFullAddr(a) }}</text>
<text class="meta">楼层 {{ a.floor }}层 · {{ Number(a.has_elevator) ? '有电梯' : '无电梯' }}</text>
<view class="ops" @tap.stop>
<view class="op clickable-hover" v-if="!Number(a.is_default)" @tap="setDefault(a)">设为默认</view>
<view class="op clickable-hover" @tap="edit(a)">编辑</view>
<view class="op danger clickable-hover" @tap="remove(a)">删除</view>
</view>
</view>
</view>
<view class="empty-box" v-else-if="!loading">
<image class="empty-ico" src="/static/icons/empty-address.svg" mode="aspectFit" />
<text class="empty-title">暂无服务地址</text>
<text class="empty-sub">添加常用上门地址,下单更省心</text>
</view>
<view class="bar">
<view class="bar-btn clickable-hover" @tap="openForm()">
<image class="bar-plus-ico" src="/static/icons/plus-white.svg" mode="aspectFit" />
<text>新增服务地址</text>
</view>
</view>
<view class="mask" v-if="showForm" @tap="showForm = false">
<view class="sheet" @tap.stop>
<view class="sheet-head">
<text class="sheet-title">{{ form.id ? '编辑地址' : '新增地址' }}</text>
<view class="sheet-close-btn clickable-hover" @tap="showForm = false">
<image class="close-ico" src="/static/icons/close.svg" mode="aspectFit" />
</view>
</view>
<view class="quick-tools">
<view class="tool-btn clickable-hover" @tap="chooseLocation">
<text class="tool-text">地图选点定位</text>
</view>
<view class="tool-btn clickable-hover" @tap="showPaste = !showPaste">
<text class="tool-text">{{ showPaste ? '收起识别' : '智能粘贴识别' }}</text>
</view>
</view>
<view class="paste-box" v-if="showPaste">
<textarea class="paste-input" v-model="pasteText" placeholder="粘贴整段地址,自动提取姓名、电话与省市区街道" />
<view class="paste-btn clickable-hover" @tap="applyPaste">一键识别填入</view>
</view>
<view class="fi">
<text class="fk">联系人</text>
<input class="fv" v-model="form.contact" placeholder="填写联系人姓名" placeholder-class="ph" />
</view>
<view class="fi">
<text class="fk">手机号</text>
<input class="fv" v-model="form.mobile" type="number" maxlength="11" placeholder="填写11位联系手机号" placeholder-class="ph" />
</view>
<view class="fi">
<text class="fk">所在地区</text>
<!-- #ifdef MP-WEIXIN -->
<picker class="region-picker" mode="region" :value="regionArr" @change="onRegionChange">
<view class="region-val" :class="{ placeholder: !regionDisplay }">
<text>{{ regionDisplay || '点击选择省 / 市 / 区' }}</text>
<image class="arrow-ico" src="/static/icons/arrow-right.svg" mode="aspectFit" />
</view>
</picker>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<view class="region-picker clickable-hover" @tap="showRegionDrawer = true">
<view class="region-val" :class="{ placeholder: !regionDisplay }">
<text>{{ regionDisplay || '点击选择省 / 市 / 区' }}</text>
<image class="arrow-ico" src="/static/icons/arrow-right.svg" mode="aspectFit" />
</view>
</view>
<!-- #endif -->
</view>
<view class="fi">
<text class="fk">详细地址</text>
<input class="fv" v-model="form.address" placeholder="街道、小区、楼栋及门牌号" placeholder-class="ph" />
</view>
<view class="fi">
<text class="fk">楼层</text>
<view class="stepper">
<view class="st-btn clickable-hover" @tap="step(-1)">
<image class="st-ico" src="/static/icons/step-minus.svg" mode="aspectFit" />
</view>
<text class="st-val">{{ form.floor }}</text>
<view class="st-btn clickable-hover" @tap="step(1)">
<image class="st-ico" src="/static/icons/step-plus.svg" mode="aspectFit" />
</view>
</view>
</view>
<view class="fi">
<text class="fk">有无电梯</text>
<view class="seg">
<view class="seg-i" :class="{ on: form.has_elevator === 1 }" @tap="form.has_elevator = 1">有电梯</view>
<view class="seg-i" :class="{ on: form.has_elevator === 0 }" @tap="form.has_elevator = 0">无电梯</view>
</view>
</view>
<view class="fi no-border">
<text class="fk">设为默认</text>
<view class="seg">
<view class="seg-i" :class="{ on: form.is_default === 1 }" @tap="form.is_default = 1">是</view>
<view class="seg-i" :class="{ on: form.is_default === 0 }" @tap="form.is_default = 0">否</view>
</view>
</view>
<view class="save clickable-hover" :class="{ disabled: saving }" @tap="save">
{{ saving ? '保存中...' : '保存地址' }}
</view>
</view>
</view>
<RegionPickerDrawer
:visible="showRegionDrawer"
:province="form.province"
:city="form.city"
:district="form.district"
@update:visible="showRegionDrawer = $event"
@confirm="onRegionDrawerConfirm"
/>
</view>
</template>
<script setup>
import { ref, reactive, computed } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import api from '@/utils/api.js';
import { isLogin, toLogin } from '@/utils/request.js';
import RegionPickerDrawer from '@/components/RegionPickerDrawer.vue';
const selectMode = ref(false);
const list = ref([]);
const loading = ref(false);
const showForm = ref(false);
const showRegionDrawer = ref(false);
const saving = ref(false);
const showPaste = ref(false);
const pasteText = ref('');
const regionInput = ref('');
const form = reactive({
id: 0,
contact: '',
mobile: '',
province: '',
city: '',
district: '',
address: '',
lng: '',
lat: '',
floor: 1,
has_elevator: 1,
is_default: 0
});
const regionArr = computed(() => [
form.province || '',
form.city || '',
form.district || ''
]);
const regionDisplay = computed(() => {
return [form.province, form.city, form.district].filter(Boolean).join(' ');
});
function formatFullAddr(a) {
const prefix = [a.province, a.city, a.district].filter(Boolean).join('');
const detail = a.address || '';
if (prefix && detail.startsWith(prefix)) return detail;
return prefix + detail;
}
function syncRegionInput() {
regionInput.value = [form.province, form.city, form.district].filter(Boolean).join(' ');
}
function parseRegionParts(raw) {
const parts = String(raw || '').trim().split(/[\s/,-]+/).filter(Boolean);
form.province = parts[0] || '';
form.city = parts[1] || '';
form.district = parts[2] || '';
}
function onRegionChange(e) {
const val = (e && e.detail && e.detail.value) || [];
form.province = val[0] || '';
form.city = val[1] || '';
form.district = val[2] || '';
syncRegionInput();
}
function onRegionDrawerConfirm(reg) {
if (!reg) return;
form.province = reg.province || '';
form.city = reg.city || '';
form.district = reg.district || '';
syncRegionInput();
}
function extractRegionFromAddress(str) {
const m = String(str || '').match(/^([^省]+省|[^自治区]+自治区|北京市|上海市|天津市|重庆市)?([^市州盟]+(?:市|州|盟))?([^区县旗]+(?:区|县|旗|市))?(.*)$/);
if (m && (m[1] || m[2] || m[3])) {
if (m[1]) form.province = m[1];
if (m[2]) form.city = m[2];
else if (m[1] && /北京市|上海市|天津市|重庆市/.test(m[1])) form.city = m[1];
if (m[3]) form.district = m[3];
syncRegionInput();
return (m[4] || '').trim();
}
return str;
}
async function load() {
if (!isLogin()) { toLogin(); return; }
loading.value = true;
try {
const res = await api.addressList();
list.value = res.list || [];
} catch (e) {
} finally {
loading.value = false;
}
}
function onCardTap(a) {
if (!selectMode.value) return;
uni.setStorageSync('yjyf_selected_address', a);
try {
const pages = getCurrentPages() || [];
const curPage = pages[pages.length - 1];
const ch = curPage && curPage.getOpenerEventChannel ? curPage.getOpenerEventChannel() : null;
if (ch && ch.emit) ch.emit('selectAddress', a);
} catch (e) {}
uni.$emit('selectAddress', a);
uni.$emit('yjyf:selectAddress', a);
uni.navigateBack();
}
function openForm(a) {
form.id = a ? Number(a.id) : 0;
form.contact = a ? (a.contact || '') : '';
form.mobile = a ? (a.mobile || '') : '';
form.province = a ? (a.province || '') : '';
form.city = a ? (a.city || '') : '';
form.district = a ? (a.district || '') : '';
form.address = a ? (a.address || '') : '';
form.lng = a ? (a.lng || '') : '';
form.lat = a ? (a.lat || '') : '';
form.floor = a ? Math.max(1, Number(a.floor) || 1) : 1;
form.has_elevator = (a && a.has_elevator !== undefined && a.has_elevator !== null && a.has_elevator !== '') ? Number(a.has_elevator) : 1;
form.is_default = a ? Number(a.is_default) : (list.value.length === 0 ? 1 : 0);
syncRegionInput();
showPaste.value = false;
pasteText.value = '';
showForm.value = true;
}
function edit(a) { openForm(a); }
function step(d) { form.floor = Math.max(1, form.floor + d); }
function chooseLocation() {
uni.chooseLocation({
success: (res) => {
const rawAddr = res.address || '';
const remain = extractRegionFromAddress(rawAddr);
form.address = [remain, res.name || ''].filter(Boolean).join(' ');
if (res.longitude) form.lng = String(res.longitude);
if (res.latitude) form.lat = String(res.latitude);
},
fail: () => {}
});
}
function applyPaste() {
if (!pasteText.value.trim()) {
uni.showToast({ title: '请先粘贴地址文本', icon: 'none' });
return;
}
const text = pasteText.value.trim();
const phoneMatch = text.match(/(1[3-9]\d{9})/);
let rawAddr = text;
if (phoneMatch) {
form.mobile = phoneMatch[1];
const parts = text.split(form.mobile);
const left = (parts[0] || '').trim().replace(/[,,::;;\s]+/g, '');
const right = (parts[1] || '').trim().replace(/^[,,::;;\s]+/, '');
if (left && left.length <= 8) {
form.contact = left;
rawAddr = right;
} else {
rawAddr = left || right;
}
}
const remain = extractRegionFromAddress(rawAddr);
form.address = remain || rawAddr;
showPaste.value = false;
uni.showToast({ title: '识别完成', icon: 'success' });
}
async function save() {
if (saving.value) return;
if (!form.contact.trim() || !/^1[3-9]\d{9}$/.test(form.mobile.trim()) || !form.address.trim()) {
uni.showToast({ title: '请填写完整的联系人、手机号及详细地址', icon: 'none' });
return;
}
saving.value = true;
try {
const res = await api.addressSave({
id: form.id,
contact: form.contact.trim(),
mobile: form.mobile.trim(),
province: form.province,
city: form.city,
district: form.district,
address: form.address.trim(),
lng: form.lng,
lat: form.lat,
floor: form.floor,
has_elevator: form.has_elevator,
is_default: form.is_default
});
uni.showToast({ title: '保存成功' });
showForm.value = false;
await load();
if (selectMode.value && res && res.id && list.value.length === 1) {
onCardTap(list.value[0]);
}
} catch (e) {
} finally {
saving.value = false;
}
}
const actingId = ref(0);
async function setDefault(a) {
if (!a || actingId.value) return;
actingId.value = a.id;
try {
await api.addressDefault(a.id);
uni.showToast({ title: '已设为默认' });
await load();
} catch (e) {
} finally {
actingId.value = 0;
}
}
function remove(a) {
if (!a || actingId.value) return;
uni.showModal({
title: '删除地址',
content: '确认删除该服务地址?',
success: async (r) => {
if (!r.confirm || actingId.value) return;
actingId.value = a.id;
try {
await api.addressDelete(a.id);
uni.showToast({ title: '已删除' });
await load();
} catch (e) {
} finally {
actingId.value = 0;
}
}
});
}
onLoad((options) => {
selectMode.value = Boolean(options && (options.select === '1' || options.select === 'true' || options.mode === 'select'));
});
onShow(() => load());
</script>
<style lang="scss" scoped>
@import '@/uni.scss';
.page {
min-height: 100vh;
background: $yjyf-bg-page;
padding-bottom: 180rpx;
}
.list {
padding: $yjyf-space-md $yjyf-space-md 0;
}
.item {
background: $yjyf-bg-card;
border-radius: $yjyf-radius-md;
padding: $yjyf-space-md;
margin-bottom: $yjyf-space-sm;
box-shadow: $yjyf-shadow-card;
}
.item.selectable:active {
opacity: 0.9;
}
.item-top {
display: flex;
align-items: center;
}
.contact {
font-size: 30rpx;
font-weight: 700;
color: $yjyf-text-main;
}
.mobile {
margin-left: $yjyf-space-sm;
font-size: $yjyf-font-sub;
color: $yjyf-text-regular;
}
.badge {
margin-left: 12rpx;
height: 36rpx;
padding: 0 14rpx;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 8rpx;
background: $yjyf-primary-bg;
color: $yjyf-primary;
font-size: $yjyf-font-micro;
font-weight: 600;
}
.addr {
display: block;
margin-top: 12rpx;
font-size: $yjyf-font-body;
color: $yjyf-text-main;
line-height: 40rpx;
}
.meta {
display: block;
margin-top: 8rpx;
font-size: $yjyf-font-sub;
color: $yjyf-text-secondary;
}
.ops {
display: flex;
justify-content: flex-end;
gap: $yjyf-space-sm;
margin-top: $yjyf-space-sm;
padding-top: $yjyf-space-sm;
border-top: 1rpx solid $yjyf-border-color;
}
.op {
height: 56rpx;
padding: 0 28rpx;
display: inline-flex;
align-items: center;
justify-content: center;
line-height: 1;
border-radius: $yjyf-radius-pill;
border: 2rpx solid $yjyf-border-color;
font-size: $yjyf-font-sub;
color: $yjyf-text-regular;
}
.op.danger {
color: $yjyf-danger;
border-color: rgba(232, 51, 27, 0.22);
}
.empty-box {
padding: 160rpx 40rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.empty-ico {
width: 160rpx;
height: 160rpx;
margin-bottom: $yjyf-space-md;
}
.empty-title {
font-size: 30rpx;
font-weight: 700;
color: $yjyf-text-main;
}
.empty-sub {
margin-top: 8rpx;
font-size: $yjyf-font-sub;
color: $yjyf-text-secondary;
}
.bar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: 120rpx;
padding: 0 $yjyf-space-md;
padding-bottom: env(safe-area-inset-bottom);
box-sizing: content-box;
display: flex;
align-items: center;
background: $yjyf-bg-card;
box-shadow: $yjyf-shadow-bar;
z-index: 10;
}
.bar-btn {
flex: 1;
height: 88rpx;
display: flex;
align-items: center;
justify-content: center;
gap: 10rpx;
line-height: 1;
border-radius: $yjyf-radius-pill;
color: $yjyf-text-inverse;
font-size: 30rpx;
font-weight: 700;
background: $yjyf-gradient-primary;
box-shadow: $yjyf-shadow-button;
}
.bar-plus-ico {
width: 28rpx;
height: 28rpx;
}
.mask {
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.48);
display: flex;
align-items: flex-end;
z-index: 99;
}
.sheet {
width: 100%;
background: $yjyf-bg-card;
border-radius: $yjyf-radius-lg $yjyf-radius-lg 0 0;
padding: 28rpx $yjyf-space-md calc(32rpx + env(safe-area-inset-bottom));
max-height: 86vh;
overflow-y: auto;
}
.sheet-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16rpx;
}
.sheet-title {
font-size: $yjyf-font-title;
font-weight: 700;
color: $yjyf-text-main;
}
.sheet-close-btn {
width: 56rpx;
height: 56rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: $yjyf-bg-page;
}
.close-ico {
width: 26rpx;
height: 26rpx;
}
.quick-tools {
display: flex;
gap: $yjyf-space-sm;
margin: 8rpx 0 16rpx;
}
.tool-btn {
flex: 1;
height: 64rpx;
background: $yjyf-primary-bg;
border: 1rpx solid $yjyf-primary-border;
border-radius: $yjyf-radius-pill;
display: flex;
align-items: center;
justify-content: center;
}
.tool-text {
font-size: $yjyf-font-sub;
color: $yjyf-primary;
font-weight: 600;
line-height: 1;
}
.paste-box {
background: $yjyf-bg-page;
border-radius: $yjyf-radius-sm;
padding: $yjyf-space-sm;
margin-bottom: $yjyf-space-sm;
border: 1rpx dashed $yjyf-primary-border;
}
.paste-input {
width: 100%;
height: 120rpx;
font-size: $yjyf-font-sub;
color: $yjyf-text-main;
}
.paste-btn {
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
line-height: 1;
background: $yjyf-gradient-primary;
color: $yjyf-text-inverse;
font-size: $yjyf-font-sub;
font-weight: 600;
border-radius: $yjyf-radius-pill;
margin-top: 12rpx;
}
.fi {
display: flex;
align-items: center;
min-height: 92rpx;
border-bottom: 1rpx solid $yjyf-border-color;
}
.fi.no-border {
border-bottom: none;
}
.fk {
width: 152rpx;
flex-shrink: 0;
font-size: $yjyf-font-body;
color: $yjyf-text-regular;
}
.fv {
flex: 1;
height: 92rpx;
font-size: $yjyf-font-body;
color: $yjyf-text-main;
}
.ph {
color: $yjyf-text-disabled;
font-size: $yjyf-font-body;
}
.region-picker {
flex: 1;
}
.region-val {
height: 92rpx;
display: flex;
align-items: center;
justify-content: space-between;
font-size: $yjyf-font-body;
color: $yjyf-text-main;
}
.region-val.placeholder {
color: $yjyf-text-disabled;
}
.arrow-ico {
width: 24rpx;
height: 24rpx;
opacity: 0.6;
}
.stepper {
display: flex;
align-items: center;
}
.st-btn {
width: 56rpx;
height: 56rpx;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: $yjyf-bg-page;
}
.st-ico {
width: 24rpx;
height: 24rpx;
}
.st-val {
min-width: 68rpx;
text-align: center;
font-size: $yjyf-font-body;
font-weight: 600;
color: $yjyf-text-main;
}
.seg {
display: flex;
gap: 12rpx;
}
.seg-i {
height: 56rpx;
padding: 0 28rpx;
display: inline-flex;
align-items: center;
justify-content: center;
line-height: 1;
border-radius: $yjyf-radius-pill;
background: $yjyf-bg-page;
font-size: $yjyf-font-sub;
color: $yjyf-text-regular;
}
.seg-i.on {
background: $yjyf-gradient-primary;
color: $yjyf-text-inverse;
font-weight: 600;
}
.save {
height: 88rpx;
margin-top: $yjyf-space-md;
display: flex;
align-items: center;
justify-content: center;
line-height: 1;
border-radius: $yjyf-radius-pill;
color: $yjyf-text-inverse;
font-size: 30rpx;
font-weight: 700;
background: $yjyf-gradient-primary;
box-shadow: $yjyf-shadow-button;
}
.save.disabled {
opacity: 0.7;
}
</style>