address.js 3.3 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 || 8039759 // TODO 待删除
    }).then(result => {
        res.render('address/index', Object.assign(responseData, result));
    }).catch(next);

};

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

    addressModel.address({
        id: req.params.id,
        uid: req.user.uid || 8039759 // TODO 待删除
    }).then(result => {
        res.render('address/address-act', Object.assign(responseData, result));
    }).catch(next);
};

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

    // TODO 开发完成后放开
    // 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: '请输入收件人姓名'
        });
    }

    // TODO 开发完成后放开
    // if (!req.body.email || !helpers.verifyEmail(req.body.email)) {
    //     res.json({
    //         code: 404,
    //         message: '输入的邮箱地址格式不正确'
    //     });
    // }

    addressModel.saveAddress({
        uid: req.user.uid || 8039759, // TODO 待删除
        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 || 8039759 // TODO 待删除
    }).then(result => {
        res.json(result);
    }).catch(next);
};

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