|
|
// 领券中心 by acgpiano 16-9-19
|
|
|
'use strict';
|
|
|
|
|
|
const Promise = require('bluebird');
|
...
|
...
|
@@ -6,8 +7,82 @@ 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, isApp) {
|
|
|
carousel_banner(data) {
|
|
|
if (!data.list || !data.list.length) {
|
|
|
return [];
|
|
|
}
|
...
|
...
|
@@ -43,8 +118,7 @@ const processFun = { |
|
|
imageSrc = helpers.image(item.image.src, 0, 0);
|
|
|
item.image.src = imageSrc;
|
|
|
|
|
|
// 貌似不需要
|
|
|
// item.image.url = isApp ? item.image.url : _getFilterUrl(item.image.url);
|
|
|
item.image.url = isApp ? item.image.url : _getFilterUrl(item.image.url);
|
|
|
switch (item.status) {
|
|
|
case '1':
|
|
|
item.isGet = true;
|
...
|
...
|
@@ -68,7 +142,7 @@ const processFun = { |
|
|
return result;
|
|
|
},
|
|
|
|
|
|
text(data, isApp) {
|
|
|
text(data) {
|
|
|
data.isTitle = true;
|
|
|
if (data.text) {
|
|
|
data.isShow = true;
|
...
|
...
|
@@ -78,12 +152,16 @@ const processFun = { |
|
|
|
|
|
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;
|
...
|
...
|
@@ -108,13 +186,12 @@ const processFun = { |
|
|
image.src = image.src.replace('?imageView/{mode}/w/{width}/h/{height}', '');
|
|
|
}
|
|
|
}
|
|
|
image.url = !image.url ? 'javascript:void(0);' : image.url;
|
|
|
|
|
|
// if(isApp){
|
|
|
// image.url = !image.url ? 'javascript:void(0);' : image.url;
|
|
|
// }else{
|
|
|
// image.url = !image.url ? 'javascript:void(0);' : Helpers::getFilterUrl(image['url']);
|
|
|
// }
|
|
|
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 = {
|
...
|
...
|
@@ -124,9 +201,6 @@ const processFun = { |
|
|
};
|
|
|
return result;
|
|
|
},
|
|
|
promotion() {
|
|
|
console.log(7);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const _getContent = (data, isApp) => {
|
...
|
...
|
@@ -173,8 +247,7 @@ const _getContent = (data, isApp) => { |
|
|
|
|
|
exports.floor = (params, isApp) => {
|
|
|
return Promise.coroutine(function*() {
|
|
|
let shareData,
|
|
|
result = {},
|
|
|
let result = {},
|
|
|
resource = yield api.get('', Object.assign(params, {
|
|
|
method: 'app.promotion.queryCouponCenter',
|
|
|
}));
|
...
|
...
|
@@ -184,8 +257,73 @@ exports.floor = (params, isApp) => { |
|
|
} else {
|
|
|
result.noData = true;
|
|
|
}
|
|
|
return result;
|
|
|
|
|
|
// shareData = _getShare(params.contentCode, '领券中心');
|
|
|
result.share = getShare(params.contentCode, '领券中心');
|
|
|
return result;
|
|
|
})();
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 前端ajax领券
|
|
|
*/
|
|
|
exports.receiveCoupon = (receiveData, isApp, uid) => {
|
|
|
let returnData = {};
|
|
|
|
|
|
// 获取优惠券 ID
|
|
|
if (receiveData.couponID) {
|
|
|
receiveData.couponID = crypto.decrypt(undefined, receiveData.couponID);
|
|
|
}
|
|
|
|
|
|
// 跳转 URl
|
|
|
let playUrl = helpers.urlFormat('/coupon/floor');
|
|
|
|
|
|
// 判断用户是否登录
|
|
|
let isLogin = false;
|
|
|
|
|
|
if (uid) {
|
|
|
isLogin = true;
|
|
|
}
|
|
|
|
|
|
if (!isLogin) {
|
|
|
// 用户未登录,跳转登录页面
|
|
|
returnData.noLogin = true;
|
|
|
if (!isApp) {
|
|
|
returnData.url = helpers.urlFormat('/signin.html', { refer: playUrl }, 'default');
|
|
|
} else {
|
|
|
returnData.isApp = true;
|
|
|
|
|
|
// let playUrlEncode = '{playUrl}?code={receiveData.code}&app_version={receiveData.app_version}'.replace('/', '\\/');
|
|
|
|
|
|
returnData.url = '{playUrl}?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"http:{playUrlEncode}","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
|
|
|
|
|
|
// playUrlEncode = strtr(playUrl, array('/: '\\/'));
|
|
|
// returnData['url'] = playUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . playUrlEncode . '","param":{"from":"app","code":"' . receiveData['code'] . '"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
|
|
|
}
|
|
|
return new Promise(resolve => {
|
|
|
resolve(returnData);
|
|
|
});
|
|
|
} else {
|
|
|
// 登录后调用领券接口
|
|
|
return api.get('', {
|
|
|
method: 'app.promotion.getCoupon',
|
|
|
couponId: receiveData.couponID,
|
|
|
uid: uid,
|
|
|
}).then(result => {
|
|
|
switch (result.code) {
|
|
|
case 200:
|
|
|
returnData = {
|
|
|
msg: '领券成功!',
|
|
|
status: true,
|
|
|
};
|
|
|
break;
|
|
|
default:
|
|
|
returnData = {
|
|
|
msg: '领券失败!',
|
|
|
status: false,
|
|
|
};
|
|
|
break;
|
|
|
}
|
|
|
return returnData;
|
|
|
});
|
|
|
}
|
|
|
}; |
...
|
...
|
|