shop.js 5.16 KB
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const shopService = require(`${mRoot}/shop-service`);
const helpers = global.yoho.helpers;
const _ = require('lodash');
const authcode = require(`${global.utils}/authcode`);

const base64StrEncode = ($input) => {
    return $input.replace('+/=', '-_.');
};

/**
 * 入口页
 */
const index = (req, res, next) => {
    let channel = req.cookies._Channel || 'boys';
    let responseData = {
        module: 'shop',
        page: 'shop',
        footerTop: true
    };

    req.ctx(headerModel).requestHeaderData(channel).then(result => {
        responseData.headerData = result.headerData;

        res.render('settled', responseData);
    }).catch(next);
};

/**
 * 申请页
 */
const apply = (req, res, next) => {
    let channel = req.cookies._Channel || 'boys';
    let uid = req.user.uid;
    let loginUrl;
    let adpic = {_project: 'adpic', format_str: []};

    if (!uid) {

        loginUrl = helpers.urlFormat('/signin.html', {
            refer: helpers.urlFormat('/settled/apply', null, 'shop')
        });

        return res.redirect(loginUrl);
    }

    let responseData = {
        module: 'shop',
        page: 'shop',
        footerTop: true
    };

    Promise.all([req.ctx(headerModel).requestHeaderData(channel),
        req.ctx(shopService).applyAction()]).then(result => {
        responseData.headerData = result[0].headerData;
        responseData.oneCategory = result[1];
        responseData.uploadKey = base64StrEncode(authcode(JSON.stringify(adpic),
            'yohobuy_upload_system', null, 'encode'));
        res.render('apply', responseData);
    }).catch(next);
};

/**
 * 获取二级品类
 */
const getTwoCategoryAction = (req, res, next) => {
    let id = req.query.id;

    if (id < 1) {
        let data = {
            message: 'id不能为空'
        };

        return data;
    }

    req.ctx(shopService).getTwoCategory(id).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 提交申请
 */
const applysave = (req, res, next) => {
    let uid = req.user.uid,
        param = {},
        data = {},
        categoryInfo = [],
        storeInfo = [];

    try {
        for (let key in req.body) {
            if (req.body[key]) {
                key = key + '';
                param[_.camelCase(key)] = req.body[key];
            }
        }
    } catch (e) {
        console.log(e);
    }

    if (param.sellerRole === '') {
        param.sellerRole = param.otherSellerRole;
    }

    if (param.supplyCycle === '') {
        param.supplyCycle = param.ortherSupplyCycle;
    }

    if (!param.brandName || param.brandName === '' || !param.registerStatus || param.registerStatus === '' ||
        !param.sellerName || param.sellerName === '' || !param.zipCode || param.zipCode === '' ||
        !param.contacts || param.contacts === '' || !param.contactPhone || param.contactPhone === '' ||
        !param.contactEmail || param.contactEmail === '' || !param.sellerRole || param.sellerRole === '') {
        data.message = '请输入必填信息';
        data.code = 400;

        return data;
    }

    if (param.categoryOne && _.isArray(param.categoryOne) && _.isArray(param.categoryTwo) &&
        _.isArray(param. categoryPrice)) {
        _.forEach(param.categoryOne, function(val, key) {

            if (val === '') {
                return;
            }

            categoryInfo.push({
                category_one: val,
                category_two: param.categoryOne[key],
                category_price: param.categoryPrice[key]
            });
        });
    } else if (param.categoryOne && param.categoryOne !== '' &&
        param.categoryTwo !== '' && param.categoryPrice !== '') {
        categoryInfo.push({
            category_one: param.categoryOne,
            category_two: param.categoryTwo,
            category_price: param.categoryPrice
        });
    }

    param.categoryInfo = JSON.stringify(categoryInfo);
    param.producer = param.originCountry + ' ' + param.originProvince + ' ' + param.originCity;
    param.quarterNum = parseInt(param.quarterNum, 10);
    param.haveStore = parseInt(param.haveStore, 10);
    param.warehouseAddress = param.warehouseProvince + ' ' + param.warehouseCity;

    if (param.newCycle === '') {
        param.newCycle = param.ortherNewCycle;
    }

    if (param.haveStore && _.isArray(param.storeAddress) && _.isArray(param.storeSalesVolume)) {
        _.forEach(param.storeAddress, function(val, key) {

            if (val === '') {
                return;
            }

            storeInfo.push({
                store_address: val,
                store_sales_volume: param.storeSalesVolume[key]
            });
        });
    } else if (param.haveStore && param.storeAddress !== '' && param.storeSalesVolume !== '') {
        storeInfo.push({
            store_address: param.storeAddress,
            store_sales_volume: param.storeSalesVolume
        });
    }

    param.storeInfo = JSON.stringify(storeInfo);
    param.uid = uid;

    req.ctx(shopService).insertApply(param).then(result => {
        res.json(result);
    }).catch(next);
};

module.exports = {
    index,
    apply,
    getTwoCategoryAction,
    applysave
};