From a870255ba6201bd168edd4b9ef8252c93f64ba77 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 30 Mar 2025 17:01:41 +0500 Subject: [PATCH] Get status (red, yellow, green) --- css/style.css | 6 +++--- js/index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/css/style.css b/css/style.css index 5a6d641..a578325 100644 --- a/css/style.css +++ b/css/style.css @@ -18,7 +18,7 @@ tr { cursor: pointer; } -tr::before { +tr > td:first-child::before { content: ''; display: none; position: absolute; @@ -43,9 +43,9 @@ tr::after { animation: spin 1s linear infinite; } -tr.loading::before, +tr.loading > td:first-child::before, tr.loading::after { - display: block; + display: inline; } td { diff --git a/js/index.js b/js/index.js index eaedae2..47004ed 100644 --- a/js/index.js +++ b/js/index.js @@ -6,7 +6,21 @@ const REDIRECT_URI = 'https://amocrm-web.sectorlambda.ru/' const ACCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjgwMDMyMjQ1Mzg5MjM2MGJlMTFjZjk5ZDkyNDZjMzYzYWUzNGM5OTliNDIyYTc5MWMwMmE1MTEyMTM4MjJlMWMyZWFmNGU2OWY4MGFkODM0In0.eyJhdWQiOiI4YzVkYmIzMi0xOTdkLTQ0ZmYtODdkZC1jNTJmYjhjYTUxZDYiLCJqdGkiOiI4MDAzMjI0NTM4OTIzNjBiZTExY2Y5OWQ5MjQ2YzM2M2FlMzRjOTk5YjQyMmE3OTFjMDJhNTExMjEzODIyZTFjMmVhZjRlNjlmODBhZDgzNCIsImlhdCI6MTc0MzI4MjcxOCwibmJmIjoxNzQzMjgyNzE4LCJleHAiOjE3NDMzNjkxMTgsInN1YiI6IjExOTQ5NDAyIiwiZ3JhbnRfdHlwZSI6IiIsImFjY291bnRfaWQiOjMyMzIyMTI2LCJiYXNlX2RvbWFpbiI6ImFtb2NybS5ydSIsInZlcnNpb24iOjIsInNjb3BlcyI6WyJwdXNoX25vdGlmaWNhdGlvbnMiLCJmaWxlcyIsImNybSIsImZpbGVzX2RlbGV0ZSIsIm5vdGlmaWNhdGlvbnMiXSwiaGFzaF91dWlkIjoiZDFjYzg0ODQtNzA1OC00NGQ2LWI5Y2MtMWIyZGE1YjFiMjJlIiwiYXBpX2RvbWFpbiI6ImFwaS1iLmFtb2NybS5ydSJ9.MyK_jsN611TWRZp5oQhqpJPmNs-5Iye5-X5bKycDvtfp1LFXA8lK9jjgK3nQm1yrdqpwGIPUcOqzXEDxSjwfKwwtbHAsLgJMcd6XF1UKADiNXpO1aFw9SJ1K9bGMDvO1qqiLFxqMpNe_VaYnXSdQKPSjNIg0qGIizvwifWo5EfoshoOIWBu0LBT0GulhVN42XcADwwkZnQhCOnO4gp2Sms3bc4Wf8u6BOW_EB5ZESVzf2Q1DetcxXz8Oo6NTAD8tJiEHEGpLJfFNI_2fUC_elCpr3i_Qn9XbnNfnFOi6APkTCtGOFUBUjOmJZz0RHlj9elPfK2_mJRGnAVb1TEumWg' const REFRESH_TOKEN = 'def5020054ff9f61f3a1d4908fad5fbb447c3411e943cd5b658707882fdf7e00652719cf68e195e864e025cd01eff1dc10610ccd53f0f9c8738288ac3712b15f7378af17746e7d2c4830cacf2dfb887349d009a63a8759238038fc9b10bdc9445584405798adb06c83cd2190d1be9b6d18da92bbbcdc45bf9a0b5d537879ed2dc7b17e001a49d8645be88ed1733425824489645fbe64c36f954cfacb83388181a113c3db9d960c0e3aa9553aa9fb948c575302606f0908fe937242ce683787b68435243950eb957110b94119c1b10cb73850ee2c8bf7b1f4af4038708e73adf4713dd2ab138fa091e52f18e17cc43ba9a58ef60748ff7fd1c6cbe2b91a4123cc54807685b93739c70622c34e4bf212c656d8bfdef22597845daafef73d503d0e63f93a5297f01d22226aa5a841a179730c44f25193efb03bec6e17aebeea55ed12745ab5739cd18db21d6e128f25a24c69bd4a751d2c303455e46ed3767e9dadb157c09ae9360c15a93da16e7f59b1f54c22fb96cf69e64e73f8c94c1cba41906186021d3304dd1032e1d63df32e818e2518243484457e5f142ca13068d2841e14b6ce6f6e9d4a7c538c0d0432c276e605418e7f46b6b3d43b96acebd99d0ab5da10f35f534a61b1476b80aadf113b0e2cd28ae2072c47f6c85dec1ac3c5681186f08f46320aac28de3e93f0aea4' +const TZ_OFFSET = new Date().getTimezoneOffset() / 60 + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) +const time = () => Math.ceil(Date.now() / 1000) + +const isToday = timestamp => { + const offsetSeconds = TZ_OFFSET * 3600000 // 60 * 60 * 1000 + const date = new Date(timestamp * 1000 + offsetSeconds) + const today = new Date(Date.now() + offsetSeconds) + + date.setHours(0, 0, 0, 0) + today.setHours(0, 0, 0, 0) + + return date.getTime() === today.getTime() +} const getTokens = async authCode => { try { @@ -62,6 +76,29 @@ const getContact = async id => { } } +const getLead = async (id) => { + try { + const response = await fetch(`https://${DOMAIN}/api/v4/leads/${id}?with=catalog_elements`, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${ACCESS_TOKEN}`, + 'Content-Type': 'application/json' + } + }) + + if (!response.ok) { + throw new Error(`Ошибка: ${response.status}`) + } + + const data = await response.json() + + return data + } catch (error) { + console.error('Ошибка при получении сделки:', error) + throw error + } +} + const getLeads = async (page = 1, limit = 2) => { try { const response = await fetch(`https://${DOMAIN}/api/v4/leads?page=${page}&limit=${limit}&with=contacts&order[id]=desc`, { @@ -92,7 +129,7 @@ const getLeads = async (page = 1, limit = 2) => { return leads } catch (error) { - console.error('Заявок больше нет') + console.error('Сделок больше нет') throw error } } @@ -106,6 +143,8 @@ const getAllLeads = async () => { for (const lead of leads) { console.log(lead) const row = document.getElementById('table').insertRow() + row.id = `lead-${lead.lead.id}` + row.onclick = async () => await getLeadInfo(lead.lead.id) const cell1 = row.insertCell() const cell2 = row.insertCell() @@ -131,6 +170,23 @@ const getAllLeads = async () => { } } +const getLeadInfo = async id => { + const leadDiv = document.getElementById(`lead-${id}`) + leadDiv.classList.add('loading') + + const lead = await getLead(id) + console.log(lead) + + if (lead.closest_task_at == null || lead.closest_task_at < time()) + leadDiv.style.background = '#f00' + else if (isToday(lead.closest_task_at)) + leadDiv.style.background = '#0f0' + else + leadDiv.style.background = '#ff0' + + leadDiv.classList.remove('loading') +} + (async () => { await getAllLeads()