mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-07 00:47:19 +08:00
模糊查询添加,添加token校验
This commit is contained in:
parent
e933dbadf3
commit
7cf0756eb6
@ -52,7 +52,7 @@ export default {
|
|||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
charset: 'utf8_general_ci',
|
charset: 'utf8_general_ci',
|
||||||
user: 'root',
|
user: 'root',
|
||||||
password: '123456789'
|
password: 'admin'
|
||||||
},
|
},
|
||||||
mongodb: {},
|
mongodb: {},
|
||||||
sqlite: {},
|
sqlite: {},
|
||||||
|
@ -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 {
|
export {
|
||||||
user
|
user
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
|
import * as mysql from "mysql2"
|
||||||
import secret from "../../config"
|
import secret from "../../config"
|
||||||
import * as jwt from "jsonwebtoken"
|
import * as jwt from "jsonwebtoken"
|
||||||
import { createHash } from "crypto"
|
import { createHash } from "crypto"
|
||||||
import Logger from "../../loaders/logger"
|
import Logger from "../../loaders/logger"
|
||||||
import { Request, Response } from "express"
|
import { Request, Response } from "express"
|
||||||
import { createMathExpr } from "svg-captcha"
|
import { createMathExpr } from "svg-captcha"
|
||||||
import { connection } from '../../utils/initMysql'
|
import getFormatDate from "../../utils/date"
|
||||||
|
import { connection } from "../../utils/initMysql"
|
||||||
export interface dataModel {
|
export interface dataModel {
|
||||||
length: number
|
length: number
|
||||||
}
|
}
|
||||||
@ -115,8 +115,9 @@ const register = async (req: Request, res: Response) => {
|
|||||||
info: "账号已被注册"
|
info: "账号已被注册"
|
||||||
})
|
})
|
||||||
} else {
|
} 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) {
|
connection.query(sql, async function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
Logger.error(err)
|
Logger.error(err)
|
||||||
@ -172,16 +173,51 @@ const searchPage = async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模糊查询
|
* @typedef SearchVague
|
||||||
* @route GET /searchVague
|
* @property {string} username.required - 用户名
|
||||||
* @summary 模糊查询
|
|
||||||
* @group searchVague - 模糊查询
|
|
||||||
* @returns {object} 200
|
|
||||||
* @security JWT
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模糊查询(根据用户名)
|
||||||
|
* @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>} SearchVague
|
||||||
|
* @headers {integer} 200.X-Rate-Limit
|
||||||
|
* @headers {string} 200.X-Expires-After
|
||||||
|
* @security JWT
|
||||||
|
*/
|
||||||
|
|
||||||
const searchVague = async (req: Request, res: Response) => {
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,7 +39,7 @@ app.get('/register', (req, res) => {
|
|||||||
register(req, res)
|
register(req, res)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/searchVague', (req, res) => {
|
app.post('/searchVague', (req, res) => {
|
||||||
searchVague(req, res)
|
searchVague(req, res)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
23
backend/src/utils/date.ts
Normal file
23
backend/src/utils/date.ts
Normal file
@ -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<Date | string> {
|
||||||
|
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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user