TimeLine.js
1.56 KB
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
/*可视化地呈现时间流信息*/
Component({
properties: {
// 时间轴节点数据
nodes: {
type: Array,
value: [],
observer(newVal) {
this.formatContent(newVal);
},
},
},
data: {
timeLineNodes: [],
},
methods: {
formatContent(nodes) {
nodes.forEach((item) => {
if (item.type == "special") {
item.content = [{ type: "text", txt: item.content }];
} else {
item.content = this.parsePhone(item.content);
}
});
this.setData({
timeLineNodes: nodes,
});
},
// 把原始文本切成 text | phone 两种节点
parsePhone(str) {
if(!str){
return [];
}
const phoneReg = /(1[3-9]\d{9}|0\d{2,3}[-]?\d{7,8})/g; // 简易手机号正则
let lastIndex = 0;
const res = [];
let match;
while ((match = phoneReg.exec(str)) !== null) {
// 1. match 之前的纯文本
if (match.index > lastIndex) {
res.push({ type: "text", txt: str.slice(lastIndex, match.index) });
}
// 2. 手机号本身
res.push({ type: "phone", txt: match[0] });
lastIndex = match.index + match[0].length;
}
// 3. 尾部剩余文本
if (lastIndex < str.length) {
res.push({ type: "text", txt: str.slice(lastIndex) });
}
return res;
},
// 拨打电话
makePhoneCall(e) {
const phone = e.currentTarget.dataset.phone;
if (phone) {
wx.makePhoneCall({
phoneNumber: phone,
});
}
},
},
});