Files
aida_front/src/views/SellerDashboard/BrandProfile/image-clip.vue

384 lines
9.1 KiB
Vue
Raw Normal View History

2026-04-13 14:35:12 +08:00
<template>
2026-04-21 16:13:55 +08:00
<div class="image-clip" :class="{ 'is-product': isProduct }" :data-crop-type="type || ''">
<div class="image-clip-body" :class="{ 'is-cover': type === 'cover' }" ref="imageClipBody">
2026-04-13 14:35:12 +08:00
<VueCropper
ref="cropper"
:img="url"
crossOrigin="Anonymous"
:autoCrop="true"
:fixedNumber="ratio"
2026-04-27 16:58:45 +08:00
:fixed="isProduct ? type !== 'apparel' : true"
2026-04-13 14:35:12 +08:00
movable
@realTime="onChange"
2026-04-17 17:59:51 +08:00
outputType="png"
2026-04-13 14:35:12 +08:00
></VueCropper>
</div>
<div class="clip_opterate">
<div class="item" @click="rotateLeft()">
<span class="icon iconfont icon-chexiao operate_icon"></span>
</div>
<div class="item" @click="rotateRight()">
<span class="icon iconfont icon-chexiao operate_icon icon_chexiao_sec"></span>
</div>
<div class="item" @click="changeScale(-0.1)">
<span class="operate_icon icon_font">-</span>
</div>
<div class="item" @click="changeScale(0.1)">
<span class="operate_icon icon_font">+</span>
</div>
<div class="item" @click="refreshCrop()">
<span class="icon iconfont icon-shuaxin operate_icon"></span>
</div>
</div>
</div>
</template>
<script setup>
2026-04-27 11:35:09 +08:00
import { ref, useAttrs, onMounted, onBeforeUnmount, computed, nextTick, watch } from "vue"
import "vue-cropper/dist/index.css"
import { VueCropper } from "vue-cropper"
const props = defineProps({
url: {
type: String,
default: ""
},
ratio: {
type: Array,
default: () => [1, 1]
},
isProduct: {
type: Boolean,
default: false
},
fixedBox: {
type: Boolean,
default: true
},
type: {
type: String,
default: () => ""
}
})
const attrs = useAttrs()
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
const autoCropHeight = computed(() => {
let height = 426
if (props.type === "cover") height = 375
else if (props.type === "apparel") height = 320
return height
})
2026-04-22 10:20:49 +08:00
2026-04-27 11:35:09 +08:00
const onChange = (data) => {
if (attrs.onChange) {
getCropUrl().then((url) => attrs.onChange(url))
}
2026-04-13 14:35:12 +08:00
}
2026-04-27 11:35:09 +08:00
const cropper = ref(null)
const imageClipBody = ref(null)
let injectLabelFrame = 0
const observer = new ResizeObserver((entries) => {
refreshCrop()
})
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
const clearCropLabels = (cropperBox) => {
if (!cropperBox) return
cropperBox.querySelectorAll(".cropper-line-label").forEach((node) => node.remove())
}
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
const createCropLabel = ({ text, top, className }) => {
const label = document.createElement("div")
label.className = `cropper-line-label ${className}`
label.textContent = text
label.style.top = top
label.style.left = className === "label-v" ? "50%" : "0"
label.style.transform =
className === "label-v" ? "translate(-50%, -50%)" : "translateY(-50%)"
2026-04-27 11:35:09 +08:00
return label
}
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
const cropLabelMap = {
cover: [
{ text: "crown", top: "2.67%", className: "label-h" },
{ text: "hip line", top: "63.47%", className: "label-h" },
{ text: "mid-thigh", top: "92.8%", className: "label-h" },
{ text: "center", top: "0", className: "label-v" }
],
mainProductImage: [
{ text: "crown", top: "2.67%", className: "label-h" },
{ text: "footbase", top: "97.6%", className: "label-h" },
{ text: "center", top: "0", className: "label-v" }
],
sketch: [
{ text: "crown", top: "2.67%", className: "label-h" },
{ text: "footbase", top: "97.6%", className: "label-h" },
{ text: "center", top: "0", className: "label-v" }
],
apparel: [{ text: "center", top: "0", className: "label-v" }]
}
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
const injectCropLabel = () => {
const cropperBox = imageClipBody.value?.querySelector(".cropper-view-box")
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
if (!cropperBox) return false
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
clearCropLabels(cropperBox)
;(cropLabelMap[props.type] || []).forEach((config) => {
cropperBox.appendChild(createCropLabel(config))
})
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
return true
}
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
const scheduleInjectCropLabel = (retry = 0) => {
cancelAnimationFrame(injectLabelFrame)
injectLabelFrame = requestAnimationFrame(() => {
if (!injectCropLabel() && retry < 10) {
scheduleInjectCropLabel(retry + 1)
}
})
}
2026-04-21 16:13:55 +08:00
2026-04-27 11:35:09 +08:00
onMounted(() => {
observer.observe(imageClipBody.value)
scheduleInjectCropLabel()
2026-04-13 14:35:12 +08:00
})
2026-04-27 11:35:09 +08:00
onBeforeUnmount(() => {
observer.disconnect()
cancelAnimationFrame(injectLabelFrame)
2026-04-13 14:35:12 +08:00
})
2026-04-27 11:35:09 +08:00
const rotateLeft = () => {
cropper.value.rotateLeft()
}
const rotateRight = () => {
cropper.value.rotateRight()
}
const refreshCrop = () => {
cropper.value.refresh()
}
const changeScale = (num = 1) => {
cropper.value.changeScale(num)
}
const getCropUrl = () => {
return new Promise((resolve, reject) => {
cropper.value.getCropData(resolve)
})
}
const getCropBlob = () => {
return new Promise((resolve, reject) => {
cropper.value.getCropBlob(resolve)
})
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
watch(
[() => props.type, () => props.url],
async () => {
await nextTick()
scheduleInjectCropLabel()
},
{ flush: "post" }
)
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
defineExpose({
getCropUrl,
getCropBlob
})
2026-04-13 14:35:12 +08:00
</script>
<style lang="less" scoped>
2026-04-27 11:35:09 +08:00
.image-clip {
2026-04-13 14:35:12 +08:00
width: 100%;
2026-04-27 11:35:09 +08:00
height: 100%;
display: flex;
flex-direction: column;
// height: 100%;
background: #fff;
border-radius: calc(2rem * 1.2);
padding: calc(1.3rem * 1.2) calc(1.3rem * 1.2) calc(2rem * 1.2);
box-sizing: border-box;
2026-04-13 14:35:12 +08:00
2026-04-27 11:35:09 +08:00
.image-clip-body {
width: 100%;
height: calc(40rem * 1.2);
// height: 53rem;
background: yellow;
:deep(.cropper-box) {
.cropper-box-canvas {
background-color: #ffffff;
2026-04-28 11:43:12 +08:00
// img {
// height: 100%;
// }
2026-04-17 17:59:51 +08:00
}
}
2026-04-27 11:35:09 +08:00
&.is-cover {
:deep(.vue-cropper) {
overflow: hidden;
}
2026-04-28 11:43:12 +08:00
// :deep(.cropper-box-canvas) {
// width: 31.1rem !important;
// left: 50% !important;
// transform: translateX(-50%) !important;
// img {
// display: none;
// }
// }
2026-04-21 16:13:55 +08:00
}
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
.clip_opterate {
margin: calc(2.7rem * 1.2) auto 0;
border-radius: calc(1.6rem * 1.2);
2026-04-13 14:35:12 +08:00
display: flex;
2026-04-27 11:35:09 +08:00
overflow: hidden;
border: 1px solid #e2e2e4;
width: calc(24rem * 1.2);
2026-04-13 14:35:12 +08:00
2026-04-27 11:35:09 +08:00
.item {
width: calc(4.7rem * 1.2);
height: calc(4rem * 1.2);
display: flex;
align-items: center;
justify-content: center;
border-right: 0.1rem solid #e6e8ea;
cursor: pointer;
2026-04-13 14:35:12 +08:00
2026-04-27 11:35:09 +08:00
.icon_chexiao_sec {
transform: rotateY(180deg); /* 垂直镜像翻转 */
}
2026-04-13 14:35:12 +08:00
2026-04-27 11:35:09 +08:00
.operate_icon {
font-size: calc(1.8rem * 1.2);
color: rgba(102, 102, 102, 1);
font-weight: bold;
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
.icon_font {
font-size: calc(2.5rem * 1.2);
position: relative;
top: calc(-0.3rem * 1.2);
user-select: none;
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
.icon-shuaxin {
font-size: calc(1.4rem * 1.2);
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
&:last-child {
border: none;
}
2026-04-21 16:13:55 +08:00
}
2026-04-22 10:20:49 +08:00
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
&.is-product {
2026-04-22 10:20:49 +08:00
.image-clip-body {
2026-04-27 11:35:09 +08:00
width: 45.7rem;
height: 45.7rem;
:deep(.cropper-modal) {
background: transparent;
}
:deep(.vue-cropper .cropper-view-box) {
position: relative;
overflow: visible !important;
/* 原有的蓝色边框outline由组件控制这里不干涉 */
}
2026-04-22 10:20:49 +08:00
:deep(.vue-cropper .cropper-view-box::after) {
2026-04-27 11:35:09 +08:00
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 9; /* 位于图片之上,但在控制点之下 */
background-image: none;
background-repeat: no-repeat;
2026-04-21 16:13:55 +08:00
}
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
&[data-crop-type="cover"] {
.image-clip-body {
:deep(.vue-cropper .cropper-view-box::after) {
background-image:
linear-gradient(to right, #4ba5ff 50%, transparent 50%),
2026-04-27 11:35:09 +08:00
linear-gradient(to right, #4ba5ff 50%, transparent 50%),
linear-gradient(to right, #4ba5ff 50%, transparent 50%),
linear-gradient(to bottom, #4ba5ff 50%, transparent 50%);
background-repeat: repeat-x, repeat-x, repeat-x, repeat-y;
background-size:
8px 1px,
8px 1px,
8px 1px,
1px 8px;
background-position:
0 2.67%,
0 63.47%,
0 92.8%,
50% 0;
2026-04-27 11:35:09 +08:00
}
2026-04-21 16:13:55 +08:00
}
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
&[data-crop-type="mainProductImage"],
&[data-crop-type="sketch"] {
.image-clip-body {
:deep(.vue-cropper .cropper-view-box::after) {
background-image:
linear-gradient(to right, #4ba5ff 50%, transparent 50%),
2026-04-27 11:35:09 +08:00
linear-gradient(to right, #4ba5ff 50%, transparent 50%),
linear-gradient(to bottom, #4ba5ff 50%, transparent 50%);
background-repeat: repeat-x, repeat-x, repeat-y;
background-size:
8px 1px,
8px 1px,
1px 8px;
background-position:
0 2.67%,
0 97.6%,
50% 0;
2026-04-27 11:35:09 +08:00
}
}
}
&[data-crop-type="apparel"] {
.image-clip-body {
:deep(.vue-cropper .cropper-view-box::after) {
background-image: linear-gradient(to bottom, #4ba5ff 50%, transparent 50%);
background-repeat: repeat-y;
background-size: 1px 8px;
background-position: 50% 0;
}
2026-04-21 16:13:55 +08:00
}
2026-04-17 17:59:51 +08:00
}
}
}
</style>
<style lang="less">
2026-04-27 11:35:09 +08:00
.cropper-line-label {
position: absolute;
color: #4ba5ff; /* 统一颜色 */
font-size: 11px;
font-weight: bold;
background: rgba(255, 255, 255); /* 浅色背景确保在深色图片上可见 */
// padding: 0 4px;
border-radius: 2px;
white-space: nowrap;
pointer-events: none;
z-index: 10;
line-height: 1.2;
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
/* 水平线名称:放在线段上方 2px */
.label-h {
transform: translateY(-100%);
margin-top: -2px;
left: 4px;
}
2026-04-17 17:59:51 +08:00
2026-04-27 11:35:09 +08:00
/* 垂直线名称:放在裁剪框顶部边缘上方 */
.label-v {
transform: translateX(-50%);
top: -20px;
left: 50%;
}
2026-04-13 14:35:12 +08:00
</style>