def sync_tb(db, step): goods = db.query( 'select gds.id,gds.status,gds.distributor_goods_id ' 'from goods_distributor_shop gds, distributor_shop ds ' 'where gds.distributor_shop_id=ds.id and gds.status <> "PREPARE" and gds.deleted=0 ' 'and gds.distributor_goods_id <> "" and ds.distributor_id=%s', options.distributor_id_taobao) goods_dict = dict([(int(g.distributor_goods_id), g) for g in goods]) api_info = db.get( 'select taobao_api_info from distributor_shop where id=%s', options.shop_id_tmall) api_info = json.loads(api_info.taobao_api_info, object_hook=json_hook) goods_slice = [goods[i:i + step] for i in range(0, len(goods), step)] for slc in goods_slice: taobao = Taobao('taobao.items.list.get') taobao.set_app_info(api_info.app_key, api_info.app_secret_key) response = taobao.sync_fetch( fields='num_iid,approve_status', num_iids=','.join(map(str, [s.distributor_goods_id for s in slc]))) taobao.parse_response(response) if not taobao.is_ok(): logging.error('sync goods status. taobao request error %s', taobao.error) continue if len(taobao.message['items'].item) < len(slc): # 有商品已被删除 slc_id_set = set([s.distributor_goods_id for s in slc]) num_iid_set = set( [str(i.num_iid) for i in taobao.message['items'].item]) deleted_ids = slc_id_set - num_iid_set db.execute( 'update goods_distributor_shop gds, distributor_shop ds ' 'set gds.deleted="1", gds.deleted_at=NOW() ' 'where gds.distributor_shop_id=ds.id and ds.distributor_id=%%s ' 'and gds.distributor_goods_id in (%s)' % ','.join(['%s'] * len(deleted_ids)), options.distributor_id_taobao, *list(deleted_ids)) logging.info('sync goods status. delete taobao item %s', ','.join(deleted_ids)) for item in taobao.message['items'].item: g = goods_dict[item.num_iid] if item.approve_status == 'instock' and g.status != 'OFF_SALE': # 下架 db.execute( 'update goods_distributor_shop gds, distributor_shop ds ' 'set gds.status="OFF_SALE", gds.offsale_at=NOW(), gds.onsale_at=NULL ' 'where gds.distributor_shop_id=ds.id and ds.distributor_id=%s ' 'and gds.distributor_goods_id=%s', options.distributor_id_taobao, g.distributor_goods_id) logging.info('sync goods status. offsale taobao item %s', g.distributor_goods_id) elif item.approve_status == 'onsale' and g.status != 'ON_SALE': # 上架 db.execute( 'update goods_distributor_shop gds, distributor_shop ds ' 'set gds.status="ON_SALE", gds.onsale_at=NOW(), gds.offsale_at=NULL ' 'where gds.distributor_shop_id=ds.id and ds.distributor_id=%s ' 'and gds.distributor_goods_id=%s', options.distributor_id_taobao, g.distributor_goods_id) logging.info('sync goods status. onsale taobao item %s', g.distributor_goods_id)
def partner_api_verify(db, coupon_sn): # 查找券信息 coupon = db.get( 'select i.*, c.*, c.id cid from item i, item_coupon c where c.sn=%s and i.id=c.item_id', coupon_sn) coupon_sn = coupon_sn.encode('utf-8') logging.info('被验证券sn:%s', coupon.sn) if not coupon: raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='券号不存在')) distributor_order = db.get( 'select do.* from distributor_order do, orders o where do.id=o.distributor_order_id ' 'and o.id=%s', coupon.order_id) if coupon.distr_id == options.distributor_id_taobao: # 淘宝验证 distributor_shop = db.get('select * from distributor_shop where id=%s', distributor_order.distributor_shop_id) order_message = json.loads(distributor_order.message, object_hook=json_hook) app_info = json.loads(distributor_shop.taobao_api_info, object_hook=json_hook) taobao = Taobao('taobao.vmarket.eticket.consume') taobao.set_app_info(app_info.app_key, app_info.app_secret_key) if app_info.app_key == options.taobao_kunran_app_key: taobao.add_fields(codemerchant_id=options.taobao_kunran_id, posid=options.taobao_coupon_posid) taobao.set_session(app_info.merchant_session) else: taobao.set_session(app_info.session) response = yield taobao(order_id=distributor_order.order_no, verify_code=coupon_sn, token=order_message.token, consume_num=1) logging.info('taobao coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body) taobao.parse_response(response.body) if taobao.is_ok(): raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功')) else: raise gen.Return( PropDict(ok=False, coupon_sn=coupon_sn, msg=taobao.error.msg.encode('utf-8'))) elif coupon.distr_id == options.distributor_id_jingdong: # 京东验证 shop = db.get( 'select taobao_seller_id, taobao_api_info from distributor_shop where id=%s', coupon.distr_shop_id) api_info = json.loads(shop.taobao_api_info, object_hook=json_hook) jingdong = Jingdong('verifyCode', str(shop.taobao_seller_id), api_info.vender_key, api_info.secret_key) response = yield jingdong(jd_order_id=distributor_order.order_no, coupon_id=coupon.distr_sn, coupon_pwd=coupon.distr_pwd) logging.info('jingdong coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body) jingdong.parse_response(response.body) if jingdong.is_ok(): raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功')) else: raise gen.Return( PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败')) elif coupon.distr_shop_id == options.shop_id_wuba: # 58验证 wuba = Wuba('order.ticketcheck') # todo 弥补之前的错误 if 211796 < coupon.cid < 219863: oid = coupon.order_id else: oid = coupon.order_no response = yield wuba(ticketId=coupon.cid, orderId=oid, ticketIdIndex=0) logging.info('wuba coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body) wuba.parse_response(response.body) if wuba.is_ok() and wuba.message.data.result == 1: raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功')) else: raise gen.Return( PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败')) else: logging.info('partner api verify skip. coupon_sn:%s', coupon_sn) raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='无需第三方验证'))
def sync_tb(db, step): goods = db.query( "select gds.id,gds.status,gds.distributor_goods_id " "from goods_distributor_shop gds, distributor_shop ds " 'where gds.distributor_shop_id=ds.id and gds.status <> "PREPARE" and gds.deleted=0 ' 'and gds.distributor_goods_id <> "" and ds.distributor_id=%s', options.distributor_id_taobao, ) goods_dict = dict([(int(g.distributor_goods_id), g) for g in goods]) api_info = db.get("select taobao_api_info from distributor_shop where id=%s", options.shop_id_tmall) api_info = json.loads(api_info.taobao_api_info, object_hook=json_hook) goods_slice = [goods[i : i + step] for i in range(0, len(goods), step)] for slc in goods_slice: taobao = Taobao("taobao.items.list.get") taobao.set_app_info(api_info.app_key, api_info.app_secret_key) response = taobao.sync_fetch( fields="num_iid,approve_status", num_iids=",".join(map(str, [s.distributor_goods_id for s in slc])) ) taobao.parse_response(response) if not taobao.is_ok(): logging.error("sync goods status. taobao request error %s", taobao.error) continue if len(taobao.message["items"].item) < len(slc): # 有商品已被删除 slc_id_set = set([s.distributor_goods_id for s in slc]) num_iid_set = set([str(i.num_iid) for i in taobao.message["items"].item]) deleted_ids = slc_id_set - num_iid_set db.execute( "update goods_distributor_shop gds, distributor_shop ds " 'set gds.deleted="1", gds.deleted_at=NOW() ' "where gds.distributor_shop_id=ds.id and ds.distributor_id=%%s " "and gds.distributor_goods_id in (%s)" % ",".join(["%s"] * len(deleted_ids)), options.distributor_id_taobao, *list(deleted_ids) ) logging.info("sync goods status. delete taobao item %s", ",".join(deleted_ids)) for item in taobao.message["items"].item: g = goods_dict[item.num_iid] if item.approve_status == "instock" and g.status != "OFF_SALE": # 下架 db.execute( "update goods_distributor_shop gds, distributor_shop ds " 'set gds.status="OFF_SALE", gds.offsale_at=NOW(), gds.onsale_at=NULL ' "where gds.distributor_shop_id=ds.id and ds.distributor_id=%s " "and gds.distributor_goods_id=%s", options.distributor_id_taobao, g.distributor_goods_id, ) logging.info("sync goods status. offsale taobao item %s", g.distributor_goods_id) elif item.approve_status == "onsale" and g.status != "ON_SALE": # 上架 db.execute( "update goods_distributor_shop gds, distributor_shop ds " 'set gds.status="ON_SALE", gds.onsale_at=NOW(), gds.offsale_at=NULL ' "where gds.distributor_shop_id=ds.id and ds.distributor_id=%s " "and gds.distributor_goods_id=%s", options.distributor_id_taobao, g.distributor_goods_id, ) logging.info("sync goods status. onsale taobao item %s", g.distributor_goods_id)
def partner_api_verify(db, coupon_sn): # 查找券信息 coupon = db.get('select i.*, c.*, c.id cid from item i, item_coupon c where c.sn=%s and i.id=c.item_id', coupon_sn) coupon_sn = coupon_sn.encode('utf-8') logging.info('被验证券sn:%s', coupon.sn) if not coupon: raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='券号不存在')) distributor_order = db.get('select do.* from distributor_order do, orders o where do.id=o.distributor_order_id ' 'and o.id=%s', coupon.order_id) if coupon.distr_id == options.distributor_id_taobao: # 淘宝验证 distributor_shop = db.get('select * from distributor_shop where id=%s', distributor_order.distributor_shop_id) order_message = json.loads(distributor_order.message, object_hook=json_hook) app_info = json.loads(distributor_shop.taobao_api_info, object_hook=json_hook) taobao = Taobao('taobao.vmarket.eticket.consume') taobao.set_app_info(app_info.app_key, app_info.app_secret_key) if app_info.app_key == options.taobao_kunran_app_key: taobao.add_fields(codemerchant_id=options.taobao_kunran_id, posid=options.taobao_coupon_posid) taobao.set_session(app_info.merchant_session) else: taobao.set_session(app_info.session) response = yield taobao(order_id=distributor_order.order_no, verify_code=coupon_sn, token=order_message.token, consume_num=1) logging.info('taobao coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body) taobao.parse_response(response.body) if taobao.is_ok(): raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功')) else: raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg=taobao.error.msg.encode('utf-8'))) elif coupon.distr_id == options.distributor_id_jingdong: # 京东验证 shop = db.get('select taobao_seller_id, taobao_api_info from distributor_shop where id=%s', coupon.distr_shop_id) api_info = json.loads(shop.taobao_api_info, object_hook=json_hook) jingdong = Jingdong('verifyCode', str(shop.taobao_seller_id), api_info.vender_key, api_info.secret_key) response = yield jingdong(jd_order_id=distributor_order.order_no, coupon_id=coupon.distr_sn, coupon_pwd=coupon.distr_pwd) logging.info('jingdong coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body) jingdong.parse_response(response.body) if jingdong.is_ok(): raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功')) else: raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败')) elif coupon.distr_shop_id == options.shop_id_wuba: # 58验证 wuba = Wuba('order.ticketcheck') # todo 弥补之前的错误 if 211796 < coupon.cid < 219863: oid = coupon.order_id else: oid = coupon.order_no response = yield wuba(ticketId=coupon.cid, orderId=oid, ticketIdIndex=0) logging.info('wuba coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body) wuba.parse_response(response.body) if wuba.is_ok() and wuba.message.data.result == 1: raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功')) else: raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败')) else: logging.info('partner api verify skip. coupon_sn:%s', coupon_sn) raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='无需第三方验证'))