Authored by TaoHuang

feat(coupon): add coupon api

... ... @@ -118,6 +118,88 @@ exports.couponSend = (req, res, next) => {
};
/**
* 优惠券是否需要验证
*/
exports.couponCheck = (req, res, next) => {
let token = req.query.token || '',
uid = req.user.uid,
app = req.query.app || {};
let resultData = {
code: 403,
message: '参数错误',
data: ''
};
// 接口要加 session 校验,跨域异步请求未添加相关参数
if (req.yoho.isApp || app.client_type === 'miniapp') {
if (app.app_version && app.client_type && app.session_key && app.uid) {
// 小程序调接口获取 session 的方式不同,H5 嵌小程序,client_type 标记为 h5
let isMiniApp = app.client_type === 'miniapp';
uid = {
toString: () => {
return _.parseInt(app.uid);
},
sessionKey: app.session_key,
appVersion: isMiniApp ? _.get(global, 'yoho.config.appVersion') : app.app_version,
appSessionType: app.client_type
};
}
}
if (token === '' || token === 0) {
return res.jsonp(resultData);
}
req.ctx(model).couponCheck(uid, token).then(result => {
res.set({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
if (result.error) {
return res.jsonp(resultData);
}
return res.jsonp({
code: 200,
data: result,
message: 'success'
});
}).catch(next);
};
exports.couponCheckMiddle = (req, res, next) => {
let token = req.query.token || '';
let uid = req.user.uid;
let resultData = {
code: 403,
message: '参数错误',
data: ''
};
if (token === '' || token === 0) {
return res.jsonp(resultData);
}
req.ctx(model).couponCheck(uid, token).then(result => {
if (result.error) {
res.set({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
return res.jsonp(result);
}
req.couponCheckRequired = result.checkRequired;
next();
}).catch(next);
};
/**
* 分享领券活动模板领券接口
* @param req
* @param res
... ...
... ... @@ -50,7 +50,7 @@ class featureModel extends global.yoho.BaseModel {
_.forEach(result, item => {
_.get(item, 'data.product_list', []).forEach(function(val) {
var obj = {
producturl: `//m.yohobuy.com/product/${val.product_skn}.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${val.product_skn}","from_page_name":"${yasProcess.getPname(ctx.req)}","from_page_param":"${_.get(ctx,'req.url', '')}"}}`, // eslint-disable-line
producturl: `//m.yohobuy.com/product/${val.product_skn}.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${val.product_skn}","from_page_name":"${yasProcess.getPname(ctx.req)}","from_page_param":"${_.get(ctx, 'req.url', '')}"}}`, // eslint-disable-line
productimg: helpers.image(val.default_images, 213, 284, 2, 60).replace('quality/80', 'quality/60'), // eslint-disable-line
productname: val.product_name,
vipprice: val.vip_price,
... ... @@ -65,7 +65,7 @@ class featureModel extends global.yoho.BaseModel {
};
if (val.shop_id) {
obj.brandurl = `//m.yohobuy.com/product/index/brand?shop_id=${val.shop_id}&openby:yohobuy={"action":"go.shop","params":{"shop_id":${val.shop_id},"shop_template_type":${val.shop_template_type || "1"},"is_red_shop":${val.is_red_shop || 1}}}`; // eslint-disable-line
obj.brandurl = `//m.yohobuy.com/product/index/brand?shop_id=${val.shop_id}&openby:yohobuy={"action":"go.shop","params":{"shop_id":${val.shop_id},"shop_template_type":${val.shop_template_type || '1'},"is_red_shop":${val.is_red_shop || 1}}}`; // eslint-disable-line
} else {
obj.brandurl = `//m.yohobuy.com/product/index/brand?domain=${val.brand_domain}&openby:yohobuy={"action":"go.brand","params":{"brand_id":${val.brand_id}}}`; // eslint-disable-line
}
... ... @@ -123,7 +123,7 @@ class featureModel extends global.yoho.BaseModel {
}
if (pro) {
defaultPros.push(Object.assign({conf: baseConf}, pro));
defaultPros.push(Object.assign({ conf: baseConf }, pro));
}
});
... ... @@ -159,7 +159,7 @@ class featureModel extends global.yoho.BaseModel {
let displayStyle = _.get(shopRawData, 'displayStyle', '0');
shop.picture = displayStyle === '0' ? shop.pic_popular : shop.shop_logo;
shop.href = `//m.yohobuy.com/product/shop?domain=${shop.shop_domain}&openby:yohobuy={"action":"go.shop","params":{"shop_id":${shop.shops_id},"shop_template_type":${shop.shop_template_type || "1"},"is_red_shop":${shop.is_red_shop || 1}}}`; // eslint-disable-line
shop.href = `//m.yohobuy.com/product/shop?domain=${shop.shop_domain}&openby:yohobuy={"action":"go.shop","params":{"shop_id":${shop.shops_id},"shop_template_type":${shop.shop_template_type || '1'},"is_red_shop":${shop.is_red_shop || 1}}}`; // eslint-disable-line
});
shopRawData.renderData = renderData;
... ... @@ -247,13 +247,13 @@ class featureModel extends global.yoho.BaseModel {
try {
floor.roundInfo = JSON.parse(floor.roundInfo);
} catch(e) { // eslint-disable-line
} catch (e) { // eslint-disable-line
floor.roundInfo = '';
}
try {
floor.replyInfo = JSON.parse(floor.replyInfo);
} catch(e) { // eslint-disable-line
} catch (e) { // eslint-disable-line
floor.replyInfo = '';
}
... ... @@ -267,7 +267,7 @@ class featureModel extends global.yoho.BaseModel {
index(params) {
const self = this;
return Promise.coroutine(function*() {
return Promise.coroutine(function* () {
if (!params.code) {
return Promise.resolve({});
}
... ... @@ -417,7 +417,7 @@ class featureModel extends global.yoho.BaseModel {
let skn = component0.SKN;
component0.producturl = `//m.yohobuy.com/product/${skn}.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${skn}","from_page_name":"${yasProcess.getPname(self.ctx.req)}","from_page_param":"${_.get(self.ctx, 'req.url', '')}"}}`, // eslint-disable-line
component0.bgImg = f.param.bgimg;
component0.bgImg = f.param.bgimg;
component0.beginTimeInt = Date.parse(new Date(component0.beginTime)) / 1000;
component0.endTimeInt = Date.parse(new Date(component0.endTime)) / 1000;
... ... @@ -530,7 +530,7 @@ class featureModel extends global.yoho.BaseModel {
};
}
return this.get({data}).then(result => {
return this.get({ data }).then(result => {
if (!result) {
result.code = 404;
... ... @@ -542,7 +542,7 @@ class featureModel extends global.yoho.BaseModel {
}
getProductBySkns(skns) {
let obj = {defaultSkns: skns};
let obj = { defaultSkns: skns };
return this._getProductBySkns(obj, this.ctx, true).then(() => {
return obj.defaultPros;
... ... @@ -560,7 +560,7 @@ class featureModel extends global.yoho.BaseModel {
/**
* 领取红包
*/
*/
getRedEnvelope(uid, token) {
let data = {
method: 'app.redpac.addRedpac',
... ... @@ -569,7 +569,7 @@ class featureModel extends global.yoho.BaseModel {
actSource: 1004,
};
return this.get({data}).then(result => {
return this.get({ data }).then(result => {
if (!result) {
result.code = 404;
result.message = '出错啦~';
... ... @@ -581,7 +581,7 @@ class featureModel extends global.yoho.BaseModel {
/**
* 领取UFO优惠券
*/
*/
couponSendUFO(uid, token) {
let data = {
method: 'ufo.coupons.send',
... ... @@ -603,6 +603,33 @@ class featureModel extends global.yoho.BaseModel {
});
}
async couponCheck(uid, token) {
let data = {
method: 'app.coupons.getCouponSendConfirmInfo',
couponIdAndTokens: token,
};
const result = await this.get({
data
});
if (result.code !== 200) {
result.error = 404;
result.code = 404;
result.message = '出错啦~';
}
const couponObj = result.data.find(i => i.couponIdToken === token);
if (!couponObj) {
return {
error: '出错了~'
};
}
return couponObj;
}
/**
* 领取活动模板分享的优惠券
* @param activityId
... ... @@ -617,7 +644,7 @@ class featureModel extends global.yoho.BaseModel {
templateId: templateId
};
return this.get({data}).then(result => {
return this.get({ data }).then(result => {
console.log(result);
if (!result) {
result.code = 404;
... ...
... ... @@ -284,7 +284,8 @@ router.get('/individuation/getShopByIds', individuation.getShopByIds); // 通过
router.get('/feature/:code.html', feature.index);
router.get('/featuresidebar/:code.html', feature.sidebar);
router.get('/featurebottombar/:code.html', feature.bottombar);
router.get('/feature/couponSend', captcha, feature.couponSend); // 获取优惠券
router.get('/feature/couponSend', feature.couponCheckMiddle, captcha, feature.couponSend); // 获取优惠券
router.get('/feature/couponCheck', feature.couponCheck); // 优惠券检查接口
router.get('/feature/webShareCouponSend', feature.webShareCouponSend); // 活动模板分享领取优惠券, 不用分券类型,不用传券ID
router.get('/tide/category', auth, tide.category); // 潮品推介
router.get('/tide/shop', auth, tide.shop); // 潮牌推介
... ...
/* eslint-disable newline-after-var */
/* eslint-disable quotes */
"use strict";
'use strict';
const _ = require('lodash');
const tencentcloud = require('tencentcloud-sdk-nodejs');
const logger = global.yoho.logger;
const CaptchaClient = tencentcloud.captcha.v20190722.Client;
const models = tencentcloud.captcha.v20190722.Models;
... ... @@ -12,12 +12,12 @@ const Credential = tencentcloud.common.Credential;
const ClientProfile = tencentcloud.common.ClientProfile;
const HttpProfile = tencentcloud.common.HttpProfile;
const {secretId, secretKey, AppSecretKey, appId} = global.yoho.config.captcha;
const { secretId, secretKey, AppSecretKey, appId } = global.yoho.config.captcha;
let cred = new Credential(secretId, secretKey);
let httpProfile = new HttpProfile();
httpProfile.endpoint = "captcha.tencentcloudapi.com";
httpProfile.endpoint = 'captcha.tencentcloudapi.com';
let clientProfile = new ClientProfile();
clientProfile.httpProfile = httpProfile;
... ... @@ -29,21 +29,21 @@ let captchaReq = new models.DescribeCaptchaResultRequest();
* 优惠券防刷验证码
*/
module.exports = function(req, res, next) {
if (!req.couponCheckRequired) {
return next();
}
const isOpenCaptcha = _.get(req.app.locals.wap, 'ufo.coupon', false);
if (!(req.query.ticket && req.query.randstr)) {
return res.jsonp({ code: 400, message: '没有验证信息' });
}
if (!isOpenCaptcha) {
return next();
}
if (req.query.coupontype === 'ufo') {
const params = {
AppSecretKey,
CaptchaAppId: appId,
CaptchaType: 9,
UserIp: req.yoho.clientIp,
Ticket: req.query.ticket,
Randstr: req.query.randstr
AppSecretKey,
CaptchaAppId: appId,
CaptchaType: 9,
UserIp: req.yoho.clientIp,
Ticket: req.query.ticket,
Randstr: req.query.randstr
};
captchaReq.from_json_string(JSON.stringify(params));
... ... @@ -51,12 +51,9 @@ module.exports = function(req, res, next) {
// eslint-disable-next-line new-cap
client.DescribeCaptchaResult(captchaReq, function(errMsg) {
if (errMsg) {
return res.jsonp({code: 400, message: errMsg.Message});
logger.error('tencent captcha fail ', req.user.uid);
return res.jsonp({ code: 400, message: errMsg.Message });
}
next();
});
} else {
// eslint-disable-next-line callback-return
next();
}
};
... ...