-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.js
164 lines (159 loc) · 3.5 KB
/
search.js
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
// 引入 models
const models = require('../models');
const code = require('../lib/code');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
indexAction = async (request) => {
try{
const openId = request.query.openId;
/**
* 默认关键字
*/
// const defaultKeyword = await models.nideshop_keywords.findAndCountAll({
// where: {
// is_default: 1
// },
// });
/**
* 取出热闹关键字
*/
const hotKeywordList = await models.nideshop_keywords.findAndCountAll({
attributes: [
[Sequelize.fn('DISTINCT', Sequelize.col('keyword')), 'keyword'],
'keyword', 'is_hot', 'is_default'
],
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
const historyData = await models.nideshop_search_history.findAndCountAll({
where: {
"user_id": openId
},
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
return {
results: {
// defaultKeyword,
hotKeywordList,
historyData,
},
dataBaseError: false
}
}
catch(e) {
return { results: e, dataBaseError: true}
}
}
/**
* 搜素的时候匹配搜索相关的
*/
helperAction = async (request) => {
try{
// const keyword = request.query.keyword;
var order = request.query.order;
if (!order) {
order = '';
orderBy = "id"
} else {
orderBy = "retail_price"
}
const keywords = await models.nideshop_goods.findAndCountAll({
where: {
name:{
[Op.like]: '%' + request.query.keyword + '%'
}
},
// 根据价格排序
// order: [
// [orderBy, order]
// ],
attributes: ['id', 'name', 'list_pic_url', 'retail_price'],
limit: request.query.limit,
offset: (request.query.page - 1) * request.query.limit,
});
return {
results: {
keywords: keywords || []
},
dataBaseError: false
}
}
catch(e) {
return { results: e, dataBaseError: true}
}
}
/**
* 添加搜索历史
*/
addHistoryAction = async (openId, keyword) => {
try{
// const { openId, keyword } = request.payload;
const oldData = await models.nideshop_search_history.findAndCountAll({
where: {
user_id: openId,
keyword: keyword
}
});
if (oldData.length == 0) {
const data = await models.nideshop_search_history.create({
user_id: openId,
keyword: keyword,
add_time: parseInt(new Date().getTime() / 1000),
});
if (data) {
return {
results: {data: "success"}
}
} else {
return {
results: { data: "fail" }
}
}
} else {
return {
results: {
data: "已经有记录了"
}
}
}
}
catch(e) {
return { results: e, dataBaseError: true}
}
}
/**
* 清除历史记录
*/
clearhistoryAction = async (parms) => {
try{
// const openId = request.body.openId;
const data = await models.nideshop_search_history.destroy({
where: {
user_id: parms.id
},
});
if (data) {
return {
results: {
data: ' 清楚成功'
}
}
} else {
return {
results: {
data: null
}
}
}
}
catch(e){
return { results: e, dataBaseError: true}
}
}
module.exports = {
indexAction,
helperAction,
addHistoryAction,
clearhistoryAction
}