xiangqin/controllers/index.js
2021-09-28 11:47:34 +08:00

82 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require("fs")
const path = require("path")
const jwt = require('jsonwebtoken')
function gettoken(info){
return jwt.sign(info, "gjhkhnjweuyujhgjh", { expiresIn: '2h' })
}
const index = async (ctx,next)=>{
ctx.body = "hello"
}
const sign = async (ctx,next)=>{
const {phone,pwd} = ctx.request.body
ctx.body = await dbs.add("user",{
phone,
pwd
})
}
const login = async (ctx,next)=>{
const {phone,pwd} = ctx.request.body
const res = await dbs.find("user",{phone,pwd})
if(res.data.length == 0){
ctx.body = JSON.stringify({
code:1,
data:"没有此用户或用户名密码错误"
})
}else{
const token = gettoken({phone,pwd})
ctx.body = JSON.stringify({
code:0,
data: token
})
}
}
const addlist = async (ctx,next)=>{
const {phone,name,addres,sex,birthday,money1,money2,desc,images} = ctx.request.body
ctx.body = await dbs.add("list",{phone,name,addres,sex,birthday,money1,money2,desc,images})
}
const upload = async (ctx,next)=>{
const file = ctx.request.files.file
// console.log(file)
const reader = fs.createReadStream(file.path)
// 创建写入流
// 3. 指定图片路径文件名(即上传图片存储目录)
const stream = fs.createWriteStream(path.join('images', file.name))
// 用管道将读出流 "倒给" 输入流
reader.pipe(stream)
// 4.打印上传文件在服器上存储的相对路径
console.log('uploading %s -> %s', file.name, stream.path)
// 5.重定向到基于根目录下的静态资源web访问路径展示图片
ctx.body = {
code:0,
path : stream.path.substr(6).replace(/\\/g,'/')
}
}
const getlist = async (ctx,next)=>{
ctx.body = await dbs.find("list")
}
const getinfo = async (ctx,next)=>{
let { num_key } = ctx.query
console.log(num_key)
num_key = parseInt(num_key)
ctx.body = await dbs.find("list",{
num_key
})
}
module.exports = {
"GET /":index,
"POST /sign": sign,
"POST /login": login,
"POST /addlist": addlist,
"POST /upload": upload,
"GET /getlist": getlist,
"GET /getinfo": getinfo,
}