{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import time\n", "import json\n", "from datetime import datetime\n", "from dateutil.relativedelta import relativedelta\n", "import calendar\n", "from datetime import timedelta\n", "import requests\n", "\n", "\n", "def generateToken() -> str:\n", " \"\"\" \n", " 生成 token,参数不需要修改\n", " \"\"\"\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", "\n", "def get_time_range(n=2):\n", " \"\"\" 获取近n个月的时间戳(单位是毫秒) 默认取最近2个月\"\"\"\n", "\n", " def delay_time(time_str, years=0, months=0, days=0, hours=0, minutes=0, seconds=0):\n", " if type(time_str) == str:\n", " time_str = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')\n", " ret = time_str + relativedelta(years=years, months=months, days=days, hours=hours, minutes=minutes, seconds=seconds)\n", " return ret\n", "\n", " # 获得当前时间\n", " now_time = datetime.now()\n", " endTime = int(time.mktime(time.strptime(now_time.date().strftime('%Y/%m/%d')+' 00:00:00', '%Y/%m/%d %H:%M:%S'))) * 1000\n", " # 2个月前 的时间\n", " ret2 = delay_time(now_time, months=-n)\n", " startTime = int(time.mktime(time.strptime(ret2.date().strftime('%Y/%m/%d')+' 00:00:00', '%Y/%m/%d %H:%M:%S'))) * 1000\n", "\n", " # print(f'时间区间:[{startTime}-{endTime}]')\n", " return startTime, endTime\n", "\n", "\n", "def get_dateRange_this_month():\n", " \"\"\" 函数功能:获取当前月份的起始日期范围时间戳(单位毫秒)\"\"\"\n", " now = datetime.now()\n", " this_month_start = datetime(now.year, now.month, 1)\n", " this_month_end = datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1])\n", " this_month_start, this_month_end = int(time.mktime(this_month_start.timetuple())), int(time.mktime(this_month_end.timetuple()))\n", "\n", " return this_month_start*1000, this_month_end*1000\n", "\n", "\n", "def read_processes_instances(token, formUuid, page=1, n=100, searchField={}):\n", " \"\"\" 函数功能:读取流程表单的所有数据 \n", " 获取表单的数据实例数据,返回表单的所有实例 -- 应用:F6客户服务\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", " 'currentPage': page,\n", " 'pageSize': n,\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", " }\n", "\n", " res = requests.post(api, headers=headers, json=formData)\n", " return res.json()\n", "\n", "\n", "def read_form_instances(token, formUuid, page=1, n=100, searchField={},createFromTimeGMT='',createToTimeGMT=''):\n", " \"\"\" 函数功能:读取普通表单的所有数据 -- 应用:F6客户服务 \"\"\"\n", "\n", " api = f'https://api.dingtalk.com/v1.0/yida/forms/instances/search'\n", "\n", " headers = {\n", " \"Content-Type\": \"application/json\",\n", " \"x-acs-dingtalk-access-token\": token\n", " }\n", "\n", " formData = {\n", " 'currentPage': page,\n", " 'pageSize': n,\n", " \"appType\": \"APP_UYZ0KG6L0CCNV80GZ66O\",\n", " \"systemToken\": \"XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2\",\n", " \"userId\": \"yida_pub_account\",\n", " \"language\": \"zh_CN\",\n", " \"formUuid\": formUuid,\n", " 'createFromTimeGMT':createFromTimeGMT,\n", " 'createToTimeGMT':createToTimeGMT,\n", " \"searchFieldJson\": json.dumps(searchField)\n", " }\n", "\n", " res = requests.post(api, headers=headers, json=formData)\n", " return res.json()\n", "\n", "\n", "def create_form_instances(token, formUuid, formData={}):\n", " \"\"\" 函数功能:创建普通表单实例 -- 应用:F6客户服务 \"\"\"\n", "\n", " api = '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\": \"yida_pub_account\",\n", " \"language\": \"zh_CN\",\n", " \"formUuid\": formUuid,\n", " \"formDataJson\": json.dumps(formData)\n", " }\n", "\n", " res = requests.post(api, headers=headers, json=payload)\n", " return res\n", "\n", "\n", "def update_form_instances(token, formInstId, formData={}):\n", " \"\"\" 函数功能:更新普通表单实例 -- 应用:F6客户服务 \"\"\"\n", "\n", " api = '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\": \"yida_pub_account\",\n", " \"language\": \"zh_CN\",\n", " \"formInstanceId\": formInstId,\n", " \"updateFormDataJson\": json.dumps(formData)\n", " }\n", "\n", " res = requests.put(api, headers=headers, json=payload)\n", " return res\n", "def get_approval_records(token: str, processInstanceId: str):\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={processInstanceId}'\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", " # print('获取流程表单的审批记录')\n", " return res.json()\n", "\n", "def get_approval_records_Batch(token, processInstanceId):\n", " \"\"\" 函数功能:批量获取流程表单的审批记录 --F6客户服务 应用 \"\"\"\n", " appType = \"APP_UYZ0KG6L0CCNV80GZ66O\"\n", " systemToken = \"XA966F81JAJOFCVVVKO64E9MIIZV1EWE5SFMKJ2\"\n", " userId = \"yida_pub_account\"\n", " language = \"zh_CN\"\n", "\n", " api = f'https://api.dingtalk.com/v1.0/yida/processes/instances/searchWithIds?appType={appType}&systemToken={systemToken}&userId={userId}&language={language}&processInstanceIds={processInstanceId}'\n", " print(api)\n", " headers = {\n", " # \"Content-Type\": \"application/json\",\n", " \"x-acs-dingtalk-access-token\": token\n", " }\n", "\n", " res = requests.get(api, headers=headers)\n", " return res.json()\n", "\n", "def processes_instances(token, formUuid):\n", " \"\"\" 函数功能:获取流程实例 -- 应用:F6客户服务 \"\"\"\n", "\n", " api = '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\": \"yida_pub_account\",\n", " \"language\": \"zh_CN\",\n", " \"formUuid\": formUuid\n", " }\n", "\n", " res = requests.post(api, headers=headers, json=payload)\n", " return res\n", "\n", "def timeStamp(timeNum):\n", " \"\"\" 函数功能:将时间戳(毫秒) 转化为时间日期格式\"\"\"\n", " timeStamp = float(timeNum/1000)\n", " timeArray = time.localtime(timeStamp)\n", " otherStyleTime = time.strftime(\"%Y-%m-%d %H:%M:%S\", timeArray)\n", " return otherStyleTime\n", "\n", "\n", "TOKEN = generateToken()\n", "# FormDatas = []\n", "# data_new = pd.read_excel(r'C:\\Users\\admin\\Downloads\\[流程]续约服务流程_20240710114231.xlsx',sheet_name='Sheet1')\n", "# for formInstanceId in data_new.values:\n", "# # 获取当前所处节点\n", "# res_new = get_approval_records(token=TOKEN, processInstanceId=formInstanceId[0])\n", "# records_new = res_new.get('result')\n", "# for a in range(0,len(records_new)):\n", "# try:\n", "# df_data = df_data._append({'showName': records_new[a]['showName'], 'operatorName': records_new[a]['operatorName'], 'action': records_new[a]['action'], 'operateTimeGMT': records_new[a]['operateTimeGMT'], 'processInstanceId': formInstanceId[0]}, ignore_index=True)\n", "# FormDatas.append(res['data'][v]['formInstanceId'])\n", "# except:\n", "# pass\n", "# # 创建DataFrame对象\n", "# df = pd.DataFrame({'ID': FormDatas})\n", "# # 将DataFrame对象写入Excel文件中\n", "# df.to_excel(r'C:\\Users\\admin\\Desktop\\data.xlsx', index=False)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "api = 'https://api.dingtalk.com/v1.0/yida/forms/instances?pageNumber=0&pageSize=10'\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\": \"yida_pub_account\",\n", " \"language\": \"zh_CN\",\n", " \"formUuid\": \"FORM-PE866MD1MJMU0WGLYRFLYEN5YN9L1I55Z7ZUK22\",\n", " 'formDataJson':'b06d50d1-ea8a-48c1-8442-4badd7ed5964'\n", "}\n", "\n", "res = requests.post(api, headers=headers, json=payload)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'code': 'MissingformDataJson',\n", " 'requestid': '5186DE56-50D5-7949-9395-64B3CCC0C8B2',\n", " 'message': 'formDataJson is mandatory for this action.'}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res.json()" ] } ], "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 }