train-guide.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { NButton } from 'naive-ui';
  2. import {
  3. defineComponent,
  4. nextTick,
  5. onMounted,
  6. onUnmounted,
  7. reactive,
  8. ref,
  9. watch
  10. } from 'vue';
  11. import styles from './index.module.less';
  12. import { getImage } from './images';
  13. import { eventGlobal, px2vw, px2vwH } from '@/utils/index';
  14. import { getGuidance, setGuidance } from './api';
  15. export default defineComponent({
  16. name: 'train-guide',
  17. emits: ['close'],
  18. setup(props, { emit }) {
  19. const data = reactive({
  20. box: {
  21. height: '0px'
  22. } as any,
  23. show: false,
  24. /**
  25. *
  26. width: px2vw(840),
  27. height: px2vw(295)
  28. */
  29. steps: [
  30. {
  31. ele: '',
  32. eleRect: {} as DOMRect,
  33. img: getImage('train3.png'),
  34. handStyle: {
  35. top: '-0.91rem'
  36. },
  37. imgStyle: {
  38. top: px2vw(-360),
  39. left: px2vw(-170),
  40. width: px2vw(591),
  41. height: px2vw(302)
  42. },
  43. btnsStyle: {
  44. top: px2vw(-205),
  45. left: px2vw(-10)
  46. },
  47. eleRectPadding: {
  48. left: 7,
  49. top: 7,
  50. width: 14,
  51. height: 14
  52. }
  53. }
  54. ],
  55. step: 0
  56. });
  57. const tipShow = ref(false);
  58. const guideInfo = ref({} as any);
  59. const getAllGuidance = async () => {
  60. try {
  61. const res = await getGuidance({ guideTag: 'teacher-guideInfo' });
  62. if (res.data) {
  63. guideInfo.value = JSON.parse(res.data?.guideValue) || null;
  64. } else {
  65. guideInfo.value = {};
  66. }
  67. if (guideInfo.value && guideInfo.value.trainGuide) {
  68. tipShow.value = false;
  69. } else {
  70. tipShow.value = true;
  71. }
  72. } catch (e) {
  73. console.log(e);
  74. }
  75. // const guideInfo = localStorage.getItem('teacher-guideInfo');
  76. };
  77. getAllGuidance();
  78. // const guideInfo = localStorage.getItem('teacher-guideInfo');
  79. // if (guideInfo && JSON.parse(guideInfo).trainGuide) {
  80. // tipShow.value = false;
  81. // } else {
  82. // tipShow.value = true;
  83. // }
  84. const getStepELe = () => {
  85. const ele: HTMLElement = document.getElementById(`train-${data.step}`)!;
  86. if (ele) {
  87. const eleRect = ele.getBoundingClientRect();
  88. const left = data.steps[data.step].eleRectPadding?.left || 0;
  89. const top = data.steps[data.step].eleRectPadding?.top || 0;
  90. const width = data.steps[data.step].eleRectPadding?.width || 0;
  91. const height = data.steps[data.step].eleRectPadding?.height || 0;
  92. data.box = {
  93. left: eleRect.x - left + 'px',
  94. top: eleRect.y - top + 'px',
  95. width: eleRect.width + width + 'px',
  96. height: eleRect.height + height + 'px'
  97. };
  98. // console.log(`coai-${data.step}`, data.box);
  99. } else {
  100. handleNext();
  101. }
  102. };
  103. const onResetGuide = async (name: string) => {
  104. try {
  105. if (name !== 'train') return;
  106. if (!guideInfo.value) {
  107. guideInfo.value = { trainGuide: false };
  108. } else {
  109. guideInfo.value.trainGuide = false;
  110. }
  111. try {
  112. await setGuidance({
  113. guideTag: 'teacher-guideInfo',
  114. guideValue: JSON.stringify(guideInfo.value)
  115. });
  116. } catch (e) {
  117. console.log(e);
  118. }
  119. data.step = 0;
  120. getStepELe();
  121. tipShow.value = true;
  122. } catch {
  123. //
  124. }
  125. };
  126. onMounted(() => {
  127. getStepELe();
  128. window.addEventListener('resize', resetSize);
  129. eventGlobal.on('teacher-guideInfo', onResetGuide);
  130. });
  131. const resetSize = () => {
  132. getStepELe();
  133. };
  134. onUnmounted(() => {
  135. window.removeEventListener('resize', resetSize);
  136. eventGlobal.off('teacher-guideInfo', onResetGuide);
  137. });
  138. const handleNext = () => {
  139. if (data.step >= 4) {
  140. endGuide();
  141. return;
  142. }
  143. data.step = data.step + 1;
  144. getStepELe();
  145. };
  146. const endGuide = async () => {
  147. // let guideInfo =
  148. // JSON.parse(localStorage.getItem('teacher-guideInfo') || '{}') || null;
  149. if (!guideInfo.value) {
  150. guideInfo.value = { trainGuide: true };
  151. } else {
  152. guideInfo.value.trainGuide = true;
  153. }
  154. try {
  155. const res = await setGuidance({
  156. guideTag: 'teacher-guideInfo',
  157. guideValue: JSON.stringify(guideInfo.value)
  158. });
  159. } catch (e) {
  160. console.log(e);
  161. }
  162. // localStorage.setItem('teacher-guideInfo', JSON.stringify(guideInfo));
  163. tipShow.value = false;
  164. // localStorage.setItem('endC')
  165. };
  166. return () => (
  167. <>
  168. {tipShow.value ? (
  169. <div
  170. v-model:show={tipShow.value}
  171. class={['n-modal-mask', 'n-modal-mask-guide']}>
  172. <div class={styles.content} onClick={() => handleNext()}>
  173. <div
  174. class={styles.backBtn}
  175. onClick={(e: Event) => {
  176. e.stopPropagation();
  177. endGuide();
  178. }}>
  179. 跳过
  180. </div>
  181. <div
  182. class={styles.box}
  183. style={{ ...data.box }}
  184. id={`modeType-${data.step}`}>
  185. {data.steps.map((item: any, index) => (
  186. <div
  187. onClick={(e: Event) => e.stopPropagation()}
  188. class={styles.item}
  189. style={
  190. item.type == 'bottom'
  191. ? {
  192. display: index === data.step ? '' : 'none',
  193. left: `${item.eleRect?.left}px`,
  194. top: `-${item.imgStyle?.height}`
  195. }
  196. : {
  197. display: index === data.step ? '' : 'none',
  198. left: `${item.eleRect?.left}px`,
  199. top: `${data.box?.height}`
  200. }
  201. }>
  202. <img
  203. class={styles.img}
  204. style={item.imgStyle}
  205. src={item.img}
  206. />
  207. {/* <img
  208. class={styles.iconHead}
  209. style={item.handStyle}
  210. src={getImage('indexDot.png')}
  211. /> */}
  212. <div class={styles.btns} style={item.btnsStyle}>
  213. {data.step + 1 == data.steps.length ? (
  214. <>
  215. <div
  216. class={[styles.endBtn]}
  217. onClick={() => endGuide()}>
  218. 完成
  219. </div>
  220. {/* <div
  221. class={styles.nextBtn}
  222. onClick={() => {
  223. data.step = 0;
  224. getStepELe();
  225. }}>
  226. 再看一遍
  227. </div> */}
  228. </>
  229. ) : (
  230. <div class={styles.btn} onClick={() => handleNext()}>
  231. 下一步 ({data.step + 1}/{data.steps.length})
  232. </div>
  233. )}
  234. </div>
  235. </div>
  236. ))}
  237. </div>
  238. </div>
  239. </div>
  240. ) : null}
  241. </>
  242. );
  243. }
  244. });