1.0.0 • Published 3 years ago

cp-sqll v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago
middleware  auth.js


'use strict';
const jwt = require("jsonwebtoken");

module.exports = () => {


    return async function auth(ctx, next) {
        let { authrization } = ctx.request.header;

        try {

            jwt.verify(authrization, "cp")
            await next()

        } catch {

            ctx.body = {
                code: 500,
                msg: "请重新登录"
            }
        }

    }
}





'use strict';

const Controller = require('egg').Controller;
const getPas = require('../../utils/md5');
const jwt = require("jsonwebtoken");
const fs = require("fs");
const path = require("path");
const pump = require("pump")



async upload() {
        const { ctx } = this;

        const stream = await ctx.getFileStream(); //接收从前端接收的图片文件流 通过ctx.getFileStream获取图片信息

        //获取用户id
        const { id } = stream.fields;
        //生成文件名
        const filename = Date.now() + path.extname(stream.filename).toLocaleLowerCase();
        //创建文件保存地址
        const fileaddress = path.join('app/public/static' + filename);
        //生成文件流
        const file = fs.createWriteStream(fileaddress);

        await pump(stream, file);

        const img = ctx.origin + "/public/static" + filename

        const row = {
            id: id,
            img: img,

        };
        const result = await this.app.mysql.update('user', row);
        ctx.body = 'hi, egg';
    }