coupon.js 3.31 KB
var API = require('../../../library/api').API;
var api = new API();
const library = '../../../library';
const sign = require(`${library}/sign`);

/**
 * 分享页面基础参数
 * @param  {object} sizeInfo [接口原始数据]
 * @return {object}          [description]
 */
const getPageInfo = (pageInfo) => {
    var dest = {};
    dest.shareTitle = pageInfo.data.shareTitle;
    dest.shareDesc = pageInfo.data.shareContent;
    dest.shareImg = pageInfo.data.shareImgUrl;
    dest.shareLink = pageInfo.data.shareUrl;
    dest.code = pageInfo.code;
    dest.activityID = pageInfo.id;
    dest.title = pageInfo.data.h5Title;
    dest.activityDesc = pageInfo.data.activityDesc;
    dest.couponPic = pageInfo.data.couponPic;
    dest.oldUserCouponPic = pageInfo.data.oldUserCouponPic;
    dest.mobile = pageInfo.data.mobile;
    //强制活动开始,活动上线产品要求这样设置
    pageInfo.data.flag = 1;
    if (pageInfo.data.flag === 1) {
        dest.bgImg = pageInfo.data.activityNormalPic;
    } else {
        dest.bgImg = pageInfo.data.activityEndPic;
        dest.ended = true;
    }
    dest.message = pageInfo.data.returnMsg;

    // 清空变量,释放内存
    pageInfo = {};
    return dest;
};

const getUserStatus = (param) => {
    var dest = {};

    dest.code = param.code;
    dest.title = param.data.h5Title;
    dest.returnCode = param.data.returnCode;
    dest.mobile = param.data.mobile;
    if (param.data.returnCode === 0) {
        dest.geted = true;
    } else if (param.data.returnCode === 1) {
        dest.wrongNumb = true;
    } else if (param.data.returnCode === 2) {
        dest.newUser = true;
    } else if (param.data.returnCode === 8) {
        dest.oldUserAskCouponOnceMore = true;
    } else if (param.data.returnCode === 5) {
        dest.vertifyWrong = true;
    } else {
        dest.ended = true;
    }
    if (param.data.newUser === 1) {
        dest.newUser = true;
    }
    dest.message = param.data.returnMsg;

    // 清空变量,释放内存
    param = {};
    return dest;
};


/**
 * 获取分享页面数据
 */
exports.getPageInfo = (data) => {
    var defaultParam = {

            method: 'app.activity.getInfoOfOrderShare'
        },

    // 处理完成后,发给后端
        infoData = Object.assign(defaultParam, data);
    return api.get('', sign.apiSign(infoData)).then(result => {
        return getPageInfo(result);
    }); // 所有数据返回一个 Promise,方便 Promise.all 调用
};

/**
 * 输入手机号领券,新用户返回验证码
 */
exports.getCoupon = (data) => {
    var defaultParam = {
            method: 'wap.order.drawOrderShareCoupon'
        },

    // 处理完成后,发给后端
        phoneData = Object.assign(defaultParam, data);

    return api.get('', sign.apiSign(phoneData)).then(result => {
        return getUserStatus(result);
    }); // 所有数据返回一个 Promise,方便 Promise.all 调用
};


/**
 * 验证注册码进行注册并发券
 */
exports.registerAndSendCoupon = (data) => {
    var defaultParam = {
            method: 'wap.order.registerAndSendCoupon'
        },

    // 处理完成后,发给后端
        verifyData = Object.assign(defaultParam, data);

    return api.get('', sign.apiSign(verifyData)).then(result => {
        return getUserStatus(result);
    });  // 所有数据返回一个 Promise,方便 Promise.all 调用
};