361 lines
7.8 KiB
Vue
361 lines
7.8 KiB
Vue
<script lang="ts" setup>
|
||
import { useRoute } from 'vue-router';
|
||
import { recordRecentTool } from '@/composable/recent-tools';
|
||
import { useHead } from '@vueuse/head';
|
||
import type { HeadObject } from '@vueuse/head';
|
||
|
||
import BaseLayout from './base.layout.vue';
|
||
import FavoriteButton from '@/components/FavoriteButton.vue';
|
||
import LicenseGate from '@/tools/license/LicenseGate.vue';
|
||
import type { Tool } from '@/tools/tools.types';
|
||
|
||
const route = useRoute();
|
||
|
||
// record into "recent tools" (shown on home page)
|
||
watch(
|
||
() => route.path,
|
||
(path) => {
|
||
const name = String(route.meta.name ?? '');
|
||
if (path && name) {
|
||
recordRecentTool(path, name);
|
||
}
|
||
},
|
||
{ immediate: true },
|
||
);
|
||
|
||
const head = computed<HeadObject>(() => ({
|
||
title: `${route.meta.name} - IT-TOOLS-PRO`,
|
||
meta: [
|
||
{
|
||
name: 'description',
|
||
content: route.meta?.description as string,
|
||
},
|
||
{
|
||
name: 'keywords',
|
||
content: ((route.meta.keywords ?? []) as string[]).join(','),
|
||
},
|
||
],
|
||
}));
|
||
useHead(head);
|
||
const { t } = useI18n();
|
||
|
||
// 宽屏工具:不受 600px 限宽,占满可用宽度(画布/多栏类工具)
|
||
const WIDE_TOOLS = new Set(['/label-designer', '/json-prettify', '/pdf-tools', '/image-tools', '/doc-convert', '/video-tools', '/seal-maker', '/signature-pad', '/ai-image-lab', '/ai-chat', '/copywriting', '/ai-gen', '/code-reader', '/my-ip']);
|
||
const isWide = computed(() => WIDE_TOOLS.has(route.path));
|
||
|
||
const i18nKey = computed<string>(() => route.path.trim().replace('/', ''));
|
||
const toolTitle = computed<string>(() => t(`tools.${i18nKey.value}.title`, String(route.meta.name)));
|
||
const toolDescription = computed<string>(() => t(`tools.${i18nKey.value}.description`, String(route.meta.description)));
|
||
|
||
// ===== 「问AI」按钮:弹窗展示当前工具的用法与特色 =====
|
||
import MarkdownIt from 'markdown-it';
|
||
const mdRenderer = new MarkdownIt({ html: false, linkify: true, breaks: true });
|
||
|
||
const showAiHelp = ref(false);
|
||
const aiHelpCache = ref<Record<string, string>>({}); // 按 path 缓存,切换回来不重复请求
|
||
const aiHelpLoading = ref(false);
|
||
const aiHelpErr = ref('');
|
||
|
||
const aiHelpHtml = computed(() => {
|
||
const src = aiHelpCache.value[route.path] || '';
|
||
return src ? mdRenderer.render(src) : '';
|
||
});
|
||
|
||
async function askAiHelp() {
|
||
showAiHelp.value = true;
|
||
if (aiHelpCache.value[route.path] || aiHelpLoading.value) return;
|
||
aiHelpLoading.value = true;
|
||
aiHelpErr.value = '';
|
||
try {
|
||
const res = await fetch('/relay/ai/tool-help', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
name: toolTitle.value,
|
||
description: toolDescription.value,
|
||
path: route.path,
|
||
}),
|
||
});
|
||
const j = await res.json().catch(() => ({}));
|
||
if (!res.ok || j.code !== 200) throw new Error(j.message || `HTTP ${res.status}`);
|
||
aiHelpCache.value = { ...aiHelpCache.value, [route.path]: String(j.data?.reply || '') };
|
||
}
|
||
catch (e: any) {
|
||
aiHelpErr.value = String(e?.message || e);
|
||
}
|
||
finally {
|
||
aiHelpLoading.value = false;
|
||
}
|
||
}
|
||
|
||
function retryAiHelp() {
|
||
const next = { ...aiHelpCache.value };
|
||
delete next[route.path];
|
||
aiHelpCache.value = next;
|
||
aiHelpErr.value = '';
|
||
askAiHelp();
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<BaseLayout fixed-footer>
|
||
<div class="tool-layout" :class="{ wide: isWide }">
|
||
<div class="tool-header">
|
||
<div flex flex-nowrap items-center gap-12px>
|
||
<button class="ask-ai-btn" title="问 AI:本工具的用法与特色" @click="askAiHelp">
|
||
🤖 问AI
|
||
</button>
|
||
<n-h1 m-0>
|
||
{{ toolTitle }}
|
||
</n-h1>
|
||
|
||
<FavoriteButton :tool="{ name: route.meta.name, path: route.path } as Tool" />
|
||
</div>
|
||
|
||
<div class="separator" />
|
||
|
||
<div class="description">
|
||
{{ toolDescription }}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 问AI 弹窗:当前工具的用法与特色 -->
|
||
<n-modal
|
||
v-model:show="showAiHelp"
|
||
preset="card"
|
||
class="ai-help-modal"
|
||
:title="`🤖 问AI · ${toolTitle}`"
|
||
:style="{ width: 'min(680px, 94vw)' }"
|
||
>
|
||
<div class="ai-help-box">
|
||
<div v-if="aiHelpLoading" class="ai-help-state">
|
||
<span class="ai-help-dots"><i /><i /><i /></span>
|
||
AI 正在整理这个工具的用法…
|
||
</div>
|
||
<div v-else-if="aiHelpErr" class="ai-help-state err">
|
||
⚠️ {{ aiHelpErr }}
|
||
<button class="ai-help-retry" @click="retryAiHelp">重试</button>
|
||
</div>
|
||
<div v-else-if="aiHelpHtml" class="ai-help-body" v-html="aiHelpHtml" />
|
||
<div v-else class="ai-help-state">暂无内容</div>
|
||
</div>
|
||
</n-modal>
|
||
|
||
<div class="tool-content" :class="{ wide: isWide }">
|
||
<slot />
|
||
</div>
|
||
|
||
<!-- 授权门:受控功能显示试用倒计时,到期锁定 -->
|
||
<LicenseGate :path="route.path" />
|
||
</BaseLayout>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.tool-content {
|
||
display: flex;
|
||
flex-direction: row;
|
||
justify-content: center;
|
||
align-items: flex-start;
|
||
flex-wrap: wrap;
|
||
gap: 16px;
|
||
|
||
::v-deep(& > *) {
|
||
flex: 0 1 600px;
|
||
}
|
||
|
||
&.wide {
|
||
::v-deep(& > *) {
|
||
flex: 1 1 100%;
|
||
}
|
||
}
|
||
}
|
||
|
||
.tool-layout {
|
||
max-width: 600px;
|
||
margin: 0 auto;
|
||
box-sizing: border-box;
|
||
|
||
&.wide {
|
||
max-width: none;
|
||
width: 100%;
|
||
padding: 0 20px;
|
||
|
||
.tool-header {
|
||
max-width: 1280px;
|
||
margin: 0 auto;
|
||
padding: 10px 0 6px;
|
||
|
||
.n-h1 {
|
||
font-size: 24px;
|
||
}
|
||
|
||
.description {
|
||
font-size: 12.5px;
|
||
margin-top: 2px;
|
||
}
|
||
}
|
||
}
|
||
|
||
.tool-header {
|
||
padding: 40px 0;
|
||
width: 100%;
|
||
|
||
.n-h1 {
|
||
opacity: 0.9;
|
||
font-size: 40px;
|
||
font-weight: 400;
|
||
margin: 0;
|
||
line-height: 1;
|
||
}
|
||
|
||
.separator {
|
||
width: 200px;
|
||
height: 2px;
|
||
background: rgb(161, 161, 161);
|
||
opacity: 0.2;
|
||
|
||
margin: 10px 0;
|
||
}
|
||
|
||
.description {
|
||
margin: 0;
|
||
|
||
opacity: 0.7;
|
||
}
|
||
}
|
||
}
|
||
</style>
|
||
|
||
<style lang="less" scoped>
|
||
// 「问AI」按钮 —— 工具页左上角
|
||
.ask-ai-btn {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 5px;
|
||
padding: 6px 14px;
|
||
border: 1px solid rgba(24, 160, 88, 0.45);
|
||
border-radius: 999px;
|
||
background: rgba(24, 160, 88, 0.1);
|
||
color: #18a058;
|
||
font-size: 14.5px;
|
||
font-weight: 600;
|
||
flex-shrink: 0;
|
||
cursor: pointer;
|
||
transition: all 0.15s;
|
||
|
||
&:hover {
|
||
background: #18a058;
|
||
color: #fff;
|
||
transform: translateY(-1px);
|
||
box-shadow: 0 4px 12px rgba(24, 160, 88, 0.3);
|
||
}
|
||
}
|
||
|
||
// 问AI 弹窗
|
||
.ai-help-box {
|
||
min-height: 170px;
|
||
}
|
||
|
||
.ai-help-state {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
padding: 26px 4px;
|
||
font-size: 15px;
|
||
opacity: 0.75;
|
||
|
||
&.err {
|
||
color: #d03050;
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
.ai-help-dots {
|
||
display: inline-flex;
|
||
gap: 5px;
|
||
|
||
i {
|
||
width: 8px;
|
||
height: 8px;
|
||
border-radius: 50%;
|
||
background: #18a058;
|
||
animation: aihelp-blink 1.2s infinite ease-in-out;
|
||
|
||
&:nth-child(2) {
|
||
animation-delay: 0.2s;
|
||
}
|
||
|
||
&:nth-child(3) {
|
||
animation-delay: 0.4s;
|
||
}
|
||
}
|
||
}
|
||
|
||
@keyframes aihelp-blink {
|
||
0%,
|
||
80%,
|
||
100% {
|
||
opacity: 0.25;
|
||
transform: scale(0.8);
|
||
}
|
||
|
||
40% {
|
||
opacity: 1;
|
||
transform: scale(1);
|
||
}
|
||
}
|
||
|
||
.ai-help-retry {
|
||
margin-left: 4px;
|
||
padding: 4px 12px;
|
||
border: none;
|
||
border-radius: 6px;
|
||
background: #d03050;
|
||
color: #fff;
|
||
font-size: 13.5px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.ai-help-body {
|
||
font-size: 15px;
|
||
line-height: 1.75;
|
||
color: rgba(0, 0, 0, 0.85);
|
||
|
||
:deep(h3) {
|
||
margin: 14px 0 8px;
|
||
font-size: 16.5px;
|
||
color: #18a058;
|
||
|
||
&:first-child {
|
||
margin-top: 0;
|
||
}
|
||
}
|
||
|
||
:deep(ol),
|
||
:deep(ul) {
|
||
margin: 0 0 10px;
|
||
padding-left: 22px;
|
||
}
|
||
|
||
:deep(li) {
|
||
margin: 5px 0;
|
||
}
|
||
|
||
:deep(p) {
|
||
margin: 8px 0;
|
||
}
|
||
|
||
:deep(code) {
|
||
padding: 1px 6px;
|
||
border-radius: 4px;
|
||
background: rgba(0, 0, 0, 0.06);
|
||
font-size: 14px;
|
||
}
|
||
|
||
:deep(strong) {
|
||
color: #18a058;
|
||
}
|
||
}
|
||
</style>
|