dk-guide.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. console.log(this.properties);
  34. const { step } = this.properties;
  35. let { guideList, stepName } = this.data;
  36. guideList = step.guideList;
  37. stepName = step.name;
  38. // const systemInfo = wx.getSystemInfoSync();
  39. // systemInfo = systemInfo;
  40. this.setData({
  41. guideList,
  42. stepName,
  43. systemInfo: wx.getSystemInfoSync(),
  44. });
  45. const guide = wx.getStorageSync(step.name);
  46. if (!guide) {
  47. this.getDomInfo();
  48. } else {
  49. this.setData({
  50. showGuide: false,
  51. });
  52. // this.showGuide = false;
  53. }
  54. },
  55. detached: function () {
  56. // 在组件实例被从页面节点树移除时执行
  57. },
  58. },
  59. /**
  60. * 组件的方法列表
  61. */
  62. methods: {
  63. // 展示新手提示
  64. viewTips(data, scrollTop) {
  65. console.log('view',data, scrollTop);
  66. let {
  67. systemInfo,
  68. tipWidth,
  69. index,
  70. guideList,
  71. arrowTop,
  72. tipPosition,
  73. guideStyle,
  74. } = this.data;
  75. if (data) {
  76. console.log('b',systemInfo,data,scrollTop,data.top);
  77. // 如果dom宽度大于或者等于窗口宽度,需要重新调整dom展示宽度
  78. let newWidth = systemInfo.windowWidth - 20;
  79. if (data.width >= newWidth) {
  80. data.width = newWidth;
  81. }
  82. // 如果距离左边为0,自动增加一点左边距
  83. if (data.left == 0) {
  84. data.left = 10;
  85. }
  86. let domRW = systemInfo.windowWidth - data.left;
  87. let left = 0;
  88. // 如果dom距离右边没有tips的宽度大的话,就要让tips向左便宜
  89. if (domRW < tipWidth) {
  90. left = domRW - tipWidth - 30;
  91. }
  92. // const index = index;
  93. // 步骤条展示的高度需要加上屏幕滚动的高度
  94. // data.top += scrollTop;
  95. // // 根据实际情况需要滚动到展示区域
  96. // wx.pageScrollTo({
  97. // scrollTop: data.top > 20 ? data.top - 20 : 0,
  98. // duration: 100,
  99. // });
  100. let obj = Object.assign(guideList[index], data);
  101. // 设置三角形高度
  102. let arrArrowTop = data.height + 9;
  103. // arrowTop = "top:" + arrArrowTop + "px;";
  104. arrowTop = "bottom:" + arrArrowTop + "px;";
  105. // 设置提示框定位
  106. // tipPosition = "top:" + (arrArrowTop + 5) + "px;left:" + left + "px;";
  107. tipPosition = "bottom:" + (arrArrowTop + 5) + "px;left:" + left + "px;";
  108. // 显示在下方
  109. if(data.top < 30){
  110. arrowTop = "top:" + arrArrowTop + "px;background: #1cbbb4";
  111. tipPosition = "top:" + (arrArrowTop + 5) + "px;left:" + left + "px;";
  112. }
  113. if(guideList[index].fixed){
  114. tipPosition += "position:fixed";
  115. }
  116. // 重新设置guideList的值
  117. guideList.splice(index, 1, obj);
  118. guideStyle = this.getStyle();
  119. if(guideList[index].fixed){
  120. guideStyle += ";position:fixed";
  121. }
  122. console.log(arrowTop, tipPosition, guideList, guideStyle);
  123. this.setData({
  124. arrowTop,
  125. tipPosition,
  126. guideList,
  127. guideStyle,
  128. });
  129. } else {
  130. index += 1;
  131. this.setData({
  132. index,
  133. });
  134. this.getDomInfo();
  135. }
  136. },
  137. // 获取步骤提示的主要样式
  138. getStyle() {
  139. const { guideList, index } = this.data;
  140. const { width, height, left, top, style } = guideList[index];
  141. let newstyle = "width:" + width + "px;";
  142. newstyle += "height:" + height + "px;";
  143. newstyle += "left:" + left + "px;";
  144. newstyle += "top:" + top + "px;";
  145. newstyle +=
  146. "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";
  147. newstyle += style;
  148. return newstyle;
  149. },
  150. // 获取dom信息
  151. getDomInfo() {
  152. const { guideList, index } = this.data;
  153. console.log(guideList, index);
  154. const { el } = guideList[index];
  155. console.log(el);
  156. // const query = wx.createSelectorQuery().in(this.$root);
  157. let query = wx.createSelectorQuery();
  158. setTimeout(() => {
  159. query.select(el).boundingClientRect();
  160. query.selectViewport().scrollOffset();
  161. var _this = this;
  162. query.exec(function (res) {
  163. console.log("打印demo的元素的信息", res);
  164. let data = res[0]; // #the-id节点的上边界坐标
  165. // data.height = data.height?data.height:'88rpx'
  166. // data.width= data.width?data.width:'100%'
  167. let scrollTop = res[1].scrollTop; // 显示区域的竖直滚动位置
  168. _this.viewTips(data, scrollTop);
  169. });
  170. }, 10);
  171. },
  172. skip() {
  173. this.setData({
  174. showGuide: false,
  175. });
  176. wx.setStorageSync(this.data.stepName, "true");
  177. },
  178. // 下一步
  179. next() {
  180. let { index, guideList, stepName } = this.data;
  181. if (index === guideList.length - 1) {
  182. this.setData({
  183. showGuide: false,
  184. });
  185. wx.setStorageSync(stepName, "true");
  186. } else {
  187. index += 1;
  188. this.setData({
  189. index,
  190. });
  191. this.getDomInfo();
  192. }
  193. },
  194. },
  195. })