GuideManager.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <template>
  2. <view class="guide-manager">
  3. <!-- 遮罩层 -->
  4. <view class="guide-mask" v-if="currentStage" @click="handleMaskClick">
  5. <!-- 高亮区域 -->
  6. <view class="highlight-area" :style="highlightStyle"></view>
  7. <!-- 提示框 -->
  8. <view class="guide-tips" :style="tipsStyle">
  9. <view class="tips-content">
  10. <text class="tips-text">{{ currentStep.tips }}</text>
  11. <view class="tips-buttons">
  12. <text class="skip-btn" @click.stop="skipStage">跳过</text>
  13. <text class="next-btn" @click.stop="nextStep">{{ isLastStep ? '完成' : '下一步' }}</text>
  14. </view>
  15. </view>
  16. <!-- 箭头 -->
  17. <view class="arrow" :style="arrowStyle"></view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import {mapState} from 'vuex'
  24. export default {
  25. name: 'GuideManager',
  26. data() {
  27. return {
  28. // 当前引导阶段
  29. currentStage: null,
  30. // 当前步骤索引
  31. currentStepIndex: 0,
  32. // 已完成阶段
  33. completedStages: [],
  34. // 已完成的主线
  35. // completedMainLines: [],
  36. // 引导配置
  37. guideConfig: {
  38. mainLines: [
  39. {
  40. name: "new immigrant",
  41. stage: "homeLand",
  42. steps: [
  43. {
  44. arrow: null,
  45. talkData: [
  46. {
  47. characterImage: '/static/island/npc.png',
  48. characterName: '罗宾',
  49. text: '你好!欢迎来到萌创星球,你的移民计划正式完成。',
  50. position: 'left',
  51. isMirror: true
  52. }
  53. ]
  54. },
  55. {
  56. arrow: {x: 350, y: 460, r: 0},
  57. talkData: [
  58. {
  59. characterImage: '/static/island/npc.png',
  60. characterName: '罗宾',
  61. text: '这里是工具台 ,它可以制作各实用和非常棒的工具和装饰物,千万不要错过哟。',
  62. position: 'left',
  63. isMirror: true
  64. },
  65. ]
  66. }
  67. ]
  68. },
  69. {
  70. name: "new craftsman",
  71. stage: "homeLand_table",
  72. steps: [
  73. {
  74. arrow: null,
  75. talkData: [
  76. {
  77. characterImage: '/static/island/npc.png',
  78. characterName: '罗宾',
  79. text: '果然很顺利的打开了工作台呢!',
  80. position: 'left',
  81. isMirror: true
  82. },
  83. {
  84. characterImage: this.picture,
  85. characterName: this.name,
  86. text: '好像可以有好多工具,但是好像没有材料呢~',
  87. position: 'right',
  88. isMirror: false
  89. },
  90. {
  91. characterImage: '/static/island/npc.png',
  92. characterName: '罗宾',
  93. text: '材料确实不太容易获取呢,可以去看看公告栏。',
  94. position: 'left',
  95. isMirror: true
  96. }
  97. ]
  98. },
  99. {
  100. arrow: {x: 885, y: 535, r: 270},
  101. talkData: [
  102. {
  103. characterImage: '/static/island/npc.png',
  104. characterName: '罗宾',
  105. text: '看到小箭头指示的地方了么...那是任务看板,可以获取一些稀有道具和铃钱哦。',
  106. position: 'left',
  107. isMirror: true
  108. }
  109. ]
  110. }
  111. ]
  112. },
  113. {
  114. name: "oh, taskBoard",
  115. stage: "homeLand_TaskBoard",
  116. steps: [
  117. {
  118. arrow: null,
  119. talkData: [
  120. {
  121. characterImage: '/static/island/npc.png',
  122. characterName: '罗宾',
  123. text: '这里就是任务面板呦....',
  124. position: 'left',
  125. isMirror: true
  126. },
  127. {
  128. characterImage: '/static/island/npc.png',
  129. characterName: '罗宾',
  130. text: '顺便说一下,你的移民计划正式开启目前您需要缴纳移民费用为388888元。也在任务面板发布了呦~',
  131. position: 'left',
  132. isMirror: true
  133. },
  134. {
  135. characterImage: this.picture,
  136. characterName: this.name,
  137. text: '什么!移民费用388888铃钱,这也太贵了吧...',
  138. position: 'right',
  139. isMirror: false
  140. },
  141. {
  142. characterImage: '/static/island/npc.png',
  143. characterName: '罗宾',
  144. text: '移民本就是一件不容易的事呢...',
  145. position: 'left',
  146. isMirror: true
  147. },
  148. {
  149. characterImage: '/static/island/npc.png',
  150. characterName: '罗宾',
  151. text: '对了,听说近期市政厅那边的花田正在招募园丁,报酬丰厚,',
  152. position: 'left',
  153. isMirror: true
  154. },
  155. {
  156. characterImage: '/static/island/npc.png',
  157. characterName: '罗宾',
  158. text: '相信以你的能力完全是没问题的,右侧过桥后就到了。',
  159. position: 'left',
  160. isMirror: true
  161. }
  162. ]
  163. }
  164. ]
  165. },
  166. {
  167. name: "welcome to mainLand~",
  168. stage: "mainLand",
  169. steps: [
  170. {
  171. arrow: null,
  172. talkData: [
  173. {
  174. characterImage: '/static/island/npc.png',
  175. characterName: '罗宾',
  176. text: '正式欢迎您来到我们的市政厅所在的主岛....',
  177. position: 'left',
  178. isMirror: true
  179. },
  180. {
  181. characterImage: '/static/island/npc.png',
  182. characterName: '罗宾',
  183. text: '因为前几天发生了台风,所以还有两条路被倒下的树给拦住了。',
  184. position: 'left',
  185. isMirror: true
  186. },
  187. {
  188. characterImage: this.picture,
  189. characterName: this.name,
  190. text: 'ah...原来如此,你刚才说的花田有没有被拦住?',
  191. position: 'right',
  192. isMirror: false
  193. }
  194. ]
  195. },
  196. {
  197. arrow: {x: 785, y: 535, r: 270},
  198. talkData: [
  199. {
  200. characterImage: '/static/island/npc.png',
  201. characterName: '罗宾',
  202. text: '哦~瞧我这记性,花田在市政厅的旁边,就在那儿。走,我会告诉你怎么种花。',
  203. position: 'left',
  204. isMirror: true
  205. }
  206. ]
  207. }
  208. ]
  209. },
  210. {
  211. name: "flowerFarm",
  212. stage: "mainLand_farm",
  213. steps: [
  214. {
  215. arrow: null,
  216. talkData: [
  217. {
  218. characterImage: '/static/island/npc.png',
  219. characterName: '罗宾',
  220. text: '快来吧,这里就是花田了。',
  221. position: 'left',
  222. isMirror: true
  223. }
  224. ]
  225. }
  226. ]
  227. }
  228. ]
  229. },
  230. // 当前主线ID
  231. currentMainLineId: 0,
  232. // 当前主线
  233. currentMainLine: null,
  234. // 对话数据
  235. currentTalkData: [],
  236. // 当前对话索引
  237. currentTalkIndex: 0
  238. }
  239. },
  240. computed: {
  241. ...mapState('switchingModule', ['picture', 'name']),
  242. // 当前步骤
  243. currentStep() {
  244. if (!this.currentStage) return null
  245. return this.currentStage.steps[this.currentStepIndex]
  246. },
  247. // 是否是最后一步
  248. isLastStep() {
  249. if (!this.currentStage) return false
  250. return this.currentStepIndex === this.currentStage.steps.length - 1
  251. },
  252. // 高亮区域样式
  253. highlightStyle() {
  254. if (!this.currentStep) return {}
  255. return this.currentStep.rect ? {
  256. top: `${this.currentStep.rect.top}px`,
  257. left: `${this.currentStep.rect.left}px`,
  258. width: `${this.currentStep.rect.width}px`,
  259. height: `${this.currentStep.rect.height}px`
  260. } : {}
  261. },
  262. // 提示框样式
  263. tipsStyle() {
  264. if (!this.currentStep || !this.currentStep.rect) return {}
  265. const rect = this.currentStep.rect
  266. const position = this.currentStep.position
  267. const systemInfo = uni.getSystemInfoSync()
  268. let style = {}
  269. switch (position) {
  270. case 'top':
  271. style = {
  272. bottom: `${systemInfo.windowHeight - rect.top + 10}px`,
  273. left: `${rect.left + rect.width / 2}px`,
  274. transform: 'translateX(-50%)'
  275. }
  276. break
  277. case 'bottom':
  278. style = {
  279. top: `${rect.bottom + 10}px`,
  280. left: `${rect.left + rect.width / 2}px`,
  281. transform: 'translateX(-50%)'
  282. }
  283. break
  284. case 'left':
  285. style = {
  286. top: `${rect.top + rect.height / 2}px`,
  287. right: `${systemInfo.windowWidth - rect.left + 10}px`,
  288. transform: 'translateY(-50%)'
  289. }
  290. break
  291. case 'right':
  292. style = {
  293. top: `${rect.top + rect.height / 2}px`,
  294. left: `${rect.right + 10}px`,
  295. transform: 'translateY(-50%)'
  296. }
  297. break
  298. }
  299. return style
  300. },
  301. // 箭头样式
  302. arrowStyle() {
  303. if (!this.currentStep) return {}
  304. const position = this.currentStep.position
  305. let style = {}
  306. switch (position) {
  307. case 'top':
  308. style = {
  309. transform: 'rotate(180deg)',
  310. bottom: '0'
  311. }
  312. break
  313. case 'bottom':
  314. style = {
  315. transform: 'rotate(0deg)',
  316. top: '0'
  317. }
  318. break
  319. case 'left':
  320. style = {
  321. transform: 'rotate(90deg)',
  322. right: '0'
  323. }
  324. break
  325. case 'right':
  326. style = {
  327. transform: 'rotate(-90deg)',
  328. left: '0'
  329. }
  330. break
  331. }
  332. return style
  333. }
  334. },
  335. methods: {
  336. // 开始引导
  337. startGuide(stage) {
  338. // 获取当前主线
  339. const mainLine = this.guideConfig.mainLines[this.currentMainLineId];
  340. // 检查主线是否存在且场景是否匹配
  341. if (!mainLine || mainLine.stage !== stage) {
  342. return false;
  343. }
  344. // 检查主线是否已完成
  345. // if (this.completedMainLines.includes(this.currentMainLineId)) {
  346. // return false;
  347. // }
  348. // 设置当前主线
  349. this.currentMainLine = mainLine;
  350. this.currentStepIndex = 0;
  351. this.currentTalkIndex = 0;
  352. // 初始化对话数据
  353. this.initTalkData();
  354. return true;
  355. },
  356. // 设置当前主线ID
  357. setMainLineId(id) {
  358. if (id >= 0 && id < this.guideConfig.mainLines.length) {
  359. this.currentMainLineId = id;
  360. // 保存到本地存储
  361. this.saveCurrentMainLineId();
  362. }
  363. },
  364. // 保存当前主线ID
  365. saveCurrentMainLineId() {
  366. uni.setStorageSync('currentGuideMainLineId', this.currentMainLineId);
  367. },
  368. // 加载当前主线ID
  369. loadCurrentMainLineId() {
  370. const id = uni.getStorageSync('currentGuideMainLineId');
  371. if (id !== '') {
  372. this.currentMainLineId = id;
  373. }
  374. },
  375. // 初始化对话数据
  376. initTalkData() {
  377. if (this.currentMainLine && this.currentMainLine.steps[this.currentStepIndex]) {
  378. this.currentTalkData = this.currentMainLine.steps[this.currentStepIndex].talkData;
  379. }
  380. },
  381. // 下一步
  382. nextStep() {
  383. console.log("-------- next step", {
  384. currentMainLine: this.currentMainLine,
  385. currentStepIndex: this.currentStepIndex,
  386. currentTalkIndex: this.currentTalkIndex,
  387. currentTalkData: this.currentTalkData
  388. });
  389. if (!this.currentMainLine) return;
  390. // 如果当前步骤是最后一步
  391. if (this.currentStepIndex === this.currentMainLine.steps.length - 1) {
  392. this.completeMainLine();
  393. } else {
  394. // 进入下一步
  395. this.currentStepIndex++;
  396. this.currentTalkIndex = 0;
  397. this.initTalkData();
  398. }
  399. console.log("-------- next step2", {
  400. currentMainLine: this.currentMainLine,
  401. currentStepIndex: this.currentStepIndex,
  402. currentTalkIndex: this.currentTalkIndex,
  403. currentTalkData: this.currentTalkData
  404. });
  405. },
  406. // 完成主线
  407. completeMainLine() {
  408. if (this.currentMainLine) {
  409. // 添加到已完成列表
  410. // this.completedMainLines.push(this.currentMainLineId);
  411. // 保存到本地存储
  412. // this.saveCompletedMainLines();
  413. // 重置状态
  414. this.currentMainLine = null;
  415. this.currentStepIndex = 0;
  416. this.currentTalkIndex = 0;
  417. this.currentTalkData = [];
  418. // 增加主线ID
  419. this.currentMainLineId++;
  420. this.saveCurrentMainLineId();
  421. }
  422. },
  423. // 保存已完成主线
  424. // saveCompletedMainLines() {
  425. // uni.setStorageSync('completedGuideMainLines', this.completedMainLines);
  426. // },
  427. // 加载已完成主线
  428. // loadCompletedMainLines() {
  429. // const mainLines = uni.getStorageSync('completedGuideMainLines');
  430. // if (mainLines) {
  431. // this.completedMainLines = mainLines;
  432. // }
  433. // },
  434. // 获取当前箭头位置
  435. getCurrentArrowPosition() {
  436. if (!this.currentMainLine || !this.currentMainLine.steps[this.currentStepIndex]) {
  437. return null;
  438. }
  439. return this.currentMainLine.steps[this.currentStepIndex].arrow;
  440. },
  441. // 获取当前对话数据
  442. getCurrentTalkData() {
  443. if (!this.currentTalkData.length) return null;
  444. return this.currentTalkData;
  445. },
  446. // 跳过当前阶段
  447. skipStage() {
  448. this.completeStage()
  449. },
  450. // 完成当前阶段
  451. completeStage() {
  452. if (this.currentStage) {
  453. // 添加到已完成列表
  454. this.completedStages.push(this.currentStage.id)
  455. // 如果当前阶段有主线ID,检查该主线的所有阶段是否都已完成
  456. if (this.currentStage.mainLineId) {
  457. const mainLineId = this.currentStage.mainLineId
  458. const mainLineStages = Object.values(this.guideConfig).filter(
  459. stage => stage.mainLineId === mainLineId
  460. )
  461. // 检查该主线的所有阶段是否都已完成
  462. const isMainLineCompleted = mainLineStages.every(
  463. stage => this.completedStages.includes(stage.id)
  464. )
  465. // 如果主线完成,添加到已完成主线列表
  466. // if (isMainLineCompleted && !this.completedMainLines.includes(mainLineId)) {
  467. // this.completedMainLines.push(mainLineId)
  468. // }
  469. }
  470. // 保存到本地存储
  471. this.saveCompletedStages()
  472. // this.saveCompletedMainLines()
  473. // 重置状态
  474. this.currentStage = null
  475. this.currentStepIndex = 0
  476. }
  477. },
  478. // 更新位置
  479. updatePositions() {
  480. if (!this.currentStep) return
  481. const query = uni.createSelectorQuery().in(this)
  482. query.select(this.currentStep.target).boundingClientRect(data => {
  483. if (data) {
  484. // 更新当前步骤的位置信息
  485. this.$set(this.currentStage.steps[this.currentStepIndex], 'rect', data)
  486. }
  487. }).exec()
  488. },
  489. // 保存已完成阶段
  490. saveCompletedStages() {
  491. uni.setStorageSync('completedGuideStages', this.completedStages)
  492. },
  493. // 加载已完成阶段
  494. loadCompletedStages() {
  495. const stages = uni.getStorageSync('completedGuideStages')
  496. if (stages) {
  497. this.completedStages = stages
  498. }
  499. },
  500. // 处理遮罩层点击
  501. handleMaskClick() {
  502. // 可以在这里添加点击遮罩层的处理逻辑
  503. }
  504. },
  505. mounted() {
  506. // 加载已完成主线和当前主线ID
  507. // this.loadCompletedMainLines();
  508. this.loadCurrentMainLineId();
  509. //测试重置主线
  510. // this.currentMainLineId=0;
  511. }
  512. }
  513. </script>
  514. <style lang="scss" scoped>
  515. .guide-manager {
  516. .guide-mask {
  517. position: fixed;
  518. top: 0;
  519. left: 0;
  520. right: 0;
  521. bottom: 0;
  522. background-color: rgba(0, 0, 0, 0.7);
  523. z-index: 9999;
  524. .highlight-area {
  525. position: absolute;
  526. box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.7);
  527. border-radius: 4px;
  528. transition: all 0.3s ease;
  529. }
  530. .guide-tips {
  531. position: absolute;
  532. background: #fff;
  533. border-radius: 8px;
  534. padding: 16px;
  535. min-width: 200px;
  536. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  537. transition: all 0.3s ease;
  538. .tips-content {
  539. .tips-text {
  540. font-size: 14px;
  541. color: #333;
  542. line-height: 1.5;
  543. margin-bottom: 12px;
  544. }
  545. .tips-buttons {
  546. display: flex;
  547. justify-content: space-between;
  548. align-items: center;
  549. .skip-btn,
  550. .next-btn {
  551. font-size: 14px;
  552. padding: 4px 12px;
  553. border-radius: 4px;
  554. cursor: pointer;
  555. }
  556. .skip-btn {
  557. color: #999;
  558. }
  559. .next-btn {
  560. color: #fff;
  561. background-color: #1890ff;
  562. }
  563. }
  564. }
  565. .arrow {
  566. position: absolute;
  567. width: 0;
  568. height: 0;
  569. border: 8px solid transparent;
  570. border-bottom-color: #fff;
  571. }
  572. }
  573. }
  574. }
  575. </style>