本文實(shí)例講述了python實(shí)現(xiàn)美團(tuán)訂單推送到測試環(huán)境,提供便利操作。分享給大家供大家參考,具體如下:
背景:
有時(shí)候需要在測試環(huán)境下一個(gè)美團(tuán)的訂單,每次都找一堆的東西,太繁瑣,于是寫了接口請求數(shù)據(jù),然后把數(shù)據(jù)推送到測試環(huán)境。實(shí)現(xiàn)了可以在測試環(huán)境進(jìn)行:生成新訂單、取消訂單、騎手搶單、騎手送達(dá)、申請整單退款、申請部分退款流程。
# -*- coding: utf-8 -*- import hashlib import time import requests from order30 import conf app_id = conf.app_id secret = conf.secret def get_md5(string):#返回字符串md5加密后的串 hl = hashlib.md5() hl.update(string.encode('utf-8')) return hl.hexdigest() def get_tamp():#獲取當(dāng)前的時(shí)間戳 t = time.time() return int(t) def get_format_time():#獲取現(xiàn)在的格式化標(biāo)準(zhǔn)時(shí)間:年-月-日 時(shí):分:秒 time_now = int(time.time()) timestr = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time_now)) return timestr def req_get_result(api_url,api_data):#get方法請求函數(shù) req_get = requests.get(api_url,api_data) result = req_get.json() return result def req_post_result(api_url,api_data):#post方法請求函數(shù) req_post = requests.post(api_url,data=api_data) result = req_post.json() return result def param_sort(param_dict):#傳入字典,返回排序后并且連接好的字符串 keys_list = sorted(param_dict.keys()) rb_str = '' for k in keys_list: key_value = k + '=' + str(param_dict[k]) rb_str = rb_str + key_value +'&' rb_str = rb_str[0:-1] #不保留字符串末尾的& return rb_str def get_order_detail(outer_order_id):#根據(jù)三方訂單號,返回訂單詳情 api_url = 'http://waimaiopen.meituan.com/api/v1/order/getOrderDetail' timestamp = get_tamp()#當(dāng)前時(shí)間的時(shí)間戳 api_data = { 'app_id':app_id, 'timestamp':timestamp, 'order_id':outer_order_id } sort_str = param_sort(api_data) #對參數(shù)進(jìn)行排序,固定格式。 params_str = api_url+'?'+sort_str+secret #參加簽名的字符串 sig = get_md5(params_str)#獲得簽名后的字符串 api_data['sig'] = sig #把簽名串加進(jìn)請求參數(shù) result = req_get_result(api_url,api_data) order_detail = result['data'] return order_detail def push_order(outer_order_id):#向測試環(huán)境推送一個(gè)美團(tuán)訂單 order_detail = get_order_detail(outer_order_id) timestamp = get_tamp() api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用 api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環(huán)境url,接收數(shù)據(jù) order_data = { 'order_id':order_detail['order_id'], #int,訂單id 'wm_order_id_view':order_detail['wm_order_id_view'], #int,訂單展示id 'app_poi_code':order_detail['app_poi_code'], #電商門店id 'wm_poi_name':order_detail['wm_poi_name'], #美團(tuán)門店名稱 'wm_poi_address':order_detail['wm_poi_address'], #美團(tuán)門店地址 'wm_poi_phone':order_detail['wm_poi_phone'], #美團(tuán)商家電話 'recipient_address':order_detail['recipient_address'], #收件人收貨地址 'shipping_fee':order_detail['shipping_fee'], #float,門店配送費(fèi) 'total':order_detail['total'], #double,總價(jià) 'original_price':order_detail['original_price'], #double,原價(jià) 'caution':order_detail['caution'], #忌口或備注 'shipper_phone':order_detail['shipper_phone'], #送餐員電話 'status':2, #int,訂單狀態(tài) 'city_id':order_detail['city_id'], #long,城市ID(目前暫時(shí)用不到此信息) 'has_invoiced':order_detail['has_invoiced'], #int,是否開發(fā)票,0不開,1開 'invoice_title':order_detail['invoice_title'], #發(fā)票抬頭 'ctime':order_detail['ctime'], #long,創(chuàng)建時(shí)間 'utime':order_detail['utime'], #long,更新時(shí)間 'delivery_time':order_detail['delivery_time'], #long,用戶預(yù)計(jì)送達(dá)時(shí)間,0表示“立即送達(dá)” 'is_third_shipping':order_detail['is_third_shipping'], #int,是否第三方配送平臺配送,0表否,1表是 'pay_type':order_detail['pay_type'], #int,支付類型,1貨到付款,2在線支付 'latitude':order_detail['latitude'], #double,實(shí)際送餐地址緯度 'longitude':order_detail['longitude'], #double,實(shí)際送餐地址經(jīng)度 'detail':order_detail['detail'], #訂單商品詳情 'extras':order_detail['extras'], #優(yōu)惠信息 'avg_send_time':order_detail['avg_send_time'], #平均送餐時(shí)間,單位為秒 'day_seq':order_detail['day_seq'], #流水號 'recipient_phone':order_detail['recipient_phone'], #收件人電話 'recipient_name':order_detail['recipient_name'], #收件人姓名 'app_id':app_id, #appid,標(biāo)識哪個(gè)商家 'timestamp':timestamp, #時(shí)間戳 } sort_str = param_sort(order_data) params_str = api_url + '?' + sort_str + secret #參加簽名的字符串 sig = get_md5(params_str) #簽名后的字符串 order_data['sig'] = sig result = req_post_result(api_url_test,order_data) return result def shipping_order(outer_order_id,logistics_status): #向測試環(huán)境推送美團(tuán)訂單配送狀態(tài) timestamp = get_tamp() api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用 api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環(huán)境url,接收數(shù)據(jù) order_data = { 'order_id':outer_order_id, #訂單號 'logistics_status':logistics_status, #10訂單確認(rèn),40騎手已送達(dá),100配送單已取消 'time':timestamp, #操作的時(shí)間 'dispatcher_name':'美團(tuán)騎手', #騎手姓名 'dispatcher_mobile':'135xxxxxxxx', #騎手電話 'app_id':app_id, #appid,標(biāo)識哪個(gè)商家 'timestamp':timestamp, #時(shí)間戳 } sort_str = param_sort(order_data) params_str = api_url + '?' + sort_str + secret #參加簽名的字符串 sig = get_md5(params_str) #簽名后的字符串 order_data['sig'] = sig result = req_post_result(api_url_test,order_data) return result def refund_order(outer_order_id):#向測試環(huán)境推送美團(tuán)訂單整單退 timestamp = get_tamp() t_reason = get_format_time() api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用 api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環(huán)境url,接收數(shù)據(jù) order_data = { 'order_id':outer_order_id, #訂單號 'notify_type':'apply', #apply:發(fā)起退款 'reason':'整單退款原因%s'%t_reason, #退款原因 'app_id':app_id, #appid,標(biāo)識哪個(gè)商家 'timestamp':timestamp, #時(shí)間戳 } sort_str = param_sort(order_data) params_str = api_url + '?' + sort_str + secret #參加簽名的字符串 sig = get_md5(params_str) #簽名后的字符串 order_data['sig'] = sig result = req_get_result(api_url_test,order_data) return result def refund_order_part(outer_order_id):#向測試環(huán)境推送美團(tuán)部分退訂單 timestamp = get_tamp() t_reason = get_format_time() api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用 api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環(huán)境url,接收數(shù)據(jù) order_detail = get_order_detail(outer_order_id) food_first = eval(order_detail['detail'])[0] #獲取第0個(gè)商品 #組裝退貨商品信息 food_dict = { 'app_food_code':food_first['app_food_code'], #商品id,即電商商品編碼 'food_name':food_first['food_name'], #商品名稱 'sku_id':food_first['sku_id'], #商品的skuid 'spec':food_first['spec'], #單位 'food_price':food_first['price'], #商品價(jià)格 'count':1, #退貨數(shù)量, 'box_num':1, #打包盒數(shù)量 'box_price':food_first['box_price'], #打包盒價(jià)格 'origin_food_price':food_first['price'], #商品原價(jià) 'refund_price':food_first['price'] #退款價(jià)格 } temp_list = [] temp_list.append(food_dict) food_info = str(temp_list) #組裝接口發(fā)送數(shù)據(jù) order_data = { 'order_id':outer_order_id, #訂單號 'notify_type':'part', #part:發(fā)起部分退款 'reason':'部分退款原因%s'%t_reason, #退款原因 'app_id':app_id, #appid,標(biāo)識哪個(gè)商家 'timestamp':timestamp, #時(shí)間戳 'food':food_info, #退款商品信息 'money':food_first['price'], #退款金額 'res_type':0 #0:未處理,5、超過24小時(shí)自動(dòng)同意 } sort_str = param_sort(order_data) params_str = api_url + '?' + sort_str + secret #參加簽名的字符串 sig = get_md5(params_str) #簽名后的字符串 order_data['sig'] = sig result = req_get_result(api_url_test,order_data) return result def cancel_order(outer_order_id):#接單前,向測試環(huán)境推送用戶發(fā)起的取消訂單 timestamp = get_tamp() t_reason = get_format_time() api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用 api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環(huán)境url,接收數(shù)據(jù) order_data = { 'order_id':outer_order_id, #訂單號 'reason_code':1002, #訂單取消原因code 'reason':'用戶取消原因%s'%t_reason, #用戶取消原因 'app_id':app_id, #appid,標(biāo)識哪個(gè)商家 'timestamp':timestamp, #時(shí)間戳 } sort_str = param_sort(order_data) params_str = api_url + '?' + sort_str + secret #參加簽名的字符串 sig = get_md5(params_str) #簽名后的字符串 order_data['sig'] = sig result = req_get_result(api_url_test,order_data) return result
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
