myColloge-guide.tsx 7.4 KB

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