-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.config.js
237 lines (220 loc) · 9.28 KB
/
vue.config.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
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
const { reverse } = require('core-js/core/array');
const fs = require('fs');
/**
*
* @param {*当前页的数量} pageSize
* @param {*当前页} currentPage
* @param {*当前数组} arr
*/
function pagination(pageSize, currentPage, arr) {
let skipNum = (currentPage - 1) * pageSize;
let newArr = (skipNum + pageSize >= arr.length) ? arr.slice(skipNum, arr.length) : arr.slice(skipNum, skipNum + pageSize);
return newArr;
}
//升序还是降序
/**
*
* @param {*排序的属性} attr
* @param {*true表示升序排序 false表示降序排序} rev
*
*/
function sortBy(attr, rev) {
if (rev === undefined) {
rev = 1;
} else {
rev = rev ? 1 : -1;
}
return function (a, b) {
a = a[attr];
b = b[attr];
if (a < b) {
return rev * -1;
}
if (a > b) {
return rev * 1;
}
return 0;
}
}
function range(arr, gt, lte) {
return arr.filter(item => item.salePrice >= gt && item.salePrice <= lte)
}
const cors = require('cors');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
module.exports = {
devServer: {
before(app, serve) {
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/api/goods/home', (req, res) => {
fs.readFile('./db/home.json', 'utf8', (err, data) => {
if (!err) {
res.json(JSON.parse(data));
}
})
})
app.get('/api/goods/allGoods', (req, res) => {
//获取的是前端地址栏上的查询参数
const page = parseInt(req.query.page);
const size = parseInt(req.query.size);
const sort = parseInt(req.query.sort);
const gt = parseInt(req.query.priceGt);
const lte = parseInt(req.query.priceLte);
const cid = req.query.cid;
let newData = []
fs.readFile('.db/allGoods.json', 'utf-8', (err, data) => {
let { result } = JSON.parse(data);
let allData = result.data;
//分页显示
newData = pagination(size, page, allData);
if (cid === '1184') {//品牌周边
newData = allData.filter((item) => item.productName.match(RegExp(/Smartisan/)));
if (sort === 1) {//价格由低到高
newData = newData.sort(sortBy('salePrice', true))
} else if (sort === -1) {//价格由高到低
newData = newData.sort(sortBy('salePrice', false))
}
} else {
if (sort == 1) {//价格由低到高
newData = newData.sort(sortBy('salePrice', true))
} else if (sort === -1) {//价格由高到低
newData = newData.sort(sortBy('salePrice', false))
}
if (gt && lte) {
//过滤10-1000
newData = range(newData, gt, lte)
}
}
if (newData.length < size) {
res.json({
data: newData,
total: newData.length
})
} else {
res.json({
data: newData,
total: allData.length
})
}
})
})
//商品详情的数据
app.get('/api/goods/productDet', (req, res) => {
const productId = req.query.productId;
console.log(productId);
fs.readFile('./db/goodsDetail.json', 'utf8', (err, data) => {
if (!err) {
let { result } = JSON.parse(data);
let newData = result.find(item => item.productId == productId)
res.json(newData)
}
})
})
//模拟一个登录的接口
app.post('/api/login', (req, res) => {
console.log(req.body.user);
//登录成功获取用户名
let username = req.body.user
//一系列的操作
res.json({
//进行加密的方法
//sing 参数一:加密的对象 参数二:加密的原则 参数三:对象
token: jwt.sign({ username: username }, 'abcd', {
//过期时间
expiresIn: "3000s"
}),
username,
state: 1,
file: '/static/images/1570600179870.png',
code: 200,
address: null,
balance: null,
description: null,
email: null,
message: null,
phone: null,
points: null,
sex: null,
id: 62
})
})
// 登录持久化验证接口 访问这个接口的时候 一定要访问token(前端页面每切换一次,就访问一下这个接口,问一下我有没有登录/登陆过期)
// 先访问登录接口,得到token,在访问这个,看是否成功
app.post('/api/validate', function (req, res) {
let token = req.headers.authorization;
console.log(token);
// 验证token合法性 对token进行解码
jwt.verify(token, 'abcd', function (err, decode) {
if (err) {
res.json({
msg: '当前用户未登录'
})
} else {
// 证明用户已经登录
res.json({
token: jwt.sign({ username: decode.username }, 'abcd', {
// 过期时间
expiresIn: "3000s"
}),
username: decode.username,
msg: '已登录',
address: null,
balance: null,
description: null,
email: null,
file: "/static/images/1570600179870.png",
id: 62,
message: null,
phone: null,
points: null,
sex: null,
state: 1,
})
}
})
})
app.post('/api/addCart', (req, res) => {
let { userId, productId, productNum } = req.body;
fs.readFile('./db/allGoods.json', (err, data) => {
let { result } = JSON.parse(data);
if (productId && userId) {
let { cartList } = cartListJSON.result.find(item => item.id == userId)
// 找到对应的商品
let newData = result.data.find(item => item.productId == productId);
newData.limitNum = 100;
let falg = true;
if (cartList && cartList.length) {
cartList.forEach(item => {
if (item.productId == productId) {
if (item.productNum >= 1) {
falg = false;
item.productNum += parseInt(productNum);
}
}
})
}
if (!cartList.length || falg) { //购物车为空
newData.productNum = parseInt(productNum)
cartList.push(newData);
}
// 序列化
fs.writeFile('./db/cartList.json', JSON.stringify(cartListJSON), (err) => {
if (!err) {
res.json({
code: 200,
message: "success",
result: 1,
success: true,
timestamp: 1571296313981,
})
}
})
}
})
})
app.get
}
}
}