forked from su-susu/j2ee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testBackend.js
599 lines (559 loc) · 15.2 KB
/
testBackend.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var multer = require('multer')
var upload = multer({dest: 'uploads/'})
var addresses = [
{
recipientName: "Foo",
phoneNumber: "13612345678",
addressDetail: "xxx",
province : "湖南省",
city:"长沙市",
isDefaultAddress:true,
addressId:111,
},
{
recipientName: "Bar",
phoneNumber: "15123456789",
province : "广东省",
city:"广州市",
addressDetail: "大学城",
isDefaultAddress:false,
addressId:222
},
{
recipientName: "Tony",
phoneNumber: "13412345678",
province : "广东省",
city:"广州市",
addressDetail: "小谷围",
isDefaultAddress: false,
addressId: 333
}
]
var tableData = [
{
title:'html从入门到放弃',
price: 500,
nums:1,
postage : 2,
quantity:10,
itemState:1,
province : '广东省',
picture: 'http://localhost:3001/1.png',
userName :'lwz',
freePostage:1,
itemId : 123
},
{
title:'Css从入门到精通',
price: 10,
nums:1,
postage : 2,
quantity:15,
itemState:1,
userName :'lwz',
province : '湖南省',
picture: 'http://localhost:3001/2.png',
freePostage:1,
itemId : 124
},
{
title:'Json的使用',
price: 200,
nums:1,
postage : 2,
quantity:20,
itemState:0,
province : '广东省',
userName :'lwz',
freePostage:1,
itemId : 125,
picture: 'http://localhost:3001/3.png',
},
{
title:'如何与项目经理相处',
price: 30,
nums:1,
postage : 2,
quantity:0,
itemState:1,
province : '广东省',
userName :'lwz',
freePostage:1,
itemId : 126,
picture: 'http://localhost:3001/4.png',
}
]
app.use(express.static('uploads'));
app.get('/', function (req, res) {
res.send("backend is now running")
})
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true}));
app.post('/BookStore/upload/', upload.single('img'), function (req, res, next) {
var file = req.body.data;
console.log(file)
console.log(req.body)
// console.log('文件类型:%s', file.mimetype); console.log('原始文件名:%s',
// file.originalname); console.log('文件大小:%s', file.size);
// console.log('文件保存路径:%s', file.path); 接收文件成功后返回数据给前端
res.json({
status: 1,
data: {
pictureAddress: 'http://localhost:3001/1.png'
}
});
})
app.post('/BookStore/', function (req, res) {
data = req.body;
let response = {}
console.log(data)
if (data['query'] == 'img') {
console.log(data.img)
console.log(typeof data.img)
}
if (data['query'] == 'signOrder') {
res.json({status: 1})
}
if (data['query'] == 'signout') {
res.json({status: 1})
}
if (data['query'] == 'finishOrder') {
res.json({status: 1})
}
if (data['query'] == 'changePassword') {
res.json({status: 1})
}
if (data['query'] == 'changeName') {
let response = {
status: 1
}
res.json(response)
}
if (data['query'] == 'addBook') {
res.json({status: 1})
}
if (data['query']=='deleteItemFromCar'){
res.json({status: 1})
}
if (data['query'] == 'getRecent') {
let response = {
status: 1,
data: {
products: [
{
itemId: 1, //商品的id
itemTitle: 'html', //商品的标题
launchDate: '2018-03-12', //发布时间
price: 30, //单价
pictureAddress: 'http://localhost:3001/1.png', //图片地址
}, {
itemId: 2, //商品的id
itemTitle: 'css', //商品的标题
launchDate: '2018-03-12', //发布时间
price: 20, //单价
pictureAddress: 'http://localhost:3001/2.png', //图片地址
}, {
itemId: 3, //商品的id
itemTitle: 'json', //商品的标题
launchDate: '2018-03-13', //发布时间
price: 32, //单价
pictureAddress: 'http://localhost:3001/3.png', //图片地址
}, {
itemId: 4, //商品的id
itemTitle: 'what', //商品的标题
launchDate: '2018-03-12', //发布时间
price: 30, //单价
quantity: 1, //库存
pictureAddress: 'http://localhost:3001/4.png', //图片地址
}
]
}
}
res.json(response)
}
if (data['query'] == 'check') {
res.json({status: 1})
}
if (data['query'] == 'search') {
let item_list = [
{
itemId: 1, //商品的id
itemTitle: 'html', //商品的标题
launchDate: '2018-03-12', //发布时间
price: 30, //单价
pictureAddress: 'http://localhost:3001/1.png', //图片地址
}, {
itemId: 2, //商品的id
itemTitle: 'css', //商品的标题
launchDate: '2018-03-12', //发布时间
price: 20, //单价
pictureAddress: 'http://localhost:3001/2.png', //图片地址
}, {
itemId: 3, //商品的id
itemTitle: 'json', //商品的标题
launchDate: '2018-03-13', //发布时间
price: 32, //单价
pictureAddress: 'http://localhost:3001/3.png', //图片地址
}, {
itemId: 4, //商品的id
itemTitle: 'what', //商品的标题
launchDate: '2018-03-12', //发布时间
price: 30, //单价
quantity: 1, //库存
pictureAddress: 'http://localhost:3001/4.png', //图片地址
}
]
let response = {
status: 1,
data: {
item_list: item_list
}
}
res.json(response)
}
if (data['query'] == 'getBuyOrder') {
let response = {
status: 1,
data: {
orderList: [
{
orderId: "0001",
purchaseTime: "2018-01-08 13:02",
itemTitle: "三体",
pictureAddress: "http://localhost:3001/1.png",
orderState: "等待发货",
totalPrice: "40",
price: "30",
quantity: "1",
deliveryTime: "",
receiptTime: "",
expressCompany: "",
expressCode: "",
postage: "10",
addressId: "123"
},
{
orderId: "0002",
purchaseTime: "2018-01-08 13:02",
itemTitle: "物理",
pictureAddress: "http://localhost:3001/2.png",
orderState: "等待付款",
totalPrice: "40",
price: "30",
quantity: "1",
deliveryTime: "",
receiptTime: "",
expressCompany: "",
expressCode: "",
postage: "10",
addressId: "123"
},
{
orderId: "0003",
purchaseTime: "2018-01-08 13:02",
itemTitle: "三体",
pictureAddress: "http://localhost:3001/1.png",
orderState: "等待收货",
totalPrice: "40",
price: "30",
quantity: "1",
deliveryTime: "",
receiptTime: "",
expressCompany: "",
expressCode: "",
postage: "",
addressId: ""
}
]
}
}
res.json(response)
}
if (data['query'] == 'cancelOrder'){
res.json({status:1})
}
if (data['query'] == 'getSellOrder') {
let response = {
status: 1,
data: {
orderList: [
{
orderId: "0001",
purchaseTime: "2018-01-08 13:02",
itemTitle: "三体",
pictureAddress: "http://localhost:3001/1.png",
orderState: "等待发货",
totalPrice: "40",
price: "30",
quantity: "1",
deliveryTime: "",
receiptTime: "",
expressCompany: "",
expressCode: "",
postage: "",
addressId: ""
},
{
orderId: "0002",
purchaseTime: "2018-01-08 13:02",
itemTitle: "三体",
pictureAddress: "http://localhost:3001/1.png",
orderState: "等待付款",
totalPrice: "40",
price: "30",
quantity: "1",
deliveryTime: "",
receiptTime: "",
expressCompany: "",
expressCode: "",
postage: "",
addressId: ""
},
{
orderId: "0003",
purchaseTime: "2018-01-08 13:02",
itemTitle: "三体",
pictureAddress: "http://localhost:3001/1.png",
orderState: "等待收货",
totalPrice: "40",
price: "30",
quantity: "1",
deliveryTime: "",
receiptTime: "",
expressCompany: "",
expressCode: "",
postage: "",
addressId: ""
}
]
}
}
res.json(response)
}
if (data['query'] == 'sendOrder'){
res.json({
status:1
})
}
if (data['query'] == 'getItemDetail') {
let products = [
{
itemId: 1, //商品的id
itemTitle: 'html', //商品的标题
bookName: 'HTML', //书名
author: 'w3school', //作者
press: 'w3school', //出版社
publicationTime: '2018-01', //出版时间
sellerId: 'w3school', //卖家
freePostage: 0, //是否包邮,0/1
launchDate: '2018-03-12', //发布时间
description: '真的好', //描述
price: 30, //单价
quantity: 1, //库存
pictureAddress: 'http://localhost:3001/1.png', //图片地址
}, {
itemId: 2, //商品的id
itemTitle: 'css', //商品的标题
bookName: 'css', //书名
author: 'w3school', //作者
press: 'w3school', //出版社
publicationTime: '2018-01', //出版时间
sellerId: 'w3school', //卖家
freePostage: 1, //是否包邮,0/1
launchDate: '2018-03-12', //发布时间
description: '真的好', //描述
price: 20, //单价
quantity: 2, //库存
pictureAddress: 'http://localhost:3001/2.png', //图片地址
}, {
itemId: 3, //商品的id
itemTitle: 'json', //商品的标题
bookName: 'json', //书名
author: 'w3school', //作者
press: 'w3school', //出版社
publicationTime: '2018-03', //出版时间
sellerId: 'w3school', //卖家
freePostage: 0, //是否包邮,0/1
launchDate: '2018-03-13', //发布时间
description: '真的好', //描述
price: 32, //单价
quantity: 3, //库存
pictureAddress: 'http://localhost:3001/3.png', //图片地址
}, {
itemId: 4, //商品的id
itemTitle: 'what', //商品的标题
bookName: 'what', //书名
author: 'w3school', //作者
press: 'w3school', //出版社
publicationTime: '2018-01', //出版时间
sellerId: 'w3school', //卖家
freePostage: 0, //是否包邮,0/1
launchDate: '2018-03-12', //发布时间
description: '真的好', //描述
price: 30, //单价
quantity: 1, //库存
pictureAddress: 'http://localhost:3001/4.png', //图片地址
}
]
let id = data.data.itemId;
let response = {
status: 1,
data: {
product: products[id - 1]
}
}
res.json(response)
}
if (data['query'] == 'login') {
let response = {
status: 1, //1是登录成功,0是登录失败
data: {
userId: 1, //该用户的用户id
userName: 'tester', //该用户的昵称
sessionId: 'asdfghjkl', //登录成功的时候返回这次登录的sessionId
},
err: '数据库访问失败', //登录失败的时候返回出错原因
};
console.log(response)
res.json(response)
}
if (data['query'] == 'register') {
let response = {
status: 1,
err: "数据库访问失败"
}
// console.log(response)
res.json(response)
}
if (data['query'] == 'getAddress') {
response = {
status: 1, //1是成功,0是失败
data: {
addresses: addresses
}
}
// console.log(response)
res.json(response)
}
if (data['query'] == 'getAddressDetail') {
response = {
status: 1, //1是成功,0是失败
data: {
recipientName: "Foo",
phoneNumber: "110",
addressDetail: "scut",
isDefaultAddress: true,
addressId: 111,
province:'广东',
city:'广州',
addressDetail:'番禺区大学城华南理工大学'
}
}
// console.log(response)
res.json(response)
}
if (data['query'] == 'addAddress') {
let newAddress = {
recipentName:data.data['recipientName'],
phoneNumber:data.data['phoneNumber'],
addressDetail:data.data['addressDetail'],
province:data.data['province'],
city:data.data['city'],
default:false,
addressId:888
}
addresses.push(newAddress)
console.log(addresses)
response = {
status: 1, //1是成功,0是失败
err: '',
data: {
addresses: addresses
}
}
console.log(response)
res.json(response)
}
if (data['query'] == 'deleteAddress') {
let addressId = data.data.addressId
function isDelete(element) {
return element.addressId != addressId
}
addresses = addresses.filter(isDelete)
console.log(addresses)
response = {
status: 1, //1是成功,0是失败
err: '',
data: {
addresses: addresses
}
}
// console.log(response)
res.json(response)
}
if (data['query'] == 'editAddress') {
let addressId = data.data.addressId;
addresses.forEach(element => {
if (element.addressId == data.data.addressId) {
element.recipientName = data.data.recipientName
element.phoneNumber = data.data.phoneNumber,
element.addressDetail = data.data.addressDetail,
element.province = data.data.province,
element.city = data.data.city,
element.isDefaultAddress = data.data.isDefaultAddress
}
});
console.log(addresses)
response = {
status: 1, //1是成功,0是失败
err: '',
data: {
addresses: addresses
}
}
// console.log(response)
res.json(response)
}
if (data['query'] == 'register') {
response = {
status:1,//1是登录成功,0是登录失败
data :{
userId:1,//该用户的用户id
userName:'tester',//该用户的昵称
sessionId:'asdfghjkl',//登录成功的时候返回这次登录的session_id
err:'',//登录失败的时候返回出错原因
}
}
res.json(response)
}
if (data['query'] == 'getShoppingCarList') {
response = {
status: 1, //1是登录成功,0是登录失败
data: {
shoppingCarList: tableData
},
err: ''
}
res.json(response)
}
if (data['query'] == 'changeShoppingCarNums') {
response = {
status: 1, //1是登录成功,0是登录失败
data: {
shoppingCarList: tableData
},
err: ''
}
res.json(response)
}
if (data['query'] == 'submitBill') {
response = {
status:1,//1是成功,0是失败
}
res.json(response)
}
});
app.listen(3001);