From 852d327076e0036eedda9c07dc45de92eb457da5 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Sun, 30 Jul 2023 10:22:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9AwebParams=20?= =?UTF-8?q?=E7=9A=84=20`[receive=5Ftime]`=20=E6=A0=87=E7=AD=BE=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E6=97=B6=E9=97=B4=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=20#327?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forwarder/utils/sender/WebhookUtils.kt | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt index 5700a3f2..4eae7a45 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/sender/WebhookUtils.kt @@ -50,8 +50,7 @@ class WebhookUtils { val deviceMark: String = SettingUtils.extraDeviceMark val appVersion: String = AppUtils.getAppVersionName() val simInfo: String = msgInfo.simInfo - @SuppressLint("SimpleDateFormat") val receiveTime = - SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()) //smsVo.getDate() + val receiveTimeTag = Regex("\\[receive_time(:(.*?))?]") var sign = "" if (!TextUtils.isEmpty(setting.secret)) { @@ -100,10 +99,9 @@ class WebhookUtils { .replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8")) .replace("[title]", URLEncoder.encode(simInfo, "UTF-8")) .replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8")) - .replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8")) - .replace(Regex("\\[receive_time:(.*?)\\]")) { - val format = it.groups[1]?.value ?: "" - formatDateTime(format) + .replace(receiveTimeTag) { + val format = it.groups[2]?.value + URLEncoder.encode(formatDateTime(msgInfo.date, format), "UTF-8") } .replace("\n", "%0A") if (!TextUtils.isEmpty(setting.secret)) { @@ -126,10 +124,9 @@ class WebhookUtils { .replace("[app_version]", appVersion) .replace("[title]", escapeJson(simInfo)) .replace("[card_slot]", escapeJson(simInfo)) - .replace("[receive_time]", receiveTime) - .replace(Regex("\\[receive_time:(.*?)\\]")) { - val format = it.groups[1]?.value ?: "" - formatDateTime(format) + .replace(receiveTimeTag) { + val format = it.groups[2]?.value + formatDateTime(msgInfo.date, format) } .replace("[timestamp]", timestamp.toString()) .replace("[sign]", sign) @@ -162,10 +159,9 @@ class WebhookUtils { .replace("[app_version]", appVersion) .replace("[title]", simInfo) .replace("[card_slot]", simInfo) - .replace("[receive_time]", receiveTime) - .replace(Regex("\\[receive_time:(.*?)\\]")) { - val format = it.groups[1]?.value ?: "" - formatDateTime(format) + .replace(receiveTimeTag) { t -> + val format = t.groups[2]?.value + formatDateTime(msgInfo.date, format) } .replace("[timestamp]", timestamp.toString()) .replace("[sign]", sign) @@ -219,10 +215,10 @@ class WebhookUtils { } @SuppressLint("SimpleDateFormat") - fun formatDateTime(format: String): String { - val currentTime = Date() - val dateFormat = SimpleDateFormat(format) - dateFormat.timeZone = TimeZone.getTimeZone("UTC") // Optional: Set the desired timezone here + fun formatDateTime(currentTime: Date, format: String?): String { + val actualFormat = format?.removePrefix(":") ?: "yyyy-MM-dd HH:mm:ss" + val dateFormat = SimpleDateFormat(actualFormat) + dateFormat.timeZone = TimeZone.getTimeZone("UTC") return dateFormat.format(currentTime) }