Пример #1
0
def address_list():
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    rv = [each.as_resp() for each in Address.get_multi_by_uid(current_user.id)]
    return success(rv)
Пример #2
0
def create_address():
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    user_id = current_user.id
    if request.json is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_MISSING)

    logger.info(json.dumps(request.json, ensure_ascii=False))

    province = request.json.get('province')
    logger.info("province = {}".format(province))

    city = request.json.get('city')
    area = request.json.get('area')
    location = request.json.get('location')
    contact_name = request.json.get('contact_name')
    contact_phone = request.json.get('contact_phone')

    addr = Address.create(
        user_id=user_id,
        province=province,
        city=city,
        area=area,
        location=location,
        contact_name=contact_name,
        contact_phone=contact_phone,
    )

    return success(addr.as_resp())
Пример #3
0
def update_address(a_id):
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    addr = Address.get(a_id)
    if addr is None or addr.user_id != current_user.id:
        return fail(HTTP_NOT_FOUND, u'地址不存在')

    if request.method == 'POST':
        province = request.json.get('province')
        city = request.json.get('city')
        area = request.json.get('area')
        location = request.json.get('location')
        contact_name = request.json.get('contact_name')
        contact_phone = request.json.get('contact_phone')

        addr.province = province
        addr.city = city
        addr.area = area
        addr.location = location
        addr.contact_name = contact_name
        addr.contact_phone = contact_phone

        addr.save()
        return success(addr.as_resp())
    elif request.method == 'GET':
        return success(addr.as_resp())

    return fail(HTTP_OK, u'未知异常..')
Пример #4
0
def collecation():
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')
    collecations = Collecation.get_multi_by_user(current_user.id)
    collecations = [each.as_resp() for each in collecations]
    return success(collecations)
Пример #5
0
def recharge():
    current_user = get_current_user()

    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    if request.json is None:
        return fail(HTTP_BAD_REQUEST, u'缺少参数')

    amount = request.json.get('amount')

    if amount is None:
        return fail(HTTP_BAD_REQUEST, u'参数为空')

    try:
        amount = int(amount)
    except Exception:
        return fail(HTTP_BAD_REQUEST, u'充值金额错误')

    amount = int(amount)
    if amount < 1:
        return fail(HTTP_BAD_REQUEST, u'请输入充值金额')

    user_id = current_user.id

    payment = Payment.create(user_id=user_id, amount=amount,
                             payment_type=Payment.TYPE_RECHARGE)

    # 开始进行微信支付
    is_success, rv = create_jsapi_params(current_user.openid, payment)
    if is_success:
        return success(rv)
    else:
        logger.warn(rv)
        return fail(HTTP_BAD_REQUEST, u'充值失败')
Пример #6
0
def pocket():
    current_user = get_current_user()

    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    user_id = current_user.id

    pocket = Pocket.get_or_create_by_user_id(user_id)

    return success(pocket.as_resp())
Пример #7
0
def delete_address(id):
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    addr = Address.get(id)
    if addr is None:
        return fail(HTTP_NOT_FOUND, u'地址未找到')

    addr.delete()

    return success(None)
Пример #8
0
def use_collecation():
    current_user = get_current_user()
    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    if request.json is None:
        return fail(HTTP_BAD_REQUEST, u'缺少参数')

    # 存储计费方式 现在默认是2块钱一天
    mode_id = request.json.get('mode_id')
    mode = Mode.get(mode_id)
    if mode is None:
        return fail(HTTP_BAD_REQUEST, u'参数错误')

    collecations = request.json.get('collecations')

    for each in collecations:
        id_ = each.get('id', None)
        if id_ is None:
            return fail(HTTP_BAD_REQUEST, u'参数错误')

    # 获得钱包信息
    pocket = Pocket.get_or_create_by_user_id(current_user.id)
    if pocket is None:
        return fail(HTTP_SERVER_ERROR, u'获取钱包信息失败')

    # 获取总金钱数目
    balance = pocket.balance
    total_fee = 0

    collection_list = []
    for each in collecations:
        c = Collecation.get(each.get('id'))
        if c is None:
            continue
        if c.status == Collecation.STATUS_USING:
            continue
        c.mode_id = mode_id
        c.status = Collecation.STATUS_USING

        # 存储入库时间
        c.requested_at = datetime.utcnow()

        # 用户添加的备注信息
        c.remark = each.get('remark')

        # 算总共需要的额度
        total_fee += Mode.get_mode_fee(mode_id)

        collection_list.append(c)

    # 如果总的消费大于余额  则不进行存储
    if total_fee > balance:
        return fail(HTTP_BAD_REQUEST,
                    u'您当前余额为 %0.2f 元,不足以支付此订单' % (pocket.balance * 1.0 / 100))

    # todo 求所有箱子的剩余天数
    # # 算出剩下来的钱
    # remain_fee = balance - total_fee
    #
    # # 计算已经入库的所有箱子的费用
    # coll_list = Collecation.get_used_coll_by_user(current_user.id)
    # cost_
    # for item in coll_list:

    # 开始存储订单
    for item in collection_list:
        db.session.add(item)
    db.session.commit()

    return success()
Пример #9
0
def create_order():
    current_user = get_current_user()

    if current_user is None:
        return fail(HTTP_UNAUTHORIZED, u'请使用微信客户端登录')

    user_id = current_user.id

    if request.json is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_MISSING)

    # 产品ID?
    product_id = request.json.get('product_id')

    # 得先判断产品是否存在,你请求个没有的产品肯定不行
    product = Product.get(product_id)

    if product is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PRODUCT_NOT_FOUND)

    # 地址ID
    address_id = request.json.get('address_id')

    # 支付方式
    payment_method = request.json.get('payment_method') or 'wechat'

    address = Address.get(address_id)
    if address is None:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_ERROR)

    if address.user_id != user_id:
        return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_ERROR)

    # 买了多个箱子
    box_num = int(request.json.get('count'))
    if box_num <= 0:
        logger.warn("箱子数目不正确: {}".format(box_num))
        return fail(HTTP_BAD_REQUEST, u"箱子数目不正确")

    # 计算箱子的费用
    product_fee = product.price * box_num

    # 邮寄费用默认为0
    express_fee = EXPRESS_FEE

    order = Order.create(
        user_id=user_id,
        address_id=address_id,
        product_fee=product_fee,
        express_fee=express_fee,
        box_num=box_num
    )

    items = []
    for each in range(box_num):
        item = OrderItem.create(
            user_id=user_id,
            product_id=product_id,
            order_id=order.id
        )
        items.append(item)

    # 总费用
    total_fee = product_fee + express_fee

    is_success, rv = False, None

    if payment_method == Order.PAYED_WECHAT:
        payment = Payment.create(
            user_id=user_id,
            amount=total_fee,
            payment_type=Payment.TYPE_BUY,
        )

        order.payment_id = payment.id
        db.session.add(order)
        db.session.commit()

        # 这里是微信支付的流程
        is_success, rv = create_jsapi_params(current_user.openid, payment)

    elif payment_method == Order.PAYED_CRASH:
        pocket = Pocket.get_or_create_by_user_id(user_id)
        if pocket.balance >= total_fee:
            pocket.cut_down(total_fee)
            db.session.commit()

            # 不明白为什么现金买的 就有详细订单,微信支付的就没有详细订单了
            for each in items:
                item_id = gen_item_id(user_id)
                Collecation.create(
                    user_id=user_id,
                    mode_id=Mode.get_default_id(),
                    item_id=item_id)
                each.item_id = item_id
                db.session.add(each)

            order.status = Order.STATUS_PAYED
            order.payment_method = payment_method
            db.session.add(order)
            is_success = True
        else:
            return fail(HTTP_BAD_REQUEST,
                        u'您当前余额为 %0.2f 元,不足以支付此订单' %
                        (pocket.balance * 1.0 / 100))

    if is_success:
        return success(rv)
    else:
        if rv:
            logger.warn(rv)
        return fail(HTTP_BAD_REQUEST, u'购买失败')