jk-2024.07.10-00:08

pull/7/head
陈@jkccchen123 7 months ago
parent 500a01d8b9
commit 1e71dc46d6

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 546 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 149 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 378 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 406 KiB

@ -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('识别系统') + '<h2>欢迎来到识别系统!</h2>\r\n';
var result_string = build_html_head('识别系统') + '<h2 style="text-align:center;">欢迎来到识别系统!</h2>\r\n';
var result_string = build_html_head('识别系统') + '<script src="/login.js"></script> <h2 style="text-align:center;">欢迎来到识别系统!</h2>\r\n';
result_string +='<table width="90%" class="table">\r\n';
result_string +='<tr><th>用户名</th><th>密码</th><th>身份</th><th>邮箱</th></tr>\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('<script>alert("用户名或密码不正确");window.location.href="welcome.html";</script>');
if (results.length > 0) {
// 登录成功
req.session.user = {
id: results[0].id,
username: username
};
res.redirect('/work1.html');
} else {// 登录失败,可以重定向到登录页面并显示错误信息
console.log('[用户名或密码不正确]');
res.send('<script>alert("用户名或密码不正确");window.location.href="welcome.html";</script>');
}
});
})
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 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 '
+'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\r\n';

@ -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 {

@ -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 {

@ -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 {

@ -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 {

@ -0,0 +1,94 @@
var mysql = require('mysql');
var express = require('express');
var cors = require('cors');
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'
});
app.use(cors())
//上传并识别功能
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 fileHash = req.query.fileHash;
console.log(fileHash, 'fileHash')
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(tempFilePath);
input.on('data', (data) => {
hash.update(data);
});
input.on('end', () => {
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) {
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"
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 {
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, username) VALUES (?, ?, ?, ?)';
connection.query(sql, [fileName, filePath, fileHash, user_name], 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 });
});
}
});
}
});
});
});
var server = app.listen(8088, '0.0.0.0', function () {
var host = server.address().address
var port = server.address().port
connection.connect(function(err){
if(err){
console.log('[query] - :'+err);
return;
}
console.log('[connection connect] succeed!');
});
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})

@ -1,5 +1,5 @@
{
"name": "www2",
"name": "草药识别系统",
"lockfileVersion": 3,
"requires": true,
"packages": {

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save