shareResources-guide.tsx 8.5 KB

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