CouponsModel.js
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const UserData=require('./UserData');
const helpers = global.yoho.helpers;
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=[];
if(!couponsInfo.data.couponList){
return result;
}
let coupons=couponsInfo.data.couponList;
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('-');
result[i].beginTime=dates[0].replace('.','-');
result[i].endTime=dates[1].replace('.','-');
if(!item.couponImageUrl){
result[i].img='//static.yohobuy.com/images/v2/activity/default_coupon.jpg';
}else{
result[i].img=item.couponImageUrl;
}
if((item.overTime-Date.now())<259200){
result[i].endSoon=true;
}else{
result[i].endSoon=false;
}
result[i].value=item.couponValue.toFixed(2);
result[i].validity=item.couponValidity;
result[i].useRemark=item.couponDetailInfomation;
if(type===USED){
result[i].orderNum=item.orderCode?item.orderCode:'';
result[i].orderDetailUrl=helpers.urlFormat('/home/orders/detail',{orderCode:item.orderCode||''});
result[i].orderSum=item.orderPrice.toFixed(2)?item.orderPrice:0;
result[i].payment=item.actuallyPaid.toFixed(2)?item.actuallyPaid:0;
let data=result[i].usedTime?moment(result[i].usedTime).format("YYYY-MM-DD"):0;
if(data){
result[i].useTime=new Date(date).getTime();
}else{
result[i].useTime='';
}
}else{
result[i].status=item.isValidity?'可使用' : '已过期';
}
if(item.couponType){
result[i].type=item.couponType;
if(item.couponType==5){
result[i].value='免邮';
}
}
});
}
return {list:result,pager:{
total:couponsInfo.data.total,
pageTotal:couponsInfo.data.totalPageNum,
page:page
}};
})();
}
module.exports = {
getCouponsList,
UNUSED,
USED,
INVALID
};