students-api.js 4.55 KB
/**
 * 学生营销接口
 * @date: 2016-08-06 14:24:04
 * @author: name<emial@yoho.cn>
 */
'use strict';

const config = global.yoho.config;
const SearchApiModel = require('./search-api');
const cache = global.yoho.cache;
const logger = global.yoho.logger;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
        this.searchApi = new SearchApiModel(ctx);
    }

    /**
     * 获取完成认证学生总数
     * @return
     */
    getVerifiedTotal() {

        return this.get({data: {method: 'app.student.verifiedStudentTotal'}, param: config.apiCache});
    }

    /**
     * 获取学历层次列表
     * @return
     */
    getEduLevelList() {

        return this.get({data: {method: 'app.studentMarket.getEducationLevelList'}, param: config.apiCache});
    }

    /**
     * 获取学校地区
     * @return
     */
    getArea() {
        let finalParams = {
            method: 'app.studentMarket.getAddressList'
        };

        return this.get({data: finalParams, param: config.apiCache});
    }

    /**
     * 获取学校列表
     * @return
     */
    getSchool(areaCode) {
        let finalParams = {
            method: 'app.studentMarket.getSchoolInfoList',
            areaCode: areaCode
        };

        return this.get({data: finalParams, param: config.apiCache});
    }

    /**
     * 学生专项商品
     * @return
     */
    getStuProducts(params) {
        let finalParams = {
            method: 'app.student.vip',
            order: 's_t_desc',
            limit: 15
        };

        Object.assign(finalParams, params);
        return this.get({data: finalParams, param: config.apiCache});
    }

    /**
     * 身份验证
     * @return
     */
    verifyIdentity(uid, certNo, name, pageUrl) {
        let finalParams = {
            method: 'app.student.verifyIdentity',
            uid: uid,
            cert_no: certNo,
            name: name,
            page_url: pageUrl
        };

        return this.get({data: finalParams, param: config.apiCache});
    }

    /**
     * 学籍验证
     * @return
     */
    verifyStudent(uid, collegeName, educationDegree, enrollmentYear) {
        let finalParams = {
            method: 'app.student.verifyStudent',
            uid: uid,
            client_type: 'web',
            college_name: collegeName,
            education_degree: educationDegree,
            enrollment_year: enrollmentYear
        };

        return this.get({data: finalParams, param: config.apiCache});
    }

    /**
     * 获取优惠券领取状态
     * @return
     */
    userAcquireStatus(uid, couponIds) {

        let finalParams = {
            method: 'app.coupons.userAcquireStatus',
            couponIds: couponIds
        };

        if (uid !== '') {
            finalParams.uid = uid;
        }

        return this.get({data: finalParams, param: config.apiCache});
    }


    /**
     * 获取学生专享商品列表
     * @return
     */
    getStudentsProduct(params) {
        let finalParams = {
            method: 'app.student.rebate',
            sales: 'Y',
            stocknumber: 1,
            need_filter: 'yes',
            limit: 60
        };

        Object.assign(finalParams, params);
        if (!config.useCache) {
            return this.searchApi.getProductListOrig(finalParams);
        } else {
            let cKey = this.searchApi.getSearchCacheKey(finalParams);

            return cache.get(cKey).catch().then(cdata => {
                let hasCache = false;

                if (cdata) {

                    try {
                        cdata = JSON.parse(cdata);
                    } catch (e) {
                        logger.debug('getProductList cache data parse fail.');
                    }

                    if (cdata.filter && cdata.standard) {
                        hasCache = true;
                        finalParams.need_filter = 'no';
                    }
                }

                return this.searchApi.getProductListOrig(finalParams).then(result => {
                    if (hasCache && result && result.data) {
                        Object.assign(result.data, cdata);
                    } else {
                        if (result && result.data && result.data.filter) {
                            cache.set(cKey, Object.assign({}, {
                                filter: result.data.filter,
                                standard: result.data.standard
                            }), 3600);
                        }
                    }

                    return result;
                });
            });
        }
    }
};