student-guide.tsx 7.9 KB

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