Files
F6--/其它系统脚本/每日工作计划,当前负责人?=运营负责人.ipynb
T
2026-01-30 11:28:35 +08:00

189 lines
6.9 KiB
Plaintext

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