address-edit.vue
5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<template>
<view class="page-shell address-edit-page">
<view class="page-blur"></view>
<view class="page-blur-secondary"></view>
<view class="page-blur-tertiary"></view>
<view class="page-content subpage-content">
<view class="subpage-nav">
<view class="subpage-back" @tap="goBack">‹</view>
<view class="subpage-title-wrap">
<text class="subpage-title">{{ pageTitle }}</text>
<text class="subpage-subtitle">以简洁的方式,保留你最常用的配送信息</text>
</view>
</view>
<view class="subpage-card">
<view class="form-item">
<text class="form-label">收货人</text>
<input v-model="form.ReceiverName" class="form-input" placeholder="请输入收货人姓名" placeholder-class="form-placeholder" />
</view>
<view class="form-item">
<text class="form-label">手机号</text>
<input v-model="form.ReceiverPhone" class="form-input" type="number" maxlength="11" placeholder="请输入手机号" placeholder-class="form-placeholder" />
</view>
<view class="form-item">
<text class="form-label">省</text>
<input v-model="form.Province" class="form-input" placeholder="省份" placeholder-class="form-placeholder" />
</view>
<view class="form-item">
<text class="form-label">市</text>
<input v-model="form.City" class="form-input" placeholder="城市" placeholder-class="form-placeholder" />
</view>
<view class="form-item">
<text class="form-label">区/县</text>
<input v-model="form.District" class="form-input" placeholder="区/县" placeholder-class="form-placeholder" />
</view>
<view class="form-item">
<text class="form-label">详细地址</text>
<input v-model="form.DetailAddress" class="form-input" placeholder="街道、门牌号等" placeholder-class="form-placeholder" />
</view>
<view class="form-item no-border switch-row">
<text class="form-label switch-label">设为默认地址</text>
<switch :checked="form.IsDefault === 1" color="#33594b" @change="onDefaultChange" />
</view>
</view>
<view class="cta-row">
<view v-if="mode !== 'create'" class="secondary-button" @tap="removeAddress">删除</view>
<view class="primary-button" @tap="saveAddress">保存地址</view>
</view>
</view>
</view>
</template>
<script>
import { getAddressList, createAddress, updateAddress, deleteAddress } from '@/api/mall'
export default {
data() {
return {
mode: 'edit',
id: '',
form: {
ReceiverName: '',
ReceiverPhone: '',
Province: '',
City: '',
District: '',
DetailAddress: '',
IsDefault: 0
}
}
},
computed: {
pageTitle() {
return this.mode === 'create' ? '新增地址' : '编辑地址'
}
},
onLoad(options) {
this.mode = options.mode || (options.id ? 'edit' : 'create')
if (options.id) {
this.id = options.id
this.loadDetail()
}
},
methods: {
loadDetail() {
getAddressList()
.then((list) => {
const item = (list || []).find(a => String(a.id) === String(this.id))
if (item) {
this.form = {
ReceiverName: item.receiverName || '',
ReceiverPhone: item.receiverPhone || '',
Province: item.province || '',
City: item.city || '',
District: item.district || '',
DetailAddress: item.detailAddress || '',
IsDefault: item.isDefault === 1 ? 1 : 0
}
}
})
.catch(() => {})
},
onDefaultChange(e) {
this.form.IsDefault = e.detail.value ? 1 : 0
},
validate() {
if (!this.form.ReceiverName) {
uni.showToast({ title: '请输入收货人', icon: 'none' })
return false
}
if (!/^1\d{10}$/.test(this.form.ReceiverPhone)) {
uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
return false
}
if (!this.form.DetailAddress) {
uni.showToast({ title: '请输入详细地址', icon: 'none' })
return false
}
return true
},
saveAddress() {
if (!this.validate()) return
const payload = { ...this.form }
const req = this.mode === 'create'
? createAddress(payload)
: updateAddress({ ...payload, Id: this.id })
req
.then(() => {
uni.showToast({ title: '地址已保存', icon: 'none' })
setTimeout(() => this.backToList(), 300)
})
.catch(() => {})
},
removeAddress() {
uni.showModal({
title: '删除地址',
content: '确认删除该地址吗?',
success: (res) => {
if (!res.confirm) return
deleteAddress(this.id)
.then(() => {
uni.showToast({ title: '地址已删除', icon: 'none' })
setTimeout(() => this.backToList(), 300)
})
.catch(() => {})
}
})
},
backToList() {
uni.navigateBack({ fail: () => uni.redirectTo({ url: '/packageProfile/address-list/address-list' }) })
},
goBack() {
this.backToList()
}
}
}
</script>
<style lang="scss" scoped>
@import '@/styles/mixins.scss';
@import '@/styles/subpage.scss';
.form-item {
padding: 18rpx 0;
border-bottom: 1rpx solid rgba(100, 140, 118, 0.08);
}
.form-item.no-border {
border-bottom: 0;
}
.form-label {
display: block;
font-size: 22rpx;
color: $text-tertiary;
letter-spacing: 2rpx;
}
.form-input {
margin-top: 14rpx;
height: 60rpx;
font-size: 30rpx;
font-weight: 600;
color: $text-primary;
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
}
.switch-label {
margin-top: 0;
font-size: 28rpx;
font-weight: 600;
color: $text-primary;
}
</style>