addressEdit.vue
10.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<template>
<LayoutApp :title="title" :show-back="true">
<Scroll ref="scroll" :options="scrollOpts">
<div class="pane-body">
<FormItem>
<CInput
label="收货人"
place-holder="请写姓名"
v-model="model.consignee"
:textValue="model.consignee"
type="text"
></CInput>
</FormItem>
<FormItem>
<CInput
label="手机号"
place-holder="请填写手机号"
v-model="model.mobile"
:textValue="model.mobile"
></CInput>
</FormItem>
<template>
<FormItem>
<div class="wrapper-area">
<label class="input-label">所在区域</label>
<div class="wrapper-arrow" @click="chooseArea">
<template v-if="model.area">
<label class="text-label">{{ model.area }}</label>
</template>
<template v-else>
<label class="choose-area">请选择</label>
</template>
<div class="arrow"></div>
</div>
</div>
</FormItem>
</template>
<FormItem>
<CInput
label="详细地址"
place-holder="请输入详细地址"
v-model="model.address"
:textValue="model.address"
></CInput>
</FormItem>
<!-- 订单修改地址隐藏 -->
<div v-if="!orderCode" class="wrapper-tag">
<p class="tag-text">设置标签:</p>
<RadioGroup class="wrapper-radio">
<div v-for="(tag, index) in addressTags" :key="index">
<Radio
class="tag-radio"
:label="{ text: `${tag.name}`, value: `${tag.code}` }"
v-model="model.tag_code"
checked="tag.code === model.tag_code"
></Radio>
</div>
</RadioGroup>
</div>
<!-- 订单修改地址隐藏 -->
<div v-if="!orderCode" class="wrapper-del">
<Radio
class="radio"
:label="{ text: '设为默认地址', value: true }"
style="flex: 0 1 100%;"
checked="model.is_default"
v-model="model.is_default"
></Radio>
</div>
</div>
<div :class="submitClass" @touchend="onSubmit">确 认</div>
</Scroll>
<AddressAct
class="address-act"
ref="addressAct"
v-show="isShowProvince"
@popHidden="popHidden"
@modifyAddressAct="modifyAddressAct"
></AddressAct>
</LayoutApp>
</template>
<script>
import Layout from '../../../components/layout/layout-app';
import Input from './components/input';
import FormItem from './components/form-item';
import Radio from './components/radio';
import AddressAct from './components/address-act';
import RadioGroup from './components/radio-group';
import { Scroll } from 'cube-ui';
import { createNamespacedHelpers } from 'vuex';
const { mapState, mapMutations, mapActions } = createNamespacedHelpers(
'address/address'
);
export default {
name: 'addressEdit',
components: {
LayoutApp: Layout,
CInput: Input,
FormItem,
Radio,
AddressAct,
RadioGroup,
Scroll
},
data() {
return {
scrollOpts: {
bounce: false
},
isShowProvince: false,
isUpdate: false,
updateMobileNum: '',
isMobileNumEdit: false,
title: '',
orderCode: '',
model: {
consignee: '',
address_id: '',
mobile: '',
area_code: '',
area: '',
address: '',
tag_code: '',
is_default: false
}
};
},
watch: {
'model.mobile': function(val) {
if (val === this.updateMobileNum) {
this.isMobileNumEdit = false;
} else {
this.isMobileNumEdit = true;
}
}
},
computed: {
...mapState(['addressTags', 'updateAddressInfo']),
submitClass() {
return [
'sure-btn',
{
active: this.inNotEmpty
}
];
},
inNotEmpty() {
return (
this.model.consignee &&
this.model.mobile &&
this.model.area &&
this.model.address
);
}
},
methods: {
...mapMutations({}),
...mapActions([
'fetchAddressTags',
'addUserAddress',
'updateUserAddress',
'deleteUserAddress',
'updateReceiptAddressInOrder'
]),
async onSubmit() {
let data = this.validator();
// 订单编辑地址
const orderCode = this.orderCode;
if (!data) {
return;
}
let result = {};
if (this.isUpdate) {
//更新和添加地址接口不同
if (orderCode) {
result = await this.updateReceiptAddressInOrder({
...data,
orderCode
});
} else {
result = await this.updateUserAddress(data);
}
} else {
result = await this.addUserAddress(data);
}
if (result && result.code === 200) {
this.$createToast({
type: 'txt',
txt: result.message.split('.').join('')
}).show();
await this.sleep(1000);
this.$router.go(-1);
} else {
this.$createToast({
type: 'error',
txt: result.message,
mask: true
}).show();
}
},
validator() {
let info = this.model;
let username = info.consignee.replace(/(^\s+)|(\s+$)/g, '');
// 简单的表单校验
if (!username) {
this.showToast('收件人不能为空');
return false;
}
if (!/^[\u4E00-\u9FA5A-Za-z0-9*]+$/gi.test(username)) {
this.showToast('收货人姓名不支持特殊符号');
return false;
}
let reg;
if (this.isUpdate && !this.isMobileNumEdit) {
reg = /^[0123456789*]{11}$/;
} else {
reg = /^[0123456789]{11}$/;
}
if (!info.mobile) {
this.showToast('手机号不能为空');
return false;
} else {
if (!reg.test(info.mobile)) {
this.showToast('请输入正确11位手机号码');
return;
}
}
if (!info.area_code || !info.area) {
this.showToast('省市区不能为空');
return false;
}
if (!info.address) {
this.showToast('地址不能为空');
return false;
}
return {
id: info.address_id || '',
consignee: username,
mobile: info.mobile,
area_code: info.area_code,
area: info.area,
address: info.address,
is_default: info.is_default ? 'Y' : 'N',
tag_code: info.tag_code
};
},
async delAddress() {
const result = await this.deleteUserAddress(this.model.address_id);
if (result && result.code === 200) {
this.$createToast({
type: 'txt',
txt: result.message.split('.').join('')
}).show();
await this.sleep(1000);
this.$router.go(-1);
} else {
this.$createToast({
type: 'error',
txt: result.message,
mask: true
}).show();
}
},
chooseArea() {
this.isShowProvince = true;
this.$refs.addressAct.parentHandleclick({
areaCode: this.model.area_code
});
},
popHidden() {
let that = this;
that.isShowProvince = false;
},
modifyAddressAct(info) {
if (info) {
let that = this;
that.model.area_code = info.code;
that.model.area = info.area;
}
},
showToast(tip) {
this.$createToast({
type: 'txt',
txt: tip
}).show();
},
sleep(n) {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, n);
});
}
},
activated() {
let addressInfo = this.updateAddressInfo;
this.isUpdate = addressInfo.isUpdate;
this.orderCode = addressInfo.orderCode;
this.title = addressInfo.isUpdate ? '编辑地址' : '添加地址';
// 订单编辑 不查标签
if (!this.orderCode) {
this.fetchAddressTags();
}
if (addressInfo.isUpdate) {
this.updateMobileNum = addressInfo.mobile;
this.model.consignee = addressInfo.consignee;
this.model.address_id = addressInfo.address_id;
this.model.mobile = addressInfo.mobile;
this.model.area_code = addressInfo.area_code || addressInfo.areaCode;
this.model.area = addressInfo.area;
this.model.address = addressInfo.address;
this.model.tag_code = addressInfo.tag_code;
this.model.is_default = addressInfo.is_default === 'Y' ? true : false;
} else {
this.model = {};
}
}
};
</script>
<style lang="scss" scoped>
/deep/ {
.cube-scroll-content {
min-height: 100%;
}
.cube-scroll-list-wrapper {
overflow: auto;
}
.cube-loading-spinners {
margin: auto;
}
}
.address-act {
z-index: 99;
}
.pane-body {
height: 100%;
overflow-y: auto;
padding-top: 12px;
padding-left: 40px;
padding-right: 40px;
}
.wrapper-del {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 80px;
.del-address {
width: 150px;
text-align: center;
line-height: 80px;
color: #cd001a;
font-size: 26px;
}
}
.wrapper-area {
display: flex;
flex-direction: column;
.input-label {
font-size: 36px;
display: inline-block;
font-weight: bold;
margin-bottom: 30px;
}
.text-label {
font-size: 28px;
width: 100%;
color: #444444;
display: inline-block;
}
.choose-area {
font-size: 28px;
width: 100%;
color: #cccccc;
}
.wrapper-arrow {
display: flex;
justify-content: space-between;
}
.arrow {
height: 44px;
width: 44px;
background: url("~statics/image/address/arrow-right.png");
background-size: cover;
}
}
.wrapper-tag {
margin: 30px 0;
.wrapper-radio {
margin: 30px 0 20px;
}
.tag-text {
font-size: 36px;
display: inline-block;
font-weight: bold;
}
.tag-radio {
margin-right: 30px;
}
}
.radio {
margin: 30px 0;
}
.sure-btn {
height: 80px;
line-height: 80px;
font-size: 32px;
font-weight: bold;
text-align: center;
background-color: #cccccc;
color: white;
border-radius: 40px;
position: absolute;
left: 32px;
right: 32px;
bottom: 24px;
&.active {
background-color: #002b47;
}
}
</style>