|
|
// 领券中心 by acgpiano 16-9-19
|
|
|
'use strict';
|
|
|
|
|
|
const Promise = require('bluebird');
|
|
|
const api = global.yoho.API;
|
|
|
const _ = require('lodash');
|
|
|
const helpers = global.yoho.helpers;
|
|
|
const crypto = global.yoho.crypto;
|
|
|
|
|
|
const SUB_DOMAIN = '.dev.yohobuy.com',
|
|
|
OLD_MAIN = '//m.yohobuy.com',
|
|
|
SITE_MAIN = '//m.dev.yohobuy.com';
|
|
|
|
|
|
/**
|
|
|
* 仿php的strrpos
|
|
|
*/
|
|
|
const strrpos = (str1, str2) => {
|
|
|
return str1.indexOf(str2) > -1 ? str1.indexOf(str2) : false;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 仿php的strstr
|
|
|
*/
|
|
|
const strstr = (str1, str2, bfsearch) => {
|
|
|
if (str1.indexOf(str2) > -1) {
|
|
|
if (bfsearch) {
|
|
|
return str1.substring(0, str1.indexOf(str2));
|
|
|
} else {
|
|
|
return str1.substr(str1.indexOf(str2));
|
|
|
}
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const rtrim = (str1, str2) => {
|
|
|
return str1[str1.length - 1] === str2 ? str1.substr(0, str1.length - 1) : str1;
|
|
|
};
|
|
|
|
|
|
const transHttpsUrl = url => {
|
|
|
return url.replace(/^\/\//, 'http://');
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* app内的分享按钮
|
|
|
*/
|
|
|
const getShare = (code, shareTitle, shareDesc, shareImg) => {
|
|
|
return {
|
|
|
shareLink: helpers.urlFormat('/coupon/floor', { code: code }, ''),
|
|
|
shareTitle: shareTitle || '',
|
|
|
shareDesc: shareDesc || '',
|
|
|
shareImg: shareImg || '',
|
|
|
hasWxShare: true,
|
|
|
};
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 过滤app的url
|
|
|
*/
|
|
|
const _getFilterUrl = url => {
|
|
|
url = url.replace('.m.yohobuy.com', SUB_DOMAIN).replace(OLD_MAIN, SITE_MAIN).replace('www.yohobuy.com', SITE_MAIN);
|
|
|
if (strrpos(url, 'm.yohobuy.com') && !strrpos(url, 'sale.m.yohobuy.com') && !strrpos(url, 'cuxiao.m.yohobuy.com') &&
|
|
|
!strrpos(url, 'activity.m.yohobuy.com') && !strrpos(url, 'huodong.m.yohobuy.com') &&
|
|
|
strrpos(url, 'cdn.yoho.cn/myohobuy') && !strrpos(url, '/home/orders/pay')) {
|
|
|
url = url.replace('http://', '//');
|
|
|
}
|
|
|
|
|
|
if (strrpos(url, 'feature.yoho.cn')) {
|
|
|
url = transHttpsUrl(url);
|
|
|
}
|
|
|
|
|
|
let filter = strstr(url, 'openby:yohobuy=', true);
|
|
|
|
|
|
if (filter) {
|
|
|
return rtrim(rtrim(filter, '?'), '&');
|
|
|
} else {
|
|
|
return url;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获得的数据处理
|
|
|
*/
|
|
|
const processFun = {
|
|
|
carousel_banner(data) {
|
|
|
if (!data.list || !data.list.length) {
|
|
|
return [];
|
|
|
}
|
|
|
for (let item of data.list) {
|
|
|
item.img = helpers.image(item.src, 0, 0);
|
|
|
if (typeof item.url !== 'string') {
|
|
|
item.url = '';
|
|
|
}
|
|
|
}
|
|
|
data.isCarouselBanner = true;
|
|
|
return data;
|
|
|
},
|
|
|
|
|
|
getCoupon(data, isApp) {
|
|
|
let result = [];
|
|
|
|
|
|
if (!data.length) {
|
|
|
return [];
|
|
|
}
|
|
|
let floorTitle = '',
|
|
|
item = data[0],
|
|
|
imageSrc = '';
|
|
|
|
|
|
for (let key in item) {
|
|
|
if (key === 'floorTitle') {
|
|
|
floorTitle = (_.has(item[key], 'text') && (item[key].text !== '')) ? item[key].text : '';
|
|
|
continue;
|
|
|
}
|
|
|
if (!item.encrypt) {
|
|
|
item.couponID = crypto.encryption('yoho9646abcdefgh', item.couponID);
|
|
|
item.encrypt = true;
|
|
|
}
|
|
|
imageSrc = helpers.image(item.image.src, 0, 0);
|
|
|
item.image.src = imageSrc;
|
|
|
|
|
|
item.image.url = isApp ? item.image.url : _getFilterUrl(item.image.url);
|
|
|
switch (item.status) {
|
|
|
case '1':
|
|
|
item.isGet = true;
|
|
|
break;
|
|
|
case '2':
|
|
|
item.isZero = true;
|
|
|
break;
|
|
|
case '3':
|
|
|
item.isGeted = true;
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if (floorTitle !== '') {
|
|
|
data[0].floorTitle = floorTitle;
|
|
|
data[0].showFloorTitle = true;
|
|
|
}
|
|
|
result = data[0];
|
|
|
result.isCoupon = true;
|
|
|
return result;
|
|
|
},
|
|
|
|
|
|
text(data) {
|
|
|
data.isTitle = true;
|
|
|
if (data.text) {
|
|
|
data.isShow = true;
|
|
|
}
|
|
|
return data;
|
|
|
},
|
|
|
|
|
|
single_image(data, isApp) {
|
|
|
data[0].isSingleImage = true;
|
|
|
data[0].url = isApp ? data[0].url : _getFilterUrl(data[0].url);
|
|
|
return data[0];
|
|
|
},
|
|
|
|
|
|
focus(data, isApp) {
|
|
|
let result = {};
|
|
|
|
|
|
for (let item of data) {
|
|
|
item.url = isApp ? item.url : _getFilterUrl(item.url);
|
|
|
}
|
|
|
result.isFocus = true;
|
|
|
result.data = data;
|
|
|
return result;
|
|
|
},
|
|
|
|
|
|
image_list(data, isApp) {
|
|
|
let result = {},
|
|
|
num,
|
|
|
width;
|
|
|
|
|
|
for (let image of data.list) {
|
|
|
num = data.title.column_num;
|
|
|
if (num === 0) {
|
|
|
image.src = image.src.replace('?imageView/{mode}/w/{width}/h/{height}', '');
|
|
|
} else {
|
|
|
if (640 % num === 0) {
|
|
|
width = parseInt(640 / num, 10);
|
|
|
width = !width ? 640 : width;
|
|
|
image.src = image.src.replace('?imageView/{mode}/w/{width}/h/{height}', '?imageView2/2/w/' + width);
|
|
|
image.src = helpers.image(image.src, width, 0);
|
|
|
} else {
|
|
|
image.src = image.src.replace('?imageView/{mode}/w/{width}/h/{height}', '');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (isApp) {
|
|
|
image.url = !image.url ? 'javascript:void(0);' : image.url;
|
|
|
} else {
|
|
|
image.url = !image.url ? 'javascript:void(0);' : _getFilterUrl(image.url);
|
|
|
}
|
|
|
}
|
|
|
result.isImageList = true;
|
|
|
result.imageList = {
|
|
|
col: data.title.column_num,
|
|
|
title: data.title.title,
|
|
|
list: data.list,
|
|
|
};
|
|
|
return result;
|
|
|
},
|
|
|
};
|
|
|
|
|
|
const _getContent = (data, isApp) => {
|
|
|
let result = [],
|
|
|
build = [];
|
|
|
|
|
|
const TEMPLATE_LIST = ['single_image', 'focus'];
|
|
|
|
|
|
if (!data) {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
for (let i = 0, n = data.length; i < n; i++) {
|
|
|
|
|
|
let fun = '';
|
|
|
|
|
|
if (!data[i] || typeof data[i] !== 'object' || !_.has(data[i], 'template_name')) {
|
|
|
if (data[i].templateName) {
|
|
|
fun = data[i].templateName;
|
|
|
} else {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
if (TEMPLATE_LIST.indexOf(fun) === -1) {
|
|
|
fun = data[i].template_name;
|
|
|
}
|
|
|
if (!data[i].data || !_.has(processFun, fun)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// tar note 处理楼层标题
|
|
|
if (fun === 'getCoupon' && _.has(data[i - 1], 'template_name') && data[i - 1].template_name === 'text') {
|
|
|
data[i].data[0].floorTitle = data[i - 1].data;
|
|
|
}
|
|
|
build = processFun[fun](data[i].data, isApp);
|
|
|
if (!build) {
|
|
|
continue;
|
|
|
}
|
|
|
result.push(build);
|
|
|
}
|
|
|
build = [];
|
|
|
return result;
|
|
|
};
|
|
|
|
|
|
exports.floor = (params, isApp) => {
|
|
|
return Promise.coroutine(function*() {
|
|
|
let result = {},
|
|
|
resource = yield api.get('', Object.assign(params, {
|
|
|
method: 'app.promotion.queryCouponCenter',
|
|
|
}));
|
|
|
|
|
|
if (resource && resource.code === 200) {
|
|
|
result = _getContent(resource.data, isApp);
|
|
|
} else {
|
|
|
result.noData = true;
|
|
|
}
|
|
|
|
|
|
result.share = getShare(params.contentCode, '领券中心');
|
|
|
return result;
|
|
|
})();
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 前端ajax领券
|
|
|
*/
|
|
|
exports.receiveCoupon = (receiveData, isApp, uid) => {
|
|
|
let returnData = {};
|
|
|
|
|
|
// 获取优惠券 ID
|
|
|
if (receiveData.couponID) {
|
|
|
receiveData.couponID = crypto.decrypt('yoho9646abcdefgh', receiveData.couponID);
|
|
|
}
|
|
|
|
|
|
// 登录后调用领券接口
|
|
|
return api.get('', {
|
|
|
method: 'app.promotion.getCoupon',
|
|
|
couponId: receiveData.couponID,
|
|
|
uid: uid,
|
|
|
}).then(result => {
|
|
|
switch (result.code) {
|
|
|
case 200:
|
|
|
returnData = {
|
|
|
msg: '领券成功!',
|
|
|
status: true,
|
|
|
};
|
|
|
break;
|
|
|
case 401:
|
|
|
returnData = {
|
|
|
code: 401,
|
|
|
message: '您已领取过优惠券'
|
|
|
};
|
|
|
break;
|
|
|
case 315:
|
|
|
returnData = {
|
|
|
code: 315,
|
|
|
message: '优惠券已过期'
|
|
|
};
|
|
|
break;
|
|
|
default:
|
|
|
returnData = {
|
|
|
msg: '领券失败!',
|
|
|
status: false,
|
|
|
};
|
|
|
break;
|
|
|
}
|
|
|
return returnData;
|
|
|
});
|
|
|
}; |
...
|
...
|
|