teacher-guide.tsx 8.0 KB

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