def orderSuccess(self, pay_order_id=0, params=None): try: pay_order_info = PayOrder.query.filter_by(id=pay_order_id).first() if not pay_order_info or pay_order_info.status not in [-8, -7]: return True pay_order_info.pay_sn = params['pay_sn'] pay_order_info.status = 1 pay_order_info.express_status = -7 pay_order_info.updated_time = getCurrentDate() pay_order_info.pay_time = getCurrentDate() db.session.add(pay_order_info) # 售卖记录 pay_order_items = PayOrderItem.query.filter_by( pay_order_id=pay_order_id).all() for order_item in pay_order_items: tmp_model_sale_log = ShopSaleChangeLog() tmp_model_sale_log.food_id = order_item.food_id tmp_model_sale_log.quantity = order_item.quantity tmp_model_sale_log.price = order_item.price tmp_model_sale_log.member_id = order_item.member_id tmp_model_sale_log.created_time = getCurrentDate() db.session.add(tmp_model_sale_log) db.session.commit() except Exception as e: db.session.rollback() return False QueueService.addQueue( "pay", { "member_id": pay_order_info.member_id, "pay_order_id": pay_order_info.id })
def closeOrder(self, pay_order_id=0): if pay_order_id < 1: return False pay_order_info = PayOrder.query.filter_by(id=pay_order_id, status=-8).first() if not pay_order_info: return False pay_order_items = PayOrderItem.query.filter_by( pay_order_id=pay_order_id).all() if pay_order_items: # 需要归还库存 for item in pay_order_items: tmp_food_info = Shop_Info.query.filter_by( id=item.food_id).first() if tmp_food_info: tmp_food_info.stock = tmp_food_info.stock + item.quantity tmp_food_info.updated_time = getCurrentDate() db.session.add(tmp_food_info) db.session.commit() pay_order_info.status = 0 pay_order_info.updated_time = getCurrentDate() db.session.add(pay_order_info) db.session.commit() return True
def getAccessToken(self): token = None token_info = OauthAccessToken.query.order_by( OauthAccessToken.id.desc()).first() if token_info and token_info.expired_time >= getCurrentDate(): token = token_info.access_token return token config_mina = app.config['MINA_APP'] url = "https://dopen.weimob.com/fuwu/b/oauth2/token?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}" \ .format(config_mina['appid'], config_mina['appkey'], token_info.refresh_token) r = requests.post(url=url) if r.status_code != 200 or not r.text: return token data = json.loads(r.text) now = datetime.datetime.now() date = now + datetime.timedelta(seconds=data['expires_in'] - 200) model_token = OauthAccessToken() model_token.access_token = data['access_token'] model_token.expired_time = date.strftime("%Y-%m-%d %H:%M:%S") model_token.created_time = getCurrentDate() model_token.refresh_token = data['refresh_token'] model_token.refresh_token_expires_in = data['refresh_token_expires_in'] model_token.scope = data['scope'] model_token.business_id = data['business_id'] model_token.public_account_id = data['public_account_id'] db.session.add(model_token) db.session.commit() return data['access_token']
def edit(): resp = {'code': '200', 'msg': '操作成功'} file_target = request.files req = request.values if len(file_target) > 0: img_url = uploadImg(file_target) else: img_url = req['ShopImageUrl'] if len(req['shopname']) < 1: resp['code'] = -1 resp['msg'] = '请输入商品名' if len(req['shopdesc']) < 1: resp['code'] = -1 resp['msg'] = '请输入商品描述' if req['merchant'] and int(req['merchant']) < 0: resp['code'] = -1 resp['msg'] = '请选择商家' if req['price'] and int(req['price']) < 0: resp['code'] = -1 resp['msg'] = '请输入价格' if req['price'] < req['floorprice']: resp['code'] = -1 resp['msg'] = '当前售价不能小于底价' info = Shop_Info.query.filter_by(Id=req['id']).first() if info: modal_shop = info else: modal_shop = Shop_Info() modal_shop.CreatTime = getCurrentDate() modal_shop.ShopStatus = 1 modal_shop.ShopName = req['shopname'] modal_shop.ShopDesc = req['shopdesc'] modal_shop.ShopFloorPrice = req['floorprice'] modal_shop.ShopPrice = req['price'] modal_shop.ShopImageUrl = img_url modal_shop.ShopMerchantId = req['merchant'] modal_shop.Stock = req['stock'] modal_shop.UpdateTime = getCurrentDate() db.session.add(modal_shop) db.session.commit() return redirect(UrlManager.buildUrl("/admin/commodity"))
def login(): resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values code = req['code'] if 'code' in req else '' if not code or len(code) < 1: resp['code'] = -1 resp['msg'] = "需要code" return jsonify(resp) openid = MemberService.getWeChatOpenId(code) if openid is None: resp['code'] = -1 resp['msg'] = "调用微信出错" return jsonify(resp) nickname = req['nickName'] if 'nickName' in req else '' sex = req['gender'] if 'gender' in req else 0 avatar = req['avatarUrl'] if 'avatarUrl' in req else '' city = req['city'] if 'city' in req else '' ''' 判断是否已经测试过,注册了直接返回一些信息 ''' bind_info = OauthMemberBind.query.filter_by(Openid=openid, Type=1).first() if not bind_info: model_member = Member() model_member.Nickname = nickname model_member.Sex = sex model_member.Avatar = avatar model_member.City = city model_member.Salt = MemberService.geneSalt() model_member.UpdatedTime = model_member.CreatedTime = getCurrentDate() db.session.add(model_member) db.session.commit() model_bind = OauthMemberBind() model_bind.Member_id = model_member.Id model_bind.Type = 1 model_bind.Openid = openid model_bind.Extra = '' model_bind.UpdateTime = model_bind.CreatTime = getCurrentDate() db.session.add(model_bind) db.session.commit() bind_info = model_bind member_info = Member.query.filter_by(Id=bind_info.Member_id).first() token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.Id) resp['data'] = {'token': token, 'openId': openid} return jsonify(resp)
def receipt_balance(): resp = {'code': 200, 'msg': '提现申请已提交'} req = request.values merchantId = req['merchantId'] if 'merchantId' in req else -1 balance = req['balance'] if 'balance' in req else 0 receipt_qrcode = req['qrcode'] if 'qrcode' in req else '' merchant_info = Merchant_Info.query.filter_by(Id=merchantId).first() merchant_info.FreezeBalance = decimal.Decimal(balance) merchant_info.TotalBalance -= merchant_info.FreezeBalance b_rule = and_(Balance_Log.merchant_id == merchant_info.Id, Balance_Log.operating == 4, Balance_Log.status == 4) b_info = Balance_Log.query.filter(b_rule).first() if b_info and b_info.status == 4: resp['code'] = -1, resp['msg'] = '存在正在提现的申请' return jsonify(resp) balance_log = Balance_Log() balance_log.createtime = balance_log.updatetime = getCurrentDate() balance_log.status = 4 balance_log.merchant_id = merchantId, balance_log.operating = 4, # 4 提现申请 status =1 提现审核 status=2 已提现 balance_log.balance = balance balance_log.total_balance = merchant_info.TotalBalance balance_log.freeze_balance = merchant_info.FreezeBalance balance_log.receipt_qrcode = receipt_qrcode db.session.add(balance_log) db.session.commit() return jsonify(resp)
def callback(): req = request.values code = req['code'] if 'code' in req else '' state = req['state'] if 'state' in req else '' config_mina = app.config['MINA_APP'] url = 'https://dopen.weimob.com/fuwu/b/oauth2/token?code={0}&grant_type=authorization_code&client_id={1}&client_secret={2}&redirect_uri={3}'.format( code, config_mina['appid'], config_mina['appkey'], 'http://47.104.176.254/api/callback') r = requests.post(url=url) data = json.loads(r.text) if data['access_token']: info = OauthAccessToken() info.access_token = data['access_token'] info.expired_time = data['expires_in'] info.created_time = getCurrentDate() info.refresh_token = data['refresh_token'] info.refresh_token_expires_in = data['refresh_token_expires_in'] info.scope = data['scope'] info.business_id = data['business_id'] info.public_account_id = data['public_account_id'] db.session.add(info) db.session.commit() return '授权成功' return jsonify(data)
def orderOps(): resp = {'code': 200, 'msg': '操作成功~', 'data': {}} req = request.values member_info = g.member_info order_sn = req['order_sn'] if 'order_sn' in req else '' act = req['act'] if 'act' in req else '' pay_order_info = PayOrder.query.filter_by(order_sn=order_sn, member_id=member_info.Id).first() if not pay_order_info: resp['code'] = -1 resp['msg'] = "系统繁忙,请稍后再试" return jsonify(resp) if act == "cancel": target_pay = PayService() ret = target_pay.closeOrder(pay_order_id=pay_order_info.id) if not ret: resp['code'] = -1 resp['msg'] = "系统繁忙,请稍后再试" return jsonify(resp) elif act == "confirm": pay_order_info.express_status = 1 pay_order_info.updated_time = getCurrentDate() db.session.add(pay_order_info) db.session.commit() return jsonify(resp)
def uploadByFile(file): config_upload = app.config['UPLOAD'] resp = {'code': 200, 'msg': '操作成功', 'data': {}} filename = secure_filename(file.filename) ext = filename.rsplit(".", 1)[1] if ext not in config_upload['ext']: resp['code'] = -1 resp['msg'] = "不允许的扩展类型文件" return resp root_path = app.root_path + config_upload['prefix_path'] # 不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容 file_dir = datetime.datetime.now().strftime("%Y%m%d") save_dir = root_path + file_dir if not os.path.exists(save_dir): os.mkdir(save_dir) os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO) file_name = str(uuid.uuid4()).replace("-", "") + "." + ext file.save("{0}/{1}".format(save_dir, file_name)) model_image = Image() model_image.File_key = file_dir + "/" + file_name model_image.CreateTime = getCurrentDate() db.session.add(model_image) db.session.commit() resp['data'] = {'file_key': model_image.File_key} return resp
def addmerchant(): resp = {'code': 200, 'msg': '申请成功'} req = request.values id = req['id'] if 'id' in req else -1 name = req['name'] if 'name' in req else '' phone = req['phone'] if 'phone' in req else '' address = req['address'] if 'address' in req else '' imageUrl = req['imageUrl'] if 'imageUrl' in req else '' openId = req['openId'] if 'openId' in req else '' info = Merchant_Info.query.filter_by(Id=id).first() if info: modal_info = info else: modal_info = Merchant_Info() modal_info.CreateTime = getCurrentDate() modal_info.Name = name modal_info.Address = address modal_info.Phone = phone modal_info.ImageUrl = imageUrl modal_info.OpenId = openId modal_info.Status = 1 modal_info.TotalBalance = 0 modal_info.FreezeBalance = 0 db.session.add(modal_info) db.session.commit() return jsonify(resp)
def statSite(self, params): act = params['act'] date = params['date'] date_from = params['date_from'] date_to = params['date_to'] app.logger.info("act:{0},from:{1},to:{2}".format(act, date_from, date_to)) stat_pay = db.session.query(func.sum(PayOrder.total_price).label("total_pay_money")) \ .filter(PayOrder.status == 1) \ .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to).first() stat_member_count = Member.query.count() stat_new_member_count = Member.query.filter(Member.CreatedTime >= date_from, Member.CreatedTime <= date_to).count() stat_order_count = PayOrder.query.filter_by(status=1) \ .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to) \ .count() stat_share_count = WxShareHistory.query.filter(WxShareHistory.CreateTime >= date_from , WxShareHistory.CreateTime <= date_to).count() tmp_stat_site = StatDailySite.query.filter_by(date=date).first() if tmp_stat_site: tmp_model_stat_site = tmp_stat_site else: tmp_model_stat_site = StatDailySite() tmp_model_stat_site.date = date tmp_model_stat_site.created_time = getCurrentDate() tmp_model_stat_site.total_pay_money = stat_pay[0] if stat_pay[0] else 0.00 tmp_model_stat_site.total_new_member_count = stat_new_member_count tmp_model_stat_site.total_member_count = stat_member_count tmp_model_stat_site.total_order_count = stat_order_count tmp_model_stat_site.total_shared_count = stat_share_count tmp_model_stat_site.updated_time = getCurrentDate() ''' 为了测试效果模拟数据 ''' # tmp_model_stat_site.total_pay_money = random.randint(1000, 1010) # tmp_model_stat_site.total_new_member_count = random.randint(50, 100) # tmp_model_stat_site.total_member_count += tmp_model_stat_site.total_new_member_count # tmp_model_stat_site.total_order_count = random.randint(900, 1000) # tmp_model_stat_site.total_shared_count = random.randint(1000, 2000) db.session.add(tmp_model_stat_site) db.session.commit()
def addQueue(queue_name, data=None): model_queue = QueueList() model_queue.queue_name = queue_name if data: model_queue.data = json.dumps(data) model_queue.created_time = model_queue.updated_time = getCurrentDate() db.session.add(model_queue) db.session.commit() return True
def statFood(self, params): act = params['act'] date = params['date'] date_from = params['date_from'] date_to = params['date_to'] app.logger.info("act:{0},from:{1},to:{2}".format(act, date_from, date_to)) stat_food_list = db.session.query(ShopSaleChangeLog.food_id, func.sum(ShopSaleChangeLog.quantity).label("total_count"), func.sum(ShopSaleChangeLog.price).label("total_pay_money")) \ .filter(ShopSaleChangeLog.created_time >= date_from, ShopSaleChangeLog.created_time <= date_to) \ .group_by(ShopSaleChangeLog.food_id).all() if not stat_food_list: app.logger.info("no data") return for item in stat_food_list: tmp_food_id = item[0] tmp_stat_food = StatDailyFood.query.filter_by(date=date, food_id=tmp_food_id).first() if tmp_stat_food: tmp_model_stat_food = tmp_stat_food else: tmp_model_stat_food = StatDailyFood() tmp_model_stat_food.date = date tmp_model_stat_food.food_id = tmp_food_id tmp_model_stat_food.created_time = getCurrentDate() tmp_model_stat_food.total_count = item[1] tmp_model_stat_food.total_pay_money = item[2] tmp_model_stat_food.updated_time = getCurrentDate() ''' 为了测试效果模拟数据 ''' # tmp_model_stat_food.total_count = random.randint(50, 100) # tmp_model_stat_food.total_pay_money = random.randint(1000, 1010) db.session.add(tmp_model_stat_food) db.session.commit() return
def statMember(self, params): act = params['act'] date = params['date'] date_from = params['date_from'] date_to = params['date_to'] app.logger.info("act:{0},from:{1},to:{2}".format(act, date_from, date_to)) member_list = Member.query.all() if not member_list: app.logger.info("no member list") return for member_info in member_list: tmp_stat_member = StatDailyMember.query.filter_by(date=date, member_id=member_info.Id).first() if tmp_stat_member: tmp_model_stat_member = tmp_stat_member else: tmp_model_stat_member = StatDailyMember() tmp_model_stat_member.date = date tmp_model_stat_member.member_id = member_info.Id tmp_model_stat_member.created_time = getCurrentDate() tmp_stat_pay = db.session.query(func.sum(PayOrder.total_price).label("total_pay_money")) \ .filter(PayOrder.member_id == member_info.Id, PayOrder.status == 1) \ .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to).first() tmp_stat_share_count = WxShareHistory.query.filter(PayOrder.member_id == member_info.Id) \ .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to).count() tmp_model_stat_member.total_shared_count = tmp_stat_share_count tmp_model_stat_member.total_pay_money = tmp_stat_pay[0] if tmp_stat_pay[0] else 0.00 ''' 为了测试效果模拟数据 ''' # tmp_model_stat_member.total_shared_count = random.randint(50, 100) # tmp_model_stat_member.total_pay_money = random.randint(1000, 1010) tmp_model_stat_member.updated_time = getCurrentDate() db.session.add(tmp_model_stat_member) db.session.commit() return
def writeoff(): resp = {'code': 200, 'msg': '操作成功'} req = request.values order_sn = req['order_sn'] if 'order_sn' in req else '' couponId = req['couponId'] if 'couponId' in req else '' merchantId = req['merchantId'] if 'merchantId' in req else '' order_info = PayOrder.query.filter_by(order_sn=order_sn).first() coupon_info = Coupon_Info.query.filter_by(Id=couponId).first() merchant_info = Merchant_Info.query.filter_by(Id=merchantId).first() if order_info.express_status == 1: resp['code'] = -1, resp['msg'] = '该订单已经核销过了' return jsonify(resp) if order_info: shop_id = PayOrderItem.query.filter_by(pay_order_id=order_info.id).first().food_id shopMerchantId = Shop_Info.query.filter_by(Id=shop_id).first().ShopMerchantId if int(shopMerchantId) != int(merchantId): resp['code'] = -1 resp['msg'] = '商户不匹配' return jsonify(resp) order_info.express_status = 1 db.session.add(order_info) db.session.commit() if coupon_info: coupon_info.Status = 4 db.session.add(coupon_info) db.session.commit() # 计算余额 merchant_info.TotalBalance += order_info.total_price db.session.add(merchant_info) db.session.commit() # 余额写入日志 balance_log = Balance_Log() balance_log.createtime = balance_log.updatetime = getCurrentDate() balance_log.status = 1 balance_log.merchant_id = merchantId, balance_log.operating = 1, # 1 添加操作 balance_log.balance = order_info.total_price balance_log.total_balance = merchant_info.TotalBalance balance_log.freeze_balance = 0 db.session.add(balance_log) db.session.commit() return jsonify(resp)
def addPayCallbackData(self, pay_order_id=0, type='pay', data=''): model_paycallback = PayOrderCallbackData() model_paycallback.pay_order_id = pay_order_id if type == "pay": model_paycallback.pay_data = data model_paycallback.refund_data = '' else: model_paycallback.pay_data = '' model_paycallback.refund_data = data model_paycallback.created_time = model_paycallback.updated_time = getCurrentDate( ) db.session.add(model_paycallback) db.session.commit() return True
import hashlib, requests, uuid, json, datetime, time
from web.controller.index import route_api
def memberShare(): resp = {'code': 200, 'msg': '操作成功~', 'data': {}} req = request.values url = req['url'] if 'url' in req else '' shopId = req['shopId'] if 'shopId' in req else 0 toOpenId = req['toOpenId'] if 'toOpenId' in req else '' memberId = req['memberId'] if 'memberId' in req else '' avatarUrl = req['avatarUrl'] if 'toOpenId' in req else '' nickName = req['nickName'] if 'toOpenId' in req else '' coupon_id = req['coupon_id'] if 'coupon_id' in req else '' member_info = Member.query.filter_by(Id=memberId).first() shop_info = Shop_Info.query.filter_by(Id=shopId).first() if member_info.Id == g.member_info.Id: resp['code'] = -1 resp['msg'] = '不能给自己砍价' return jsonify(resp) coupon_price = 0 if coupon_id and coupon_id != '0': modal_coupon = Coupon_Info.query.filter_by(Id=coupon_id).first() modal_coupon.UpdateTime = getCurrentDate() coupon_price = modal_coupon.Price else: modal_coupon = Coupon_Info() modal_coupon.Coupon_Name = (shop_info.ShopName + '-' + member_info.Nickname + '- 优惠券') modal_coupon.ShopId = shopId modal_coupon.Member_Id = member_info.Id modal_coupon.Price = shop_info.ShopPrice modal_coupon.Status = 1 modal_coupon.CreateTime = modal_coupon.UpdateTime = getCurrentDate() if modal_coupon.Price == shop_info.ShopFloorPrice: resp['code'] = -1 resp['msg'] = '已经是最低价了' return jsonify(resp) model_share = WxShareHistory() rule = and_(WxShareHistory.Coupon_Id == modal_coupon.Id, WxShareHistory.ToOpenId == toOpenId) ss = WxShareHistory.query.filter(rule).first() if ss: resp['code'] = -1 resp['msg'] = '你已经帮忙砍过价了' return jsonify(resp) if member_info: model_share.Member_Id = member_info.Id model_share.Share_Url = url model_share.Shop_Id = shopId model_share.ToOpenId = toOpenId model_share.ToNickName = nickName model_share.ToAvatar = avatarUrl model_share.CreateTime = getCurrentDate() model_share.Coupon_Id = -1 db.session.add(model_share) db.session.commit() pp = shop_info.ShopPrice - shop_info.ShopFloorPrice if coupon_price == 0 else coupon_price - shop_info.ShopFloorPrice yhprice = random.randint(1, int(pp)) model_share.Price = yhprice if yhprice >= pp: modal_coupon.Price = shop_info.ShopFloorPrice # 如果优惠金额低于底价 直接变成底价 modal_coupon.Coupon_Price = decimal.Decimal(shop_info.ShopPrice - shop_info.ShopFloorPrice) modal_coupon.Status = 2 # 无法再进行砍价 else: modal_coupon.Price -= yhprice pprice = modal_coupon.Coupon_Price if pprice: modal_coupon.Coupon_Price = decimal.Decimal( pprice) + decimal.Decimal(yhprice) else: modal_coupon.Coupon_Price = decimal.Decimal(yhprice) db.session.add(modal_coupon) db.session.commit() model_share.Coupon_Id = modal_coupon.Id db.session.add(model_share) db.session.commit() data = {'coupon_price': str(model_share.Price)} resp['data'] = data return jsonify(resp)
from application import app, db
from application import app, db
def createOrder( self, member_id, items=None, coupon_Id=None, params=None, ): resp = {'code': 200, 'msg': "操作成功", 'data': {}} pay_price = decimal.Decimal(0.00) continue_count = 0 foods_id = [] items = [items] for item in items: if decimal.Decimal(item['price']) < 0: continue_count += 1 continue pay_price = pay_price + decimal.Decimal(item['price']) foods_id.append(item['id']) if continue_count >= len(items): resp['code'] = -1 resp['msg'] = "商品为空" return resp yun_price = 0 note = '' yun_price = decimal.Decimal(yun_price) total_price = yun_price + pay_price # 并发处理 try: # 开启事务 tmp_food_list = db.session.query(Shop_Info).filter( Shop_Info.Id.in_(foods_id)).with_for_update().all() tmp_food_stock_mapping = {} for tmp_item in tmp_food_list: tmp_food_stock_mapping[tmp_item.Id] = tmp_item.Stock order_sn = self.geneOrderSn() if coupon_Id: coupon_info = Coupon_Info.query.filter_by(Id=coupon_Id).first() if coupon_info: coupon_info.Order_sn = order_sn total_price -= coupon_info.Coupon_Price pay_price = total_price db.session.add(coupon_info) # 支付订单 model_pay_order = PayOrder() model_pay_order.member_id = member_id model_pay_order.order_sn = order_sn model_pay_order.total_price = total_price model_pay_order.yun_price = yun_price model_pay_order.pay_price = pay_price model_pay_order.note = note model_pay_order.status = -8 model_pay_order.express_status = -8 model_pay_order.updated_time = model_pay_order.created_time = getCurrentDate( ) db.session.add(model_pay_order) for item in items: tmp_left_stock = tmp_food_stock_mapping[item['id']] if decimal.Decimal(item['price']) < 0: continue if 1 > int(tmp_left_stock): # int(item['number']) raise Exception("该商品剩余: %s" % (tmp_left_stock)) tmp_ret = Shop_Info.query.filter_by(Id=item['id']).update( {'Stock': int(tmp_left_stock) - 1}) if not tmp_ret: raise Exception("下单失败") tmp_pay_item = PayOrderItem() tmp_pay_item.pay_order_id = model_pay_order.id tmp_pay_item.member_id = member_id tmp_pay_item.quantity = 1 tmp_pay_item.price = item['price'] tmp_pay_item.food_id = item['id'] tmp_pay_item.note = note tmp_pay_item.updated_time = tmp_pay_item.created_time = getCurrentDate( ) db.session.add(tmp_pay_item) # 提交事务 db.session.commit() resp['data'] = { 'id': model_pay_order.id, 'order_sn': model_pay_order.order_sn, 'total_price': str(model_pay_order.total_price) } except Exception as e: db.session.rollback() print(e) resp['code'] = -1 resp['msg'] = "下单失败请重新下单" resp['msg'] = str(e) return resp