Exemplo n.º 1
0
def del_product():
    p_id = request.args.get('p_id')
    if p_id is None or p_id == '':
        return utils.ret_err(-1, "p_id is required")

    product = Product.query.filter_by(p_id=p_id).first()
    if product is None:
        return utils.ret_err(-1, "Product doesn't exists")
    db.session.delete(product)
    db.session.commit()
    return utils.ret_msg('Success')
Exemplo n.º 2
0
def list_product():
    order_no = request.args.get('order_no')
    if order_no:
        order = Order.query.filter_by(order_no=order_no).first()
        if order:
            return utils.ret_objs(order.product)
        return utils.ret_err(-1, "order_no is wrong")

    stat = Product.query

    p_id = request.args.get('p_id')
    if p_id:
        stat = stat.filter_by(p_id=p_id)
    name = request.args.get('name')
    if name:
        stat = stat.filter(Product.name.like('%' + name + '%'))
    title = request.args.get('title')
    if title:
        stat = stat.filter(Product.title.like('%' + title + '%'))
    detail = request.args.get('detail')
    if detail:
        stat = stat.filter(Product.detail.like('%' + detail + '%'))
    color = request.args.get('color')
    if color:
        stat = stat.filter(Product.color.like('%' + color + '%'))
    min_price = request.args.get('min_price', type=float)
    if min_price:
        stat = stat.filter(Product.price >= min_price)
    max_price = request.args.get('max_price', type=float)
    if max_price:
        stat = stat.filter(Product.price <= max_price)

    return utils.ret_objs(stat.all())
Exemplo n.º 3
0
def update_product():
    p_id = request.args.get('p_id')
    if p_id is None or p_id == '':
        return utils.ret_err(-1, "p_id is required")
    product = Product.query.filter_by(p_id=p_id).first()
    if product is None:
        return utils.ret_err(-1, "Product doesn't exists")

    params = {}
    name = request.args.get('name', '')
    if name != '' and len(name) < 32:
        params["name"] = name
    price = request.args.get('price', 0, float)
    if price > 0:
        params["price"] = price
    original_price = request.args.get('original_price', 0, float)
    if original_price > 0:
        params["original_price"] = original_price
    inventory = request.args.get('inventory', -1, int)
    if inventory >= 0:
        params["inventory"] = inventory
    title = request.args.get('title')
    if title and len(title) < 64:
        params["title"] = title
    detail = request.args.get('detail')
    if detail and len(detail) < 128:
        params["detail"] = detail
    sale_count = request.args.get('sale_count', type=int)
    if sale_count:
        params["sale_count"] = sale_count
    color = request.args.get('color')
    if color:
        params["color"] = color
    icon = request.args.get('icon')
    if icon:
        params["icon"] = icon

    if bool(params):
        Product.query.filter_by(p_id=p_id).update(params)
        db.session.commit()
        return utils.ret_msg_objs('Success', product)
    return utils.ret_msg('Nothing happen')
Exemplo n.º 4
0
def add_product():
    name = request.args.get('name', '')
    title = request.args.get('title', '')
    price = request.args.get('price', 0, float)
    inventory = request.args.get('inventory', 0, int)
    if name == '' or len(name) > 32:
        return utils.ret_err(-1, 'name(32) is required or too long')
    if title == '' or len(title) > 64:
        return utils.ret_err(-1, 'title(64) is required or too long')
    if price <= 0:
        return utils.ret_err(-1, 'price must > 0')
    if inventory <= 0:
        return utils.ret_err(-1, 'inventory must > 0')

    original_price = request.args.get('original_price', 0, float)
    detail = request.args.get('detail', '')
    color = request.args.get('color', '')
    icon = request.args.get('icon', '')
    if original_price < 0:
        return utils.ret_err(-1, "original_price must > 0")
    if len(detail) > 128:
        return utils.ret_err(-1, "detail(128) is too long")
    if len(color) > 16:
        return utils.ret_err(-1, "color(16) is too long")
    if len(icon) > 128:
        return utils.ret_err(-1, "icon(128) is too long")

    product = Product(name, price, inventory, title)
    product.original_price = original_price
    product.detail = detail
    product.color = color
    product.icon = icon
    product.sale_count = request.args.get("sale_count", 0, int)
    db.session.add(product)
    db.session.commit()
    return utils.ret_msg_objs('Suceess', product)
Exemplo n.º 5
0
def list_order():
    """
    查询订单
    @args: trade_state 订单状态[SUCCESS, NOTPAY] 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
    @args: order_by_time 按时间排序[asc, desc], 默认是 desc
    :return:
    """
    openid = request.cookies.get("openid")
    if not openid:
        openid = request.args.get("openid")
        if not openid:
            return utils.ret_err(-1, "ERR_INVALID_OPENID")

    stat = Order.query.filter_by(openid=openid)

    order_no = request.args.get('order_no', '')
    if order_no:
        stat = stat.filter_by(order_no=order_no)
        _order = stat.first()
        if _order is None:
            return utils.ret_err(-1, '订单不存在')
        if not verify_order(order_no):
            return utils.ret_err(-1, _order.trade_state_desc)

    trade_state = request.args.get('trade_state')
    if trade_state:
        stat = stat.filter_by(trade_state=trade_state)
    order_by_time = request.args.get("order_by_time")
    if order_by_time:
        if order_by_time == "asc":
            stat = stat.order_by(Order.create_time.asc())
        else:
            stat = stat.order_by(Order.create_time.desc())

    objs = []
    orders = stat.all()
    for order in orders:
        p = order.product
        obj = {
            "order_no": str(order.order_no),
            "p_id": str(p.p_id),
            "transaction_id": order.transaction_id,
            "promotion_path": order.promotion_path,
            "p_name": p.name,
            "p_title": p.title,
            "p_detail": p.detail,
            "p_price": p.price,
            "p_color": p.color,
            "p_icon": p.icon,
            "p_count": order.p_count,
            "price_sum": order.price_sum,
            "username": order.username,
            "phone": order.phone,
            "address": order.address,
            "raw_address": order.raw_address,
            "record_address": order.record_address,
            "comment": order.comment,
            "order_time": order.create_time,
            "pay_time": order.pay_time,
            "track_no": order.track_no,
            "track_state": order.track_state,
            "track_time": order.track_time,
            "is_sign": order.is_sign,
            "sign_time": order.sign_time,
            "postcode": order.postcode
        }
        objs.append(obj)

    return utils.ret_objs(objs)
Exemplo n.º 6
0
def create_order():
    """
    @api {GET} /api/createOrder 创建订单
    @apiDescription 创建订单,生成并返回调用 jsapi 所需要的数据
    @apiPermission 要求当前用户已经微信登录
    @apiGroup Order
    @apiVersion 1.0.0
    @apiParam {Integer} p_id 商品id
    @apiParam {Integer} p_count 商品数量
    @apiParam {String} username 用户名
    @apiParam {String} phone 用户的手机号
    @apiParam {String} address 用户的收货地址
    @apiParam {String} [comment] 用户留言
    @apiExample {js} 用法:
    /api/createOrder?p_id=xxx&p_count=1&username=xxx&phone=13712341234&address=xxx&comment=xxx
    @apiSuccess {Integer} code 0 代表成功, -1 代表失败
    @apiSuccess {Json} data 调用jsapi所需要的数据
    @apiSuccess {String} order_no 订单号
    @apiSuccessExample {json} 返回结果:
    {
        "code": 0,
        "data": {
            "appId": "xxx",
            "timeStamp": "1536042748",
            "nonceStr": "xxx",
            "package": "prepay_id=xxx",
            "signType": "MD5",
            "paySign": "XXX"
        },
        "order_no": "xxx"
    }
    """
    openid = request.cookies.get("openid")
    if not openid:
        openid = request.args.get("openid")
        if not openid:
            return utils.ret_err(-1, "ERR_INVALID_OPENID")

    p_id = request.args.get('p_id')
    product = Product.query.filter_by(p_id=p_id).first()
    if product is None:
        return utils.ret_err(-1, '无效的商品')
    if product.inventory < 1:
        return utils.ret_err(-1, '库存不足')

    p_count = request.args.get('p_count', 0, int)
    if p_count <= 0:
        return utils.ret_err(-1, '商品数量必须大于0')
    if p_count > product.inventory:
        return utils.ret_err(-1, '库存不足')

    username = request.args.get('username', '')
    if username == '' or len(username) > 16:
        return utils.ret_err(-1, '姓名为空或过长')
    phone = request.args.get('phone', '')
    if phone == '' or len(phone) > 16:
        return utils.ret_err(-1, '手机号为空或过长')
    address = request.args.get('address', '')
    if address == '' or len(address) > 128:
        return utils.ret_err(-1, '收货地址为空或过长')
    raw_address = request.args.get('raw_address', '')
    if raw_address == '' or len(raw_address) > 128:
        return utils.ret_err(-1, 'raw收货地址为空或过长')
    record_address = request.args.get("record_address", '')
    if record_address == '' or not (record_address == "YES" or record_address == "NO"):
        record_address = "NO"
    comment = request.args.get('comment', '')
    if len(comment) > 128:
        return utils.ret_err(-1, '留言过长')
    postcode = request.args.get('postcode', '')
    if len(postcode) > 8:
        return utils.ret_err(-1, '邮政编码过长')
    promotion_path = request.args.get("promotion_path", '')
    if len(promotion_path) > 64:
        return utils.ret_err(-1, 'promotion_path is too long')

    order = Order(p_id, p_count, username, phone, address, raw_address, comment)

    order.price_sum = p_count * product.price
    order.postcode = postcode
    order.record_address = record_address
    order.promotion_path = promotion_path
    order.trade_state = "NOTPAY"
    order.trade_type = "JSAPI"
    order.openid = openid

    data = dict()
    data["openid"] = openid
    data["out_trade_no"] = str(order.order_no)
    data["body"] = product.title
    data["total_fee"] = int(order.price_sum * 100)
    data["trade_type"] = order.trade_type

    raw = dict()
    try:
        raw = wxsdk.jsapi(**data)
    except WXPayError as e:
        logging.warning("createOrder: openid=%s, errmsg=%s" % (openid, str(e)))
        return utils.ret_err(-1, str(e))

    db.session.add(order)
    db.session.commit()

    obj = {
        "data": raw,
        "order_no": str(order.order_no)
    }
    return utils.ret_objs(obj)