Authored by 郝肖肖

ctx vip

... ... @@ -62,6 +62,10 @@ if (!app.locals.devEnv) {
}
app.set('subdomain offset', 2);
// 添加请求上下文
app.use(global.yoho.httpCtx());
app.use(global.yoho.hbs({
extname: '.hbs',
defaultLayout: 'layout',
... ...
... ... @@ -5,7 +5,7 @@ let couponsModel = require('../models/coupons-model');
const index = (req, res, next)=>{
let uid = req.user.uid;
couponsModel.couponsData(uid, req.query).then(result => {
req.ctx(couponsModel).couponsData(uid, req.query).then(result => {
res.render('coupons', result);
}).catch(next);
};
... ...
... ... @@ -4,7 +4,7 @@ const vipModel = require('../models/vip-service');
const index = (req, res, next) => {
let uid = req.user.uid;
vipModel.vipIndex(uid).then(result => {
req.ctx(vipModel).vipIndex(uid).then(result => {
res.render('vip', result);
}).catch(next);
};
... ... @@ -12,7 +12,7 @@ const index = (req, res, next) => {
const details = (req, res, next) => {
let uid = req.user.uid;
vipModel.getVipRecord(uid, req.query || {}).then(result => {
req.ctx(vipModel).getVipRecord(uid, req.query || {}).then(result => {
res.render('vip-details', result);
}).catch(next);
};
... ...
... ... @@ -18,148 +18,154 @@ const UNUSED = 'notuse';
const USED = 'use';
const INVALID = 'overtime';
const getCouponsList = (uid, type, page, limit)=>{
return co(function*() {
let couponsInfo = yield UserData.getCouponsList(uid, type, page, limit);
let result = [],
coupons = _.get(couponsInfo, 'data.couponList');
if (!coupons) {
return {
list: result
};
}
if (coupons) {
coupons.forEach(function(item, i) {
result[i] = {};
result[i].id = item.couponId;
result[i].code = item.couponCode;
// 格式化有效日期 "couponValidity": "2016.03.15-2016.03.31"
let dates = item.couponValidity.split('-'),
extra = ['秒杀', '限定', '境外', '预售'],
limits = _.get(item, 'shopPriceLimits', '');
result[i].beginTime = dates[0];
result[i].endTime = dates[1];
if (!item.couponImageUrl) {
result[i].img = '//static.yohobuy.com/images/v2/activity/default_coupon.jpg';
} else {
result[i].img = item.couponImageUrl;
}
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
result[i].value = item.couponValue;
result[i].validity = item.couponValidity;
result[i].useRemark = item.couponDetailInfomation;
if (!_.isEmpty(item.sortNameLimit)) {
result[i].categorys = item.sortNameLimit.join('、');
}
if (!_.isEmpty(item.brandNameLimit)) {
result[i].brands = item.brandNameLimit.join('、');
}
if (limits.indexOf('1') >= 0) {
extra.push('3折以下');
}
if (limits.indexOf('2') >= 0) {
extra.push('限量');
}
getCouponsList(uid, type, page, limit) {
let userDataModel = new UserData(this.ctx);
return co(function*() {
let couponsInfo = yield userDataModel.getCouponsList(uid, type, page, limit);
let result = [],
coupons = _.get(couponsInfo, 'data.couponList');
result[i].extraPro = extra.join('、');
result[i].isNoLimit = item.isNoLimit === 'true' ? true : false;
result[i].explains = _.isEmpty(item.explains) ? false : item.explains;
result[i].proListUrl = helpers.urlFormat('', {cpc_id: item.couponId,
phrase: encodeURIComponent('以下商品可使用 【' + item.couponDetailInfomation + '】优惠券')}, 'list');
result[i].rule = item.rule4ShortName || '';
result[i].overState = item.overState || '';
if (type === USED) {
result[i].orderNum = _.get(item, 'orderCode', '');
result[i].orderDetailUrl = helpers.urlFormat('/home/orders/detail',
{orderCode: item.orderCode || ''});
result[i].orderSum = _.get(item, 'orderPrice', 0).toFixed(2);
result[i].payment = _.get(item, 'actuallyPaid', 0).toFixed(2);
let date = item.usedTime ? moment(item.usedTime).format('YYYY-MM-DD H:m') : 0;
if (date) {
result[i].useTime = date;
if (!coupons) {
return {
list: result
};
}
if (coupons) {
coupons.forEach(function(item, i) {
result[i] = {};
result[i].id = item.couponId;
result[i].code = item.couponCode;
// 格式化有效日期 "couponValidity": "2016.03.15-2016.03.31"
let dates = item.couponValidity.split('-'),
extra = ['秒杀', '限定', '境外', '预售'],
limits = _.get(item, 'shopPriceLimits', '');
result[i].beginTime = dates[0];
result[i].endTime = dates[1];
if (!item.couponImageUrl) {
result[i].img = '//static.yohobuy.com/images/v2/activity/default_coupon.jpg';
} else {
result[i].useTime = '';
result[i].img = item.couponImageUrl;
}
} else {
result[i].status = item.isValidity ? '可使用' : '已过期';
}
if (item.couponType) {
result[i].type = item.couponType;
if (Number(item.couponType) === 5) {
result[i].value = '免邮';
result[i].value = item.couponValue;
result[i].validity = item.couponValidity;
result[i].useRemark = item.couponDetailInfomation;
if (!_.isEmpty(item.sortNameLimit)) {
result[i].categorys = item.sortNameLimit.join('、');
}
}
if (!_.isEmpty(item.brandNameLimit)) {
result[i].brands = item.brandNameLimit.join('、');
}
if (limits.indexOf('1') >= 0) {
extra.push('3折以下');
}
if (limits.indexOf('2') >= 0) {
extra.push('限量');
}
result[i].extraPro = extra.join('、');
result[i].isNoLimit = item.isNoLimit === 'true' ? true : false;
result[i].explains = _.isEmpty(item.explains) ? false : item.explains;
result[i].proListUrl = helpers.urlFormat('', {cpc_id: item.couponId,
phrase: encodeURIComponent('以下商品可使用 【' + item.couponDetailInfomation + '】优惠券')}, 'list');
result[i].rule = item.rule4ShortName || '';
result[i].overState = item.overState || '';
if (type === USED) {
result[i].orderNum = _.get(item, 'orderCode', '');
result[i].orderDetailUrl = helpers.urlFormat('/home/orders/detail',
{orderCode: item.orderCode || ''});
result[i].orderSum = _.get(item, 'orderPrice', 0).toFixed(2);
result[i].payment = _.get(item, 'actuallyPaid', 0).toFixed(2);
let date = item.usedTime ? moment(item.usedTime).format('YYYY-MM-DD H:m') : 0;
if (date) {
result[i].useTime = date;
} else {
result[i].useTime = '';
}
} else {
result[i].status = item.isValidity ? '可使用' : '已过期';
}
if (item.couponType) {
result[i].type = item.couponType;
if (Number(item.couponType) === 5) {
result[i].value = '免邮';
}
}
});
}
let pageNum = pager(couponsInfo.data.total, {
page: page,
limit: limit,
type: type
});
}
let pageNum = pager(couponsInfo.data.total, {
page: page,
limit: limit,
type: type
});
return {
list: result,
pager: Object.assign({
count: couponsInfo.data.total || 0,
curPage: page,
totalPages: couponsInfo.data.totalPageNum
}, pageNum)
};
})();
};
const couponsData = (uid, params)=>{
let type = params.type || UNUSED;
let page = params.page || 1;
let limit = params.limit || 10;
return co(function*() {
let coupons = yield getCouponsList(uid, type, page, limit);
let data = {};
if (type === UNUSED) {
data.unUseCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.unUse = true;
} else if (type === USED) {
data.usedCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.used = true;
} else if (type === INVALID) {
data.noValidCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.noValid = true;
}
data.tabs = [
{
active: type === UNUSED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: UNUSED}),
name: '未使用优惠券'
},
{
active: type === USED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: USED}),
name: '已使用优惠券'
},
{
active: type === INVALID ? true : false,
url: helpers.urlFormat('/home/coupons', {type: INVALID}),
name: '已失效优惠券'
return {
list: result,
pager: Object.assign({
count: couponsInfo.data.total || 0,
curPage: page,
totalPages: couponsInfo.data.totalPageNum
}, pageNum)
};
})();
}
couponsData(uid, params) {
let type = params.type || UNUSED;
let page = params.page || 1;
let limit = params.limit || 10;
let _this = this;
return co(function*() {
let coupons = yield _this.getCouponsList(uid, type, page, limit);
let data = {};
if (type === UNUSED) {
data.unUseCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.unUse = true;
} else if (type === USED) {
data.usedCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.used = true;
} else if (type === INVALID) {
data.noValidCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.noValid = true;
}
];
data.pager = coupons.pager;
data.tabs = [
{
active: type === UNUSED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: UNUSED}),
name: '未使用优惠券'
},
{
active: type === USED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: USED}),
name: '已使用优惠券'
},
{
active: type === INVALID ? true : false,
url: helpers.urlFormat('/home/coupons', {type: INVALID}),
name: '已失效优惠券'
}
];
return data;
})();
};
data.pager = coupons.pager;
module.exports = {
couponsData
return data;
})();
}
};
... ...
'use strict';
const api = global.yoho.API;
const getVIPInfoByUid = (uid)=>{
let options = {
method: 'app.passport.vip',
uid: uid,
private_key: '0ed29744ed318fd28d2c07985d3ba633'
};
return api.get('', options);
};
/**
* [vip累计记录]
* @param {[uid:uid,limit:每页数量, page:第几页]} params [参数]
* @return {[type]} [description]
*/
const getVipRecord = (params)=>{
let options = {
method: 'app.passport.getVipRecord'
};
return api.get('', Object.assign(options, params));
};
const getVipRecordDemo = (params)=>{
let options = {
method: 'app.inbox.getlistnew'
};
return api.get('', Object.assign(options, params));
};
const getCouponsList = (uid, type, page, limit)=>{
page = page || 1;
type = type || 'notuse';
limit = limit || 10;
let options = {
method: 'app.coupons.lists',
uid: uid,
type: type,
page: page,
limit: limit
};
return api.get('', options);
};
module.exports = {
getVIPInfoByUid,
getCouponsList,
getVipRecord,
getVipRecordDemo
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getVIPInfoByUid(uid) {
let options = {
method: 'app.passport.vip',
uid: uid,
private_key: '0ed29744ed318fd28d2c07985d3ba633'
};
return this.get({data: options});
}
/**
* [vip累计记录]
* @param {[uid:uid,limit:每页数量, page:第几页]} params [参数]
* @return {[type]} [description]
*/
getVipRecord(params) {
let options = {
method: 'app.passport.getVipRecord'
};
return this.get({data: Object.assign(options, params)});
}
getVipRecordDemo(params) {
let options = {
method: 'app.inbox.getlistnew'
};
return this.get({data: Object.assign(options, params)});
}
getCouponsList(uid, type, page, limit) {
page = page || 1;
type = type || 'notuse';
limit = limit || 10;
let options = {
method: 'app.coupons.lists',
uid: uid,
type: type,
page: page,
limit: limit
};
return this.get({data: options});
}
};
... ...
... ... @@ -2,100 +2,103 @@
const Promise = require('bluebird');
const co = Promise.coroutine;
const userData = require('./user-data');
const UserData = require('./user-data');
const moment = require('moment');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const setPager = require(`${global.utils}/pager`).setPager;
const vipIndex = (uid)=>{
return co(function*() {
let vipInfo = yield userData.getVIPInfoByUid(uid);
let data = vipInfo.data, proportion = '0%';
if (+data.next_need_cost !== 0) {
proportion = data.current_year_cost * 100 / data.next_need_cost;
proportion = proportion > 100 ? 100 : proportion;
proportion = proportion + '%';
}
let remainDays = Math.ceil(((data.vip_end_time) * 1000 - Date.now()) / 86400000);
let preferences = [];
if (data.enjoy_preferential) {
preferences = data.enjoy_preferential.map(function(item) {
return {
id: item.id,
favTxt: item.title,
imgType: helpers.https(item.pic),
description: item.description
};
});
}
let isVip = data.current_vip_level > 0 ? true : false;
let vip = {
title: data.current_vip_title,
nextTitle: data.next_vip_title,
nextLevel: data.next_vip_level,
yearCost: Number(data.current_year_cost).toFixed(2),
totalCost: (+data.current_total_cost).toFixed(2),
list: preferences,
level: data.current_vip_level,
platinum: data.upgrade_need_cost > 0 ? false : true,
nextCost: Number(data.next_need_cost).toFixed(2),
balan: Number(data.upgrade_need_cost).toFixed(2),
proportion: proportion,
vipLevel: data.next_vip_title,
reach: moment(data.vip_reach_time * 1000).format('YYYY.MM.DD'),
valid: moment(data.vip_start_time * 1000).format('YYYY.MM.DD'),
end: moment(data.vip_end_time * 1000).format('YYYY.MM.DD'),
remainDays: remainDays,
doubtLevel: isVip,
commonVip: !isVip,
morePreferences: helpers.urlFormat('/help/detail', {id: 7})
};
return vip;
})();
};
const getVipRecord = (uid, params) => {
let page = parseInt(params.page, 10) || 1;
let size = parseInt(params.size, 10) || 1;
let type = parseInt(params.type, 10) || 1;
type = type > 0 && type < 3 ? type : 1;//1 or 2
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
vipIndex(uid) {
let userDataModel = new UserData(this.ctx);
return co(function*() {
let vipInfo = yield userDataModel.getVIPInfoByUid(uid);
let data = vipInfo.data, proportion = '0%';
if (+data.next_need_cost !== 0) {
proportion = data.current_year_cost * 100 / data.next_need_cost;
proportion = proportion > 100 ? 100 : proportion;
proportion = proportion + '%';
}
let remainDays = Math.ceil(((data.vip_end_time) * 1000 - Date.now()) / 86400000);
let preferences = [];
if (data.enjoy_preferential) {
preferences = data.enjoy_preferential.map(function(item) {
return {
id: item.id,
favTxt: item.title,
imgType: helpers.https(item.pic),
description: item.description
};
});
}
let isVip = data.current_vip_level > 0 ? true : false;
let vip = {
title: data.current_vip_title,
nextTitle: data.next_vip_title,
nextLevel: data.next_vip_level,
yearCost: Number(data.current_year_cost).toFixed(2),
totalCost: (+data.current_total_cost).toFixed(2),
list: preferences,
level: data.current_vip_level,
platinum: data.upgrade_need_cost > 0 ? false : true,
nextCost: Number(data.next_need_cost).toFixed(2),
balan: Number(data.upgrade_need_cost).toFixed(2),
proportion: proportion,
vipLevel: data.next_vip_title,
reach: moment(data.vip_reach_time * 1000).format('YYYY.MM.DD'),
valid: moment(data.vip_start_time * 1000).format('YYYY.MM.DD'),
end: moment(data.vip_end_time * 1000).format('YYYY.MM.DD'),
remainDays: remainDays,
doubtLevel: isVip,
commonVip: !isVip,
morePreferences: helpers.urlFormat('/help/detail', {id: 7})
};
return vip;
})();
}
getVipRecord(uid, params) {
let page = parseInt(params.page, 10) || 1;
let size = parseInt(params.size, 10) || 1;
let type = parseInt(params.type, 10) || 1;
let userDataModel = new UserData(this.ctx);
type = type > 0 && type < 3 ? type : 1;// 1 or 2
return userDataModel.getVipRecordDemo({
uid: uid,
page: page,
size: size || 10
}).then(d => {
var resData = {type: type};
if (d.code !== 200) {
return resData;
}
let pagerList = setPager(_.get(d.data, 'page_total', 1), Object.assign(params, {page: page}));
resData.data = d.data.list;
resData.pager = Object.assign({
count: _.get(d.data, 'total', 0),
curPage: page,
totalPages: _.get(d.data, 'page_total', 0)
}, pagerList);
return userData.getVipRecordDemo({
uid: uid,
page: page,
size: size || 10
}).then(d => {
var resData = {type: type};
if (d.code !== 200) {
return resData;
}
let pagerList = setPager(_.get(d.data, 'page_total', 1), Object.assign(params, {page: page}));
resData.data = d.data.list;
resData.pager = Object.assign({
count: _.get(d.data, 'total', 0),
curPage: page,
totalPages: _.get(d.data, 'page_total', 0)
}, pagerList);
return resData;
});
};
module.exports = {
vipIndex,
getVipRecord
});
}
};
... ...
... ... @@ -50,7 +50,7 @@
"urlencode": "^1.1.0",
"uuid": "^2.0.2",
"yoho-express-session": "^2.0.0",
"yoho-node-lib": "0.2.14",
"yoho-node-lib": "^0.2.23",
"yoho-zookeeper": "^1.0.8"
},
"devDependencies": {
... ...
... ... @@ -244,6 +244,7 @@
text-align: center;
border-bottom: 1px solid #dedede;
border-right: 1px solid #dedede;
font-size: 14px;
.t-header {
background-color: #efefef;
... ... @@ -256,11 +257,10 @@
div {
width: 33.33%;
display: table-cell;
height: 30px;
vertical-align: middle;
border-top: 1px solid #dedede;
border-left: 1px solid #dedede;
padding: 10px 0;
padding: 10px 5px;
line-height: 18px;
}
}
... ...