address.js
2.43 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
/**
* address model
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/09/27
*/
'use strict';
const _ = require('lodash');
const pinyin = require('../models/province-pinyin');
const AddressApi = require('../models/address-api');
const worldSort = 'abcdefghijklmnopqrstuvwxyz';
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getAreaListData(id) {
return new AddressApi(this.ctx).getAreaListAsync(id).then(result => {
let list = _.get(result, 'data', []);
if (id * 1 === 0 && list.length) {
_.forEach(list, value => {
value.initial = pinyin[value.caption] || 'z';
value.pySort = _.indexOf(worldSort, value.initial);
});
result.data = list;
}
return result;
});
}
getAddressListData(uid) {
return new AddressApi(this.ctx).getAddressListAsync(uid).then(result => {
if (result.code !== 200) {
return result;
}
let d = result.data;
_.forEach(d, dd => {
if (dd.is_default === 'Y') {
dd.default = true;
}
dd.id = dd.address_id;
delete dd.address_id;
delete dd.uid; // 删除uid,用户数据保密
});
return result;
});
}
delAddressById(uid, id) {
return new AddressApi(this.ctx).delAddressAsync(uid, id);
}
saveAddressData(uid, info) {
let addressApiModel = new AddressApi(this.ctx);
if (info.id) {
let id = info.id;
return addressApiModel.updateAddressAsync(uid, id, info.consignee, info.areaCode, info.address,
info.mobile, info.phone, info.zipCode, info.email);
} else {
return addressApiModel.addAddressAsync(uid, info.consignee, info.areaCode, info.address, info.mobile,
info.phone, info.zipCode, info.email).then(result => {
if (result.code === 200) {
let d = result.data;
d.id = d.address_id;
delete d.address_id;
delete d.uid;
}
return result;
});
}
}
setDefaultAddress(uid, id) {
return new AddressApi(this.ctx).setDefaultAddressAsync(uid, id);
}
};