currency-model.js
5.67 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @author: weiqingting<qingting.wei@yoho.cn>
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const path = require('path');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const CurrencyApi = require('./currency-api');
// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const moment = require('moment');
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
convertUnitTime(src) {
return moment.unix(src).format('YYYY-MM-DD');
}
currencyTabs(type) {
let tabs = ['全部明细', '全部收入', '全部支出'],
result = [];
tabs.forEach(function(val, key) {
result.push({
active: key === parseInt(type, 10) ? true : false,
url: helpers.urlFormat('/home/currency', {type: key}),
name: val
});
});
return result;
}
currencyOptions(condition) {
let that = this;
let result = [], paramUrl = {};
let tabs = {90: '最近3个月明细', 180: '最近半年明细', 360: '最近一年明细'};
for (let name in tabs) {
if (tabs.hasOwnProperty(name)) {
if (condition.queryType) {
paramUrl.type = condition.queryType;
}
paramUrl.beginTime = that.convertUnitTime(new Date() / 1000 - parseInt(name, 10) * 3600 * 24);
result.push({
url: helpers.urlFormat('/home/currency', paramUrl),
name: tabs[name],
selected: condition.beginTime && paramUrl.beginTime === condition.beginTime ? true : false
});
}
}
return result;
}
currencyList(uid, condition) {
let that = this;
return co(function*() {
let result = {list: [], pager: []};
condition.limit = condition.limit || 15;
let currencyDataModel = new CurrencyApi(that.ctx);
let data = yield currencyDataModel.yohoCoinList(uid, condition);
if (_.get(data, 'code') === 200 && data.data.coinlist && !_.isEmpty(data.data.coinlist)) {
for (let key = 0; key < data.data.coinlist.length; key++) {
let val = data.data.coinlist[key];
result.list[key] = {
date: val.date,
desc: val.message,
isIncome: true
};
// 2:订单取消退还,9:下单使用,10:退货退还
if ([2, 9, 10].indexOf(val.type) > -1 && val.key) {
result.list[key].detailUrl = helpers.urlFormat('/home/orders/detail', {orderCode: val.key});
} else if (Number(val.type) === 14 && val.key) { // 晒单奖励
let product = yield currencyDataModel.getProduct(Number(val.key), 1);
if (_.get(product, 'code') === 200 &&
!_.isEmpty(product.data.product_list) &&
!_.isEmpty(product.data.product_list[0].goods_list)) {
let productSkn = _.get(product, 'data.product_list[0].product_skn');
result.list[key].detailUrl = helpers.getUrlBySkc(productSkn);
}
}
if (Number(val.num) < 0) {
result.list[key].isIncome = false;
}
result.list[key].value = val.num > 0 ? '+' + val.num : val.num;
}
// 分页
result.pager = Object.assign({
count: data.data.total,
curPage: data.data.page,
totalPages: Math.ceil(data.data.total / condition.limit)
}, pager(data.data.total, {
page: condition.page,
limit: condition.limit,
type: condition.queryType,
beginTime: condition.beginTime
}));
}
return result;
})();
}
currencyData(uid, prama) {
let that = this;
let condition = {
page: prama.page || 1,
queryType: prama.type || 0,
beginTime: prama.beginTime || that.convertUnitTime(new Date() / 1000 - 3600 * 24 * 90)
};
return co(function*() {
let result = {};
let currencyDataModel = new CurrencyApi(that.ctx);
let yohoCoinInfo = yield currencyDataModel.yohoCoinTotal(uid);
if (_.get(yohoCoinInfo, 'code') === 200) {
let yohoCoinInfoData = yohoCoinInfo.data;
result.myCurrency = yohoCoinInfoData.yohocoin_num ? yohoCoinInfoData.yohocoin_num : 0;
if (_.get(yohoCoinInfoData, 'nearExpCoinNum') > 0) {
result.tip = {
count: yohoCoinInfoData.nearExpCoinNum,
date: new Date().getFullYear() + '年12月31日'
};
}
}
let currency = yield that.currencyList(uid, condition);
Object.assign(result, {
currency: currency.list,
pager: currency.pager,
coinHelperUrl: '//www.yohobuy.com/help/detail?id=105', // yoho币帮助
tabs: that.currencyTabs(condition.queryType),
options: that.currencyOptions(condition)
});
return result;
})();
}
};