address.js 3.25 KB
/**
 * [个人中心]收货地址
 * @author: jiangmin
 * @date: 2016/07/05
 */

'use strict';

const api = global.yoho.API;

/**
 * 地址列表数据
 *
 * @param  uid 用户ID
 * @param  limit 分页大小参数(默认10条)
 * @return array 地址接口返回的数据
 */
const getAddressDataAsync = (uid, limit) => {
    return api.get('', {
        method: 'app.address.get',
        uid: uid,
        limit: limit
    }).then(result => {
        return 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);
};

/**
 * 保存地址数据
 *
 * @param uid 用户ID
 * @param address 地址信息
 * @param areaCode 城市码
 * @param consignee 收货人
 * @param mobile 手机号码
 * @param phone 电话号码
 * @param boolean init 是否是初始地址
 * @return array 地址接口返回的数据
 */
const addAddressData = (uid, address, areaCode, consignee, mobile, phone, init) => {
    return api.get('', {
        method: 'app.address.add',
        uid: uid,
        address: address,
        area_code: areaCode,
        consignee: consignee,
        mobile: mobile,
        phone: phone
    }).then(result => {
        if (result.code === 200 && init) {
            return setDefaultAddress(result.data.id, uid).then(defaultResult => {
                if (defaultResult.code === 200) {

                    // 若设置默认成功,扩展result设置默认标志位为Y
                    result.data.is_default = 'Y';
                }

                // 否则,返回原result
                return result;
            });
        }

        // 非初始地址或者新建不成功,直接返回result
        return result;
    });
};

/**
 * 修改地址数据
 *
 * @param id 地址id
 * @param uid 用户ID
 * @param address 地址信息
 * @param areaCode 城市码
 * @param consignee 收货人
 * @param mobile 手机号码
 * @param phone 电话号码
 * @return array 地址接口返回的数据
 */
const updateAddressData = (id, uid, address, areaCode, consignee, mobile, phone) => {
    return api.get('', {
        method: 'app.address.update',
        id: id,
        uid: uid,
        address: address,
        area_code: areaCode,
        consignee: consignee,
        mobile: mobile,
        phone: phone
    }).then(result => result);
};

/**
 * 删除地址数据
 *
 * @param id 地址id
 * @param uid 用户ID
 * @return array 地址接口返回的数据
 */
const delAddressData = (id, uid) => {
    return api.get('', {
        method: 'app.address.del',
        id: id,
        uid: uid
    }).then(result => result);
};

/**
 * 获取地址信息
 * @function getAddressData
 * @return { Array } [返回带有areaId的地址数组]
 * @param id
 * @param caption
 * @param is_support_express
 */
const getAddressData = (areaId) => {
    return api.get('', {
        method: 'app.address.provinces',
        id: areaId || 0
    }).then(result => result);
};

module.exports = {
    getAddressDataAsync,
    addAddressData,
    updateAddressData,
    delAddressData,
    setDefaultAddress,
    getAddressData
};