Authored by xuqi

set default when add address

@@ -74,20 +74,8 @@ const addAddressData = (req, res) => { @@ -74,20 +74,8 @@ const addAddressData = (req, res) => {
74 74
75 let isInit = req.body.init; // 是否是初始地址 75 let isInit = req.body.init; // 是否是初始地址
76 76
77 - addressModel.addAddressData(uid, address, areaCode, consignee, mobile, phone).then(result => {  
78 - if (isInit) {  
79 - _setDefault(uid, result.data.id).then(defaultResult => {  
80 - if (defaultResult.code === 200) {  
81 - Object.assign(result.data, {  
82 - is_default: 'Y'  
83 - });  
84 -  
85 - res.send(result);  
86 - }  
87 - });  
88 - } else {  
89 - res.send(result);  
90 - } 77 + addressModel.addAddressData(uid, address, areaCode, consignee, mobile, phone, isInit).then(result => {
  78 + res.send(result);
91 }); 79 });
92 }; 80 };
93 81
@@ -23,6 +23,22 @@ const getAddressDataAsync = (uid, limit) => { @@ -23,6 +23,22 @@ const getAddressDataAsync = (uid, limit) => {
23 }).then(result => result); 23 }).then(result => result);
24 }; 24 };
25 25
  26 +
  27 +/**
  28 + * 设置默认地址
  29 + *
  30 + * @param id 地址id
  31 + * @param uid 用户ID
  32 + * @return array 地址接口返回的数据
  33 + */
  34 +const setDefaultAddress = (id, uid) => {
  35 + return api.get('', {
  36 + method: 'app.address.setdefault',
  37 + id: id,
  38 + uid: uid
  39 + }).then(result => result);
  40 +};
  41 +
26 /** 42 /**
27 * 保存地址数据 43 * 保存地址数据
28 * 44 *
@@ -32,9 +48,10 @@ const getAddressDataAsync = (uid, limit) => { @@ -32,9 +48,10 @@ const getAddressDataAsync = (uid, limit) => {
32 * @param consignee 收货人 48 * @param consignee 收货人
33 * @param mobile 手机号码 49 * @param mobile 手机号码
34 * @param phone 电话号码 50 * @param phone 电话号码
  51 + * @param boolean init 是否是初始地址
35 * @return array 地址接口返回的数据 52 * @return array 地址接口返回的数据
36 */ 53 */
37 -const addAddressData = (uid, address, areaCode, consignee, mobile, phone) => { 54 +const addAddressData = (uid, address, areaCode, consignee, mobile, phone, init) => {
38 return api.get('', { 55 return api.get('', {
39 method: 'app.address.add', 56 method: 'app.address.add',
40 uid: uid, 57 uid: uid,
@@ -44,7 +61,22 @@ const addAddressData = (uid, address, areaCode, consignee, mobile, phone) => { @@ -44,7 +61,22 @@ const addAddressData = (uid, address, areaCode, consignee, mobile, phone) => {
44 mobile: mobile, 61 mobile: mobile,
45 phone: phone 62 phone: phone
46 }).then(result => { 63 }).then(result => {
47 - // console.log("添加result",result); 64 + if (result.code === 200 && init) {
  65 + return setDefaultAddress(result.data.id, uid).then(defaultResult => {
  66 +
  67 + // 若设置默认成功,扩展result设置默认标志位为Y
  68 + if (defaultResult.code === 200) {
  69 + return Object.assign(result.data, {
  70 + is_default: 'Y'
  71 + });
  72 + }
  73 +
  74 + // 否则,返回原result
  75 + return result;
  76 + });
  77 + }
  78 +
  79 + // 非初始地址或者新建不成功,直接返回result
48 return result; 80 return result;
49 }); 81 });
50 }; 82 };
@@ -96,24 +128,6 @@ const delAddressData = (id, uid) => { @@ -96,24 +128,6 @@ const delAddressData = (id, uid) => {
96 }; 128 };
97 129
98 /** 130 /**
99 - * 设置默认地址  
100 - *  
101 - * @param id 地址id  
102 - * @param uid 用户ID  
103 - * @return array 地址接口返回的数据  
104 - */  
105 -const setDefaultAddress = (id, uid) => {  
106 - return api.get('', {  
107 - method: 'app.address.setdefault',  
108 - id: id,  
109 - uid: uid  
110 - }).then(result => {  
111 - // console.log('设置默认result', result);  
112 - return result;  
113 - });  
114 -};  
115 -  
116 -/**  
117 * 获取地址信息 131 * 获取地址信息
118 * @function getAddressData 132 * @function getAddressData
119 * @return { Array } [返回带有areaId的地址数组] 133 * @return { Array } [返回带有areaId的地址数组]