diff --git a/www2/main.js b/www2/main.js
index 41a6972..958cac3 100644
--- a/www2/main.js
+++ b/www2/main.js
@@ -11,7 +11,7 @@ const multer = require('multer');
var connection = mysql.createConnection({
host : 'localhost',
- port : '13307 ',
+ port : '13307',
user : 'root',
password : '123456',
database : 'mailbox'
@@ -25,7 +25,7 @@ app.use(express.static('static'));
app.get('/', function (req, res) {
//var result_string = build_html_head('识别系统') + '
欢迎来到识别系统!
\r\n';
- var result_string = build_html_head('识别系统') + '欢迎来到识别系统!
\r\n';
+ var result_string = build_html_head('识别系统') + ' 欢迎来到识别系统!
\r\n';
result_string +='\r\n';
result_string +='用户名 | 密码 | 身份 | 邮箱 |
\r\n';
connection.query('SELECT * FROM mail;', function (error, results, fields) {
@@ -67,6 +67,14 @@ app.use(session({ // 这里是在Express应用
}));
+app.get('/user-info', function(req, res) {
+ if (req.session.user) {
+ res.json(req.session.user);
+ } else {
+ res.status(401).json({ message: '未登录' });
+ }
+});
+
app.post('/login', urlencodedParser, function(req, res) {//用户登录功能
var username = req.body.username;
var password = req.body.passwd;
@@ -77,16 +85,25 @@ app.post('/login', urlencodedParser, function(req, res) {//用户登录功能
res.send('数据库查询错误!'); // 可以重定向到登录页面或其他处理方式
return;
}
- if (results.length > 0) {// 登录成功
- res.redirect('/work1.html?username=' + username + '&userid=' + results[0].id); // 登录成功后重定向到系统主页面
- }
- else {// 登录失败,可以重定向到登录页面并显示错误信息
- console.log('[用户名或密码不正确]');
- res.send('');
+ if (results.length > 0) {
+ // 登录成功
+ req.session.user = {
+ id: results[0].id,
+ username: username
+ };
+ res.redirect('/work1.html');
+ } else {// 登录失败,可以重定向到登录页面并显示错误信息
+ console.log('[用户名或密码不正确]');
+ res.send('');
}
});
})
+app.get('/logout', function(req, res) {//用户退出登录功能
+ req.session.user = null
+ res.redirect('/welcome.html');
+})
+
app.post('/idcheck', urlencodedParser, function(req, res) {//处理一个POST请求,路径为/idcheck,并传入一个中间件urlencodedParser用于解析POST请求中的表单数据。回调函数包含了请求对象req和响应对象res。
var username = req.body.username;
@@ -119,7 +136,6 @@ app.post('/insertrecord', urlencodedParser, function (req, res) {//注册用户
})
-
app.get('/api/users', function (req, res) {
// 查询所有用户
connection.query('SELECT * FROM mail', function (error, users) {
@@ -211,8 +227,6 @@ app.post('/api/deleteUser', urlencodedParser, function (req, res) {
});
-
-
function build_html_head(title) {//页面头创建
var html_fragment = '\r\n';
diff --git a/www2/main2.js b/www2/main2.js
index 449ef23..c58706d 100644
--- a/www2/main2.js
+++ b/www2/main2.js
@@ -21,6 +21,7 @@ var connection = mysql.createConnection({
app.use(cors())
+
//上传并识别功能
const upload = multer({ dest: 'uploads/' }); // 上传文件的临时存储目录 // Multer是一个Node.js中间件,用于处理multipart/form-data类型的表单数据
app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件上传
@@ -30,7 +31,9 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
const file = req.file;
const tempFilePath = file.path;
+ const fileHash = req.query.fileHash;
+ console.log(fileHash, 'fileHash')
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(tempFilePath);
@@ -38,7 +41,6 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
hash.update(data);
});
input.on('end', () => {
- const fileHash = hash.digest('hex');
const fileName = file.originalname;
const filePath = path.join(__dirname, 'uploads', fileName);
const user_name = req.query.user_name
@@ -52,6 +54,7 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
if (result.length > 0) {
// If file with same hash exists, return "has been uploaded"
+ console.log(result[0], 'result[0]')
fs.unlinkSync(tempFilePath); // Delete the uploaded file
return res.json({ message: 'File with same hash already uploaded', fileName: '识别成功,结果为:' + result[0].goods});//如果文件已经存在,则返回这个文件的对应的匹配结果
} else {
diff --git a/www2/main3.js b/www2/main3.js
index 93dfff8..6f855bb 100644
--- a/www2/main3.js
+++ b/www2/main3.js
@@ -30,7 +30,9 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
const file = req.file;
const tempFilePath = file.path;
+ const fileHash = req.query.fileHash;
+ console.log(fileHash, 'fileHash')
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(tempFilePath);
@@ -38,7 +40,6 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
hash.update(data);
});
input.on('end', () => {
- const fileHash = hash.digest('hex');
const fileName = file.originalname;
const filePath = path.join(__dirname, 'uploads', fileName);
const user_name = req.query.user_name
@@ -52,6 +53,7 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
if (result.length > 0) {
// If file with same hash exists, return "has been uploaded"
+ console.log(result[0], 'result[0]')
fs.unlinkSync(tempFilePath); // Delete the uploaded file
return res.json({ message: 'File with same hash already uploaded', fileName: '识别成功,结果为:' + result[0].goods});//如果文件已经存在,则返回这个文件的对应的匹配结果
} else {
diff --git a/www2/main4.js b/www2/main4.js
index 2e88401..bdac3ba 100644
--- a/www2/main4.js
+++ b/www2/main4.js
@@ -30,7 +30,9 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
const file = req.file;
const tempFilePath = file.path;
+ const fileHash = req.query.fileHash;
+ console.log(fileHash, 'fileHash')
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(tempFilePath);
@@ -38,7 +40,6 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
hash.update(data);
});
input.on('end', () => {
- const fileHash = hash.digest('hex');
const fileName = file.originalname;
const filePath = path.join(__dirname, 'uploads', fileName);
const user_name = req.query.user_name
@@ -52,6 +53,7 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
if (result.length > 0) {
// If file with same hash exists, return "has been uploaded"
+ console.log(result[0], 'result[0]')
fs.unlinkSync(tempFilePath); // Delete the uploaded file
return res.json({ message: 'File with same hash already uploaded', fileName: '识别成功,结果为:' + result[0].goods});//如果文件已经存在,则返回这个文件的对应的匹配结果
} else {
diff --git a/www2/main5.js b/www2/main5.js
index 19947f6..1b0ce76 100644
--- a/www2/main5.js
+++ b/www2/main5.js
@@ -30,7 +30,9 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
const file = req.file;
const tempFilePath = file.path;
+ const fileHash = req.query.fileHash;
+ console.log(fileHash, 'fileHash')
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(tempFilePath);
@@ -38,7 +40,6 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
hash.update(data);
});
input.on('end', () => {
- const fileHash = hash.digest('hex');
const fileName = file.originalname;
const filePath = path.join(__dirname, 'uploads', fileName);
const user_name = req.query.user_name
@@ -52,6 +53,7 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
if (result.length > 0) {
// If file with same hash exists, return "has been uploaded"
+ console.log(result[0], 'result[0]')
fs.unlinkSync(tempFilePath); // Delete the uploaded file
return res.json({ message: 'File with same hash already uploaded', fileName: '识别成功,结果为:' + result[0].goods});//如果文件已经存在,则返回这个文件的对应的匹配结果
} else {
diff --git a/www2/main6.js b/www2/main6.js
index 3fec600..a7b26a0 100644
--- a/www2/main6.js
+++ b/www2/main6.js
@@ -30,7 +30,9 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
const file = req.file;
const tempFilePath = file.path;
+ const fileHash = req.query.fileHash;
+ console.log(fileHash, 'fileHash')
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(tempFilePath);
@@ -38,11 +40,10 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
hash.update(data);
});
input.on('end', () => {
- const fileHash = hash.digest('hex');
const fileName = file.originalname;
const filePath = path.join(__dirname, 'uploads', fileName);
const user_name = req.query.user_name
- // 检查是否相同哈西值的数据存在
+ // Check if file with same hash already exists in database
const sqlCheck = 'SELECT * FROM images WHERE hash = ?';
connection.query(sqlCheck, [fileHash], function(err, result) {
if (err) {
@@ -52,6 +53,7 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
if (result.length > 0) {
// If file with same hash exists, return "has been uploaded"
+ console.log(result[0], 'result[0]')
fs.unlinkSync(tempFilePath); // Delete the uploaded file
return res.json({ message: 'File with same hash already uploaded', fileName: '识别成功,结果为:' + result[0].goods});//如果文件已经存在,则返回这个文件的对应的匹配结果
} else {
@@ -76,7 +78,7 @@ app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件
});
-var server = app.listen(8087, '0.0.0.0', function () {
+var server = app.listen(8088, '0.0.0.0', function () {
var host = server.address().address
var port = server.address().port
@@ -89,4 +91,4 @@ var server = app.listen(8087, '0.0.0.0', function () {
console.log('[connection connect] succeed!');
});
console.log("应用实例,访问地址为 http://%s:%s", host, port)
-})
+})
\ No newline at end of file
diff --git a/www2/node_modules/.package-lock.json b/www2/node_modules/.package-lock.json
index 4c58d3a..e8392d0 100644
--- a/www2/node_modules/.package-lock.json
+++ b/www2/node_modules/.package-lock.json
@@ -1,5 +1,5 @@
{
- "name": "www2",
+ "name": "草药识别系统",
"lockfileVersion": 3,
"requires": true,
"packages": {
diff --git a/www2/package-lock.json b/www2/package-lock.json
index 1754865..a72ae3d 100644
--- a/www2/package-lock.json
+++ b/www2/package-lock.json
@@ -1,5 +1,5 @@
{
- "name": "www2",
+ "name": "草药识别系统",
"lockfileVersion": 3,
"requires": true,
"packages": {
diff --git a/www2/static/login.js b/www2/static/login.js
new file mode 100644
index 0000000..45beb3d
--- /dev/null
+++ b/www2/static/login.js
@@ -0,0 +1,13 @@
+fetch('/user-info')
+.then(response => {
+ if (!response.ok) {
+ throw new Error('未登录');
+ }
+ return response.json();
+})
+.then(data => {
+})
+.catch(error => {
+ alert('未登录')
+ window.location.href = 'welcome.html';
+});
\ No newline at end of file
diff --git a/www2/static/work1.html b/www2/static/work1.html
index 18fc2f0..accdd8f 100644
--- a/www2/static/work1.html
+++ b/www2/static/work1.html
@@ -119,6 +119,10 @@
max-width: 1000px;
max-height: 600px;
}
+ a {
+ color: #000;
+ text-decoration: none;
+ }
+
返回模块选择
@@ -316,39 +332,68 @@
电子邮件:aa893824054@163.com
电话:184-7635-1355
-
+
\r\n';
+ return html_fragment;
+}
+function build_html_foot() {//no'de页面尾部
+ return '
diff --git a/www2/uploads/1.jpg b/www2/uploads/1.jpg
deleted file mode 100644
index ad07db7..0000000
Binary files a/www2/uploads/1.jpg and /dev/null differ
diff --git a/www2/uploads/1000001044.jpg b/www2/uploads/1000001044.jpg
deleted file mode 100644
index bc2d656..0000000
Binary files a/www2/uploads/1000001044.jpg and /dev/null differ
diff --git a/www2/uploads/1720487843718.jpg b/www2/uploads/1720487843718.jpg
deleted file mode 100644
index 707329a..0000000
Binary files a/www2/uploads/1720487843718.jpg and /dev/null differ
diff --git a/www2/uploads/2.jfif b/www2/uploads/2.jfif
new file mode 100644
index 0000000..2667038
Binary files /dev/null and b/www2/uploads/2.jfif differ
diff --git a/www2/uploads/20240707203524.png b/www2/uploads/20240707203524.png
deleted file mode 100644
index 9a8a2c3..0000000
Binary files a/www2/uploads/20240707203524.png and /dev/null differ
diff --git a/www2/uploads/36392d5e768d1acfd38dd149379b98fc b/www2/uploads/36392d5e768d1acfd38dd149379b98fc
new file mode 100644
index 0000000..2667038
Binary files /dev/null and b/www2/uploads/36392d5e768d1acfd38dd149379b98fc differ
diff --git a/www2/uploads/5.PNG b/www2/uploads/5.PNG
deleted file mode 100644
index 00abfad..0000000
Binary files a/www2/uploads/5.PNG and /dev/null differ
diff --git a/www2/uploads/77697cfc8f785d500e26da369bf39c27 b/www2/uploads/77697cfc8f785d500e26da369bf39c27
new file mode 100644
index 0000000..2667038
Binary files /dev/null and b/www2/uploads/77697cfc8f785d500e26da369bf39c27 differ
diff --git a/www2/uploads/Zombatar_1.jpg b/www2/uploads/Zombatar_1.jpg
deleted file mode 100644
index 5a52b02..0000000
Binary files a/www2/uploads/Zombatar_1.jpg and /dev/null differ
diff --git a/www2/uploads/zh1112211121u.png b/www2/uploads/zh1112211121u.png
deleted file mode 100644
index 1bd5381..0000000
Binary files a/www2/uploads/zh1112211121u.png and /dev/null differ
diff --git a/www2/uploads/zh1112221u.png b/www2/uploads/zh1112221u.png
deleted file mode 100644
index 88f81a9..0000000
Binary files a/www2/uploads/zh1112221u.png and /dev/null differ
diff --git a/www2/uploads/zh111u.jpg b/www2/uploads/zh111u.jpg
deleted file mode 100644
index 9603542..0000000
Binary files a/www2/uploads/zh111u.jpg and /dev/null differ
diff --git a/www2/www2/main.js b/www2/www2/main.js
new file mode 100644
index 0000000..811530d
--- /dev/null
+++ b/www2/www2/main.js
@@ -0,0 +1,210 @@
+var mysql = require('mysql');
+var express = require('express');
+var app = express();
+var bodyParser = require('body-parser');
+const crypto = require('crypto');
+const fs = require('fs');
+const path = require('path');
+const fileUpload = require('express-fileupload');
+const session = require('express-session');
+const multer = require('multer');
+
+var connection = mysql.createConnection({
+ host : 'localhost',
+ port : '13307',
+ user : 'root',
+ password : '123456',
+ database : 'mailbox'
+});
+
+
+// 创建 application/x-www-form-urlencoded 编码解析
+var urlencodedParser = bodyParser.urlencoded({ extended: false })
+app.use(express.static('static'));
+
+
+app.get('/', function (req, res) {
+ //var result_string = build_html_head('识别系统') + '
欢迎来到识别系统!
\r\n';
+ var result_string = build_html_head('识别系统') + '
欢迎来到识别系统!
\r\n';
+ result_string +='
\r\n';
+ result_string +='用户名 | 密码 | 身份 | 邮箱 |
\r\n';
+ connection.query('SELECT * FROM mail;', function (error, results, fields) {
+ if (error) {
+ console.log('[SELECT ERROR] - ',error.message);
+ return;
+ }
+ console.log('[SELECT SUCCESS]');
+ for (var i = results.length - 1; i >= 0; i--) {
+ result_string +='\r\n';
+ result_string += (''
+ + results[i].name
+ + ' | '
+ + results[i].passwd
+ + ' | '
+ + results[i].id
+ + ' | '
+ + results[i].msg
+ + ' | \r\n');
+ result_string +='
\r\n';
+ }
+ result_string +='
\r\n';
+ result_string +='注册账户\r\n'
+ result_string +='
\r\n';
+ result_string +='
前往登陆\r\n';
+ result_string +='
\r\n';
+ res.send(result_string + build_html_foot())
+ });
+})
+
+
+
+//上传并识别功能
+const upload = multer({ dest: 'uploads/' }); // 上传文件的临时存储目录 // Multer是一个Node.js中间件,用于处理multipart/form-data类型的表单数据
+app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件上传
+ if (!req.file) {
+ return res.status(400).send('No file uploaded.');
+ }
+
+ const file = req.file;
+ const tempFilePath = file.path;
+
+ const hash = crypto.createHash('sha256');
+ const input = fs.createReadStream(tempFilePath);
+
+ input.on('data', (data) => {
+ hash.update(data);
+ });
+
+ input.on('end', () => {
+ const fileHash = hash.digest('hex');
+ const fileName = file.originalname;
+ const filePath = path.join(__dirname, 'uploads', fileName);
+
+ // Check if file with same hash already exists in database
+ const sqlCheck = 'SELECT name FROM images WHERE hash = ?';
+ connection.query(sqlCheck, [fileHash], function(err, result) {
+ if (err) {
+ console.log(err);
+ return res.status(500).send('Error checking file in database');
+ }
+
+ if (result.length > 0) {
+ // If file with same hash exists, return "has been uploaded"
+ fs.unlinkSync(tempFilePath); // Delete the uploaded file
+ return res.json({ message: 'File with same hash already uploaded', fileName: 'hasbeen uploaded'});
+ } else {
+ fs.rename(tempFilePath, filePath, function(err) {
+ if (err) {
+ console.log(err);
+ return res.status(500).send('Error uploading file');
+ } else {
+ const sql = 'INSERT INTO images (name, path, hash) VALUES (?, ?, ?)';
+ connection.query(sql, [fileName, filePath, fileHash], function(err, result) {
+ if (err) {
+ console.log(err);
+ return res.status(500).send('Error uploading file to database');
+ }
+ res.json({ message: 'File uploaded successfully', fileName: fileName });
+ });
+ }
+ });
+ }
+ });
+ });
+});
+
+
+app.use(session({ // 这里是在Express应用程序中使用会话中间件,并传入一个包含会话配置选项的对象。
+ secret: 'your_secret_key', // 指定会话的密钥,用于加密会话数据,确保会话数据的安全性。
+ resave: false, // 在会话数据没有发生变化的情况下,是否强制重新保存会话数据。这里设置为false,表示不强制重新保存。
+ saveUninitialized: true, // 是否在会话初始化时保存未修改过的会话。设置为true,表示保存未修改过的会话。
+ cookie: { // 配置会话的cookie参数,
+ maxAge: 60000 // 指定了会话的过期时间,单位是毫秒。在这里,会话会在60秒(即60000毫秒)后过期。
+ }
+}));
+
+
+app.post('/login', urlencodedParser, function(req, res) {//用户登录功能
+ var username = req.body.username;
+ var password = req.body.passwd;
+ // 查询数据库中是否存在匹配的用户名和密码
+ connection.query('SELECT * FROM mail WHERE name = ? AND passwd = ?', [username, password], function(error, results, fields) {
+ if (error) {
+ console.log('[SELECT ERROR] - ', error.message);
+ res.send('数据库查询错误!'); // 可以重定向到登录页面或其他处理方式
+ return;
+ }
+ if (results.length > 0) {
+ // 登录成功
+ //req.session.username = username; // 将用户名存储在session中
+ res.redirect('/work1.html?username=' + username + '&userid=' + results[0].id); // 登录成功后重定向到系统主页面
+ } else {// 登录失败,可以重定向到登录页面并显示错误信息
+ console.log('[用户名或密码不正确]');
+ res.send('');
+ }
+ });
+})
+
+
+app.post('/idcheck', urlencodedParser, function(req, res) {//处理一个POST请求,路径为/idcheck,并传入一个中间件urlencodedParser用于解析POST请求中的表单数据。回调函数包含了请求对象req和响应对象res。
+ var username = req.body.username;
+ connection.query('SELECT id FROM mail WHERE name = ?', [username], function(error, results, fields) {//查询结果通过回调函数传递给results
+ if (error) {
+ console.log('[SELECT ERROR] - ', error.message);
+ res.send('数据库查询错误!');
+ return;
+ }
+ if (results.length > 0 && results[0].id === 'admin') {//在查询结果中,如果存在匹配的用户且该用户的ID为admin,则返回一个包含用户ID为admin的响应对象。
+ res.send({ userid: 'admin' });//响应对象res返回一个包含用户ID为admin的JSON对象。
+ } else {//如果查询结果为空或者查询到的用户ID不是admin,则返回一个空的用户ID。
+ res.send({ userid: '' });
+ }
+ });
+});
+
+
+app.post('/insertrecord', urlencodedParser, function (req, res) {//注册用户功能
+ var insert_sql = 'INSERT INTO mail(name, passwd, id, msg) VALUES(?,?,?,?)';
+ var insert_params = [req.body.name, req.body.passwd, req.body.id, req.body.msg];
+
+ connection.query(insert_sql, insert_params, function(error, result) {
+ if (error) {
+ console.log('[INSERT ERROR] - ',error.message);
+ return;
+ }
+ console.log('[INSERT SUCCESS]');
+ });
+ res.redirect('/');
+})
+
+
+function build_html_head(title) {//页面头创建
+ var html_fragment = '\r\n';
+ html_fragment += '\r\n';
+ html_fragment += '
\r\n';
+ html_fragment += ' \r\n';
+ html_fragment += '
' + title +'\r\n';
+ html_fragment += ' \r\n';
+ html_fragment += '\r\n';
+ html_fragment += '
\r\n