address.js 5.88 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: '地址管理'
    };

    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: decodeURIComponent(req.query.id),
        uid: req.user.uid
    }).then(result => {
        let responseData = {
            module: 'home',
            page: 'address-act',
            pageHeader: headerModel.setNav({
                navTitle: result && result.navTitle,
                navBtn: false,
                backUrl: false
            }),
            title: result.navTitle
        };

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

/**
 * 新版地区选择
 */
exports.addressActVue = (req, res) => {
    res.render('address/address-act-vue', {
        vue: true,
        localCss: true
    });
};

/**
 * TODO DELETE 模拟省数据
 */
exports.provinces = (req, res) => {
    res.json({ "provinces": [{ "name": "北京", "id": "1" }, { "name": "上海", "id": "2" }, { "name": "天津", "id": "3" }, { "name": "重庆", "id": "4" }, { "name": "河北", "id": "5" }, { "name": "山西", "id": "6" }, { "name": "河南", "id": "7" }, { "name": "辽宁", "id": "8" }, { "name": "吉林", "id": "9" }, { "name": "黑龙江", "id": "10" }, { "name": "内蒙古", "id": "11" }, { "name": "江苏", "id": "12" }, { "name": "山东", "id": "13" }, { "name": "安徽", "id": "14" }, { "name": "浙江", "id": "15" }, { "name": "福建", "id": "16" }, { "name": "湖北", "id": "17" }, { "name": "湖南", "id": "18" }, { "name": "广东", "id": "19" }, { "name": "广西", "id": "20" }, { "name": "江西", "id": "21" }, { "name": "四川", "id": "22" }, { "name": "海南", "id": "23" }, { "name": "贵州", "id": "24" }, { "name": "云南", "id": "25" }, { "name": "西藏", "id": "26" }, { "name": "陕西", "id": "27" }, { "name": "甘肃", "id": "28" }, { "name": "青海", "id": "29" }, { "name": "宁夏", "id": "30" }, { "name": "新疆", "id": "31" }, { "name": "台湾", "id": "32" }, { "name": "钓鱼岛", "id": "84" }, { "name": "港澳", "id": "52993" }] });
};

/**
 * 修改地址或者添加新地址
 * @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: '输入的邮箱地址格式不正确'
    //     });
    // }

    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) => {
    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) => {
    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: '选择地址'
    };

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

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