dvlyadmin_pro/backend/utils/kuaidi100.py
2025-03-18 08:46:50 +08:00

125 lines
6.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/python
#coding: utf-8
# +-------------------------------------------------------------------
# | system: django-vue-lyadmin
# +-------------------------------------------------------------------
# | Author: lybbn
# +-------------------------------------------------------------------
# | QQ: 1042594286
# +-------------------------------------------------------------------
# | Email: 1042594286@qq.com
# +-------------------------------------------------------------------
# | Version: 1.1
# +-------------------------------------------------------------------
# | Date: 2023-12-15
# +-------------------------------------------------------------------
# ------------------------------
# 快递100快递API接口
# ------------------------------
# 官网文档地址https://api.kuaidi100.com/document/shishichaxunchanpinjieshao
# ------------------------------
import hashlib
import json
import requests
import time
from config import KUAIDI100_KEY,KUAIDI100_CUSTOMER,KUAIDI100_SECRET
class KuaiDi100:
def __init__(self):
self.key = KUAIDI100_KEY # TODO 客户授权key
self.customer = KUAIDI100_CUSTOMER #customer
self.secret = KUAIDI100_SECRET #secret
#公共请求
def do_request(self,url="", key=None, secret=None, method="", t="", param={}):
if not key:
key = self.key
if not secret:
secret = self.secret
md = hashlib.md5()
md.update((param + t + key + secret).encode())
sign = md.hexdigest().upper()
payload = {
'key': key,
'method': method,
't': t,
'param': param,
'sign': sign
}
return requests.post(url, payload).text
def track(self, com="", num="",phone="",ship_from="",ship_to="",resultv2='0',show = '0',order = 'desc'):
"""
物流轨迹实时查询
:param com: 查询的快递公司的编码,一律用小写字母
:param num: 查询的快递单号单号的最大长度是32个字符
:param phone: 收件人或寄件人的手机号或固话(也可以填写后四位,如果是固话,请不要上传分机号)
:param ship_from: 出发地城市,省-市-区,非必填,填了有助于提升签收状态的判断的准确率,请尽量提供
:param ship_to: 目的地城市,省-市-区,非必填,填了有助于提升签收状态的判断的准确率,且到达目的地后会加大监控频率,请尽量提供
:return: requests.Response.text
https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc
"""
url = 'https://poll.kuaidi100.com/poll/query.do' # 请求地址
param = {
'com': com,
'num': num,
'phone': phone,
'from': ship_from,
'to': ship_to,
'resultv2': resultv2, # 添加此字段表示开通行政区域解析功能。0关闭默认1开通行政区域解析功能2开通行政解析功能并且返回出发、目的及当前城市信息
'show': show, # 返回数据格式。0json默认1xml2html3text
'order': order # 返回结果排序方式。desc降序默认asc升序
}
param_str = json.dumps(param) # 转json字符串
# 签名加密, 用于验证身份, 按param + key + customer 的顺序进行MD5加密注意加密后字符串要转大写 不需要“+”号
temp_sign = param_str + self.key + self.customer
md = hashlib.md5()
md.update(temp_sign.encode())
sign = md.hexdigest().upper()
request_data = {'customer': self.customer, 'param': param_str, 'sign': sign}
return requests.post(url, request_data).text # 发送请求
def eorder_create(self,printType='NON',tempId="",kuaidicom="",cargo="文件",count=1,recMan={},sendMan={},siid="",remark="",needChild=0,partnerId="",partnerKey="",partnerSecret="",partnerName="",net=""):
"""
电子面单下单接口V2
partnerId:电子面单客户账户或月结账号,需贵司向当地快递公司网点申请(参考电子面单申请指南); 是否必填该属性请查看https://api.kuaidi100.com/document/5f0ff6e82977d50a94e10237.html
partnerKey:电子面单密码,需贵司向当地快递公司网点申请; 是否必填该属性,请查看如上链接
printType:打印类型NON只下单不打印默认 IMAGE:生成图片短链HTML:生成html短链 CLOUD:使用快递100云打印机打印使用CLOUD时siid必填
tempId:主单模板通过快递100管理后台的[快递公司模板V2]信息获取
kuaidicom:快递公司的编码一律用小写字母请查看https://api.kuaidi100.com/document/5f0ff6e82977d50a94e10237.html
cargo物品名称,例:文件
needChild:是否需要子单: 1需要 0不需要(默认) 如果需要子单(指同一个订单打印出多张电子面单,即同一个订单返回多个面单号); needChild = 1、count 需要大于1如count = 2 一个主单 一个子单count = 3 一个主单 二个子单返回的子单号码见返回结果的childNum字段
count包裹总数量。该属性与子单有关如果需要子单指同一个订单打印出多张电子面单即同一个订单返回多个面单号needChild = 1、count 需要大于1如count = 2 则一个主单 一个子单count = 3则一个主单 二个子单返回的子单号码见返回结果的childNum字段
recMan:收件人信息{"name": "张三","mobile": "13888888888","printAddr": "广东深圳市南山区金蝶软件园","company": ""}
sendMan:寄件人信息{"name": "李四","mobile": "13888888888","printAddr": "广东深圳市南山区金蝶软件园","company": ""}
siid:打印设备通过打印机输出的设备码进行获取printType为CLOUD时必填
remark:备注
https://api.kuaidi100.com/document/dianzimiandanV2
"""
url = 'https://api.kuaidi100.com/label/order' # 请求地址
method = 'order'#业务类型默认order
param = {
"partnerId": partnerId,
"partnerKey": partnerKey,
"code": "",
"printType":printType,
"tempId":tempId,
"kuaidicom":kuaidicom,
"count":count,
"cargo":cargo,
"recMan":recMan,
"sendMan":sendMan,
"siid":siid,
"remark":remark
}
t = str(int(round(time.time() * 1000)))
result = self.do_request(url=url, method = method, t = t, param=json.dumps(param))
return result
#查询示例
# result = KuaiDi100().track(com = 'yuantong', num = 'YT9693083639795')
# print(result)