예제 #1
0
async def goods_add(
        goods_info: goods_schema.GoodsCreate,
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
):
    logger.info(goods_info)
    return response_code.resp_200(data="ok")
예제 #2
0
async def login_access_token(
    *,
    db: Session = Depends(deps.get_db),
    user_info: user_schema.UserEmailAuth,
) -> Any:
    """
    用户登录
    :param db:
    :param user_info:
    :return:
    """

    # 验证用户
    user = curd_user.authenticate(db,
                                  email=user_info.username,
                                  password=user_info.password)
    if not user:
        logger.info(
            f"用户邮箱认证错误: email{user_info.username} password:{user_info.password}"
        )
        return response_code.resp_500(message="用户名或者密码错误")
    elif not curd_user.is_active(user):
        return response_code.resp_500(message="用户邮箱未激活")

    access_token_expires = timedelta(
        minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)

    # 登录token 只存放了user.id
    return response_code.resp_200(
        data={
            "token":
            security.create_access_token(user.id,
                                         expires_delta=access_token_expires),
        })
예제 #3
0
async def add_category(
        category_info: category_schema.CategoryCreate,
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
):
    logger.info(f"添加分类->用户id:{token_data.sub}分类名:{category_info.name}")
    curd_category.create(db=db, obj_in=category_info)
    return response_code.resp_200(message="分类添加成功")
예제 #4
0
async def query_category(
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
        cate_id: int = Query(..., title="查询当前分类"),
):
    logger.info(f"查询分类->用户id:{token_data.sub}分类:{cate_id}")
    response_result = curd_category.query_obj(db, cate_id=cate_id)
    return response_code.resp_200(data=response_result)
예제 #5
0
async def modify_category(
        cate_ids: category_schema.CategoryDel,
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
):
    logger.info(f"修改分类->用户id:{token_data.sub}分类id:{cate_ids.ids}")
    for cate_id in cate_ids.ids:
        curd_category.remove(db, id=cate_id)
    return response_code.resp_200(message="删除成功")
예제 #6
0
async def modify_category(
        cate_info: category_schema.CategoryUpdate,
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
):
    logger.info(f"修改分类->用户id:{token_data.sub}分类id:{cate_info.id}")
    curd_category.update_cate(db=db, obj_in=cate_info)

    return response_code.resp_200(message="修改成功")
예제 #7
0
async def user_logout(
        token_data: Union[str, Any] = Depends(deps.check_jwt_token), ):
    """
    用户退出
    :param token_data:
    :return:
    """
    logger.info(f"用户退出->用户id:{token_data.sub}")
    return response_code.resp_200(message="logout success")
예제 #8
0
async def query_category_list(db: Session = Depends(deps.get_db),
                              token_data: Union[str, Any] = Depends(
                                  deps.check_jwt_token),
                              page: int = Query(1, ge=1, title="当前页"),
                              page_size: int = Query(10, le=50, title="页码长度")):
    logger.info(f"查询分类列表->用户id:{token_data.sub}当前页{page}长度{page_size}")
    response_result = curd_category.query_all(db,
                                              page=page,
                                              page_size=page_size)
    return response_code.resp_200(data=response_result)
예제 #9
0
async def search_category(
        cate_info: category_schema.CategorySearch,
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
):
    logger.info(
        f"搜索分类操作->用户id:{token_data.sub}搜索{cate_info.key_world}:{cate_info.key_world}"
        f"页码:{cate_info.page}长度{cate_info.page_size}")
    response_result = curd_category.search_field(db, cate_info=cate_info)
    return response_code.resp_200(data=response_result)
예제 #10
0
async def enabled_category(
        cate_info: category_schema.CategoryEnable,
        db: Session = Depends(deps.get_db),
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
):
    logger.info(
        f"开启分类操作->用户id:{token_data.sub}分类id:{cate_info.ids}操作:{cate_info.enabled}"
    )
    for cate_id in cate_info.ids:
        curd_category.update_enabled(db, id=cate_id, enabled=cate_info.enabled)
    return response_code.resp_200(message="操作成功")
예제 #11
0
async def get_user_info(
    *,
    db: Session = Depends(deps.get_db),
    current_user: auth.AdminUser = Depends(deps.get_current_user)
) -> Any:
    """
    获取用户信息
    :param db:
    :param current_user:
    :return:
    """
    role_info = curd_role.query_role(db, role_id=current_user.role_id)

    return response_code.resp_200(
        data={
            "role_id": current_user.role_id,
            "role": role_info.role_name,
            "nickname": current_user.nickname,
            "avatar": current_user.avatar
        })
예제 #12
0
async def upload_image(
        token_data: Union[str, Any] = Depends(deps.check_jwt_token),
        file: UploadFile = File(...)
):
    logger.info(f"用户{token_data.sub}->上传文件:{file.filename}")

    # 本地存储临时方案,一般生产都是使用第三方云存储OSS(如七牛云, 阿里云)
    save_dir = f"{settings.BASE_DIR}/assets"
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    try:
        suffix = Path(file.filename).suffix

        with NamedTemporaryFile(delete=False, suffix=suffix, dir=save_dir) as tmp:
            shutil.copyfileobj(file.file, tmp)
            tmp_file_name = Path(tmp.name).name
    finally:
        file.file.close()

    return response_code.resp_200(data={"image": f"http://127.0.0.1:8010/assets/{tmp_file_name}"})