rax-medical/node-server/core/common.js
2023-11-27 18:28:07 +08:00

126 lines
3.7 KiB
JavaScript

import express from "express";
import mysql from "mysql";
/**
* 401 需要跳转登录验证
* 500 接口错误
*/
const app = express();
const mysqlParams = {
host: '123.57.147.184',
port: '3306',
user: 'root',
password: 'Xg137839',
connectTimeout: 10000
};
// 连接 mysql 数据库
const connection = (database) => {
return mysql.createConnection({
...mysqlParams,
database: database || ''
});
}
// 连接 mysql
const con = mysql.createConnection({
...mysqlParams
});
// 查询数据方法 SELECT * FROM table
const m_get = (table, Clause, order, start, end) => {
return `SELECT * FROM ${table} ${Clause ? 'WHERE ' + Clause : ``} ORDER BY ${order || `ID`} DESC ${!end ? `` : `LIMIT ` + start + `,` + end};`
}
// 插入数据方法 INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
const m_add = (table, field, value) => {
return `INSERT INTO ${table} ( ${field} ) VALUES ( ${value} );`
}
// 更新数据方法 UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
const m_update = (table, data, Clause) => {
return `UPDATE ${table} SET ${data} WHERE ${Clause};`
}
// 删除数据方法 DELETE FROM table_name [WHERE Clause]
const m_delete = (table, Clause) => {
return `DELETE FROM ${table} WHERE ${Clause};`
}
const nowTime = () => {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const date = now.getDate() >= 10 ? now.getDate() : ('0' + now.getDate());
const hour = now.getHours() >= 10 ? now.getHours() : ('0' + now.getHours());
const miu = now.getMinutes() >= 10 ? now.getMinutes() : ('0' + now.getMinutes());
const sec = now.getSeconds() >= 10 ? now.getSeconds() : ('0' + now.getSeconds());
return +year + "-" + (month + 1) + "-" + date + " " + hour + ":" + miu + ":" + sec;
}
/**
* 格式化时间
* @returns
*/
const dateFormater = (formater, time) => {
let date = time ? new Date(time) : new Date(),
Y = date.getFullYear() + '',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
return formater.replace(/YYYY|yyyy/g, Y)
.replace(/YY|yy/g, Y.substr(2, 2))
.replace(/MM/g, (M < 10 ? '0' : '') + M)
.replace(/DD|dd/g, (D < 10 ? '0' : '') + D)
.replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
.replace(/mm/g, (m < 10 ? '0' : '') + m)
.replace(/ss/g, (s < 10 ? '0' : '') + s)
}
// 获取数据库列表
const getDatabases = () => app.post('/getdatabases', function (req, res) {
// con.connect();
con.query("SHOW DATABASES", function (error, results, fields) {
if (error) {
console.log(`${nowTime()} 错误:${JSON.stringify(error)}`);
return res.status(500).send(error);
} else {
res.send(results);
// con.end();
}
});
});
// 公共查询方法
const mysqlQuery = (res, database, sql) => {
if (!database) return res.status(500).send('database is ' + database);
try {
connection(database).connect();
connection(database).query(sql, (error, results, fields) => {
if (error) {
console.log(`${nowTime()} 错误:${JSON.stringify(error)}`);
return res.status(500).send(error);
} else {
res.send(results);
}
connection(database).end();
});
} catch (error) {
console.log(error);
return res.status(500).send('连接失败', error);
}
}
export default {
app,
connection,
con,
mysqlQuery,
m_get,
m_add,
m_update,
m_delete,
nowTime,
dateFormater,
getDatabases
};