This commit is contained in:
Your Name
2026-03-11 09:49:47 +08:00
parent 02ae537b4c
commit 38ad60f4bb
290 changed files with 36917 additions and 123 deletions
+67
View File
@@ -0,0 +1,67 @@
export function removeC2C(str: string): string {
return str.replace(/C2C/g, '');
}
export function isJSON(str: string) {
if (typeof str === 'string') {
try {
const data = JSON.parse(str);
if (data) {
return true;
}
return false;
} catch (error) {
return false;
}
}
return false;
}
export function JSONToObject(str: string) {
if (!str || !isJSON(str)) {
return str;
}
return JSON.parse(str);
};
export function formatDuration(seconds: number): string {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return [
minutes.toString().padStart(2, '0'),
secs.toString().padStart(2, '0')
].join(':');
}
export function deepCopy(data: any, hash = new WeakMap()) {
if (typeof data !== 'object' || data === null || data === undefined) {
return data;
}
if (hash.has(data)) {
return hash.get(data);
}
const newData: any = Object.create(Object.getPrototypeOf(data));
const dataKeys = Object.keys(data);
dataKeys.forEach((value) => {
const currentDataValue = data[value];
if (typeof currentDataValue !== 'object' || currentDataValue === null) {
newData[value] = currentDataValue;
} else if (Array.isArray(currentDataValue)) {
newData[value] = [...currentDataValue];
} else if (currentDataValue instanceof Set) {
newData[value] = new Set([...currentDataValue]);
} else if (currentDataValue instanceof Map) {
newData[value] = new Map([...currentDataValue]);
} else {
hash.set(data, data);
newData[value] = deepCopy(currentDataValue, hash);
}
});
return newData;
}
export function shallowCopyMessage(message) {
return Object.assign({}, message);
}
@@ -0,0 +1,60 @@
import { formatDuration, JSONToObject } from './index';
export function isCallSignaling(message: any) {
const callMessage: any = JSONToObject(message.payload.data);
if (callMessage?.actionType) {
return true;
}
return false;
}
export function handleCallKitSignaling(message: any) {
if (!message || !message.payload) {
return { callTip: '', callType: '' }
}
const callMessage: any = JSONToObject(message.payload.data);
const objectData = JSONToObject(callMessage?.data);
const callType = objectData.call_type;
let callTip = '';
switch (callMessage?.actionType) {
case 1: {
if (objectData?.data?.cmd === 'hangup') {
callTip = `通话时长: ${formatDuration(Number(objectData?.call_end))}`;
} else {
callTip = "发起通话";
}
}
break;
case 2:
callTip = message?.flow === 'out' ? "已取消" : "对方已取消";
break;
case 4:
if (objectData?.line_busy === 'line_busy' || objectData?.data?.message === 'lineBusy') {
if (message?.flow === 'out') {
callTip = "对方忙线中";
} else {
callTip = "忙线未接听";
}
} else {
if (message?.flow === 'out') {
callTip = "对方已拒绝";
} else {
callTip = "已拒绝";
}
}
break;
case 5:
callTip = message?.flow === 'out' ? "对方无应答" : "超时无应答";
break;
default:
callTip = '';
break;
}
return {
callTip,
callType
}
}
+59
View File
@@ -0,0 +1,59 @@
export function calculateTimestamp(timestamp: number): string {
const todayZero = new Date().setHours(0, 0, 0, 0);
const thisYear = new Date(
new Date().getFullYear(),
0,
1,
0,
0,
0,
0,
).getTime();
const target = new Date(timestamp*1000);
const oneDay = 24 * 60 * 60 * 1000;
const oneWeek = 7 * oneDay;
const diff = todayZero - target.getTime();
function formatNum(num: number): string {
return num < 10 ? '0' + num : num.toString();
}
if (diff <= 0) {
// today, only display hour:minute
return `${formatNum(target.getHours())}:${formatNum(target.getMinutes())}`;
} else if (diff <= oneDay) {
// yesterday, display yesterday:hour:minute
return `昨天 ${formatNum(
target.getHours(),
)}:${formatNum(target.getMinutes())}`;
} else if (diff <= oneWeek - oneDay) {
// Within a week, display weekday hour:minute
const weekdays = [
'星期日',
'星期一',
'星期二',
'星期三',
'星期四',
'星期五',
'星期六',
];
const weekday = weekdays[target.getDay()];
return `${weekday} ${formatNum(
target.getHours(),
)}:${formatNum(target.getMinutes())}`;
} else if (target.getTime() >= thisYear) {
// Over a week, within this year, display mouth/day hour:minute
return `${target.getMonth() + 1}${target.getDate()}${formatNum(
target.getHours(),
)}:${formatNum(target.getMinutes())}`;
} else {
// Not within this year, display year/mouth/day hour:minute
return `${target.getFullYear()}${
target.getMonth() + 1
}${target.getDate()}${formatNum(target.getHours())}:${formatNum(
target.getMinutes(),
)}`;
}
}