Commit 6d8198f7 by liimin

feat(TimeLine): 重构时间轴组件以支持电话号码高亮和点击

重构时间轴组件,将电话号码解析逻辑从Express组件移至TimeLine组件
- 新增电话号码解析功能,支持文本和电话号码的混合渲染
- 优化数据结构,使用timeLineNodes替代nodes
- 移除Express组件中的电话号码高亮逻辑
- 调整样式以支持电话号码高亮显示
- 保持原有拨打电话功能
1 parent d97dadf7
......@@ -5,17 +5,59 @@ Component({
nodes: {
type: Array,
value: [],
}
},
methods: {
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
phoneNumber: phone,
});
}
}
}
});
\ No newline at end of file
},
},
});
......@@ -97,7 +97,6 @@
.timeline-node__content--special-active,
.timeline-node__content--special-small-active,
.timeline-node__content--special-active-small {
.timeline-node__title,
.timeline-node__tip {
font-weight: 400;
......@@ -141,6 +140,12 @@
font-weight: 400;
font-size: 24rpx;
line-height: 1.5;
.content {
white-space: pre-wrap;
.phone {
color: #fb8a32;
}
}
}
.timeline-node__content--active,
......@@ -175,4 +180,4 @@
background-color: var(--btnColor);
padding: 4rpx 12rpx;
border-radius: 24rpx;
}
\ No newline at end of file
}
<!-- 时间轴组件 -->
<view class="timeline-container">
<view wx:for="{{nodes}}" wx:key="index" class="timeline-node">
<view wx:for="{{timeLineNodes}}" wx:key="index" class="timeline-node">
<!-- 时间轴节点 -->
<view class="timeline-node__dot-wrapper">
<!-- 使用预处理的样式类 -->
......@@ -15,18 +15,16 @@
<view wx:if="{{item.operateTime}}" class="timeline-node__time">{{item.operateTime}}</view>
</view>
<view class="timeline-node__tip">
<view class="timeline-node__tip">
<rich-text nodes="{{item.content}}"></rich-text>
<!-- 手机号码点击区域 -->
<view wx:if="{{item.phones && item.phones.length > 0}}" class="phone-numbers">
<view wx:for="{{item.phones}}" wx:for-item="phone" wx:key="*this"
class="phone-number" catchtap="makePhoneCall" data-phone="{{phone}}">
<!-- <mp-icon icon="cellphone" color="var(--btnColor)" size="{{14}}"></mp-icon> -->
<text class="phone-call-text">拨打电话: {{phone}}</text>
</view>
<view class="content">
<block wx:for="{{item.content}}" wx:key="index" wx:for-item="content">
<block wx:if="{{content.type==='text'}}">{{content.txt}}</block>
<label wx:elif="{{content.type==='phone'}}"
catchtap="makePhoneCall"
data-phone="{{content.txt}}"
class="phone">{{content.txt}}</label>
</block>
</view>
</view>
</view>
</view>
</view>
</view>
\ No newline at end of file
......@@ -136,22 +136,22 @@ Component({
}
}
// 提取手机号码
const phoneRegex = /(1[3-9]\d{9}|0\d{2,3}[-]?\d{7,8})/g;
const phones = [];
let match;
while ((match = phoneRegex.exec(content)) !== null) {
phones.push(match[1]);
}
// 高亮手机号码和座机号码
const highlightedContent = content.replace(
phoneRegex,
'<span style="color: #fb8a32;">$1</span>'
);
// const phoneRegex = /(1[3-9]\d{9}|0\d{2,3}[-]?\d{7,8})/g;
// const phones = [];
// let match;
// while ((match = phoneRegex.exec(content)) !== null) {
// phones.push(match[1]);
// }
// // 高亮手机号码和座机号码
// const highlightedContent = content.replace(
// phoneRegex,
// '<span style="color: #fb8a32;" bindtap="makePhoneCall" data-phone="$1">$1</span>'
// );
return {
...track,
content: highlightedContent,
phones: phones, // 添加提取的手机号码数组
content,
// phones: phones, // 添加提取的手机号码数组
operateTime,
type,
status,
......@@ -194,7 +194,6 @@ Component({
console.warn("Express组件: expressInfo参数无效");
return;
}
const {
logisticsStatus,
shipmentTrack,
......
......@@ -71,7 +71,7 @@ Page({
receiveMobile,
} = data.receiveAddress;
const receiveAddress = provinceName + cityName + countyName + address;
const receiveInfo = receiveName + receiveMobile;
const receiveInfo = `${receiveName} ${receiveMobile}`;
this.setData({
orderInfo: {
...data,
......@@ -79,7 +79,7 @@ Page({
receiveAddress,
receiveInfo,
},
receiver: `${receiveAddress}<br/>${receiveInfo}`,
receiver: `${receiveAddress}\n${receiveInfo}`,
// 如果有expressInfo也一并设置
...(data.expressName && this.data.orderInfo.goodsSource == GoodsSource.SELF ? {
expressInfo: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!