Переглянути джерело

模糊查询添加,添加token校验

zhangyiming 4 роки тому
батько
коміт
7cf0756eb6

+ 1 - 1
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: {},

+ 1 - 1
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

+ 48 - 12
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>} 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
+        })
+      }
+    })
+  })
 }
 
 /**

+ 1 - 1
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)
 })
 

+ 23 - 0
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<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
+}