feat:用户上传头像
This commit is contained in:
父節點
660dfb10fd
當前提交
008ffdfd7c
@ -14,6 +14,7 @@
|
||||
"dependencies": {
|
||||
"@types/cors": "^2.8.12",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/express-fileupload": "^1.1.7",
|
||||
"@types/express-jwt": "^6.0.2",
|
||||
"@types/jsonwebtoken": "^8.5.4",
|
||||
"@types/node": "^16.4.1",
|
||||
@ -25,8 +26,10 @@
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1",
|
||||
"express-async-handler": "^1.1.4",
|
||||
"express-fileupload": "^1.2.1",
|
||||
"express-jwt": "^6.0.0",
|
||||
"express-validator": "^6.12.1",
|
||||
"form-data": "^4.0.0",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"promise-mysql": "^5.0.3",
|
||||
"qs": "^6.10.1",
|
||||
|
@ -5,6 +5,7 @@ export function fillIUser(single: any) {
|
||||
id: single.id,
|
||||
username: single.username,
|
||||
realName: single.realName,
|
||||
avatar: single.avatar,
|
||||
role: single.role
|
||||
}
|
||||
}
|
||||
|
@ -9,10 +9,12 @@ import type {ErrorRequestHandler} from "express"
|
||||
import {IResultJson} from "./types"
|
||||
import {postRouter} from "./routers/post-router"
|
||||
import {adminRouter} from "./routers/admin-router"
|
||||
import fileUpload from "express-fileupload"
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(cors())
|
||||
app.use(fileUpload())
|
||||
app.use(express.urlencoded({extended: true}))
|
||||
app.use(
|
||||
expressJWT({
|
||||
|
@ -28,4 +28,8 @@ export class UserModel {
|
||||
}
|
||||
return fillIUser(user[0])
|
||||
}
|
||||
|
||||
async updateAvatar(id: number, avatar: string) {
|
||||
await db.query('update users set avatar=? where id=?', [avatar, id])
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,11 @@ import 'reflect-metadata'
|
||||
import express from "express"
|
||||
import {Container} from "typedi"
|
||||
import {UserService} from "../services/user-service"
|
||||
import {body, validationResult,} from "express-validator"
|
||||
import {hasValidationErrors} from "../includes"
|
||||
import {body, check, validationResult,} from "express-validator"
|
||||
import {hasValidationErrors, resultJson} from "../includes"
|
||||
import {JWTUserPayload} from "../types"
|
||||
import expressAsyncHandler from "express-async-handler"
|
||||
import {UploadedFile} from "express-fileupload"
|
||||
|
||||
let userService = Container.get(UserService)
|
||||
|
||||
@ -18,8 +19,17 @@ userRouter.post('/login',
|
||||
res.json(await userService.login(req.body.username, req.body.password))
|
||||
})
|
||||
)
|
||||
userRouter.get('/me',expressAsyncHandler(async (req:express.Request,res:express.Response)=>{
|
||||
userRouter.get('/me', expressAsyncHandler(async (req: express.Request, res: express.Response) => {
|
||||
res.json(await userService.me(req.user as JWTUserPayload))
|
||||
}))
|
||||
userRouter.post('/uploadAvatar',
|
||||
expressAsyncHandler(async (req: express.Request, res: express.Response) => {
|
||||
if (!req.files?.image) {
|
||||
res.json(resultJson.error('请选择要上传的图片'))
|
||||
return
|
||||
}
|
||||
res.json(await userService.uploadAvatar(req.user as JWTUserPayload, req.files.image as UploadedFile))
|
||||
})
|
||||
)
|
||||
|
||||
export {userRouter}
|
||||
|
@ -7,6 +7,9 @@ import qs from 'qs'
|
||||
import jwt from "jsonwebtoken"
|
||||
import {appConfig} from "../config"
|
||||
import {JWTUserPayload} from "../types"
|
||||
import fileUpload, {UploadedFile} from "express-fileupload"
|
||||
import axios from "axios"
|
||||
import FormData from "form-data"
|
||||
|
||||
@Service()
|
||||
export class UserService {
|
||||
@ -29,6 +32,7 @@ export class UserService {
|
||||
rsa,
|
||||
ul: username.length.toString(),
|
||||
pl: password.length.toString(),
|
||||
lt: ticket,
|
||||
execution: 'e1s1',
|
||||
_eventId: 'submit'
|
||||
}))
|
||||
@ -51,7 +55,21 @@ export class UserService {
|
||||
}
|
||||
|
||||
async me(jwtUserPayload: JWTUserPayload) {
|
||||
let user=await this.userModel.findUserById(jwtUserPayload.id)
|
||||
let user = await this.userModel.findUserById(jwtUserPayload.id)
|
||||
return resultJson.success(user)
|
||||
}
|
||||
|
||||
async uploadAvatar(jwtUserPayload: JWTUserPayload, image: UploadedFile) {
|
||||
let form = new FormData()
|
||||
form.append('image', image.data, image.name)
|
||||
let resp = await axios.post('https://image.kieng.cn/upload.html?type=jd', form, {
|
||||
headers: form.getHeaders()
|
||||
})
|
||||
if (resp.data.code !== 200) {
|
||||
return resultJson.error(resp.data.msg)
|
||||
}
|
||||
let avatar: string = resp.data.data.url
|
||||
await this.userModel.updateAvatar(jwtUserPayload.id, avatar)
|
||||
return resultJson.success(avatar)
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ export interface IUser {
|
||||
id?: number,
|
||||
username: string,
|
||||
realName: string,
|
||||
avatar?: string,
|
||||
role: TUserRole
|
||||
}
|
||||
|
||||
|
載入中…
x
新增問題並參考
Block a user