address.js 2.05 KB
/**
 * 个人中心---地址管理
 * @author gaohongwei <hongwei.gao@yoho.cn>
 * @date: 2016/9/5
 */
'use strict';

const mRoot = '../models';
const addressService = require(`${mRoot}/address-service`); // user  model
const helpers = global.yoho.helpers;

/**
 * 地址管理列表
 */
exports.index = (req, res, next) => {
    if (!req.user.uid) {
        res.redirect(helpers.urlFormat('/signin.html'));
    }
    let uid = req.user.uid;

    let responseData = {
        module: 'home',
        page: 'address'
    };

    // 真实数据输出
    addressService.getAddressInfo(uid).then(result => {
        responseData.meAddressPage = true;
        responseData.address = result.address;
        res.render('home/address/address', responseData);
    }).catch(next);
};

 /**
 * 编辑修改地址
 */
exports.editAddress = (req, res, next) => {
    if (!req.user.uid) {
        res.redirect(helpers.urlFormat('/signin.html'));
    }
    let uid = req.user.uid;

    // 真实数据输出
    addressService.editAddress(req.query, uid).then(result => {
        res.json(result);
    }).catch(next);
};

 /**
 * 添加保存地址
 */
exports.saveAddress = (req, res, next) => {
    if (!req.user.uid) {
        res.redirect(helpers.urlFormat('/signin.html'));
    }
    let uid = req.user.uid;

    // 真实数据输出
    addressService.saveAddress(req.body, uid).then(result => {
        res.json(result);
    }).catch(next);
};

 /**
 * 删除地址
 */
exports.delAddress = (req, res, next) => {
    if (!req.user.uid) {
        res.redirect(helpers.urlFormat('/signin.html'));
    }
    let uid = req.user.uid;

    // 真实数据输出
    addressService.delAddress(req.query, uid).then(result => {
        res.json(result);
    }).catch(next);
};

 /**
 * 设置默认地址
 */
exports.defaultAddress = (req, res, next) => {
    if (!req.user.uid) {
        res.redirect(helpers.urlFormat('/signin.html'));
    }
    let uid = req.user.uid;

    // 真实数据输出
    addressService.defaultAddress(req.query, uid).then(result => {
        res.json(result);
    }).catch(next);
};