GuideManager.vue 18 KB

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