music-guide.tsx 8.6 KB

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