public-handler.js
6.88 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* @Author: Targaryen
* @Date: 2016-06-02 15:50:47
* @Last Modified by: Targaryen
* @Last Modified time: 2016-06-03 18:25:57
*/
'use strict';
const _ = require('lodash');
/**
* 处理用于筛选的 URL
* @param {[type]} originParam [description]
* @param {[type]} newParam [description]
* @return {[type]} [description]
*/
const handleFilterUrl = (originParam, newParam) => {
let dest = '?';
let tempOriginParam = {};
// 创建 originParam、newParam 键的数组
let originParamArray = _.keys(originParam);
let newParamArray = _.keys(newParam);
// 判断参数是否已经存在
let isExist = !_.isEmpty(_.find(originParamArray, function(o) {
return o === newParamArray[0];
}));
if (isExist) {
tempOriginParam = Object.assign(tempOriginParam, originParam);
_.forEach(newParam, function(value, key) {
tempOriginParam[key] = value;
});
} else {
tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
}
_.forEach(tempOriginParam, function(value, key) {
dest = dest + key + '=' + value + '&';
});
return _.trim(dest, '&');
};
/**
* 筛选类链接处理的对外接口
* @type {[type]}
*/
exports.handleFilterUrl = handleFilterUrl;
/**
* 处理选中数据
* @param {[type]} params [description]
* @param {[type]} origin [description]
* @param {[type]} param [description]
* @return {[type]} [description]
*/
exports.handleCheckedData = (params, origin, param) => {
var dest = [];
// 品牌选中数据
if (!_.isEmpty(origin)) {
_.forEach(origin, (value) => {
if (value.checked) {
let checked = {};
checked.name = value.name;
let tempParam = {};
tempParam[param] = '';
checked.href = handleFilterUrl(params, tempParam);
dest.push(checked);
}
});
}
return dest;
};
/**
* 处理分页数据
* @param {[type]} total [description]
* @param {[type]} params [description]
* @return {[type]} [description]
*/
exports.handleSalePagerData = (total, params) => {
// 当前页
let currentPage = parseInt((_.isEmpty(params.page) ? 1 : params.page), 10);
let perPageCount = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);
let totalPage = parseInt(total / perPageCount, 10) + 1;
let dest = '<a href ="' + handleFilterUrl(params, { page: (currentPage) }) +
'" class="cur">' + currentPage + '</a>';
// 先处理大多数情况
if ((currentPage > 2) && (currentPage < (parseInt(total, 10) / perPageCount) - 1)) {
for (let i = 1; i < 3; i++) {
dest = '<a href ="' + handleFilterUrl(params, { page: (currentPage - i) }) + '">' +
(currentPage - i) + '</a> ' + dest + ' <a href ="' +
handleFilterUrl(params, { page: (currentPage + i) }) +
'">' + (currentPage + i) + '</a>';
}
// 处理页码小于2的情况
} else if (currentPage <= 2) {
for (let i = currentPage - 1; i > 0; i--) {
dest = '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>' + dest;
}
for (let i = currentPage + 1; i < (totalPage < 6 ? totalPage : 6); i++) {
dest += '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>';
}
// 处理页码大于最大页-2的情况
} else if (currentPage > totalPage - 2) {
for (let i = currentPage + 1; i < totalPage + 1; i++) {
dest += '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>';
}
for (let i = currentPage - 1; i > (currentPage - 5 > 1 ? currentPage - 5 : 1); i--) {
dest = '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>' + dest;
}
}
// 处理后省略号的情况
if ((totalPage > 5) && (currentPage < (totalPage - 2))) {
dest += ' ... <a href ="' + handleFilterUrl(params, { page: totalPage }) +
'">' + totalPage + '</a>';
}
// 处理前省略号的情况
if (currentPage > 3) {
dest = '<a href ="' + handleFilterUrl(params, { page: 1 }) +
'">1</a> ... ' + dest;
}
// 处理上一页下一页
if (currentPage !== 1) {
dest = '<a href="' + handleFilterUrl(params, { page: currentPage - 1 }) +
'"><span class="iconfont">上一页</span></a>' + dest;
}
if (currentPage !== totalPage) {
dest += '<a href="' + handleFilterUrl(params, { page: currentPage + 1 }) +
'"><span class="iconfont">下一页</span></a>';
}
return dest;
};
/**
* 处理 opts 排序数据 待处理 待确认
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleSaleOptsData = (params, total) => {
var dest = {};
dest.sortType = [];
for (let i = 0; i < 3; i++) {
let opt = {};
switch (i) {
case 0:
opt.href = handleFilterUrl(params, { order: 's_t_desc' });
opt.name = '最新';
break;
case 1:
opt.href = handleFilterUrl(params, { order: 's_p_asc' });
opt.name = '价格';
break;
case 2:
opt.href = handleFilterUrl(params, { order: 'p_d_asc' });
opt.name = '折扣';
break;
default:
break;
}
opt.hasSortOrient = {};
opt.hasSortOrient.active = {};
opt.hasSortOrient.active.desc = {};
dest.sortType.push(opt);
}
// 上下翻页数据处理
dest.pageCounts = [{
href: handleFilterUrl(params, { limit: 100 }),
count: 100
}, {
href: handleFilterUrl(params, { limit: 80 }),
count: 80
}, {
href: handleFilterUrl(params, { limit: 60 }),
count: 60
}];
dest.curPage = _.isEmpty(params.page) ? 1 : params.page; // 当前页码数
// 上一页下一页
let preHref = (!_.isEmpty(params.page) && params.page > 1) ? params.page - 1 : 1;
let nextHref = (!_.isEmpty(params.page)) ? params.page + 1 : 2;
dest.preHref = handleFilterUrl(params, { page: preHref });
dest.nextHref = handleFilterUrl(params, { page: nextHref });
// 每页商品数量
dest.countPerPage = _.isEmpty(params.limit) ? 60 : params.limit;
// 全部页码数量
dest.pageCount = parseInt(total / (_.isEmpty(params.limit) ? 60 : params.limit), 10) + 1;
// 每页多少商品
let paramsLimit = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);
// 商品开始结束数
dest.start = (!_.isEmpty(params.page) ? params.page - 1 : 0) * paramsLimit;
dest.end = parseInt(dest.start, 10) + paramsLimit;
return dest;
};