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'充值失败')
def decorator(*args, **kwargs): signature = request.args.get('signature', '') timestamp = request.args.get('timestamp', '') nonce = request.args.get('nonce', '') token = config.WECHAT_TOKEN cal_signature = _generate_signature(timestamp, nonce, token) if not cal_signature == signature: logger.warn("%s != %s" % (signature, cal_signature)) return fail(HTTP_FORBIDDEN) return func(*args, **kwargs)
def admin_login(): if request.json is None: logger.warn("参数错误...") return fail(HTTP_BAD_REQUEST, EMSG_PARAMS_MISSING) username = request.json.get('username', None) password = request.json.get('password', None) if username is None or password is None: logger.warn("用户账号密码没有传过来...") return fail(HTTP_UNAUTHORIZED, EMSG_PARAMS_ERROR) admin = Admin.get_admin_by_username(username) if admin is None: logger.warn("当前用户不存在: {}".format(username)) return fail(HTTP_UNAUTHORIZED, u"当前用户不存在") if admin.password != password: logger.warn("当前用户密码错误: {} {}".format(username, password)) return fail(HTTP_UNAUTHORIZED, u"密码错误!") # 登录用户信息 login_user(admin, remember=True) logger.info("登录成功: {}".format(username)) return success(admin.as_resp())
def get_collection_list(cls, page, stage, put_in_status, size=10): result_list = [] collection_list = cls.collection_paginate(page, stage, put_in_status, size) if collection_list is None: logger.warn("查询存储订单失败...") return result_list collection_list = collection_list.items if collection_list is None: logger.warn("存储订单list获取items字段失败...") return result_list for item in collection_list: result = { # 存储ID 'id': item.id, # 箱子编码 'item_id': item.item_id, # 订单时间 'requested_at': item.requested_at.strftime('%Y-%m-%d %H:%I:%S'), # 存储天数 'days': item.days, # 剩余天数 'remain_days': item.remain_days, # 用户姓名 'nickname': User.get_by_user_id(item.user_id).nickname, # 阶段 'stage': item.stage, # 仓库状态 'put_in_status': item.put_in_status, # 货架号 'goods_address': item.goods_address, # 'created_at': item.created_at.strftime('%Y-%m-%d %H:%I:%S'), # 'username': item.username, # 'mobile': item.mobile, # 'address': item.address, # 'box_num': item.box_num, # 'box_item': box_item, # 'status': item.status, # 'logistics_no': item.logistics_no, } # box_item_list = OrderItem.get_multi_by_order_id(item.id) # for b_item in box_item_list: # box_item.append(b_item.item_id) result_list.append(result) return result_list
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'购买失败')