Files
test/sf_wxwork_approval/models/wx_work_api.py
2023-05-30 11:26:03 +08:00

209 lines
9.7 KiB
Python

# wxwork_api.py
import requests
class WxWorkAPI:
def __init__(self, corp_id, secret):
self.corp_id = corp_id
self.secret = secret
self.access_token = self.get_access_token()
# self.type = type
def get_access_token(self):
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={self.corp_id}&corpsecret={self.secret}"
response = requests.get(url)
result = response.json()
if 'access_token' in result:
return result['access_token']
else:
raise Exception(f"获取AccessToken失败: {result}")
def create_approval_request(self, approval_data):
url = f"https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token={self.access_token}"
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=approval_data, headers=headers)
result = response.json()
if result['errcode'] == 0:
print(result)
return result['sp_no']
else:
raise Exception(f"发起审批请求失败: {result}")
def get_approval_result(self, event_id):
url = f"https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token={self.access_token}"
headers = {'Content-Type': 'application/json'}
data = {"apply_eventid": event_id}
response = requests.post(url, json=data, headers=headers)
result = response.json()
if result['errcode'] == 0:
return result
else:
raise Exception(f"获取审批结果失败: {result}")
def upload_temp_file(self):
"""
上传临时素材
:return:
"""
url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={self.access_token}&type=file"
headers = {'Content-Type': 'application/json'}
file_path = '长沙研究院2023年月度考勤.xlsx'
media_type = 'file'
with open(file_path, 'rb') as f:
file_content = f.read()
files = {'media': (file_path, file_content)}
# response = requests.post(url=url.format(access_token=access_token, type=media_type), files=files)
response = requests.post(url=url, files=files)
result = response.json()
print('upload_result%s' % result)
return result['media_id']
template_id_dict = {"test": "ZvmJBAt7U2iUju9JLePxfkfuN22meQMVTAHymA"}
creator_userid = "MaGuangWei"
corid = 'wweaf7f1caab27a136'
secret = 'L6z-kjrUBOWfhBiLOmX_fNSY3jydDkLUNNWnemafn00'
wx = WxWorkAPI(corid, secret)
media_id = wx.upload_temp_file()
print(media_id)
# access = wx.get_access_token()
approval_datas = {
"creator_userid": creator_userid,
"template_id": template_id_dict['test'],
"use_template_approver": 0,
# "choose_department": 2,
"approver": [
{
"attr": 2,
"userid": ["MaGuangWei", "YangJinLing"]
},
{
"attr": 1,
"userid": ["MaGuangWei"]
}
],
"notifyer": ["MaGuangWei", "PengYuBo"],
# "notifyer": ["MaGuangWei"],
"notify_type": 1,
"apply_data": {
"contents": [
# 申请事项
{
"control": "Text",
"id": "Text-1640339319582",
"value": {
"text": "测试测试测试测试测试"
}
},
# 申请内容
{
"control": "Textarea",
"id": "Textarea-1640339335659",
"value": {
"text": "odoo>>>企业微信审批"
}
},
# 附件
{
"control": "File",
"id": "File-1640339381728",
"value": {
"files": [
{
"file_id": media_id
}
]
}
},
]
},
"summary_list": [
{
"summary_info": [{
"text": "测试附件上传",
"lang": "zh_CN"
}]
},
# {
# "summary_info": [{
# "text": "摘要第2行",
# "lang": "zh_CN"
# }]
# },
# {
# "summary_info": [{
# "text": "摘要第3行",
# "lang": "zh_CN"
# }]
# }
]
}
wx.create_approval_request(approval_datas)
print('创建成功')
aa = {"errcode": 0, "errmsg": "ok", "template_names": [{"text": "对接开发测试", "lang": "zh_CN"}], "template_content": {
"controls": [{"property": {"control": "Text", "id": "Text-1640339319582",
"title": [{"text": "申请事项", "lang": "zh_CN"}],
"placeholder": [{"text": "请输入申请事项", "lang": "zh_CN"}], "require": 1, "un_print": 0,
"un_replace": 0, "display": 1}},
{
"property": {"control": "Textarea", "id": "Textarea-1640339335659",
"title": [{"text": "申请内容", "lang": "zh_CN"}],
"placeholder": [{"text": "请输入申请详情描述", "lang": "zh_CN"}], "require": 1, "un_print": 0,
"un_replace": 0, "display": 1}},
{
"property": {"control": "Textarea", "id": "Textarea-1684823453770",
"title": [{"text": "多行文本", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0}},
{
"property": {"control": "Money", "id": "Money-1684823460232",
"title": [{"text": "金额", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0}},
{
"property": {"control": "Contact", "id": "Contact-1684823477216",
"title": [{"text": "部门", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0},
"config": {"contact": {"type": "multi", "mode": "department"}}},
{
"property": {"control": "Contact", "id": "Contact-1684823465317",
"title": [{"text": "成员", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0},
"config": {"contact": {"type": "multi", "mode": "user"}}},
{
"property": {"control": "Table", "id": "Table-1684823492261",
"title": [{"text": "明细", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0},
"config": {"table": {"children": [{"property": {"control": "Selector",
"id": "Selector-1684823501307",
"title": [{"text": "单选", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}],
"require": 0, "un_print": 0}, "config": {
"selector": {"type": "single", "exp_type": 0, "options": [
{"key": "option-1684823501307", "value": [{"text": "选项1", "lang": "zh_CN"}]},
{"key": "option-1684823501308", "value": [{"text": "选项2", "lang": "zh_CN"}]}],
"op_relations": [],
"external_option": {"use_external_option": False, "external_url": ""}}}}, {
"property": {"control": "Selector",
"id": "Selector-1684823504236",
"title": [{"text": "多选", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}],
"require": 0, "un_print": 0}, "config": {
"selector": {"type": "multi", "exp_type": 0, "options": [
{"key": "option-1684823504236", "value": [{"text": "选项1", "lang": "zh_CN"}]},
{"key": "option-1684823504237", "value": [{"text": "选项2", "lang": "zh_CN"}]}],
"op_relations": [],
"external_option": {"use_external_option": False, "external_url": ""}}}}],
"stat_field": [], "sum_field": [], "print_format": 0}}},
{
"property": {"control": "RelatedApproval", "id": "RelatedApproval-1684823485883",
"title": [{"text": "关联申请单", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0}},
{
"property": {"control": "File", "id": "File-1640339381728",
"title": [{"text": "附件", "lang": "zh_CN"}],
"placeholder": [{"text": "", "lang": "zh_CN"}], "require": 0, "un_print": 0,
"un_replace": 0, "display": 1}, "config": {"file": {"is_only_photo": 0}}}]}}