예제 #1
0
    def to_data(self: "GaoYongForm",
                logger: BoundLogger) -> Awaitable[Optional[GaoYongArgs]]:
        logic = UserV2Logic(logger)
        user = async_to_sync(logic.get_user_by_token(self.token))
        if user is None:
            logger.bind(token=self.token).error("user token is invalid")
            # noinspection PyTypeChecker
            return None

        pid = SConfig.AliPid
        sid = SConfig.ZTKSid

        args = GaoYongArgs(num_iid=self.item_id, pid=pid, sid=sid)
        try:
            info = TBChannelIdModel.objects.get(user=user)
            if info.relation_id is not None:
                args.relation_id = info.relation_id
            if info.special_id is not None:
                args.special_id = info.special_id
            # noinspection PyTypeChecker
            return args
        except ObjectDoesNotExist:
            logger.bind(user=user).error("user not bind taobao")
            # noinspection PyTypeChecker
            return args
예제 #2
0
    async def wrapper(func: Callable[[User], Any]):
        try:
            logic = UserV2Logic(logger)

            user = await logic.get_user_by_token(token)
            if user is None:
                return ApiResp.from_errno(AppErrno.auth_failed).to_dict()

            ret = await func(user)
            if isinstance(ret, ApiResp):
                return ret.to_dict()
            else:
                return ret
        except ApiException as e:
            tb = traceback.format_tb(e.__traceback__)
            logger.bind(exec=e, tb=tb).error("内部处理错误")

            return convert_exception_to_api_ret(e).to_dict()
        except InterfaceError:  # 数据库连接失败
            connection.close()
            logger.error("数据库错误")
            return ApiResp.system_error().to_dict()
        except Exception as e:
            tb = traceback.format_tb(e.__traceback__)
            logger.bind(exce=e, tb=tb).error("其他严重错误")

            return ApiResp.system_error().to_dict()
예제 #3
0
async def user_auth(
        g: UserNativeAuthForm = Body(..., title="请求参数"),
        logger: BoundLogger = Depends(get_logger),
):
    ul = UserV2Logic(logger)
    token = await ul.auth(username=g.username, password=g.password)
    if token is None:
        return UserAuthResponseModel(errno=AppErrno.auth_failed,
                                     errmsg=str(AppErrno.auth_failed))
    return UserAuthResponseModel(errno=AppErrno.success,
                                 errmsg="",
                                 data=UserNativeAuthDataModel(token=token))
예제 #4
0
파일: auth.py 프로젝트: ZKvAH/QiYuTkServer
async def user_auth(
    request: HttpRequest, g: UserNativeAuthForm
) -> UserAuthResponseModel:
    logger = get_logger()
    ul = UserV2Logic(logger)
    token = await ul.auth(username=g.username, password=g.password)
    if token is None:
        return UserAuthResponseModel(
            errno=AppErrno.auth_failed, errmsg=str(AppErrno.auth_failed)
        )
    return UserAuthResponseModel(
        errno=AppErrno.success, errmsg="", data=UserNativeAuthDataModel(token=token)
    )
    def _do_add_new_order(order: OrderDto, logger: BoundLogger):
        """
        添加一个新的订单 [这个 `order` 没有在 订单库中出现过]
        :param order:
        :param logger:
        :return:
        """
        assert order.relation_id is not None
        channel_info: TBChannelIdModel = TBChannelIdModel.get_by_relation_id(
            order.relation_id
        )
        user = channel_info.user

        model: OrderModel = OrderModel(
            user=user,
            order_platform=order.order_platform(),
            order_no=order.trade_id,
            order_parent_no=order.trade_parent_id,
            order_num=order.item_num,
            order_ctime=order.order_ctime(),
            order_status=str(order.tk_status),
            pay_price=order.ali_pay_price(),
            pay_time=order.pay_time(),
            end_time=order.end_time(),
            item_id=str(order.item_id),
            item_title=order.item_title,
            item_pic=order.item_img,
            item_price=order.item_price,
            item_category=order.item_category_name,
            shop_title=order.seller_shop_title,
            income=order.income(),
            score=int(order.income() * 100),
            status=OrderStatusEnum.wait,
            detail=order.to_dict(),
        )

        # noinspection PyArgumentList
        model.save(force_insert=True)

        if order.is_order_paid():  # 订单已经付款 这是默认的状态 什么都不需要做
            pass
        elif order.is_order_canceled():  # 订单已经取消 这个状态需要重新设置 订单的状态
            model.status = OrderStatusEnum.cancel
        elif order.is_order_success() or order.is_order_done():
            model.status = OrderStatusEnum.success
            event = f"购买: {order.trade_id}"
            logic = UserV2Logic(logger)
            logic.add_score(model.user, event, model.score)

        # noinspection PyArgumentList
        model.save()  # 设置 订单状态为 已经取消
예제 #6
0
    def _do_update_old_order(order: OrderDto, model: OrderModel,
                             logger: BoundLogger):
        """
        更新一个订单信息
        当前的订单信息为 order
        数据库中的信息为 model

        :param order:
        :param model:
        :param logger:
        :return:
        """
        model.order_ctime = order.order_ctime()
        model.order_status = str(order.tk_status)
        model.pay_price = order.ali_pay_price()
        model.pay_time = order.pay_time()
        model.end_time = order.end_time()
        model.income = order.income()
        model.score = (int(order.income() * 100), )
        model.detail = order.to_dict()

        if order.is_order_paid():  # 默认状态
            model.status = OrderStatusEnum.wait

        elif order.is_order_canceled():  # 需要查询之前的状态
            if model.status == OrderStatusEnum.wait:
                model.status = OrderStatusEnum.cancel
            elif model.status == OrderStatusEnum.cancel:  # 已经是这个状态 没有必要更新
                pass
            elif model.status == OrderStatusEnum.success:
                logger.bind(order_no=order.trade_id).error("revoke score ?")
                model.status = OrderStatusEnum.cancel

        elif order.is_order_success() or order.is_order_done():
            if model.status == OrderStatusEnum.wait:  # 之前的状态是等待 现在变成 成功状态
                event = f"购买: {order.trade_id}"
                logic = UserV2Logic(logger)
                logic.add_score(model.user, event, int(order.income() * 100))

                model.status = OrderStatusEnum.success
            elif model.status == OrderStatusEnum.cancel:  # 取消之后不可能成功
                logger.bind(
                    order_no=order.trade_id).error("sth impossible happened")
            elif model.status == OrderStatusEnum.success:  # 已经是这个状态 没有必要更新
                pass

        # noinspection PyArgumentList
        model.save()
예제 #7
0
 async def inner():
     user = UserV2Logic(logger)
     ret = await user.cancel_user_account(g.token)
     return ApiResp.from_data(ret)
예제 #8
0
 async def inner():
     logic = UserV2Logic(logger)
     await logic.ensure_bind_tb(g.token)
     return ApiResp.from_data(True)