lessons-guide.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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: 'lessons-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('lessons1.png'),
  34. handStyle: {
  35. top: '0.91rem'
  36. },
  37. imgStyle: {
  38. top: px2vw(-4),
  39. left: px2vw(-50),
  40. width: px2vw(420),
  41. height: px2vw(228)
  42. },
  43. btnsStyle: {
  44. bottom: px2vw(35),
  45. left: px2vw(111)
  46. },
  47. eleRectPadding: {
  48. left: 7,
  49. top: 7,
  50. width: 14,
  51. height: 14
  52. }
  53. },
  54. {
  55. ele: '',
  56. img: getImage('lessons2.png'),
  57. imgStyle: {
  58. left: px2vw(-647),
  59. width: px2vw(647),
  60. height: px2vw(223)
  61. },
  62. btnsStyle: {
  63. bottom: '30px',
  64. left: px2vw(-488)
  65. },
  66. eleRectPadding: {
  67. left: 7,
  68. top: 7,
  69. width: 14,
  70. height: 14
  71. },
  72. type: 'left'
  73. },
  74. {
  75. ele: '',
  76. img: getImage('lessons3.png'),
  77. imgStyle: {
  78. top: px2vw(-4),
  79. left: px2vw(-471),
  80. width: px2vw(471),
  81. height: px2vw(223)
  82. },
  83. btnsStyle: {
  84. bottom: px2vw(20),
  85. left: px2vw(-310)
  86. },
  87. eleRectPadding: {
  88. left: 7,
  89. top: 7,
  90. width: 14,
  91. height: 14
  92. },
  93. type: 'left'
  94. },
  95. {
  96. ele: '',
  97. img: getImage('lessons4.png'),
  98. imgStyle: {
  99. top: px2vw(-355),
  100. left: px2vw(-185),
  101. width: px2vw(515),
  102. height: px2vw(302)
  103. },
  104. btnsStyle: {
  105. top: px2vw(-205),
  106. left: px2vw(-22)
  107. },
  108. eleRectPadding: {
  109. left: 7,
  110. top: 7,
  111. width: 14,
  112. height: 14
  113. }
  114. },
  115. {
  116. ele: '',
  117. img: getImage('lessons5.png'),
  118. imgStyle: {
  119. top: px2vw(-4),
  120. left: px2vw(-290),
  121. width: px2vw(648),
  122. height: px2vw(228)
  123. },
  124. btnsStyle: {
  125. bottom: px2vw(30),
  126. left: px2vw(-127)
  127. },
  128. eleRectPadding: {
  129. left: 7,
  130. top: 7,
  131. width: 14,
  132. height: 14
  133. }
  134. }
  135. ],
  136. step: 0
  137. });
  138. const tipShow = ref(false);
  139. const guideInfo = ref({} as any);
  140. const getAllGuidance = async () => {
  141. try {
  142. const res = await getGuidance({ guideTag: 'teacher-guideInfo' });
  143. if (res.data) {
  144. guideInfo.value = JSON.parse(res.data?.guideValue) || null;
  145. } else {
  146. guideInfo.value = {};
  147. }
  148. if (guideInfo.value && guideInfo.value.lessonsGuide) {
  149. tipShow.value = false;
  150. } else {
  151. tipShow.value = true;
  152. }
  153. } catch (e) {
  154. console.log(e);
  155. }
  156. // const guideInfo = localStorage.getItem('teacher-guideInfo');
  157. };
  158. getAllGuidance();
  159. // const guideInfo = localStorage.getItem('teacher-guideInfo');
  160. // if (guideInfo && JSON.parse(guideInfo).lessonsGuide) {
  161. // tipShow.value = false;
  162. // } else {
  163. // tipShow.value = true;
  164. // }
  165. const getStepELe = () => {
  166. const ele: HTMLElement = document.getElementById(`lessons-${data.step}`)!;
  167. if (ele) {
  168. const eleRect = ele.getBoundingClientRect();
  169. const left = data.steps[data.step].eleRectPadding?.left || 0;
  170. const top = data.steps[data.step].eleRectPadding?.top || 0;
  171. const width = data.steps[data.step].eleRectPadding?.width || 0;
  172. const height = data.steps[data.step].eleRectPadding?.height || 0;
  173. data.box = {
  174. left: eleRect.x - left + 'px',
  175. top: eleRect.y - top + 'px',
  176. width: eleRect.width + width + 'px',
  177. height: eleRect.height + height + 'px'
  178. };
  179. console.log(`coai-${data.step}`, data.box);
  180. } else {
  181. handleNext();
  182. }
  183. };
  184. const onResetGuide = async (name: string) => {
  185. try {
  186. if (name !== 'courseware') return;
  187. if (!guideInfo.value) {
  188. guideInfo.value = { lessonsGuide: false };
  189. } else {
  190. guideInfo.value.lessonsGuide = false;
  191. }
  192. try {
  193. await setGuidance({
  194. guideTag: 'teacher-guideInfo',
  195. guideValue: JSON.stringify(guideInfo.value)
  196. });
  197. } catch (e) {
  198. console.log(e);
  199. }
  200. data.step = 0;
  201. getStepELe();
  202. tipShow.value = true;
  203. } catch {
  204. //
  205. }
  206. };
  207. onMounted(() => {
  208. getStepELe();
  209. window.addEventListener('resize', resetSize);
  210. eventGlobal.on('prepare-lessons-guide', (name: string) =>
  211. onResetGuide(name)
  212. );
  213. });
  214. const resetSize = () => {
  215. getStepELe();
  216. };
  217. onUnmounted(() => {
  218. window.removeEventListener('resize', resetSize);
  219. eventGlobal.off('prepare-lessons-guide', onResetGuide);
  220. });
  221. const handleNext = () => {
  222. if (data.step >= 4) {
  223. endGuide();
  224. return;
  225. }
  226. data.step = data.step + 1;
  227. getStepELe();
  228. };
  229. const endGuide = async () => {
  230. // let guideInfo =
  231. // JSON.parse(localStorage.getItem('teacher-guideInfo')|| '{}') || null;
  232. if (!guideInfo.value) {
  233. guideInfo.value = { lessonsGuide: true };
  234. } else {
  235. guideInfo.value.lessonsGuide = true;
  236. }
  237. try {
  238. const res = await setGuidance({
  239. guideTag: 'teacher-guideInfo',
  240. guideValue: JSON.stringify(guideInfo.value)
  241. });
  242. } catch (e) {
  243. console.log(e);
  244. }
  245. // localStorage.setItem('teacher-guideInfo', JSON.stringify(guideInfo));
  246. tipShow.value = false;
  247. // localStorage.setItem('endC')
  248. };
  249. return () => (
  250. <>
  251. {tipShow.value ? (
  252. <div
  253. v-model:show={tipShow.value}
  254. class={['n-modal-mask', 'n-modal-mask-guide']}>
  255. <div class={styles.content} onClick={() => handleNext()}>
  256. <div
  257. class={styles.backBtn}
  258. onClick={(e: Event) => {
  259. e.stopPropagation();
  260. endGuide();
  261. }}>
  262. 跳过
  263. </div>
  264. <div
  265. class={styles.box}
  266. style={{ ...data.box }}
  267. id={`modeType-${data.step}`}>
  268. {data.steps.map((item: any, index) => (
  269. <div
  270. onClick={(e: Event) => e.stopPropagation()}
  271. class={styles.item}
  272. style={
  273. item.type == 'bottom'
  274. ? {
  275. display: index === data.step ? '' : 'none',
  276. left: `${item.eleRect?.left}px`,
  277. top: `-${item.imgStyle?.height}`
  278. }
  279. : item.type == 'left'
  280. ? {
  281. display: index === data.step ? '' : 'none',
  282. top: `${
  283. parseFloat(data.box?.height) / 2 -
  284. (parseFloat(item.imgStyle?.height) * 1920) /
  285. 100 /
  286. 2 -
  287. 14
  288. }px`
  289. }
  290. : {
  291. display: index === data.step ? '' : 'none',
  292. left: `${item.eleRect?.left}px`,
  293. top: `${data.box?.height}`
  294. }
  295. }>
  296. <img
  297. class={styles.img}
  298. style={item.imgStyle}
  299. src={item.img}
  300. />
  301. {/* <img
  302. class={styles.iconHead}
  303. style={item.handStyle}
  304. src={getImage('indexDot.png')}
  305. /> */}
  306. <div class={styles.btns} style={item.btnsStyle}>
  307. {data.step + 1 == data.steps.length ? (
  308. <>
  309. <div
  310. class={[styles.endBtn]}
  311. onClick={() => endGuide()}>
  312. 完成
  313. </div>
  314. <div
  315. class={styles.nextBtn}
  316. onClick={() => {
  317. data.step = 0;
  318. getStepELe();
  319. }}>
  320. 再看一遍
  321. </div>
  322. </>
  323. ) : (
  324. <div class={styles.btn} onClick={() => handleNext()}>
  325. 下一步 ({data.step + 1}/{data.steps.length})
  326. </div>
  327. )}
  328. </div>
  329. </div>
  330. ))}
  331. </div>
  332. </div>
  333. </div>
  334. ) : null}
  335. </>
  336. );
  337. }
  338. });