import { Image, Icon, showLoadingToast, showSuccessToast, showFailToast, Popup, Swipe, SwipeItem } from 'vant'; import { PropType, defineComponent, nextTick, onMounted, reactive, ref, watch } from 'vue'; import styles from './index.module.less'; import iconPreviewClose from '@/common/images/icon-preview-close.png'; import iconPreviewDownload from '@/common/images/icon-preview-download.png'; import { promisefiyPostMessage } from '@/helpers/native-message'; import { checkFile } from '@/helpers/toolsValidate'; import MVideo from '../m-video'; import { state } from '@/state'; import { browser } from '@/helpers/utils'; import deepClone from '@/helpers/deep-clone'; export default defineComponent({ name: 'm-image-preview', props: { show: { tyep: Boolean, default: false }, images: { type: Array as PropType, default: () => [] }, showIndex: { type: Boolean, default: true }, startPosition: { type: Number, default: 0 }, loop: { type: Boolean, default: false }, showDownload: { type: Boolean, default: true }, teleport: { type: String, default: '' } }, emits: ['update:show'], setup(props, { emit }) { const forms = reactive({ show: false, showButton: true, index: props.startPosition + 1, saveLoading: false, preLoading: false }); const onSave = async (img: string) => { if (forms.saveLoading) return; forms.saveLoading = true; showLoadingToast({ message: '下载中...', forbidClick: true }); try { const res = await promisefiyPostMessage({ api: 'saveFile', content: { img, type: checkFile(img, 'image') ? 'image' : 'video' } }); if (res?.content?.status === 'success') { showSuccessToast('保存成功'); } else { showFailToast('保存失败'); } } catch { // } forms.saveLoading = false; }; const videoRef: any = ref([]); const onPlay = (index: any) => { videoRef.value.forEach((item: any, child: any) => { if (child !== index) { item?.onStop(); item?.onExitScreen(); } }); }; onMounted(() => { forms.show = props.show; // console.log(window.document.body.clientWidth); document.documentElement.style.setProperty( '--window-page-width', (window.document.body.clientWidth || window.document.body.offsetWidth) + 'px' ); onChnageLeftWidth(forms.index - 1); }); const onChnageLeftWidth = (index: number) => { document.documentElement.style.setProperty( '--window-page-position-left', (window.document.body.clientWidth || window.document.body.offsetWidth) * index + 'px' ); }; watch( () => props.show, () => { forms.show = props.show; forms.index = props.startPosition + 1; forms.preLoading = props.show; onChnageLeftWidth(props.startPosition); // console.log(forms.preLoading, 'show'); // nextTick(() => { // // 判断打开的内容是否为视频,是则自动播放 // const defaultUrl = props.images[props.startPosition]; // console.log(defaultUrl, 'defaultUrl'); // if (checkFile(defaultUrl, 'video') && props.show) { // console.log(1111, videoRef.value); // // videoRef.value[props.startPosition]?.onPlay(); // videoRef.value.forEach((item: any, child: any) => { // if (child === props.startPosition) { // console.log(item, 'item'); // item?.onPlay(); // } // }); // } // }); } ); watch( () => props.startPosition, () => { forms.index = props.startPosition + 1; onChnageLeftWidth(props.startPosition); } ); return () => ( {forms.show ? ( <> {forms.showButton && ( <> { onPlay(-1); emit('update:show', false); }} />
{forms.index} / {props.images.length}
{props.showDownload && browser().isApp ? ( { // console.log( // forms.index, // 'index', // props.images[forms.index - 1] // ); onSave(props.images[forms.index - 1]); }} /> ) : ( '' )} )} { forms.index = index + 1; // forms.preLoading = true; onPlay(index); onChnageLeftWidth(index); }} lazyRender> {props.images.map((url: string, index: number) => ( { onPlay(-1); emit('update:show', false); }}> {checkFile(url, 'image') ? ( ) : (
{ e.stopPropagation(); e.preventDefault(); }}> { videoRef.value[index] = el; // if (forms.index === index + 1 && forms.preLoading) { // console.log(el, 'player'); // el?.onPlay(); // forms.preLoading = false; // } }} // onReady={player => { // if ( // props.startPosition === index && // forms.preLoading // ) { // console.log(player, 'player'); // player?.play(); // forms.preLoading = false; // } // }} src={url} onPlay={() => onPlay(index)} // preLoading={false} // onEnterfullscreen={() => (forms.showButton = false)} // onExitfullscreen={() => (forms.showButton = true)} />
)}
))}
) : ( '' )}
); } });