diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index c14c8d890..01fd4d15a 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -52,7 +52,7 @@ export default { host: 'localhost', charset: 'utf8_general_ci', user: 'root', - password: '123456789' + password: 'admin' }, mongodb: {}, sqlite: {}, diff --git a/backend/src/models/mysql/index.ts b/backend/src/models/mysql/index.ts index 6efc09ad0..f8749910a 100644 --- a/backend/src/models/mysql/index.ts +++ b/backend/src/models/mysql/index.ts @@ -1,5 +1,5 @@ // 创建用户表 -const user = 'CREATE TABLE if not EXISTS users(id int PRIMARY key auto_increment,username varchar(32),password varchar(32))' +const user = 'CREATE TABLE if not EXISTS users(id int PRIMARY key auto_increment,username varchar(32),password varchar(32),time DATETIME)' export { user diff --git a/backend/src/router/api/mysql.ts b/backend/src/router/api/mysql.ts index fb04afca7..012ed8ac7 100644 --- a/backend/src/router/api/mysql.ts +++ b/backend/src/router/api/mysql.ts @@ -1,12 +1,12 @@ - +import * as mysql from "mysql2" import secret from "../../config" import * as jwt from "jsonwebtoken" import { createHash } from "crypto" import Logger from "../../loaders/logger" import { Request, Response } from "express" import { createMathExpr } from "svg-captcha" -import { connection } from '../../utils/initMysql' - +import getFormatDate from "../../utils/date" +import { connection } from "../../utils/initMysql" export interface dataModel { length: number } @@ -115,8 +115,9 @@ const register = async (req: Request, res: Response) => { info: "账号已被注册" }) } else { - let sql: string = 'insert into users (username,password) value(' + "'" + username + "'" + ',' + "'" + createHash('md5').update(password).digest('hex') + - "'" + ')' + let time = await getFormatDate() + let sql: string = 'insert into users (username,password,time) value(' + "'" + username + "'" + ',' + "'" + createHash('md5').update(password).digest('hex') + + "'" + ',' + "'" + time + "'" + ')' connection.query(sql, async function (err) { if (err) { Logger.error(err) @@ -172,16 +173,51 @@ const searchPage = async (req: Request, res: Response) => { } /** - * 模糊查询 - * @route GET /searchVague - * @summary 模糊查询 - * @group searchVague - 模糊查询 - * @returns {object} 200 - * @security JWT + * @typedef SearchVague + * @property {string} username.required - 用户名 */ +/** +* 模糊查询(根据用户名) +* @route POST /searchVague +* @param {SearchVague.model} point.body.required - the new point +* @produces application/json application/xml +* @consumes application/json application/xml +* @returns {Response.model} 200 +* @returns {Array.} SearchVague +* @headers {integer} 200.X-Rate-Limit +* @headers {string} 200.X-Expires-After +* @security JWT +*/ + const searchVague = async (req: Request, res: Response) => { - res.json({ code: 1, msg: "成功" }) + const { username } = req.body + let payload = null + try { + const authorizationHeader = req.get("Authorization") + const accessToken = authorizationHeader.substr("Bearer ".length) + payload = jwt.verify(accessToken, secret.jwtSecret) + } catch (error) { + return res.status(401).end() + } + if (username === "" || username === null) return res.json({ + code: -1, + info: "搜索信息不能为空" + }) + let sql = 'select * from users' + sql += " WHERE username LIKE " + mysql.escape("%" + username + "%") + connection.query(sql, function (err, data) { + connection.query(sql, async function (err) { + if (err) { + Logger.error(err) + } else { + await res.json({ + code: 0, + info: data + }) + } + }) + }) } /** diff --git a/backend/src/server.ts b/backend/src/server.ts index 38dcb1639..29526b26e 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -39,7 +39,7 @@ app.get('/register', (req, res) => { register(req, res) }) -app.get('/searchVague', (req, res) => { +app.post('/searchVague', (req, res) => { searchVague(req, res) }) diff --git a/backend/src/utils/date.ts b/backend/src/utils/date.ts new file mode 100644 index 000000000..f5183a719 --- /dev/null +++ b/backend/src/utils/date.ts @@ -0,0 +1,23 @@ +interface dateModel { + getMonth: () => any + getDate: () => string | number + getFullYear: () => string | number + getHours: () => string | number + getMinutes: () => string | number + getSeconds: () => string | number +} + +export default async function getFormatDate(): Promise { + let date: dateModel = new Date() + let month: string | number = date.getMonth() + 1 + let strDate = date.getDate() + if (month >= 1 && month <= 9) { + month = "0" + month + } + if (strDate >= 0 && strDate <= 9) { + strDate = "0" + strDate + } + let currentDate = date.getFullYear() + "-" + month + "-" + strDate + + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + return currentDate +} \ No newline at end of file