# 配置
API_URL = ""
TOKEN_URL = ""
X_USER_TOKEN = ""
FEISHU_WEBHOOK = ""
# 获取Token
resp = requests.get(TOKEN_URL, headers={"X-User-Token": X_USER_TOKEN})
token = resp.json()["data"].replace("Bearer ", "")
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
def main(DATA_AUTH,WH_CODE,DAY_NUM):
# 查询数据
params = {"data_auth": DATA_AUTH, "wh_code": WH_CODE, "day_num": DAY_NUM}
resp = requests.get(API_URL, headers=headers, params=params)
records = resp.json()["data"]
records.sort(key=lambda x: x.get("有效期") or x.get("expiry_date") or "")
# 构建卡片
if len(records) == 0:
content = "✅ **当前没有超期或临近超期的物料**"
else:
lines = ["| 物料 | 数量 | 有效期 |"]
for r in records:
lines.append(f"| {r.get('物料') or r.get('material') or '-'} | {r.get('数量') or r.get('quantity') or '0'} | {r.get('有效期') or r.get('expiry_date') or '-'} |")
content = "\n".join(lines)
card = {
"msg_type": "interactive",
"card": {
"config": {"wide_screen_mode": True, "enable_forward": True},
"header": {
"title": {"tag": "plain_text", "content": "📋 超期/临期物料库存预警"},
"template": "red" if len(records) > 0 else "green"
},
"elements": [
{"tag": "div", "text": {"tag": "lark_md", "content": f"**查询时间:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n**组织:** {DATA_AUTH}\n**仓库:** {WH_CODE}\n**天数阈值:** {DAY_NUM} 天\n**物料总数:** {len(records)} 条"}},
{"tag": "hr"},
{"tag": "div", "text": {"tag": "lark_md", "content": content}},
{"tag": "hr"},
{"tag": "div", "text": {"tag": "plain_text", "content": "🔔 请及时关注并处理超期物料,避免库存损失"}}
]
}
}
# 发送到飞书
requests.post(FEISHU_WEBHOOK, headers={"Content-Type": "application/json"}, json=card)
if __name__ == "__main__":
main("组织","仓库",180)
on the top