Authored by xuqi

set default when add address

... ... @@ -74,20 +74,8 @@ const addAddressData = (req, res) => {
let isInit = req.body.init; // 是否是初始地址
addressModel.addAddressData(uid, address, areaCode, consignee, mobile, phone).then(result => {
if (isInit) {
_setDefault(uid, result.data.id).then(defaultResult => {
if (defaultResult.code === 200) {
Object.assign(result.data, {
is_default: 'Y'
});
addressModel.addAddressData(uid, address, areaCode, consignee, mobile, phone, isInit).then(result => {
res.send(result);
}
});
} else {
res.send(result);
}
});
};
... ...
... ... @@ -23,6 +23,22 @@ const getAddressDataAsync = (uid, limit) => {
}).then(result => result);
};
/**
* 设置默认地址
*
* @param id 地址id
* @param uid 用户ID
* @return array 地址接口返回的数据
*/
const setDefaultAddress = (id, uid) => {
return api.get('', {
method: 'app.address.setdefault',
id: id,
uid: uid
}).then(result => result);
};
/**
* 保存地址数据
*
... ... @@ -32,9 +48,10 @@ const getAddressDataAsync = (uid, limit) => {
* @param consignee 收货人
* @param mobile 手机号码
* @param phone 电话号码
* @param boolean init 是否是初始地址
* @return array 地址接口返回的数据
*/
const addAddressData = (uid, address, areaCode, consignee, mobile, phone) => {
const addAddressData = (uid, address, areaCode, consignee, mobile, phone, init) => {
return api.get('', {
method: 'app.address.add',
uid: uid,
... ... @@ -44,7 +61,22 @@ const addAddressData = (uid, address, areaCode, consignee, mobile, phone) => {
mobile: mobile,
phone: phone
}).then(result => {
// console.log("添加result",result);
if (result.code === 200 && init) {
return setDefaultAddress(result.data.id, uid).then(defaultResult => {
// 若设置默认成功,扩展result设置默认标志位为Y
if (defaultResult.code === 200) {
return Object.assign(result.data, {
is_default: 'Y'
});
}
// 否则,返回原result
return result;
});
}
// 非初始地址或者新建不成功,直接返回result
return result;
});
};
... ... @@ -96,24 +128,6 @@ const delAddressData = (id, uid) => {
};
/**
* 设置默认地址
*
* @param id 地址id
* @param uid 用户ID
* @return array 地址接口返回的数据
*/
const setDefaultAddress = (id, uid) => {
return api.get('', {
method: 'app.address.setdefault',
id: id,
uid: uid
}).then(result => {
// console.log('设置默认result', result);
return result;
});
};
/**
* 获取地址信息
* @function getAddressData
* @return { Array } [返回带有areaId的地址数组]
... ...