cl-guide.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <view class="cl-guide">
  3. <slot></slot>
  4. <!-- 遮罩层 -->
  5. <view
  6. class="cl-guide__mask"
  7. :style="{
  8. 'mix-blend-mode': mode,
  9. }"
  10. v-if="visible"
  11. >
  12. <!-- 显示区域 -->
  13. <view
  14. class="cl-guide__display"
  15. :style="[display.style, step.style]"
  16. @tap="onTap"
  17. ></view>
  18. <!-- 工具 -->
  19. <view
  20. class="cl-guide__tools"
  21. :class="[step.justify]"
  22. :style="[tools.style]"
  23. v-if="step"
  24. >
  25. <slot name="tools" :step="step" :current="current">
  26. <!-- 图片 -->
  27. <image
  28. class="cl-guide__image"
  29. :src="step.image.url"
  30. :style="[step.image.style]"
  31. v-if="step.image"
  32. />
  33. <!-- 内容 -->
  34. <view class="cl-guide__content" v-if="step.content">
  35. {{ step.content }}
  36. </view>
  37. <!-- 按钮组 -->
  38. <view class="cl-guide__op is-left">
  39. <!-- 上一步按钮 -->
  40. <button @tap="toPrev" v-if="isPrev && step.showPrev">
  41. {{ step.prevText || "上一步" }}
  42. </button>
  43. <!-- 下一步按钮 -->
  44. <button @tap="toNext" v-if="isNext && step.showNext">
  45. {{ step.nextText || "下一步" }}
  46. </button>
  47. <!-- 跳过按钮 -->
  48. <button @tap="toSkip" v-if="step.showSkip">
  49. {{ step.skipText || "跳过" }}
  50. </button>
  51. <!-- 完成按钮 -->
  52. <button @tap="toDone" v-if="!isNext">{{ step.doneText || "完成" }}</button>
  53. </view>
  54. </slot>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. const { windowHeight } = uni.getSystemInfoSync();
  61. /**
  62. * guide 操作引导
  63. * @description 步骤引导
  64. * @tutorial https://docs.cool-js.com/uni/components/advanced/guide.html
  65. * @property {Number} value 当前步骤序号
  66. * @property {String} mode mix-blend-mode 选项 hard-light | darken
  67. * @event {Function} change 切换步骤时触发
  68. * @event {Function} done 完成时触发
  69. * @event {Function} skip 跳过时触发
  70. * @example <cl-guide ref="guide" />
  71. */
  72. export default {
  73. name: "cl-guide",
  74. props: {
  75. value: {
  76. type: Number,
  77. default: 0,
  78. },
  79. mode: {
  80. type: String,
  81. default: "hard-light",
  82. },
  83. },
  84. data() {
  85. return {
  86. visible: false,
  87. current: 0,
  88. steps: [],
  89. display: {
  90. style: {},
  91. },
  92. tools: {
  93. style: {},
  94. },
  95. };
  96. },
  97. watch: {
  98. value: {
  99. immediate: true,
  100. handler(v) {
  101. this.current = v;
  102. },
  103. },
  104. },
  105. computed: {
  106. step() {
  107. let step = this.steps[this.current];
  108. if (step) {
  109. if (step.showPrev === undefined) {
  110. step.showPrev = true;
  111. }
  112. if (step.showNext === undefined) {
  113. step.showNext = true;
  114. }
  115. }
  116. return step;
  117. },
  118. isPrev() {
  119. return this.current > 0;
  120. },
  121. isNext() {
  122. return this.current < this.steps.length - 1;
  123. },
  124. },
  125. methods: {
  126. // 设置数据
  127. defineSteps(steps) {
  128. this.steps = steps;
  129. },
  130. // 开始
  131. start(index) {
  132. this.visible = true;
  133. this.$nextTick(() => {
  134. this.current = index || 0;
  135. this.init();
  136. });
  137. },
  138. // 关闭
  139. close() {
  140. this.visible = false;
  141. },
  142. // 重置
  143. reset() {
  144. this.current = 0;
  145. },
  146. // 初始化
  147. init() {
  148. return Promise.all(
  149. this.steps.map((e) => {
  150. return new Promise((resolve) => {
  151. uni.createSelectorQuery()
  152. .select(e.selector)
  153. .boundingClientRect((res) => {
  154. e.rect = res;
  155. resolve(e);
  156. })
  157. .exec();
  158. });
  159. })
  160. ).then(() => {
  161. this.updateStyle();
  162. });
  163. },
  164. // 更新显示区域样式,工具栏样式
  165. updateStyle() {
  166. let { height, width, left, top } = this.step.rect || {};
  167. this.display.style = {
  168. height: height + "px",
  169. width: width + "px",
  170. marginLeft: left + "px",
  171. marginTop: top + "px",
  172. "background-color": this.mode == "hard-light" ? "gray" : "#fff",
  173. };
  174. this.$nextTick(() => {
  175. uni.createSelectorQuery()
  176. .in(this)
  177. .select(".cl-guide__tools")
  178. .boundingClientRect((res) => {
  179. let _top = top + height;
  180. if (top + height + res.height > windowHeight) {
  181. _top = top - res.height - 10;
  182. }
  183. this.tools.style = {
  184. top: _top + "px",
  185. };
  186. })
  187. .exec();
  188. });
  189. },
  190. // 上一步
  191. async toPrev() {
  192. let prev = () => {
  193. if (this.current > 0) {
  194. this.current -= 1;
  195. this.onChange();
  196. }
  197. };
  198. if (this.step.onPrev) {
  199. await this.step.onPrev({
  200. prev,
  201. next: this.toNext,
  202. skip: this.toSkip,
  203. done: this.toDone,
  204. current: this.current,
  205. step: this.step,
  206. });
  207. } else {
  208. prev();
  209. }
  210. },
  211. // 下一步
  212. async toNext() {
  213. let next = () => {
  214. if (this.current < this.steps.length - 1) {
  215. this.current += 1;
  216. this.onChange();
  217. }
  218. };
  219. if (this.step.onNext) {
  220. await this.step.onNext({
  221. next,
  222. prev: this.toPrev,
  223. skip: this.toSkip,
  224. done: this.toDone,
  225. current: this.current,
  226. step: this.step,
  227. });
  228. } else {
  229. next();
  230. }
  231. },
  232. // 跳过
  233. toSkip() {
  234. this.close();
  235. this.$emit("skip", this.current);
  236. },
  237. // 完成
  238. toDone() {
  239. this.close();
  240. this.$emit("done", this.step);
  241. },
  242. // 点击
  243. onTap() {
  244. if (this.step.onClick) {
  245. this.step.onClick({
  246. next: this.toNext,
  247. prev: this.toPrev,
  248. skip: this.toSkip,
  249. done: this.toDone,
  250. current: this.current,
  251. step: this.step,
  252. });
  253. }
  254. },
  255. // 切换
  256. onChange() {
  257. this.updateStyle();
  258. this.$emit("change", this.current);
  259. },
  260. },
  261. };
  262. </script>