GuideManager.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. guideConfig: {
  35. // 主岛引导
  36. mainLand: {
  37. id: 'mainLand',
  38. steps: [
  39. {
  40. target: '.house-image',
  41. tips: '这是你的房屋,点击可以进入查看',
  42. position: 'bottom'
  43. },
  44. {
  45. target: '.farm-image',
  46. tips: '这是农场,可以种植作物',
  47. position: 'bottom'
  48. },
  49. {
  50. target: '.shop-image',
  51. tips: '这是商店,可以购买物品',
  52. position: 'bottom'
  53. }
  54. ]
  55. },
  56. // 家园引导
  57. homeLand: {
  58. id: 'homeLand',
  59. steps: [
  60. {
  61. target: '.house-image',
  62. tips: '这是你的家,点击可以进入',
  63. position: 'bottom'
  64. },
  65. {
  66. target: '.table-image',
  67. tips: '这是工作台,可以制作物品',
  68. position: 'bottom'
  69. },
  70. {
  71. target: '.taskBoard-image',
  72. tips: '这是任务板,可以查看任务',
  73. position: 'bottom'
  74. }
  75. ]
  76. }
  77. }
  78. }
  79. },
  80. computed: {
  81. // 当前步骤
  82. currentStep() {
  83. if (!this.currentStage) return null
  84. return this.currentStage.steps[this.currentStepIndex]
  85. },
  86. // 是否是最后一步
  87. isLastStep() {
  88. if (!this.currentStage) return false
  89. return this.currentStepIndex === this.currentStage.steps.length - 1
  90. },
  91. // 高亮区域样式
  92. highlightStyle() {
  93. if (!this.currentStep) return {}
  94. return this.currentStep.rect ? {
  95. top: `${this.currentStep.rect.top}px`,
  96. left: `${this.currentStep.rect.left}px`,
  97. width: `${this.currentStep.rect.width}px`,
  98. height: `${this.currentStep.rect.height}px`
  99. } : {}
  100. },
  101. // 提示框样式
  102. tipsStyle() {
  103. if (!this.currentStep || !this.currentStep.rect) return {}
  104. const rect = this.currentStep.rect
  105. const position = this.currentStep.position
  106. const systemInfo = uni.getSystemInfoSync()
  107. let style = {}
  108. switch (position) {
  109. case 'top':
  110. style = {
  111. bottom: `${systemInfo.windowHeight - rect.top + 10}px`,
  112. left: `${rect.left + rect.width / 2}px`,
  113. transform: 'translateX(-50%)'
  114. }
  115. break
  116. case 'bottom':
  117. style = {
  118. top: `${rect.bottom + 10}px`,
  119. left: `${rect.left + rect.width / 2}px`,
  120. transform: 'translateX(-50%)'
  121. }
  122. break
  123. case 'left':
  124. style = {
  125. top: `${rect.top + rect.height / 2}px`,
  126. right: `${systemInfo.windowWidth - rect.left + 10}px`,
  127. transform: 'translateY(-50%)'
  128. }
  129. break
  130. case 'right':
  131. style = {
  132. top: `${rect.top + rect.height / 2}px`,
  133. left: `${rect.right + 10}px`,
  134. transform: 'translateY(-50%)'
  135. }
  136. break
  137. }
  138. return style
  139. },
  140. // 箭头样式
  141. arrowStyle() {
  142. if (!this.currentStep) return {}
  143. const position = this.currentStep.position
  144. let style = {}
  145. switch (position) {
  146. case 'top':
  147. style = {
  148. transform: 'rotate(180deg)',
  149. bottom: '0'
  150. }
  151. break
  152. case 'bottom':
  153. style = {
  154. transform: 'rotate(0deg)',
  155. top: '0'
  156. }
  157. break
  158. case 'left':
  159. style = {
  160. transform: 'rotate(90deg)',
  161. right: '0'
  162. }
  163. break
  164. case 'right':
  165. style = {
  166. transform: 'rotate(-90deg)',
  167. left: '0'
  168. }
  169. break
  170. }
  171. return style
  172. }
  173. },
  174. methods: {
  175. // 开始引导
  176. startGuide(stageId) {
  177. // 检查是否已完成
  178. if (this.completedStages.includes(stageId)) {
  179. return
  180. }
  181. // 设置当前阶段
  182. this.currentStage = this.guideConfig[stageId]
  183. this.currentStepIndex = 0
  184. // 等待DOM更新后更新位置
  185. this.$nextTick(() => {
  186. this.updatePositions()
  187. })
  188. },
  189. // 下一步
  190. nextStep() {
  191. if (this.isLastStep) {
  192. this.completeStage()
  193. } else {
  194. this.currentStepIndex++
  195. this.$nextTick(() => {
  196. this.updatePositions()
  197. })
  198. }
  199. },
  200. // 跳过当前阶段
  201. skipStage() {
  202. this.completeStage()
  203. },
  204. // 完成当前阶段
  205. completeStage() {
  206. if (this.currentStage) {
  207. // 添加到已完成列表
  208. this.completedStages.push(this.currentStage.id)
  209. // 保存到本地存储
  210. this.saveCompletedStages()
  211. // 重置状态
  212. this.currentStage = null
  213. this.currentStepIndex = 0
  214. }
  215. },
  216. // 更新位置
  217. updatePositions() {
  218. if (!this.currentStep) return
  219. const query = uni.createSelectorQuery().in(this)
  220. query.select(this.currentStep.target).boundingClientRect(data => {
  221. if (data) {
  222. // 更新当前步骤的位置信息
  223. this.$set(this.currentStage.steps[this.currentStepIndex], 'rect', data)
  224. }
  225. }).exec()
  226. },
  227. // 保存已完成阶段
  228. saveCompletedStages() {
  229. uni.setStorageSync('completedGuideStages', this.completedStages)
  230. },
  231. // 加载已完成阶段
  232. loadCompletedStages() {
  233. const stages = uni.getStorageSync('completedGuideStages')
  234. if (stages) {
  235. this.completedStages = stages
  236. }
  237. },
  238. // 处理遮罩层点击
  239. handleMaskClick() {
  240. // 可以在这里添加点击遮罩层的处理逻辑
  241. }
  242. },
  243. mounted() {
  244. // 加载已完成阶段
  245. this.loadCompletedStages()
  246. }
  247. }
  248. </script>
  249. <style lang="scss" scoped>
  250. .guide-manager {
  251. .guide-mask {
  252. position: fixed;
  253. top: 0;
  254. left: 0;
  255. right: 0;
  256. bottom: 0;
  257. background-color: rgba(0, 0, 0, 0.7);
  258. z-index: 9999;
  259. .highlight-area {
  260. position: absolute;
  261. box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.7);
  262. border-radius: 4px;
  263. transition: all 0.3s ease;
  264. }
  265. .guide-tips {
  266. position: absolute;
  267. background: #fff;
  268. border-radius: 8px;
  269. padding: 16px;
  270. min-width: 200px;
  271. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  272. transition: all 0.3s ease;
  273. .tips-content {
  274. .tips-text {
  275. font-size: 14px;
  276. color: #333;
  277. line-height: 1.5;
  278. margin-bottom: 12px;
  279. }
  280. .tips-buttons {
  281. display: flex;
  282. justify-content: space-between;
  283. align-items: center;
  284. .skip-btn,
  285. .next-btn {
  286. font-size: 14px;
  287. padding: 4px 12px;
  288. border-radius: 4px;
  289. cursor: pointer;
  290. }
  291. .skip-btn {
  292. color: #999;
  293. }
  294. .next-btn {
  295. color: #fff;
  296. background-color: #1890ff;
  297. }
  298. }
  299. }
  300. .arrow {
  301. position: absolute;
  302. width: 0;
  303. height: 0;
  304. border: 8px solid transparent;
  305. border-bottom-color: #fff;
  306. }
  307. }
  308. }
  309. }
  310. </style>