address.js 4.25 KB
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/8/17
 * Time: 13:41
 */
'use strict';

const api = global.yoho.API;
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const crypto = global.yoho.crypto;

/**
 * 从接口获取地址列表
 * @param params
 */
const _getAddressData = (params) => {
    return api.get('', {
        method: 'app.address.gethidden',
        uid: params.uid
    }, {code: 200});
};

/**
 * 获取三级地址列表数据
 * @param params
 * @returns {*}
 * @private
 */
const _getThrAddressData = (params) => {
    return api.get('', {
        method: 'app.address.getlist',
        uid: params.uid
    }, {code: 200, cache: true});
};

/**
 * 调用接口保存地址数据
 * @param params
 * @private
 */
const _saveAddressData = (params) => {
    return api.get('', {
        method: params.id ? 'app.address.update' : 'app.address.add',
        id: params.id,
        address: params.address,
        area_code: params.area_code,
        consignee: params.consignee,
        email: params.email,
        mobile: params.mobile,
        zip_code: params.zip_code,
        uid: params.uid
    });
};

/**
 * 调用接口设置默认地址
 * @param params
 * @private
 */
const _setDefaultAddress = (params) => {
    return api.get('', {
        method: 'app.address.setdefault',
        uid: params.id,
        id: params.id
    });
};

/**
 * 调用接口删除地址
 * @param params
 * @private
 */
const _delAddressData = (params) => {
    return api.get('', {
        method: 'app.address.del',
        id: params.id,
        uid: params.uid
    });
};

/**
 * 地址管理页面
 * @param params
 */
const address = (params) => {
    let apiList = [
        _getAddressData(params)
    ];

    if (params.id) { // 如果有 ID 是编辑地址页面
        apiList.push(_getThrAddressData(params));
    }

    return api.all(apiList).then(result => {
        let selfAddress = result[0];

        if (selfAddress.data) {

            /* 加密地址 ID */
            _.forEach(selfAddress.data, (value, key) => {
                selfAddress.data[key].address_id = encodeURIComponent(crypto.encryption(null, selfAddress.data[key].address_id));
            });

            /* 如果有 ID 是地址编辑页面 */
            if (params.id) {
                return {
                    address: camelCase(_.find(selfAddress.data, o => {
                        return decodeURIComponent(o.address_id) === params.id;
                    })),
                    addressList: camelCase(result[1] ? result[1].data : []),
                    navTitle: '编辑地址'
                };
            } else {
                return {
                    address: camelCase(selfAddress.data),
                    addressList: camelCase(result[1] ? result[1].data : []),
                    navTitle: '添加地址'
                };
            }
        }
    });
};

/**
 * 保存地址数据
 * @param params
 */
const saveAddress = (params) => {
    if (params.id) {
        params.id = crypto.decrypt(null, params.id);
    }
    return _saveAddressData(params).then(result => {
        return result;
    });
};

/**
 * 设置默认地址
 * @param params
 */
const setDefaultAddress = (params) => {
    if (params.id) {
        params.id = crypto.encryption(null, params.id);
    }
    return _setDefaultAddress(params).then(result => {
        return result;
    });
};

/**
 * 删除地址
 * @param params
 */
const delAddress = (params) => {
    if (params.id) {
        params.id = crypto.decrypt(null, params.id);
    }
    return _delAddressData(params).then(result => {
        return result;
    });
};

/**
 * 三级地址数据
 * @param params
 */
const locationList = (params) => {
    return _getThrAddressData(params).then(result => {
        if (result.data) {
            return {
                addressList: result.data
            };
        }
    });
};

/**
 * 新版地址选择获取地址
 */
const newGetAddress = (params) => {
    return api.get('', {
        method: 'app.address.provinces',
        id: params.id || '0'
    }, {code: 200, cache: true}).then(result => {
        return result && result.data;
    });
};

module.exports = {
    address,
    saveAddress,
    delAddress,
    setDefaultAddress,
    locationList,
    newGetAddress
};