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

const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header'); // 头部model
const addressModel = require(`${mRoot}/address`); // 地址管理 MODEL
const helpers = global.yoho.helpers;
/**
 * 地址管理页面
 * @param req
 * @param res
 * @param next
 */
exports.address = (req, res, next) => {
    let responseData = {
        module: 'home',
        page: 'address',
        pageHeader: headerModel.setNav({
            navTitle: '地址管理'
        }),
        title: '地址管理'
    };

    addressModel.address({
        uid: req.user.uid
    }).then(result => {
        res.render('address/index', Object.assign(responseData, result));
    }).catch(next);

};

/**
 * 地址编辑页面
 * @param req
 * @param res
 * @param next
 */
exports.addressAct = (req, res, next) => {
    addressModel.address({
        id: req.params.id,
        uid: req.user.uid
    }).then(result => {
        let responseData = {
            module: 'home',
            page: 'address-act',
            pageHeader: headerModel.setNav({
                navTitle: result.navTitle
            }),
            title: result.navTitle
        };

        res.render('address/address-act', Object.assign(responseData, result));
    }).catch(next);
};

/**
 * 修改地址或者添加新地址
 * @param req
 * @param res
 * @param next
 */
exports.saveAddress = (req, res, next) => {

    /* 没有用户,返回 */
    if (!req.body.uid) {
        res.json({
            code: 400,
            message: '用户不可用'
        });
    }

    /* 没有地址信息,返回 */
    if (!req.body.address) {
        res.json({
            code: 401,
            message: '请输入可用的地址信息'
        });
    }

    /* 没有地区码,返回 */
    if (!req.body.area_code) {
        res.json({
            code: 402,
            message: '地区码不可用'
        });
    }

    /* 没有收件人姓名,返回 */
    if (!req.body.consignee) {
        res.json({
            code: 403,
            message: '请输入收件人姓名'
        });
    }

    /* 邮箱地址校验不通过,返回 */
    if (!req.body.email || !helpers.verifyEmail(req.body.email)) {
        res.json({
            code: 404,
            message: '输入的邮箱地址格式不正确'
        });
    }

    addressModel.saveAddress({
        uid: req.user.uid,
        address: req.body.address,
        area_code: req.body.area_code,
        consignee: req.body.consignee,
        email: req.body.email,
        id: req.body.id,
        mobile: req.body.mobile,
        zip_code: req.body.zip_code
    }).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 删除地址
 * @param req
 * @param res
 * @param next
 */
exports.delAddress = (req, res, next) => {
    addressModel.delAddress({
        id: req.body.id,
        uid: req.user.uid
    }).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 设置默认地址
 * @param req
 * @param res
 * @param next
 */
exports.defaultAddress = (req, res, next) => {
    addressModel.setDefaultAddress({
        uid: req.user.uid,
        id: req.body.id
    }).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 订单详情页——地址列表
 * @param req
 * @param res
 * @param next
 */
exports.addressModify = (req, res, next) => {
    let responseData = {
        module: 'home',
        page: 'address-modify',
        pageHeader: headerModel.setNav({
            navTitle: '选择地址'
        }),
        title: '选择地址'
    };

    addressModel.address({
        uid: req.user.uid
    }).then(result => {
        res.render('address/address-modify', Object.assign(responseData, result, {
            showAddBtn: (result.address.length <= 5),
            orderCode: req.query.orderCode,
            relation: req.query.relation
        }));
    }).catch(next);
};

/**
 * 异步获取三级地址数据
 * @param req
 * @param res
 * @param next
 */
exports.locationList = (req, res, next) => {
    addressModel.locationList({
        uid: req.user.uid
    }).then(result => {
        res.render('address/location-list', result);
    }).catch(next);
};