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

'use strict';
const mcHandler = require('../models/menu-crumb-handler');
const addressModel = require('../models/address');

const _ = require('lodash');

/**
 * 默认地址
 */
const _setDefault = (id, uid) => {
    return addressModel.setDefaultAddress(id, uid).then(result => result);
};

/**
 * 收货地址页面加载
 */
const index = (req, res, next) => {
    let uid = req.user.uid || '123456';

    addressModel.getAddressDataAsync(uid, 20).then(result => {
        let resultData = result.data;
        let length = resultData.length ? resultData.length : 0;

        for (let i = 0; i < length; i++) {
            resultData[i].default = resultData[i].is_default === 'Y';
            resultData[i].mobile = resultData[i].mobile.substring(0, 3) + '****' +
                resultData[i].mobile.substring(7, 11);
        }
        resultData.leftLength = 20 - length;
        resultData.length = length;
        res.render('index', {
            module: 'me',
            page: 'address',
            isMe: true,
            content: {
                nav: mcHandler.getMeCrumb(),
                navigation: mcHandler.getSideMenu('收货地址'),
                banner: 'http://placehold.it/150x120',
                address: true,
                title: '收货地址',
                data: resultData
            }
        });
    }).catch(next);
};

const getAddressList = (req, res, next) => {
    addressModel.getAddressDataAsync(req.user.uid, 20).then(result => {
        if (result.data) {
            let defaultAd = _.find(result.data, o => o.is_default === 'Y');

            defaultAd && (defaultAd.focus = true);
        }
        res.send(result);
    }).catch(next);
};

/**
 * 添加地址
 */
const addAddressData = (req, res) => {
    let uid = req.user.uid || '123456';
    let address = req.body.address;
    let areaCode = req.body.area_code;
    let consignee = req.body.consignee;
    let mobile = req.body.mobile;
    let phone = req.body.phone;

    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'
                    });

                    res.send(result);
                }
            });
        } else {
            res.send(result);
        }
    });
};

/**
 * 修改地址
 */
const updateAddressData = (req, res) => {
    let id = req.body.id;
    let uid = req.user.uid || '123456';
    let address = req.body.address;
    let areaCode = req.body.area_code;
    let consignee = req.body.consignee;
    let mobile = req.body.mobile;
    let phone = req.body.phone;

    addressModel.updateAddressData(id, uid, address, areaCode, consignee, mobile, phone).then(result => {
        res.send(result);
    });
};

/**
 * 删除地址
 */
const delAddressData = (req, res) => {
    let id = req.body.id;
    let uid = req.user.uid || '123456';

    addressModel.delAddressData(id, uid).then(result => {
        res.send(result);
    });
};

/**
 * 设置默认地址
 */
const setDefaultAddress = (req, res) => {
    let id = req.body.id;
    let uid = req.user.uid || '123456';

    _setDefault(id, uid).then(result => {
        res.send(result);
    });
};

/**
 * 获取地址信息
 * @function getAddressData
 * @return { Object }
 */
const getAddressData = (req, res) => {
    addressModel.getAddressData(req.params.areaId).then(result => {
        if (result.code === 200) {
            result.data = result.data.map((item) => {
                return {
                    is_support: item.is_support_express,
                    text: item.caption,
                    value: item.id
                };
            });
            res.json(result);
        } else {
            res.json(result);
        }
    });
};

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