students.js 5.1 KB
/**
 * 学生营销数据处理
 * @date: 2016-08-06 14:24:04
 * @author: name<emial@yoho.cn>
 */

'use strict';

// const utils = '../../../utils';

// const logger = global.yoho.logger;
// const camelCase = global.yoho.camelCase;
const api = global.yoho.API;
const queryString = require('querystring');
const _ = require('lodash');

const studentsApi = require('./students-api');
const stuHandler = require('./students-handler');
const helpers = global.yoho.helpers;
const crypto = global.yoho.crypto;

// const productProcess = require(`${utils}/product-process`);
// const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header');
const serviceApi = global.yoho.ServiceAPI;

// 学生营销资源码
const studentsCode = '989396a17443bf61e3e269702e51ef04'; // h5 a83b7d55324fb65f96c1f85a3387ebd8
// const studentsCode = 'a83b7d55324fb65f96c1f85a3387ebd8'; // h5 a83b7d55324fb65f96c1f85a3387ebd8

/**
 * 获取学生营销页面数据
 * @params channel Object 频道
 * @return name returnType 返回值描述
 */
exports.getStudentsData = (channel, req) => {
    let apiMethod = [
        headerModel.requestHeaderData(channel),

        // 资源位数据
        serviceApi.get('operations/api/v5/resource/get', {content_code: studentsCode}),

        // 认证总数
        studentsApi.getVerifiedTotal(),

        // 商品数据
        studentsApi.getStuProducts()
    ];

    if (req.user.uid && req.query.collegeName && req.query.educationDegree &&
        req.query.enrollmentYear && req.query.sign) {
        apiMethod.push(
            studentsApi.verifyStudent(req.user.uid, req.query.collegeName,
                req.query.educationDegree, req.query.enrollmentYear)
        );
    }
    return api.all(apiMethod).then(result => {
        let responseData = {
            module: 'product',
            page: 'students',

            // 头部数据
            headerData: result[0],

            realData: {
                sortItem: [{title: '学生权益介绍', href: 'stu-rights'}, {title: '我要验证身份', href: 'stu-identity'},
                {title: '学生热门单品', href: 'stu-good'}, {title: '更多活动推荐', href: 'stu-activity'}],
                identityItem: {
                    stuNum: []
                }
            },

            footerTop: true
        };

        // 资源位数据
        if (result[1].code === 200) {
            Object.assign(responseData.realData, stuHandler.studentResource(result[1].data));
        }

        if (result[2].code === 200) {
            responseData.realData.identityItem.stuNum = stuHandler.studentsNum(result[2].data);
        }

        if (result[3].code === 200) {
            Object.assign(responseData.realData, {proItem: stuHandler.stuProducts(result[3].data.product_list)});
        }

        if (result[4]) {
            Object.assign(responseData.realData, {verifyRusult: stuHandler.stuVerify(result[4])});
        }

        return responseData;
    });
};

/**
 * 获取学校地区数据
 * @return Object 接口数据
 */
exports.getSchoolArea = () => {
    return studentsApi.getArea().then(result => {
        return result;
    });
};

/**
 * 根据地区码查询学校列表
 * @params areaCode int 地区码
 * @return Object 接口数据
 */
exports.getSchoolList = (areaCode) => {
    return studentsApi.getSchool(areaCode).then(result => {
        return result;
    });
};

/**
 * 学历层次
 * @params areaCode int 地区码
 * @return Object 接口数据
 */
exports.getEduLevel = () => {
    return studentsApi.getEduLevelList().then(result => {
        return result;
    });
};

/**
 * 身份验证
 * @params areaCode int 地区码
 * @return Object 接口数据
 */
exports.verifyIdentity = (uid, params) => {
    let pageUrl = helpers.urlFormat('/product/students?') +
    queryString.stringify({collegeName: params.collegeName,
        educationDegree: params.educationDegree, enrollmentYear: params.enrollmentYear}) + '&';

    return studentsApi.verifyIdentity(uid, params.certNo, params.name, pageUrl).then(result => {
        return result;
    });
};

/**
 * 学生验证
 * @params areaCode int 地区码
 * @return Object 接口数据
 */
exports.verifyStudents = (params) => {
    return studentsApi.verifyStudent(params.uid, params.collegeName, params.educationDegree, params.enrollmentYear).
    then(result => {
        return result;
    });
};

/**
 * 获取优惠券领取状态
 * @params
 * @return
 */
exports.userAcquireStatus = (uid, couponIds) => {
    let ids = '';

    for (let i = 0; i < couponIds.length; i++) {
        if (i === couponIds.length - 1) {
            ids += crypto.decrypt('yoho9646abcdefgh', couponIds[i]);
        } else {
            ids += crypto.decrypt('yoho9646abcdefgh', couponIds[i]) + ',';
        }
    }

    return studentsApi.userAcquireStatus(uid, ids).
    then(result => {
        if (result.code === 200) {
            _.forEach(result.data, (value) => {
                let couponId = value.couponId.toString(),
                    cryptoId = crypto.encryption('yoho9646abcdefgh', couponId);

                value.couponId = cryptoId;
            });
        }
        return result;
    });
};