NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name 蓝鲸任务助手 // @namespace https://devops.ztn.cn/ // @version 0.6 // @description 任务自动开关 // @author GQZ // @match https://devops.ztn.cn/* // @icon https://www.google.com/s2/favicons?sz=64&domain=ztn.cn // @grant GM_xmlhttpRequest // @run-at document-idle // @license MIT // ==/UserScript== (function() { 'use strict'; const currentDate = new Date(); const todayDate = formatDate(currentDate); //任务日期间隔 eg:3 获取3天前到今天的任务 let day_diff = 3 //关闭任务的 eg:16 16点后开始关闭当天任务 let close_task_house = 18 const lastRun = parseInt(currentDate.getTime()/1000) //判断重复执行 if(window.localStorage.getItem('aoto_task') && lastRun - parseInt(window.localStorage.getItem('aoto_task')) < 600){ console.log('蓝鲸任务助手 今日已执行'); return ; } console.log('蓝鲸任务助手 初始化') function formatDate(date){ const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } function getPreviousDate(days) { const date = new Date(); date.setDate(date.getDate() - days); return formatDate(date); } function request(method, url, data = {}) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ url: url, method: method, headers: { }, onload: function (xhr) { resolve(JSON.parse(xhr.responseText)) }, onerror: function (err) { console.log('error', err) reject(err) } }); }) } async function get(url) { return await request('get', url) } async function post(url, data,method='POST') { return new Promise((resolve, reject) => { let dataStr = JSON.stringify(data); GM_xmlhttpRequest({ url: url, method: method, responseType: "json", data: dataStr, headers: { "Content-Type": "application/json" }, onload: function (xhr) { resolve(JSON.parse(xhr.responseText)) }, onerror: function (err) { console.log('error', err) reject(err) } }); }) } async function getUserTasks(){ let postData = [ {"name":"relation","value":["INVOLVED"]}, {"name": "state", "value": ["7587f439a61446d4aaa9447471fb12b3", "a3b074b96fec4be598f22563667741d3"]}, {"name": "estimate_end_time", "value": [getPreviousDate(day_diff), todayDate]}, {"name": "estimate_start_time", "value": [getPreviousDate(day_diff), todayDate]}, {"name":"exclude","value":[]}]; let userTask = await post('https://devops.ztn.cn/ms/vteam/api/user/issue/c8d874/table/TASK?num=1&size=100&remember=false&order_by=DESC&sort_by=estimate_end_time',postData); //console.log('userTask',userTask) return userTask; } async function getUserInfo(){ let userInfo = await get('https://devops.ztn.cn/ms/usermanager/api/user/userInfo'); //console.log('userInfo',userInfo) return userInfo; } function formatTask(taskList,userId){ let todoList = []; taskList.map((taskInfo) =>{ const {property:task} = taskInfo; if( (task.estimate_start_time.displayValue == todayDate || task.estimate_end_time.displayValue == todayDate) && task.operator_user.value == userId && task.state.displayValue !== '已完成' ){ todoList.push({ "title": task.title.displayValue,//标题 "state":task.state.displayValue,//状态 "operator_user":task.operator_user.displayValue,//经办人 "estimate_man_hour":task.estimate_man_hour.value,//预估工时 "estimate_end_time":task.estimate_end_time.value,//预计结束时间 "actual_man_hour":task.actual_man_hour.id,//实际工时更新字段ID "iteration":task.iteration.value,//迭代 "task_id":task.id.value,//更新任务状态 更新字段ID "task_act_type":task.estimate_end_time.displayValue == todayDate ? "end":"open" }) } }) return todoList; } async function changeTask(task,userId){ let res = []; res.push(await post('https://devops.ztn.cn/ms/vteam/api/user/instance_value/c8d874',{"issueId":task.task_id,"fieldId":task.actual_man_hour,"value":task.estimate_man_hour},'PUT')); if(task.state === '待处理'){ res.push(await post('https://devops.ztn.cn/ms/vteam/api/user/issue_direction/c8d874/next',{"issueId":task.task_id,"nextNodeId":"a3b074b96fec4be598f22563667741d3","comment":{"atUser":[],"comment":""},"directionFields":[{fieldId: "684f26d36cd747f68dde32104a701956", value: task.iteration }],"operators":[userId]})) } if(task.task_act_type != "end"){ return; } // 创建Date对象获取当前时间 let now = new Date(); // 使用getHours()方法获取当前小时数(24小时制,0-23) let currentHour = now.getHours(); if(currentHour>=close_task_house){ res.push(await post('https://devops.ztn.cn/ms/vteam/api/user/issue_direction/c8d874/next',{"issueId":task.task_id,"nextNodeId":"cc29264c44054c329b5ed8e52a48a112","comment":{"atUser":[],"comment":""},"directionFields":[],"operators":[userId]})) } console.log('蓝鲸任务助手 执行:',...res) } (async function () { try{ let { data:userData } = await getUserInfo(); const {data:tasks} = await getUserTasks(); const taskList = tasks.records.content; const todoList = formatTask(taskList,userData.id); if(todoList.length){ todoList.map(async (task) =>{ await changeTask(task,userData.id)}); } console.log('蓝鲸任务助手 今日任务:',todoList) window.localStorage.setItem('aoto_task', lastRun); //TODO 飞书通知 }catch (error) { console.log('蓝鲸任务助手 Error:',error) } })(); })();