-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
232 lines (212 loc) · 6.4 KB
/
app.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
var express = require('express');
var app = express();
var bodyParser = require('body-parser'); //
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const Web3 = require('web3');
var crypto = require('crypto');
var formidable = require('formidable');
var fs = require('fs');
var cookie = require('cookie');
var system = "0x688c9c79017dCC57A33cDc26bbc6AA23d1cA3321";
let Database = require('./lib/database.js');
users_database = new Database();
goods_database = new Database();
//var multer = require('multer');
app.use(express.static('public')); //get the html, css and js
app.use(bodyParser.json()); //
app.use(bodyParser.urlencoded({ extended: false }));
init();
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("http://140.113.207.54:7545"));
}
async function init() {
//console.log(Object.getOwnPropertyNames(cookie));
users_db = await users_database.connect("mongodb://localhost:27017/", 'points', 'test');
//users_db = await users_database.connect("mongodb://localhost:27017/", 'points', 'users');
console.log('connect to users db.');
goods_db = await goods_database.connect("mongodb://localhost:27017/", 'points', 'goods');
console.log('connect to goods db.');
app.listen(7000, function () {
console.log("listening on port 7000");
});
}
function MD5(str){
var md5 = crypto.createHash('md5');
return md5.update(str).digest('hex');
}
app.post('/registering', function (req, res) {
//Check if the account has been used, if not add new user, or return the current page
var users_data = req.body;
console.log(users_data);
users_data.password = MD5(users_data.password);
var err = false;
users_db.contain({account:users_data.account}).then(function(result){
if(result){
console.log("This account has been used!");
err = true;
}
else{
var start = Date.now();
web3.eth.personal.newAccount('', (err, result) => {
var end = Date.now();
var spend = end - start;
console.log("spend: " + spend);
if (err) throw err;
users_data.address = result;
console.log(users_data);
console.log("Register OK!");
users_db.insert(users_data);
res.cookie("identity", users_data.identity);
res.cookie("username", users_data.username);
res.cookie("address", users_data.address);
res.cookie("account", users_data.account);
if(err){
res.redirect("/register.html");
}
else{
//check the identity to redirect to the pages
var identity = req.body.identity;
if(identity == 1){
res.redirect("/userpage.html");
}
else if(identity == 2){
res.redirect("/point-provider-page.html");
}
else if(identity == 3){
res.redirect("/goods-provider-page.html");
}
else{
res.redirect("/index.html");
}
}
});
}
});
});
app.post('/login', function (req, res) {
console.log(req.body);
//connect to database to check who it is
var account = req.body.account;
var password = req.body.password;
var hashPassword = MD5(password);
var identity = -1;
users_db.find({account:account, password:hashPassword}).then(function(result){
if(result != null){
identity = result.identity;
res.cookie("identity", identity);
res.cookie("username", result.username);
res.cookie("address", result.address);
res.cookie("account", result.account);
}
else{
console.log("This user doesn't exist!");
}
if(identity == 1){
res.redirect("/userpage.html");
}
else if(identity == 2){
res.redirect("/point-provider-page.html");
}
else if(identity == 3){
res.redirect("/goods-provider-page.html");
}
else{
res.redirect("/index.html");
}
});
});
app.post('/check', function (req, res){
console.log(req.body);
console.log('req received');
var user = req.body.receiver;
var answer;
users_db.find({account:user}).then(function(result){
if(result != null){
answer = {isExisted : true, address : result.address};
answer = JSON.stringify(answer);
}
else{
answer = {isExisted : false};
answer = JSON.stringify(answer);
}
}).then(function(){
res.contentType('json');
res.send(answer);
});
});
app.post('/increaseGoods', function(req, res){
var form = new formidable.IncomingForm();
form.encoding = 'utf-8'; //设置编辑
form.uploadDir = 'public/file/' ; //设置上传目录
form.keepExtensions = true; //保留后缀
form.maxFieldsSize = 2 * 1024 * 1024; //文件大小
form.parse(req, function(err, fields, files) {
var answer;
if(err) {
answer = {Done: false};
answer = JSON.stringify(answer);
return res.send(answer);
}
console.log('received fields: ');
console.log(fields);
var goodsInfo = fields;
var owner = fields.owner;
console.log('received files: ');
console.log(files);
//insert file to db
//gfs? mongo?
var extName = 'jpg'; //后缀名
switch (files.inputfile.type) {
case 'image/pjpeg':
extName = 'jpg';
break;
case 'image/jpeg':
extName = 'jpg';
break;
case 'image/png':
extName = 'png';
break;
case 'image/x-png':
extName = 'png';
break;
}
var date = new Date();
var fileName = owner + '_' + date.getTime() + '.' + extName;
var newPath = form.uploadDir + fileName;
console.log(newPath);
fs.renameSync(files.inputfile.path, newPath); //重命名
goodsInfo.filename = fileName;
console.log(goodsInfo);
goods_db.insert(goodsInfo).then(function(){
answer = {Done: true};
answer = JSON.stringify(answer);
return res.send(answer);
});
});
});
app.post('/getImg', function(req, res){
console.log(req.body);
var owner = req.body.owner;
//{owner:owner}
goods_db.list(req.body).then(function(result){
res.send(result);
});
});
app.post('/deleteGoods', function(req, res){
console.log(req.body);
var id = req.body._id;
var filename = req.body.filename;
var data = {_id:ObjectID(id), filename:filename};
/*goods_db.find(data).then(function(result){
console.log(result);
res.send(result);
});*/
fs.unlinkSync("public/file/"+ filename);
goods_db.delete(data).then(function(result){
//console.log(result);
res.send(result);
});
});