路泰科技体检小程序UI设计新版本
1
wwl
5 天以前 a6cdbcfe28fcc40ebb4919f57d60fb20122e8e57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 * @fileoverview search 插件
 */
function Search (vm) {
  /**
   * @description 关键词搜索
   * @param {regexp|string} key 要搜索的关键词
   * @param {boolean} anchor 是否将搜索结果设置为锚点
   * @param {string} style 搜索结果的样式
   */
  vm.search = function (key, anchor, style = 'background-color:yellow') {
    const res = []
    const stack = [];
 
    // 遍历搜索
    (function traversal (nodes) {
      for (let i = 0; i < nodes.length; i++) {
        let node = nodes[i]
        if (node.type === 'text' && key) {
          const text = node.text
          const arr = text.split(key)
          if (arr.length > 1) {
            node = {
              name: 'span',
              attrs: {},
              type: 'node',
              c: 1,
              s: 1,
              children: []
            }
            vm.$set(nodes, i, node)
            for (let j = 0; j < arr.length; j++) {
              if (arr[j]) {
                node.children.push({
                  type: 'text',
                  text: arr[j]
                })
              }
              if (j !== arr.length - 1) {
                // 关键词转为一个 span
                node.children.push({
                  name: 'span',
                  attrs: {
                    id: anchor ? 'search' + (res.length + 1) : undefined, // 用于锚点的 id
                    style: style
                  },
                  // #ifdef VUE3
                  c: 1,
                  // #endif
                  children: [{
                    type: 'text',
                    text: key instanceof RegExp ? key.exec(text)[0] : key
                  }]
                })
                res.push(node.children[node.children.length - 1].attrs)
              }
            }
            if (key instanceof RegExp) {
              key.exec(text)
            }
            if (anchor) {
              for (let l = stack.length; l--;) {
                if (stack[l].c) {
                  break
                } else {
                  vm.$set(stack[l], 'c', 1)
                }
              }
            }
          }
        } else if (node.s) {
          let text = ''
          // 复原上一次的结果
          for (let k = 0; k < node.children.length; k++) {
            const child = node.children[k]
            if (child.text) {
              text += child.text
            } else {
              text += child.children[0].text
            }
          }
          vm.$set(nodes, i, {
            type: 'text',
            text
          })
          if (key && (key instanceof RegExp ? key.test(text) : text.includes(key))) {
            i--
          }
        } else if (node.children) {
          stack.push(node)
          traversal(node.children)
          stack.pop()
        }
      }
    })(vm.nodes)
 
    return new Promise(function (resolve) {
      setTimeout(() => {
        resolve({
          num: res.length, // 结果数量
          /**
           * @description 高亮某一个结果
           * @param {number} i 第几个
           * @param {string} hlstyle 高亮的样式
           */
          highlight (i, hlstyle = 'background-color:#FF9632') {
            if (i < 1 || i > res.length) return
            if (this.last) {
              res[this.last - 1].style = style
            }
            this.last = i
            res[i - 1].style = hlstyle
          },
          /**
           * @description 跳转到搜索结果
           * @param {number} i 第几个
           * @param {number} offset 偏移量
           */
          jump: anchor
            ? (i, offset) => {
                if (i > 0 && i <= res.length) {
                  vm.navigateTo('search' + i, offset)
                }
              }
            : undefined
        })
      }, 200)
    })
  }
}
 
module.exports = Search