Files
F6--/其它系统脚本/yida_Meirigongzuojihua_fuzeren_tongbu.py
2026-01-30 11:28:35 +08:00

145 lines
4.8 KiB
Python

import pandas as pd
import numpy as np
import requests
import json
import time
import re
from datetime import datetime
from dateutil.relativedelta import relativedelta
from pathlib import Path
from urllib.parse import quote
from io import BytesIO
ROOT = Path('.').absolute() # 当前工作目录
# 生成 token,参数不需要修改
def generateToken() -> str:
""" 生成 token """
token_api = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
# 该信息在钉钉开放应用中
data = {
"appKey": "ding5kqocon5s9oph5uq",
"appSecret": 'HL1jgsIIfLAC0eTH0A1m4mwxUDqbgsiPeCCGGE3ocM6qJBTIW7Ivt9drxF_Z4Kb_'
}
res = requests.post(token_api, json=data)
TOKEN = res.json()['accessToken']
return TOKEN
def getOperationRecords(TOKEN,ID):
""" 函数功能:获取流程表单的审批记录 --F6客户服务 应用 """
appType = "APP_UYZ0KG6L0CCNV80GZ66O"
systemToken = "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2"
userId = "yida_pub_account"
api = f'https://api.dingtalk.com/v1.0/yida/processes/operationRecords?appType={appType}&systemToken={systemToken}&userId={userId}&language=zh_CN&processInstanceId={ID}'
headers = {
"Content-Type": "application/json",
"x-acs-dingtalk-access-token": TOKEN
}
res = requests.get(api, headers=headers)
res_new = res.json()
records_new = res_new.get('result')
records_new = [item for item in records_new if item.get('type') == "TODO"]
# showName_new = records_new[0].get('showName')
# taskId = records_new[0].get('taskId')
operatorUserId = records_new[0]['operatorUserId']
return operatorUserId
def read_instances_new(TOKEN, formUuid, page, n):
""" 函数功能:读取流程表单的所有数据 """
api = f'https://api.dingtalk.com//v1.0/yida/processes/instances?pageNumber={page}&pageSize={n}'
headers = {
"Content-Type": "application/json",
"x-acs-dingtalk-access-token": TOKEN
}
formData = {
"appType": "APP_UYZ0KG6L0CCNV80GZ66O",
"systemToken": "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
"userId": "yida_pub_account", # 超级管理员账号
"language": "zh_CN",
"formUuid": formUuid,
# "searchFieldJson": json.dumps(searchField), # 如果增加上这一项会要求升级宜搭存储
"instanceStatus": "RUNNING"
}
res = requests.post(api, headers=headers, json=formData)
return res.json()
def instances(TOKEN,data_new,formInstanceId):
api = f'https://api.dingtalk.com//v1.0/yida/forms/instances'
headers = {
"Content-Type": "application/json",
"x-acs-dingtalk-access-token": TOKEN
}
payload = {
"appType" : "APP_UYZ0KG6L0CCNV80GZ66O",
"systemToken" : "XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2",
"userId" : "2268275546837446",
"language" : "zh_CN",
"formInstanceId" : formInstanceId,
"useLatestVersion" : 'false',
"updateFormDataJson" : json.dumps(data_new, cls=NpEncoder) #json.dumps(data_new, cls=NpEncoder)
}
res = requests.put(api, headers=headers,json =payload)
res_new = res.json()
return res_new
# print(i,res_new)
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
""" 处理流程开始 """
# 每日工作计划
FORMID = "FORM-4V966N81OMEEH85QDP8XW4AH1AIZ2GKRLZTML21"
TOKEN = generateToken()
json_data = {}
json_data_new = []
read_data = read_instances_new(TOKEN, FORMID, 1, 100)
PAGES = read_data.get('totalCount')//100 + 1
# print(read_instances_new,read_data.get('totalCount'))
for a in range(1,PAGES + 1):
read_data = read_instances_new(TOKEN, FORMID, a, 100)
for i in range(0,len(read_data["data"])):
json_data[read_data["data"][i]['processInstanceId']]=read_data["data"][i]['data']['employeeField_lmeqk429_id'][0] # id 对于 运营负责人
for key, value in json_data.items():
operatorUserId = getOperationRecords(TOKEN,key)
if operatorUserId != value:
# print(f"Key: {key}, Value: {value},operatorUserId:{operatorUserId}")
data_one = {
'employeeField_lmrga6dh' : operatorUserId , # 自主派发 运营顾问
}
data_two = {
'employeeField_lnsi0v6w' : operatorUserId # 自动派发 运营顾问
}
data_new= {
'employeeField_lmeqk429' : operatorUserId , # 运营顾问
'tableField_lmelabau' : [data_one],
'tableField_lnsi0v71': [data_two]
}
res_one = instances(TOKEN,data_new,key)
print(key,res_one)