TimeLine.js 1.56 KB
/*可视化地呈现时间流信息*/
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,
        });
      }
    },
  },
});