myResources-guide.tsx 7.6 KB

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