Commit 6445e9d7 by liimin

feat: 新增统一素材预览功能并重构现有预览逻辑

新增全局通用的素材预览弹窗组件与独立预览页面,封装统一的素材预览解析工具类,注册预览页面路由。重构精品首页面板和接待页的预览交互逻辑,替换原有内嵌预览弹窗,移除接待页中冗余的旧预览代码与样式,统一全平台素材预览体验。
1 parent 6d991c63
<template>
<un-popup v-model:show="show"
mode="center"
:mask-closeable="true"
:closeable="false"
round="16px"
:z-index="10080"
:duration="200"
@maskClick="onMaskClick">
<view class="mp-root" :style="rootStyle">
<!-- 头部:标题 + 关闭按钮 -->
<view class="mp-header">
<view class="mp-header-info">
<view v-if="typeBadgeLabel !== ''" class="mp-type-badge">
<text class="mp-type-badge-text">{{ typeBadgeLabel }}</text>
</view>
<text class="mp-header-title" :lines="1">{{ displayTitle }}</text>
</view>
<view class="mp-close"
hover-class="mp-close--hover"
@click="close">
<un-icon name="close" size="16" color="rgba(255,255,255,0.7)"></un-icon>
</view>
</view>
<!-- 图片预览:scroll-view 纵向滚动 -->
<scroll-view v-if="previewKind === 'image'"
class="mp-scroll"
:style="scrollStyle"
scroll-y="true"
:show-scrollbar="false">
<view class="mp-image-wrap">
<image class="mp-image"
:src="previewUrl"
mode="widthFix" />
</view>
</scroll-view>
<!-- 视频预览 -->
<view v-else-if="previewKind === 'video'"
class="mp-video-area"
:style="scrollStyle">
<video v-if="previewUrl !== ''"
class="mp-video"
:src="previewUrl"
:controls="true"
:show-center-play-btn="true"
:autoplay="false"
object-fit="contain"></video>
</view>
<!-- 不支持预览 -->
<view v-else class="mp-unsupported"
:style="scrollStyle">
<text class="mp-unsupported-text">{{ unsupportedMessage }}</text>
</view>
</view>
</un-popup>
</template>
<script setup lang="uts">
import { ref, computed } from "vue";
import { isEmptyStr } from "@/utils/common.uts";
const show = ref<boolean>(false);
const previewKind = ref<string>("image");
const previewUrl = ref<string>("");
const displayTitle = ref<string>("");
const typeBadgeLabel = ref<string>("");
const unsupportedMessage = ref<string>("该素材暂不支持预览");
/** 根据窗口尺寸动态计算弹窗宽度(取 88% 屏宽,最大 680px) */
const rootStyle = computed((): UTSJSONObject => {
const info = uni.getWindowInfo();
const winW = info.windowWidth > 0 ? info.windowWidth : 375;
const w = Math.max(300, Math.min(980, Math.floor(winW * 0.88)));
return {
width: `${w}px`
} as UTSJSONObject;
});
/** 媒体区高度 = 总高度 - 头部高度(约48px) */
const scrollStyle = computed((): UTSJSONObject => {
const info = uni.getWindowInfo();
const winH = info.windowHeight > 0 ? info.windowHeight : 667;
const maxH = Math.floor(winH * 0.90);
return {
maxHeight: `${maxH}px`
} as UTSJSONObject;
});
function close(): void {
show.value = false;
}
function onMaskClick(): void {
close();
}
/**
* 打开预览弹窗
* @param kind 预览类型:image | video | unsupported
* @param url 资源地址
* @param title 标题
* @param contentType 素材类型码 0=图片 1=视频 2=PDF 3=富文本 4=Excel 5=Word
* @param message 不支持预览时的提示文案
*/
function open(
kind: string,
url: string,
title: string,
contentType: number,
message: string = "该素材暂不支持预览"
): void {
previewKind.value = kind;
previewUrl.value = url;
displayTitle.value = !isEmptyStr(title) ? title : "";
if (contentType == 0) {
typeBadgeLabel.value = "图片";
} else if (contentType == 1) {
typeBadgeLabel.value = "视频";
} else if (contentType == 2) {
typeBadgeLabel.value = "PDF";
} else if (contentType == 3) {
typeBadgeLabel.value = "图文";
} else if (contentType == 4) {
typeBadgeLabel.value = "表格";
} else if (contentType == 5) {
typeBadgeLabel.value = "文档";
} else {
typeBadgeLabel.value = "";
}
unsupportedMessage.value = message;
show.value = true;
}
defineExpose({ open, close });
</script>
<style scoped lang="scss">
.mp-root {
background-color: #0b0f1a;
border-radius: 16px;
border-width: 1px;
border-style: solid;
border-color: rgba(255, 255, 255, 0.12);
display: flex;
flex-direction: column;
}
/* 头部 */
.mp-header {
flex-shrink: 0;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 12px 8px 10px 14px;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: rgba(255, 255, 255, 0.08);
}
.mp-header-info {
flex: 1;
min-width: 0;
display: flex;
flex-direction: row;
align-items: center;
}
.mp-type-badge {
flex-shrink: 0;
height: 20px;
padding: 0 8px;
border-radius: 4px;
background-color: rgba(44, 102, 247, 0.85);
display: flex;
align-items: center;
justify-content: center;
margin-right: 8px;
}
.mp-type-badge-text {
font-size: 10px;
font-weight: 600;
color: #ffffff;
}
.mp-header-title {
flex: 1;
min-width: 0;
font-size: 14px;
font-weight: 500;
color: rgba(255, 255, 255, 0.88);
line-height: 20px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
/* 关闭按钮 */
.mp-close {
flex-shrink: 0;
width: 32px;
height: 32px;
border-radius: 16px;
background-color: rgba(255, 255, 255, 0.08);
display: flex;
align-items: center;
justify-content: center;
margin-left: 8px;
}
.mp-close--hover {
background-color: rgba(255, 255, 255, 0.18);
}
/* 图片滚动区 */
.mp-scroll {
flex: 1;
min-height: 0;
}
.mp-image-wrap {
width: 100%;
}
.mp-image {
width: 100%;
}
/* 视频居中 */
.mp-video-area {
display: flex;
align-items: center;
justify-content: center;
background-color: #000000;
}
.mp-video {
width: 100%;
background-color: #000000;
}
/* 不支持预览 */
.mp-unsupported {
display: flex;
align-items: center;
justify-content: center;
padding: 48px 24px;
}
.mp-unsupported-text {
font-size: 14px;
line-height: 22px;
color: rgba(255, 255, 255, 0.4);
text-align: center;
}
</style>
...@@ -14,6 +14,14 @@ ...@@ -14,6 +14,14 @@
"navigationBarTextStyle": "black" "navigationBarTextStyle": "black"
} }
}, },
{
"path": "pages/common/materialPreview/index",
"style": {
"navigationBarTitleText": "素材预览",
"navigationBarBackgroundColor": "#FFFFFF",
"navigationBarTextStyle": "black"
}
},
{ {
"path": "pages/common/selectStore/index", "path": "pages/common/selectStore/index",
"style": { "style": {
......
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
</view> </view>
</view> </view>
</PackageDetailPopup> </PackageDetailPopup>
<MaterialPreviewPopup ref="previewPopup" />
</view> </view>
</template> </template>
...@@ -57,6 +59,8 @@ import ProductIntroGrid from "@/components/ProductIntroGrid.uvue"; ...@@ -57,6 +59,8 @@ import ProductIntroGrid from "@/components/ProductIntroGrid.uvue";
import ProductIntroSkeleton from "@/components/Skeleton/ProductIntroSkeleton.uvue"; import ProductIntroSkeleton from "@/components/Skeleton/ProductIntroSkeleton.uvue";
import RecentReceptionList from "@/components/RecentReceptionList.uvue"; import RecentReceptionList from "@/components/RecentReceptionList.uvue";
import MaterialPreviewPopup from "@/components/MaterialPreviewPopup.uvue";
import { resolvePreviewObj, readMaterialPreviewUrl, readMaterialCoverUrl, readMaterialTitle } from "@/utils/material-preview.uts";
const props = defineProps<{ const props = defineProps<{
/** 可选的业务逻辑对象,不传时内部自动创建(遵循场景 panel 自包含约定) */ /** 可选的业务逻辑对象,不传时内部自动创建(遵循场景 panel 自包含约定) */
...@@ -101,6 +105,7 @@ const boutiqueIntroEmptyText = computed((): string => { ...@@ -101,6 +105,7 @@ const boutiqueIntroEmptyText = computed((): string => {
}); });
const packageRef = ref(null); const packageRef = ref(null);
const previewPopup = ref<ComponentPublicInstance | null>(null);
function callExposedMethod(comp: any, methodName: string): void { function callExposedMethod(comp: any, methodName: string): void {
if (comp == null) return; if (comp == null) return;
...@@ -116,8 +121,48 @@ function toDetail(index: number): void { ...@@ -116,8 +121,48 @@ function toDetail(index: number): void {
} }
} }
function hasPackageDetail(item: any | null): boolean {
if (item == null) {
return false;
}
const obj = item as UTSJSONObject;
const content = obj["content"];
if (content != null && `${content}` !== "") {
return true;
}
const intro = obj["scriptIntroduction"];
return intro != null && `${intro}` !== "";
}
function onIntroSlideTap(index: number): void { function onIntroSlideTap(index: number): void {
toDetail(index); const list = logic.introCarouselItems.value;
const item = index >= 0 && index < list.length ? list[index] : null;
if (item == null) {
return;
}
const p = resolvePreviewObj(item);
const pKind = `${p["kind"]}`;
const pUrl = `${p["url"]}`;
const pTitle = `${p["title"]}`;
const pContentType = p["contentType"] as number;
const pMsg = `${p["unsupportedMessage"]}`;
if (pKind == "web") {
uni.navigateTo({
url: `/pages/common/webview/index?title=${encodeURIComponent(pTitle)}&url=${encodeURIComponent(pUrl)}`
});
return;
}
if (pKind == "image" || pKind == "video") {
const previewUrl = pUrl != "" ? pUrl : (pKind == "image" ? readMaterialCoverUrl(item) : readMaterialPreviewUrl(item));
const title = readMaterialTitle(item);
(previewPopup.value as ComponentPublicInstance).$callMethod("open", pKind, previewUrl, title, pContentType, pMsg);
return;
}
if (hasPackageDetail(item)) {
toDetail(index);
return;
}
uni.showToast({ title: pMsg, icon: "none" });
} }
function closePackageDetail(): void { function closePackageDetail(): void {
......
<template>
<!-- #ifdef APP -->
<view v-if="previewKind !== 'web'" class="preview-page">
<scroll-view v-if="previewKind === 'image'"
class="preview-scroll"
scroll-y="true"
:show-scrollbar="false">
<view class="preview-image-box">
<image class="preview-image"
:src="previewUrl"
mode="widthFix" />
</view>
</scroll-view>
<view v-else-if="previewKind === 'video'" class="preview-video-box">
<video v-if="previewUrl !== ''"
class="preview-video"
:src="previewUrl"
:controls="true"
:show-center-play-btn="true"
object-fit="contain"></video>
</view>
<view v-else class="preview-empty-box">
<text class="preview-empty-text">{{ emptyText }}</text>
</view>
</view>
<view v-else class="preview-web-root">
<web-view v-if="previewUrl !== ''"
class="preview-webview"
:src="previewUrl"
:fullscreen="false"
:webview-styles="{ progress: { color: '#2C66F7' } }"
@error="onWebViewError"></web-view>
</view>
<!-- #endif -->
<!-- #ifndef APP -->
<web-view v-if="previewKind === 'web' && previewUrl !== ''"
:src="previewUrl"
:webview-styles="{ progress: { color: '#2C66F7' } }"
@error="onWebViewError"></web-view>
<view v-else class="preview-page">
<scroll-view v-if="previewKind === 'image'"
class="preview-scroll"
scroll-y="true"
:show-scrollbar="false">
<view class="preview-image-box">
<image class="preview-image"
:src="previewUrl"
mode="widthFix" />
</view>
</scroll-view>
<view v-else-if="previewKind === 'video'" class="preview-video-box">
<video v-if="previewUrl !== ''"
class="preview-video"
:src="previewUrl"
:controls="true"
:show-center-play-btn="true"
object-fit="contain"></video>
</view>
<view v-else class="preview-empty-box">
<text class="preview-empty-text">{{ emptyText }}</text>
</view>
</view>
<!-- #endif -->
</template>
<script setup lang="uts">
import { ref } from "vue";
import { isEmptyStr } from "@/utils/common.uts";
const previewKind = ref<string>("unsupported");
const previewUrl = ref<string>("");
const emptyText = ref<string>("该素材暂不支持预览");
function scheduleBack(): void {
setTimeout((): void => {
const pages = getCurrentPages();
if (pages.length > 1) {
uni.navigateBack({});
} else {
uni.reLaunch({ url: "/pages/common/login/index" });
}
}, 1600);
}
function onWebViewError(): void {
// 保持静默,避免文件服务端短暂跳转时频繁打断用户。
}
function isValidPreviewUrl(kind: string, url: string): boolean {
if (isEmptyStr(url)) {
return false;
}
if (kind == "web") {
return url.startsWith("https://") || url.startsWith("http://");
}
return true;
}
onLoad((options: UTSJSONObject | null): void => {
let titleText = "素材预览";
let kindText = "unsupported";
let urlText = "";
if (options != null) {
const titleValue = options["title"];
if (titleValue != null) {
const titleString = `${titleValue}`;
if (!isEmptyStr(titleString)) {
titleText = titleString;
}
}
const kindValue = options["kind"];
if (kindValue != null) {
const kindString = `${kindValue}`;
if (!isEmptyStr(kindString)) {
kindText = kindString;
}
}
const urlValue = options["url"];
if (urlValue != null) {
const urlString = `${urlValue}`;
if (!isEmptyStr(urlString)) {
urlText = urlString;
}
}
}
uni.setNavigationBarTitle({ title: titleText });
if (!isValidPreviewUrl(kindText, urlText)) {
uni.showToast({
title: "预览地址无效",
icon: "none"
});
scheduleBack();
return;
}
previewKind.value = kindText;
previewUrl.value = urlText;
});
</script>
<style scoped lang="scss">
.preview-page {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
background-color: #0f172a;
}
.preview-scroll {
flex: 1;
width: 100%;
min-height: 0;
}
.preview-image-box {
min-height: 100%;
padding: 16px;
box-sizing: border-box;
display: flex;
align-items: flex-start;
justify-content: center;
}
.preview-image {
width: 100%;
border-radius: 12px;
background-color: #ffffff;
}
.preview-video-box {
flex: 1;
min-height: 0;
display: flex;
align-items: center;
justify-content: center;
padding: 12px;
box-sizing: border-box;
}
.preview-video {
width: 100%;
height: 100%;
background-color: #000000;
border-radius: 12px;
}
.preview-empty-box {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
box-sizing: border-box;
}
.preview-empty-text {
font-size: 14px;
line-height: 20px;
color: rgba(255, 255, 255, 0.7);
text-align: center;
}
.preview-web-root {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
background-color: #ffffff;
}
.preview-webview {
flex: 1;
width: 100%;
min-height: 0;
}
</style>
...@@ -141,18 +141,6 @@ ...@@ -141,18 +141,6 @@
</view> </view>
</view> </view>
<un-popup v-model:show="previewPopupShow"
mode="center"
:mask-closeable="true"
round="20px">
<view class="reception-preview-popup">
<text class="reception-preview-title">{{ previewTitle }}</text>
<image class="reception-preview-image"
:src="previewImageUrl"
mode="widthFix" />
</view>
</un-popup>
<ReceptionResultPopup ref="resultPopup" <ReceptionResultPopup ref="resultPopup"
@select="onResultSelect" @select="onResultSelect"
@confirm="onResultConfirm" @confirm="onResultConfirm"
...@@ -171,12 +159,14 @@ ...@@ -171,12 +159,14 @@
cancelText="继续接待" cancelText="继续接待"
confirmText="退出" confirmText="退出"
@confirm="onBackConfirm" /> @confirm="onBackConfirm" />
<MaterialPreviewPopup ref="previewPopup" />
</view> </view>
</layout-root> </layout-root>
</template> </template>
<script setup lang="uts"> <script setup lang="uts">
import { computed, ref, watch, onUnmounted, type Ref, type ComponentPublicInstance } from "vue"; import { computed, ref, watch, type Ref, type ComponentPublicInstance } from "vue";
import ProductIntroGrid from "@/components/ProductIntroGrid.uvue"; import ProductIntroGrid from "@/components/ProductIntroGrid.uvue";
import ReceptionResultPopup from "@/components/ReceptionResultPopup.uvue"; import ReceptionResultPopup from "@/components/ReceptionResultPopup.uvue";
import ConfirmDialog from "@/components/ConfirmDialog.uvue"; import ConfirmDialog from "@/components/ConfirmDialog.uvue";
...@@ -184,14 +174,13 @@ import { useOrientation } from "@/services/adapter/hooks/index.uts"; ...@@ -184,14 +174,13 @@ import { useOrientation } from "@/services/adapter/hooks/index.uts";
import { recordService } from "@/services/recorder/index.uts"; import { recordService } from "@/services/recorder/index.uts";
import { useReceptionStore, type CustomerInfo } from "@/store/modules/reception.uts"; import { useReceptionStore, type CustomerInfo } from "@/store/modules/reception.uts";
import { type RecordData } from "@/services/recorder/types"; import { type RecordData } from "@/services/recorder/types";
import MaterialPreviewPopup from "@/components/MaterialPreviewPopup.uvue";
import { resolvePreviewObj, readMaterialPreviewUrl, readMaterialCoverUrl, readMaterialTitle } from "@/utils/material-preview.uts";
import receptionPageVars from "./styles/reception.vars.uts"; import receptionPageVars from "./styles/reception.vars.uts";
import { import {
createEmptyReceptionForm, createEmptyReceptionForm,
loadReceptionMaterials, loadReceptionMaterials,
readMaterialCoverUrl,
readMaterialPreviewUrl,
readMaterialTitle,
type CustomerReceptionForm type CustomerReceptionForm
} from "./useReceptionDesk.uts"; } from "./useReceptionDesk.uts";
...@@ -244,12 +233,10 @@ const materialsLoading = ref<boolean>(false); ...@@ -244,12 +233,10 @@ const materialsLoading = ref<boolean>(false);
const loadedFromPadType = ref<boolean>(false); const loadedFromPadType = ref<boolean>(false);
const currentTypeCode = ref<string>(""); const currentTypeCode = ref<string>("");
const formData = ref<CustomerReceptionForm>(createEmptyReceptionForm()); const formData = ref<CustomerReceptionForm>(createEmptyReceptionForm());
const previewPopupShow = ref<boolean>(false);
const previewTitle = ref<string>("");
const previewImageUrl = ref<string>("");
const resultPopup = ref<ComponentPublicInstance | null>(null); const resultPopup = ref<ComponentPublicInstance | null>(null);
const finishDialog = ref<ComponentPublicInstance | null>(null); const finishDialog = ref<ComponentPublicInstance | null>(null);
const backDialog = ref<ComponentPublicInstance | null>(null); const backDialog = ref<ComponentPublicInstance | null>(null);
const previewPopup = ref<ComponentPublicInstance | null>(null);
let hasClosed = false; let hasClosed = false;
...@@ -293,10 +280,6 @@ function resetReceptionDraft(): void { ...@@ -293,10 +280,6 @@ function resetReceptionDraft(): void {
formData.value = createEmptyReceptionForm(); formData.value = createEmptyReceptionForm();
} }
function isWebUrl(url: string): boolean {
return url.startsWith("http://") || url.startsWith("https://");
}
function closeReception(isNotSave: boolean): void { function closeReception(isNotSave: boolean): void {
if (hasClosed) { if (hasClosed) {
return; return;
...@@ -339,21 +322,21 @@ function onMaterialTap(index: number): void { ...@@ -339,21 +322,21 @@ function onMaterialTap(index: number): void {
if (item == null) { if (item == null) {
return; return;
} }
const previewUrl = readMaterialPreviewUrl(item); const p = resolvePreviewObj(item);
if (previewUrl !== "" && isWebUrl(previewUrl)) { const pKind = `${p["kind"]}`;
const pUrl = `${p["url"]}`;
const pTitle = `${p["title"]}`;
const pContentType = p["contentType"] as number;
const pMsg = `${p["unsupportedMessage"]}`;
if (pKind == "web") {
uni.navigateTo({ uni.navigateTo({
url: `/pages/common/webview/index?url=${encodeURIComponent(previewUrl)}` url: `/pages/common/webview/index?title=${encodeURIComponent(pTitle)}&url=${encodeURIComponent(pUrl)}`
}); });
return; return;
} }
const localPreviewUrl = previewUrl !== "" ? previewUrl : readMaterialCoverUrl(item); const previewUrl = pUrl != "" ? pUrl : (pKind == "image" ? readMaterialCoverUrl(item) : readMaterialPreviewUrl(item));
if (localPreviewUrl !== "") { const title = readMaterialTitle(item);
previewTitle.value = readMaterialTitle(item); (previewPopup.value as ComponentPublicInstance).$callMethod("open", pKind, previewUrl, title, pContentType, pMsg);
previewImageUrl.value = localPreviewUrl;
previewPopupShow.value = true;
return;
}
uni.showToast({ title: "该素材暂不支持预览", icon: "none" });
} }
async function initMaterialList(typeCode: string): Promise<void> { async function initMaterialList(typeCode: string): Promise<void> {
...@@ -388,9 +371,6 @@ onUnload(() => { ...@@ -388,9 +371,6 @@ onUnload(() => {
} }
}); });
onUnmounted(() => {
previewPopupShow.value = false;
});
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
...@@ -790,25 +770,4 @@ onUnmounted(() => { ...@@ -790,25 +770,4 @@ onUnmounted(() => {
overflow: hidden; overflow: hidden;
background-color: #f7f9fc; background-color: #f7f9fc;
} }
/* 素材预览弹层 */
.reception-preview-popup {
width: 320px;
padding: 18px;
box-sizing: border-box;
}
.reception-preview-title {
font-size: 16px;
font-weight: 600;
line-height: 22px;
color: #0f172a;
margin-bottom: 12px;
}
.reception-preview-image {
width: 100%;
border-radius: 12px;
background-color: #f7f9fc;
}
</style> </style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!