dk-guide.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*******************************************************************************
  2. * Copyright(c) 2022 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:
  5. * 2.功能描述:dk-guide 新手引导
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * admin 2024-05-14 1.00 新建
  9. *******************************************************************************/
  10. Component({
  11. /**
  12. * 组件的对外属性
  13. */
  14. properties: {
  15. step: {
  16. type: Object,
  17. default: () => { },
  18. },
  19. },
  20. data: {
  21. stepName: "step", //该提示步骤的名称,用于不在重复展示
  22. guideList: [],
  23. index: 0, // 当前展示的索引
  24. showGuide: true, // 是否显示引导
  25. guideStyle: "", // 默认样式
  26. arrowTop: "", //步骤提示三角形的定位
  27. tipPosition: "", //步骤提示的定位
  28. systemInfo: "", //屏幕宽度高度等信息
  29. tipWidth: 200, //步骤提示默认的宽度
  30. },
  31. lifetimes: {
  32. attached: function () {
  33. const { step } = this.properties;
  34. let { guideList, stepName } = this.data;
  35. guideList = step.guideList;
  36. stepName = step.name;
  37. // const systemInfo = wx.getSystemInfoSync();
  38. // systemInfo = systemInfo;
  39. this.setData({
  40. guideList,
  41. stepName,
  42. systemInfo: wx.getSystemInfoSync(),
  43. });
  44. const guide = wx.getStorageSync(step.name);
  45. if (!guide) {
  46. this.getDomInfo();
  47. } else {
  48. this.setData({
  49. showGuide: false,
  50. });
  51. // this.showGuide = false;
  52. }
  53. },
  54. detached: function () {
  55. // 在组件实例被从页面节点树移除时执行
  56. },
  57. },
  58. /**
  59. * 组件的方法列表
  60. */
  61. methods: {
  62. // 展示新手提示
  63. viewTips(data, scrollTop) {
  64. let {
  65. systemInfo,
  66. tipWidth,
  67. index,
  68. guideList,
  69. arrowTop,
  70. tipPosition,
  71. guideStyle,
  72. } = this.data;
  73. if (data) {
  74. // 如果dom宽度大于或者等于窗口宽度,需要重新调整dom展示宽度
  75. let newWidth = systemInfo.windowWidth - 20;
  76. if (data.width >= newWidth) {
  77. data.width = newWidth;
  78. }
  79. // 如果距离左边为0,自动增加一点左边距
  80. if (data.left == 0) {
  81. data.left = 10;
  82. }
  83. let domRW = systemInfo.windowWidth - data.left;
  84. let left = 0;
  85. // 如果dom距离右边没有tips的宽度大的话,就要让tips向左便宜
  86. if (domRW < tipWidth) {
  87. left = domRW - tipWidth - 30;
  88. }
  89. // const index = index;
  90. // 步骤条展示的高度需要加上屏幕滚动的高度
  91. // data.top += scrollTop;
  92. // // 根据实际情况需要滚动到展示区域
  93. // wx.pageScrollTo({
  94. // scrollTop: data.top > 20 ? data.top - 20 : 0,
  95. // duration: 100,
  96. // });
  97. let obj = Object.assign(guideList[index], data);
  98. // 设置三角形高度
  99. let arrArrowTop = data.height + 9;
  100. // arrowTop = "top:" + arrArrowTop + "px;";
  101. arrowTop = "bottom:" + arrArrowTop + "px;";
  102. // 设置提示框定位
  103. // tipPosition = "top:" + (arrArrowTop + 5) + "px;left:" + left + "px;";
  104. tipPosition = "bottom:" + (arrArrowTop + 5) + "px;left:" + left + "px;";
  105. // 显示在下方
  106. if(data.top < 30){
  107. arrowTop = "top:" + arrArrowTop + "px;background: #1cbbb4";
  108. tipPosition = "top:" + (arrArrowTop + 5) + "px;left:" + left + "px;";
  109. }
  110. if(guideList[index].fixed){
  111. tipPosition += "position:fixed";
  112. }
  113. // 重新设置guideList的值
  114. guideList.splice(index, 1, obj);
  115. guideStyle = this.getStyle();
  116. if(guideList[index].fixed){
  117. guideStyle += ";position:fixed";
  118. }
  119. this.setData({
  120. arrowTop,
  121. tipPosition,
  122. guideList,
  123. guideStyle,
  124. });
  125. } else {
  126. index += 1;
  127. this.setData({
  128. index,
  129. });
  130. this.getDomInfo();
  131. }
  132. },
  133. // 获取步骤提示的主要样式
  134. getStyle() {
  135. const { guideList, index } = this.data;
  136. const { width, height, left, top, style } = guideList[index];
  137. let newstyle = "width:" + width + "px;";
  138. newstyle += "height:" + height + "px;";
  139. newstyle += "left:" + left + "px;";
  140. newstyle += "top:" + top + "px;";
  141. newstyle +=
  142. "box-shadow: rgb(33 33 33 / 80%) 0px 0px 0px 0px, rgb(33 33 33 / 50%) 0px 0px 0px 5000px;border-radius:8rpx:margin:0";
  143. newstyle += style;
  144. return newstyle;
  145. },
  146. // 获取dom信息
  147. getDomInfo() {
  148. const { guideList, index } = this.data;
  149. const { el } = guideList[index];
  150. // const query = wx.createSelectorQuery().in(this.$root);
  151. let query = wx.createSelectorQuery();
  152. setTimeout(() => {
  153. query.select(el).boundingClientRect();
  154. query.selectViewport().scrollOffset();
  155. var _this = this;
  156. query.exec(function (res) {
  157. let data = res[0]; // #the-id节点的上边界坐标
  158. // data.height = data.height?data.height:'88rpx'
  159. // data.width= data.width?data.width:'100%'
  160. let scrollTop = res[1].scrollTop; // 显示区域的竖直滚动位置
  161. _this.viewTips(data, scrollTop);
  162. });
  163. }, 10);
  164. },
  165. skip() {
  166. this.setData({
  167. showGuide: false,
  168. });
  169. wx.setStorageSync(this.data.stepName, "true");
  170. },
  171. // 下一步
  172. next() {
  173. let { index, guideList, stepName } = this.data;
  174. if (index === guideList.length - 1) {
  175. this.setData({
  176. showGuide: false,
  177. });
  178. wx.setStorageSync(stepName, "true");
  179. } else {
  180. index += 1;
  181. this.setData({
  182. index,
  183. });
  184. this.getDomInfo();
  185. }
  186. },
  187. },
  188. })