perf: 修改获取图片主色调指令逻辑

This commit is contained in:
rd
2025-08-04 13:55:46 +08:00
parent abea67be9c
commit 759c8cfbdc
4 changed files with 46 additions and 20 deletions

View File

@ -1,24 +1,49 @@
import { getImageMainColor } from '@/utils/tools'; import { getImageMainColor } from '@/utils/tools';
// 创建图片主色调指令 const DEFAULT_COLOR = '#E6E6E8';
const pendingRequests = new Map<HTMLElement, Promise<string | void>>();
const imageMainColorDirective = { const imageMainColorDirective = {
mounted(el: HTMLElement, binding: any) { mounted(el: HTMLElement, binding: any) {
const imageUrl = binding.value; updateColor(el, binding.value);
},
if (!imageUrl) return; updated(el: HTMLElement, binding: any) {
if (binding.value !== binding.oldValue) {
updateColor(el, binding.value);
}
},
getImageMainColor(imageUrl) beforeUnmount(el: HTMLElement) {
.then(color => { pendingRequests.delete(el);
el.style.backgroundColor = color;
})
.catch(error => {
console.error('获取图片主色调失败:', error);
// 设置默认背景色
el.style.backgroundColor = '#E6E6E8';
});
} }
}; };
function updateColor(el: HTMLElement, imageUrl: string) {
if (!imageUrl) {
el.style.backgroundColor = DEFAULT_COLOR;
return;
}
// 创建新的请求
const request = getImageMainColor(imageUrl)
.then(color => {
if (pendingRequests.get(el) === request) {
el.style.backgroundColor = color;
pendingRequests.delete(el);
}
return color;
})
.catch(error => {
console.error('获取图片主色调失败:', error);
el.style.backgroundColor = DEFAULT_COLOR;
pendingRequests.delete(el);
});
pendingRequests.set(el, request);
}
// 获取图片主色调指令
export default { export default {
install(app: any) { install(app: any) {
app.directive('image-main-color', imageMainColorDirective); app.directive('image-main-color', imageMainColorDirective);

View File

@ -6,9 +6,10 @@
</div> </div>
<div class="workflow-container"> <div class="workflow-container">
<div class="left-wap mr-24px" v-if="isCollapsed == false"> <div class="left-wap mr-24px" v-if="isCollapsed == false">
<div class="w-full w-100% mb-15px h-160px rounded-8px bg-#E6E6E8" v-image-main-color="cozeInfo.image_url"> <div class="w-full w-100% mb-15px h-160px rounded-8px" v-image-main-color="cozeInfo.image_url">
<img v-if="cozeInfo?.image_url" :src="cozeInfo?.image_url" class="w-full h-full object-contain" /> <img v-if="cozeInfo?.image_url" :src="cozeInfo?.image_url" class="w-full h-full object-contain" />
</div> </div>
<div class="content mb-15px"> <div class="content mb-15px">
<div class="title-body"> <div class="title-body">
<div class="text mr-4px">{{ cozeInfo?.name }}</div> <div class="text mr-4px">{{ cozeInfo?.name }}</div>
@ -104,10 +105,10 @@ const query = reactive({
id: id, id: id,
}); });
const cozeInfo = reactive({ const cozeInfo = ref({
title: '', title: '',
description: '', description: '',
icon_url: '', image_url: '',
bot_id: '', bot_id: '',
auth: {}, auth: {},
}); });
@ -119,7 +120,7 @@ const initChat = async () => {
return false; return false;
} }
Object.assign(cozeInfo, data.info); Object.assign(cozeInfo.value, data.info);
await loadScript('https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.17/libs/cn/index.js'); await loadScript('https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.17/libs/cn/index.js');
let cozeConfig = await cozeWebSdkConfig(data.info.bot_id, data.info.name, data.info.auth, data.info.user_info); let cozeConfig = await cozeWebSdkConfig(data.info.bot_id, data.info.name, data.info.auth, data.info.user_info);
cozeWebSDK = cozeWebSDK = new CozeWebSDK.WebChatClient(cozeConfig); cozeWebSDK = cozeWebSDK = new CozeWebSDK.WebChatClient(cozeConfig);

View File

@ -23,7 +23,7 @@
:xxl="4" :xxl="4"
v-for="(product, k) in item.agent_products" :key="k"> v-for="(product, k) in item.agent_products" :key="k">
<div class="card-container cursor-pointer !h-252px" @click="goDetail(product?.type, product?.id)"> <div class="card-container cursor-pointer !h-252px" @click="goDetail(product?.type, product?.id)">
<div class="card-image h-120px w-100% bg-cover bg-center bg-#E6E6E8 mb-8px" v-image-main-color="product.image_url"> <div class="card-image h-120px w-100% bg-cover bg-center mb-8px" v-image-main-color="product.image_url">
<img class="object-contain h-full w-100% " :src="product?.image_url"/> <img class="object-contain h-full w-100% " :src="product?.image_url"/>
</div> </div>

View File

@ -6,7 +6,7 @@
</div> </div>
<div class="workflow-container flex-1"> <div class="workflow-container flex-1">
<div class="left-wap mr-24px" v-if="isCollapsed == false"> <div class="left-wap mr-24px" v-if="isCollapsed == false">
<div class="w-full w-100% mb-15px h-160px rounded-8px bg-#E6E6E8" v-image-main-color="cozeInfo.image_url"> <div class="w-full w-100% mb-15px h-160px rounded-8px" v-image-main-color="cozeInfo.image_url">
<img v-if="cozeInfo?.image_url" :src="cozeInfo?.image_url" class="w-full h-full object-contain" /> <img v-if="cozeInfo?.image_url" :src="cozeInfo?.image_url" class="w-full h-full object-contain" />
</div> </div>
<div class="content mb-15px"> <div class="content mb-15px">