address.js 5.82 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

/**
 * 地址管理页面
 * @param req
 * @param res
 * @param next
 */
exports.address = (req, res, next) => {
    let responseData = {
        module: 'home',
        page: 'address',
        pageHeader: headerModel.setNav({
            navTitle: '地址管理'
        }),
        title: '地址管理'
    };

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

};

/**
 * 地址列表json
 * @param req
 * @param res
 * @param next
 */
exports.addressJson = (req, res, next) => {
    req.ctx(addressModel).address({
        uid: req.user.uid
    }).then(result => {
        if (result && result.address) {
            result.address.map(value => {
                value.uid && delete value.uid;
                return value;
            });
        }
        res.json(result);
    }).catch(next);
};

/**
 * 地址编辑页面
 * @param req
 * @param res
 * @param next
 */
exports.addressAct = (req, res, next) => {
    req.ctx(addressModel).address({
        id: req.query.id ? decodeURIComponent(req.query.id) : null,
        uid: req.user.uid
    }).then(result => {
        let responseData = {
            module: 'home',
            page: 'address-act',
            vue: true,
            localCss: true,
            pageHeader: headerModel.setNav({
                navTitle: result && result.navTitle,
                navBtn: false,
                backUrl: false
            }),
            title: result && result.navTitle
        };

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

/**
 * 地址数据
 */
exports.newGetAddress = (req, res, next) => {
    req.ctx(addressModel).newGetAddress({
        id: req.query.id
    }).then(result => {
        res.json(result);
    }).catch(next);
};

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

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

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

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

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

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

    req.ctx(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: decodeURIComponent(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) => {
    req.ctx(addressModel).delAddress({
        id: decodeURIComponent(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) => {
    req.ctx(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: '选择地址',
            navBtn: false
        }),
        title: '选择地址',
        pageFooter: true
    };

    req.ctx(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) => {
    res.set({
        'Cache-Control': 'max-age=600'
    });

    req.ctx(addressModel).locationList({
        uid: req.user.uid
    }).then(result => {
        res.render('address/location-list', Object.assign(result, { layout: false }));
    }).catch(next);
};

/**
 * 订单详情页修改地址
 * @param req
 * @param res
 * @param next
 */
exports.chooseAddress = (req, res, next) => {
    let uid = req.user.uid;
    let orderCode = req.query.orderCode;
    let addressId = decodeURIComponent(req.query.addressId);

    if (!addressId || !orderCode) {
        return res.json({
            code: 400,
            message: '缺失必填项',
            data: ''
        });
    }

    req.ctx(addressModel).chooseAddress(orderCode, addressId, uid).then(result => {
        res.json(result);
    }).catch(next);
};