Commit 69e82626 authored by Libcat's avatar Libcat 👷
Browse files

add 用户头像功能(可修改)

fix 修复用户登录和注册中的一些bug
fix 首页显示出DIY商品的bug
parent 5472b8a9
......@@ -14,11 +14,15 @@ router = APIRouter(tags=["产品CRUD"], prefix="/products")
async def get_list(category: int = None,
categoryName: str = None,
name: str = None,
orderby:str = 'id',
desc:bool = False,
limit: int = None):
orderby: str = 'id',
desc: bool = False,
limit: int = None,
exclude_diy: bool = False):
q = Product.select(Product, Category).join(Category)
if exclude_diy:
q = q.where(Category.flexi==False)
if categoryName:
q = q.where(Category.name == categoryName)
......@@ -28,20 +32,20 @@ async def get_list(category: int = None,
if name:
q = q.where(Product.name.contains(name))
if orderby=='sales':
if orderby == 'sales':
if desc:
q = q.order_by(Product.sales.desc())
else:
q = q.order_by(Product.sales)
if orderby=='id':
if orderby == 'id':
if desc:
q = q.order_by(Product.id.desc())
else:
q = q.order_by(Product.id)
if limit!=None:
q=q.limit(limit)
if limit != None:
q = q.limit(limit)
return list(q)
......
import uuid
from typing import List, Optional
from fastapi import APIRouter, Response, Cookie, Form, status, Depends
from fastapi import APIRouter, Response, Form, Depends, UploadFile
from pathlib import Path
import apischemas as api
from database import *
from .authorize import get_user
......@@ -21,22 +21,42 @@ async def get_one(id: int):
return User.get_or_none(User.id == id)
@router.post('/signup',
response_model=api.SigninSignupResult,
description="用户注册")
async def create_user(item: api.UserCreate):
dbitem = User.create(**item.dict())
return api.SigninSignupResult(succ=True, msg="注册成功", user=dbitem)
@router.post('/signup', response_model=api.UserBase, description="用户注册")
async def create_user(username: str = Form(...),
password: str = Form(...),
email: str = Form(...),
image: UploadFile = Form(...)):
if User.get_or_none(User.username == username):
return Response("用户名重复", 400)
if len(password) < 6:
return Response("密码不能少于6位字符", 400)
filename = None
if image:
content = await image.read()
suffix = Path(image.filename).suffix
# 获取图片后缀
if suffix == "" or suffix == None:
suffix = ".jpg"
filename = f"user-{username}{suffix}"
# 保存图片
with open("upload/" + filename, 'wb') as f:
f.write(content)
dbitem = User.create(username=username,
password=password,
email=email,
image=filename)
return dbitem
@router.post('/login',
response_model=api.SigninSignupResult,
description="用户登录")
async def user_login(response: Response, user: api.UserLogin):
async def user_login(user: api.UserLogin):
if user.username != "":
u = User.get_or_none(User.username == user.username)
else:
return api.SigninSignupResult(succ=False, msg="用户名或密码不能为空!")
return Response("用户名或密码不能为空!", 400)
if u:
if u.password == user.password:
......@@ -46,11 +66,14 @@ async def user_login(response: Response, user: api.UserLogin):
# 生成sessionid
sid = str(uuid.uuid4())
Session.create(id=sid, user=u)
return api.SigninSignupResult(succ=True, msg="登录成功!", user=u, sessionid=sid)
return api.SigninSignupResult(succ=True,
msg="登录成功!",
user=u,
sessionid=sid)
else:
return api.SigninSignupResult(succ=False, msg="用户名或密码错误!")
return Response("用户名或密码错误!", 400)
else:
return api.SigninSignupResult(succ=False, msg="用户名不存在!")
return Response("用户名不存在!", 400)
@router.post('/logout', description="用户退出")
......@@ -65,18 +88,27 @@ async def get_user_profile(user: User = Depends(get_user)):
@router.put('/modify', response_model=api.UserBase, description="修改用户信息")
async def change_user_profile(data: api.UserUpdate,
async def change_user_profile(email: str = Form(None),
image: UploadFile = Form(None),
user: User = Depends(get_user)):
if data.email:
user.email = data.email
if data.image:
user.image = data.image
if email:
user.email = email
if image:
content = await image.read()
suffix = Path(image.filename).suffix
# 获取图片后缀
if suffix == "" or suffix == None:
suffix = ".jpg"
filename = f"user-{user.username}{suffix}"
# 保存图片
with open("upload/" + filename, 'wb') as f:
f.write(content)
user.image = filename
user.save()
return user
@router.post('/changePassword',
description="修改用户密码")
@router.post('/changePassword', description="修改用户密码")
async def change_user_password(data: api.UserPassword,
user: User = Depends(get_user)):
if user.password != data.oldPassword:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment