import { NButton } from 'naive-ui'; import { defineComponent, nextTick, onMounted, onUnmounted, reactive, ref, watch } from 'vue'; import styles from './index.module.less'; import { getImage } from './images'; import { eventGlobal, px2vw, px2vwH } from '@/utils/index'; import { useRoute } from 'vue-router'; import { getGuidance, setGuidance } from './api'; export default defineComponent({ name: 'attent-guide', emits: ['close'], setup(props, { emit }) { const route = useRoute(); console.log(route.query); const data = reactive({ box: { height: '0px' } as any, show: false, /** * width: px2vw(840), height: px2vw(295) */ steps: [ { ele: '', eleRect: {} as DOMRect, img: getImage('attent1.png'), handStyle: { top: '0.91rem' }, imgStyle: { width: px2vw(647), height: px2vw(223), left: px2vw(-647), top: px2vw(-150) }, btnsStyle: { bottom: px2vw(180), left: px2vw(-490) }, eleRectPadding: { left: 7, top: 7, width: 14, height: 14 }, type: 'left' }, { ele: '', img: getImage('attent2.png'), imgStyle: { left: px2vw(-290), top: px2vw(20), width: px2vw(477), height: px2vw(277) }, btnsStyle: { bottom: px2vw(70), left: px2vw(-135) }, boxStyle: { borderRadius: '50%' }, eleRectPadding: { left: 7, top: 7, width: 14, height: 14 }, type: 'bottom' }, { ele: '', img: getImage('attent3.png'), imgStyle: { left: px2vw(-290), top: px2vw(20), width: px2vw(382), height: px2vw(277) }, btnsStyle: { bottom: px2vw(70), left: px2vw(-135) }, boxStyle: { borderRadius: '50%' }, eleRectPadding: { left: 7, top: 7, width: 14, height: 14 }, type: 'bottom' }, { ele: '', img: route.query.type == 'preview' ? getImage('attent5.png') : getImage('attent4.png'), imgStyle: { top: '100%', left: px2vw(-2), width: px2vw(515), height: px2vw(227) }, btnsStyle: { bottom: px2vw(30), left: px2vw(-20) }, boxStyle: { borderRadius: '50px' }, eleRectPadding: { left: 7, top: 7, width: 14, height: 14 } } ], step: 0 }); const tipShow = ref(false); const guideInfo = ref({} as any); const getAllGuidance = async () => { try { const res = await getGuidance({ guideTag: 'teacher-guideInfo' }); if (res.data) { guideInfo.value = JSON.parse(res.data?.guideValue) || null; } else { guideInfo.value = {}; } if (guideInfo.value && guideInfo.value.attentGuide) { tipShow.value = false; } else { tipShow.value = true; } } catch (e) { console.log(e); } // const guideInfo = localStorage.getItem('teacher-guideInfo'); }; getAllGuidance(); const getStepELe = () => { const ele: HTMLElement = document.getElementById(`attent-${data.step}`)!; if (ele) { const eleRect = ele.getBoundingClientRect(); const left = data.steps[data.step].eleRectPadding?.left || 0; const top = data.steps[data.step].eleRectPadding?.top || 0; const width = data.steps[data.step].eleRectPadding?.width || 0; const height = data.steps[data.step].eleRectPadding?.height || 0; data.box = { left: eleRect.x - left + 'px', top: eleRect.y - top + 'px', width: eleRect.width + width + 'px', height: eleRect.height + height + 'px' }; // console.log(`coai-${data.step}`, data.box); } else { handleNext(); } }; const onResetGuide = async (name: string) => { try { if (name !== 'attend-class') return; if (!guideInfo.value) { guideInfo.value = { attentGuide: false }; } else { guideInfo.value.attentGuide = false; } try { await setGuidance({ guideTag: 'teacher-guideInfo', guideValue: JSON.stringify(guideInfo.value) }); } catch (e) { console.log(e); } data.step = 0; getStepELe(); tipShow.value = true; } catch { // } }; onMounted(() => { getStepELe(); window.addEventListener('resize', resetSize); eventGlobal.on('teacher-guideInfo', (name: string) => onResetGuide(name)); }); const resetSize = () => { getStepELe(); }; onUnmounted(() => { window.removeEventListener('resize', resetSize); eventGlobal.off('teacher-guideInfo', onResetGuide); }); const handleNext = () => { if (data.step >= 4) { endGuide(); return; } data.step = data.step + 1; getStepELe(); }; const endGuide = async () => { if (!guideInfo.value) { guideInfo.value = { attentGuide: true }; } else { guideInfo.value.attentGuide = true; } // localStorage.setItem('teacher-guideInfo', JSON.stringify(guideInfo)); tipShow.value = false; try { const res = await setGuidance({ guideTag: 'teacher-guideInfo', guideValue: JSON.stringify(guideInfo.value) }); } catch (e) { console.log(e); } // localStorage.setItem('endC') }; return () => ( <> {tipShow.value ? (
handleNext()}>
{ e.stopPropagation(); endGuide(); }}> 跳过
{data.steps.map((item: any, index) => (
e.stopPropagation()} class={styles.item} style={ item.type == 'bottom' ? { display: index === data.step ? '' : 'none', left: `${item.eleRect?.left}px`, top: `-${item.imgStyle?.height}` } : item.type == 'left' ? { display: index === data.step ? '' : 'none', left: `${item.eleRect?.left}px`, top: `${ parseFloat(data.box?.height) / 2 - parseFloat(item.imgStyle?.height) / 20 + 14 }px` } : { display: index === data.step ? '' : 'none', left: `${item.eleRect?.left}px`, top: `${data.box?.height}` } }> {/* */}
{data.step + 1 == data.steps.length ? ( <>
endGuide()}> 完成
{ data.step = 0; getStepELe(); }}> 再看一遍
) : (
handleNext()}> 下一步 ({data.step + 1}/{data.steps.length})
)}
))}
) : null} ); } });