GuideManager.vue 15 KB

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