GuideManager.vue 16 KB

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