add new front

This commit is contained in:
Kseninia Mikhaylova 2024-11-20 15:53:24 +03:00
parent 768bea9967
commit 6cdc2d7f9a
6 changed files with 1913 additions and 45 deletions

View File

@ -11,11 +11,29 @@ from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from app.constants import * from app.constants import *
app = FastAPI() app = FastAPI()
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
origins = [
"http://localhost",
"http://localhost:3000",
"http://localhost:3001",
# "http://192.168.100.242"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
try: try:
locale.setlocale(locale.LC_TIME, "ru_RU") locale.setlocale(locale.LC_TIME, "ru_RU")
except Exception as e: except Exception as e:
@ -42,6 +60,7 @@ statuses = {
"7": "Отклонена", "7": "Отклонена",
} }
# Создаем кастомный фильтр для форматирования дат # Создаем кастомный фильтр для форматирования дат
def format_status(value): def format_status(value):
value = str(value) value = str(value)
@ -139,35 +158,9 @@ async def tg_intgr_get(request: Request):
return {"status": "error"} return {"status": "error"}
@app.post("/deal_tab") @app.post("/deal_tab/{deal_id}")
async def deal_tab( async def deal_tab(request: Request, deal_id):
request: Request,
DOMAIN: str | None = None,
PROTOCOL: str | None = None,
LANG: str | None = None,
APP_SID: str | None = None,
):
try: try:
body = await request.body()
b_str = body.decode()
result = parse_qs(b_str)
q = [DOMAIN, PROTOCOL, LANG, APP_SID]
def extract_id(params):
json_strings = params.get("PLACEMENT_OPTIONS", [])
for json_str in json_strings:
try:
data = json.loads(json_str)
if "ID" in data:
return data["ID"]
except json.JSONDecodeError:
print(f"Ошибка: Некорректная JSON-строка - {json_str}")
deal_id = extract_id(result)
if not deal_id:
raise('not deal id')
deal_id = 49
select = [ select = [
"ID", "ID",
"TITLE", "TITLE",
@ -182,8 +175,8 @@ async def deal_tab(
task_data_json = task_data.json() task_data_json = task_data.json()
result = task_data_json["result"]["tasks"] result = task_data_json["result"]["tasks"]
while len(task_data_json["result"]["tasks"]) == 50 and len(result) < 50*4: while len(task_data_json["result"]["tasks"]) == 50 and len(result) < 50 * 4:
limit = (len(result)//50) * 50 limit = (len(result) // 50) * 50
task_data = requests.get(get_task_hook + f"&start={limit}") task_data = requests.get(get_task_hook + f"&start={limit}")
task_data_json = task_data.json() task_data_json = task_data.json()
result += task_data_json["result"]["tasks"] result += task_data_json["result"]["tasks"]
@ -191,21 +184,18 @@ async def deal_tab(
# logger.info(task_data_json["result"]["tasks"]) # logger.info(task_data_json["result"]["tasks"])
parts = WEBHOOK.split("/") parts = WEBHOOK.split("/")
domain = f"https://{parts[2]}" domain = f"https://{parts[2]}"
return templates.TemplateResponse(
request=request,
name="deal_tab.html",
context={"tasks": result, "domain": domain},
)
return {"status": "success", "result": task_data_json} return {"status": "success", "result": result, "format": {"domain": domain, "statuses": statuses}}
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
return {"status": "error"} return {"status": "error"}
# app.mount("/widget", StaticFiles(directory="front/dist", html=True), name="static") # app.mount("/widget", StaticFiles(directory="front/dist", html=True), name="static")
NUXT_DIST = Path("front/dist") NUXT_DIST = Path("front/dist")
@app.post("/widget") @app.post("/widget")
async def handle_widget_post(request: Request): async def handle_widget_post(request: Request):
# Обработка данных POST-запроса # Обработка данных POST-запроса
@ -215,11 +205,25 @@ async def handle_widget_post(request: Request):
body = await request.body() body = await request.body()
b_str = body.decode() b_str = body.decode()
result = parse_qs(b_str) result = parse_qs(b_str)
def extract_id(params):
json_strings = params.get("PLACEMENT_OPTIONS", [])
for json_str in json_strings:
try:
data = json.loads(json_str)
if "ID" in data:
return data["ID"]
except json.JSONDecodeError:
print(f"Ошибка: Некорректная JSON-строка - {json_str}")
deal_id = extract_id(result)
if not deal_id:
raise ("not deal id")
deal_id = 49
logger.info(result) logger.info(result)
html_file = NUXT_DIST / "index.html" html_file = NUXT_DIST / f"deal/index.html?deal_id={deal_id}"
if html_file.exists(): if html_file.exists():
html_content = html_file.read_text() html_content = html_file.read_text()
return HTMLResponse(content=html_content, status_code=200) return HTMLResponse(content=html_content, status_code=200)
return {"error": "Nuxt.js page not found"}, 404 return {"error": "Nuxt.js page not found"}, 404

View File

@ -1,6 +1,3 @@
<template> <template>
<div> <NuxtPage/>
<NuxtRouteAnnouncer />
<NuxtWelcome />
</div>
</template> </template>

View File

@ -2,9 +2,17 @@
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-04-03', compatibilityDate: '2024-04-03',
devtools: { enabled: true }, devtools: { enabled: true },
app: { app: {
baseURL: '/widget/', // Базовый путь baseURL: '/widget/', // Базовый путь
// buildAssetsDir: '/widget/_nuxt/', // Путь для статических файлов // buildAssetsDir: '/widget/_nuxt/', // Путь для статических файлов
}, },
}) runtimeConfig: {
public: {
apiBase: process.env.NODE_ENV == 'development' ? 'http://localhost:8000' : ''
}
},
modules: ['@nuxt/ui'],
})

1754
front/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
"postinstall": "nuxt prepare" "postinstall": "nuxt prepare"
}, },
"dependencies": { "dependencies": {
"@nuxt/ui": "^2.19.2",
"nuxt": "^3.14.1592", "nuxt": "^3.14.1592",
"vue": "latest", "vue": "latest",
"vue-router": "latest" "vue-router": "latest"

104
front/pages/deal.vue Normal file
View File

@ -0,0 +1,104 @@
<script setup lang="ts">
const route = useRoute()
const config = useRuntimeConfig()
const data = ref()
const formatter = ref()
export interface Data {
status: string;
result: Result[];
format: {
domain: string;
statuses: { [key: string]: string };
}
}
export interface Result {
id: string;
title: string;
responsibleId: string;
createdDate: Date;
deadline: Date;
status: string;
groupId: string;
group: any[] | GroupClass;
responsible: string;
subStatus: string;
}
export interface GroupClass {
id: string;
name: string;
opened: boolean;
membersCount: number;
image: string;
additionalData: any[];
}
const loadData = async (deal_id: number) => {
const res = await $fetch<Data>(`${config.public.apiBase}/deal_tab/${deal_id}`, { method: 'POST' })
if (res.status == 'success') {
data.value = res.result
formatter.value = res.format
}
}
const columns = [{
key: 'id',
label: 'ID'
}, {
key: 'title',
label: 'Название'
}, {
key: 'status',
label: 'Статус'
}, {
key: 'responsible',
label: 'Исполнитель'
}, {
key: 'createdDate',
label: 'Создана'
}, {
key: 'deadline',
label: 'Крайний срок',
}]
onMounted(() => {
const deal_id = route.query.deal_id
if (!deal_id) return
loadData(parseInt(deal_id as string))
})
</script>
<template>
<UTable :rows="data" :columns="columns">
<template #responsible-data="{ row }">
{{ row.responsible.name }}
</template>
<template #deadline-data="{ row }">
{{ new Date(Date.parse(row.deadline)).toLocaleDateString('ru-RU', {
year: 'numeric',
month: 'long',
day: 'numeric',
}) }}
</template>
<template #createdDate-data="{ row }">
{{ new Date(Date.parse(row.createdDate)).toLocaleDateString('ru-RU', {
year: 'numeric',
month: 'long',
day: 'numeric',
}) }}
</template>
<template #status-data="{ row }">
{{ formatter.statuses[row.status] }}
</template>
<template #title-data="{ row }">
<a :href="`${formatter.domain}/workgroups/group/${row.groupId}/tasks/task/view/${row.id}/`" target="_blank" class="text-primary-600 underline hover:no-underline">
{{ row.title }}
</a>
</template>
</UTable>
</template>