Blame view

apps/shop/controllers/shop.js 5.16 KB
htoooth authored
1
'use strict';
zhangxiaoru authored
2 3 4 5 6
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const shopService = require(`${mRoot}/shop-service`);
const helpers = global.yoho.helpers;
const _ = require('lodash');
zhangxiaoru authored
7 8
const authcode = require(`${global.utils}/authcode`);
zhangxiaoru authored
9
const base64StrEncode = ($input) => {
zhangxiaoru authored
10
    return $input.replace('+/=', '-_.');
zhangxiaoru authored
11
};
htoooth authored
12
13 14 15
/**
 * 入口页
 */
htoooth authored
16
const index = (req, res, next) => {
zhangxiaoru authored
17 18 19 20 21 22 23
    let channel = req.cookies._Channel || 'boys';
    let responseData = {
        module: 'shop',
        page: 'shop',
        footerTop: true
    };
yyq authored
24
    req.ctx(headerModel).requestHeaderData(channel).then(result => {
zhangxiaoru authored
25 26 27 28
        responseData.headerData = result.headerData;

        res.render('settled', responseData);
    }).catch(next);
htoooth authored
29 30
};
31 32 33
/**
 * 申请页
 */
zhangxiaoru authored
34 35 36 37
const apply = (req, res, next) => {
    let channel = req.cookies._Channel || 'boys';
    let uid = req.user.uid;
    let loginUrl;
yyq authored
38
    let adpic = {_project: 'adpic', format_str: []};
zhangxiaoru authored
39 40 41 42

    if (!uid) {

        loginUrl = helpers.urlFormat('/signin.html', {
43
            refer: helpers.urlFormat('/settled/apply', null, 'shop')
zhangxiaoru authored
44 45
        });
周少峰 authored
46
        return res.redirect(loginUrl);
zhangxiaoru authored
47 48 49 50 51 52 53 54
    }

    let responseData = {
        module: 'shop',
        page: 'shop',
        footerTop: true
    };
yyq authored
55 56
    Promise.all([req.ctx(headerModel).requestHeaderData(channel),
        req.ctx(shopService).applyAction()]).then(result => {
zhangxiaoru authored
57 58
        responseData.headerData = result[0].headerData;
        responseData.oneCategory = result[1];
yyq authored
59 60
        responseData.uploadKey = base64StrEncode(authcode(JSON.stringify(adpic),
            'yohobuy_upload_system', null, 'encode'));
zhangxiaoru authored
61 62 63 64
        res.render('apply', responseData);
    }).catch(next);
};
65 66 67
/**
 * 获取二级品类
 */
zhangxiaoru authored
68 69 70 71 72 73 74 75 76 77 78
const getTwoCategoryAction = (req, res, next) => {
    let id = req.query.id;

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

        return data;
    }
王水玲 authored
79
    req.ctx(shopService).getTwoCategory(id).then(result => {
zhangxiaoru authored
80 81 82 83
        res.json(result);
    }).catch(next);
};
84 85 86
/**
 * 提交申请
 */
zhangxiaoru authored
87 88 89 90 91 92 93
const applysave = (req, res, next) => {
    let uid = req.user.uid,
        param = {},
        data = {},
        categoryInfo = [],
        storeInfo = [];
zhangxiaoru authored
94 95
    try {
        for (let key in req.body) {
zhangxiaoru authored
96
            if (req.body[key]) {
郝肖肖 authored
97 98 99
                key = key + '';
                param[_.camelCase(key)] = req.body[key];
            }
yyq authored
100
        }
zhangxiaoru authored
101 102
    } catch (e) {
        console.log(e);
zhangxiaoru authored
103 104 105 106 107 108
    }

    if (param.sellerRole === '') {
        param.sellerRole = param.otherSellerRole;
    }
zhangxiaoru authored
109 110 111 112
    if (param.supplyCycle === '') {
        param.supplyCycle = param.ortherSupplyCycle;
    }
zhangxiaoru authored
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    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]
            });
        });
yyq authored
137 138
    } else if (param.categoryOne && param.categoryOne !== '' &&
        param.categoryTwo !== '' && param.categoryPrice !== '') {
zhangxiaoru authored
139 140 141 142 143
        categoryInfo.push({
            category_one: param.categoryOne,
            category_two: param.categoryTwo,
            category_price: param.categoryPrice
        });
zhangxiaoru authored
144 145 146 147 148 149
    }

    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);
yyq authored
150
    param.warehouseAddress = param.warehouseProvince + ' ' + param.warehouseCity;
zhangxiaoru authored
151 152 153 154 155 156 157 158 159 160 161 162 163 164

    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,
zhangxiaoru authored
165
                store_sales_volume: param.storeSalesVolume[key]
zhangxiaoru authored
166 167
            });
        });
zhangxiaoru authored
168 169 170 171 172
    } else if (param.haveStore && param.storeAddress !== '' && param.storeSalesVolume !== '') {
        storeInfo.push({
            store_address: param.storeAddress,
            store_sales_volume: param.storeSalesVolume
        });
zhangxiaoru authored
173 174 175 176 177
    }

    param.storeInfo = JSON.stringify(storeInfo);
    param.uid = uid;
王水玲 authored
178
    req.ctx(shopService).insertApply(param).then(result => {
zhangxiaoru authored
179 180 181
        res.json(result);
    }).catch(next);
};
htoooth authored
182 183

module.exports = {
zhangxiaoru authored
184 185 186 187
    index,
    apply,
    getTwoCategoryAction,
    applysave
htoooth authored
188
};