const mysql = require('mysql');
const config = require('../config');
const pool = mysql.createPool(config.mysql);
const utils = require('../utils')
const fs = require('fs')
//Sql语句
const commands = {
login: "select password from admin where username = ?",
queryByToken: "select * from admin where token = ?",
updateLastLogin: "update admin set token = ?,lastLoginIp = ?,lastLoginTime = ? where username = ?",
updatePassword: "update admin set password = ? where token = ?",
updateInfo: "update admin set name = ?, sex = ?, email = ?, introduction = ? where token = ?",
updateAvatar: "update admin set avatar = ? where token = ?",
};
// 导出的方法对象
const admin = {
/**
* 管理员登录
* @res 返回登录成功与否的消息
*/
login: function(req, res, next) {
const param = req.body || req.query || req.params;
//取出连接
pool.getConnection(function(err, connection) {
if (err) {
console.log("数据库连接失败")
} else {
connection.query(commands.login, param.username, function(err, row) {
if (err) {
res.send(err);
} else {
if (row[0] && row[0].password == param.password) {
let token = utils.token();
let ip = req.headers['x-forwarded-for'] ||
req.ip ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress || '';
connection.query(commands.updateLastLogin, [token, ip, Date.now(), param.username]);
res.json(utils.builder({ 'token': token }, "", 200));
} else
res.json(401, utils.builder({ isLogin: true }, '账户或密码错误', 401));
}
// 释放连接
connection.release();
});
}
});
},
/**
* 获得管理员信息
*/
getInfo: function(req, res, next) {
const token = req.get('access-token');
//取出连接
pool.getConnection(function(err, connection) {
if (err) {
console.log("数据库连接失败")
} else {
connection.query(commands.queryByToken, token, function(err, row) {
if (err) {
res.send(err);
} else {
if (row[0]) {
res.json(utils.builder(JSON.parse(JSON.stringify(row[0])), "", 200));
} else
res.status(401).json(utils.builder({ isLogin: true }, "没有信息", 401));
}
// 释放连接
connection.release();
});
}
});
},
/**
* 登出
*/
logout: function(req, res, next) {
res.json('logout');
},
/**
* 上头像并存入数据库
*/
uploadAvatar: function(req, res, next) {
const token = req.get('access-token');
if (req.files != [] && req.files != undefined) {
for (let i = 0; i < req.files.length; i++) {
let oldPath = req.files[i].path;
let newFileName = req.files[i].filename;
console.log(req.files[i]);
//存入数据库
pool.getConnection(function(err, connection) {
if (err) {
console.log("数据库连接失败")
} else {
connection.query(commands.queryByToken, token, function(err, row) {
if (!err) {
if (row[0]) {
fs.unlinkSync('uploads\\avatar\\' + row[0].avatar, function() { err })
}
}
});
connection.query(commands.updateAvatar, [newFileName + ".jpg", token], function(err, row) {
if (err) {
res.send(err);
} else {
fs.rename(oldPath, 'uploads\\avatar\\' + newFileName + ".jpg", function(err) {
if (!err) {
res.json(utils.builder({ url: newFileName + ".jpg", status: 'done' }, "", 200))
} else {
res.status(401).json(utils.builder({}, "上传失败", 401));
}
})
}
// 释放连接
connection.release();
});
}
});
}
} else {
res.send('上传的文件为空!');
}
},
/**
* 修改简介
*/
updateInfo: function(req, res, next) {
const param = req.body || req.query || req.params;
const token = req.get('access-token');
//取出连接
pool.getConnection(function(err, connection) {
if (err) {
console.log("数据库连接失败")
} else {
connection.query(commands.updateInfo, [param.name, param.sex, param.email, param.introduction, token],
function(err, row) {
if (err) {
res.send(err);
} else {
if (row.affectedRows == 1) {
res.json(utils.builder({ status: 'done' }, "修改成功", 200));
} else
res.status(401).json(utils.builder({}, '修改失败', 401));
}
// 释放连接
connection.release();
});
}
});
},
}
module.exports = admin;
没有合适的资源?快使用搜索试试~ 我知道了~
基于Vue.js+express+MySQL的全栈图书馆信息管理系统的后端服务器,本科毕业设计。.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共18个文件
js:10个
json:2个
ejs:2个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 187 浏览量
2024-04-12
21:20:53
上传
评论
收藏 96KB ZIP 举报
温馨提示
【资源说明】【毕业设计】 1、该资源内项目代码都是经过测试运行成功,功能正常的情况下才上传的,请放心下载使用。 2、适用人群:主要针对计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、数学、电子信息等)的同学或企业员工下载使用,具有较高的学习借鉴价值。 3、不仅适合小白学习实战练习,也可作为大作业、课程设计、毕设项目、初期项目立项演示等,欢迎下载,互相学习,共同进步!
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 18 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
zero2100
- 粉丝: 165
- 资源: 2464
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)