新增:webParams 的 [receive_time] 标签支持自定义时间格式 #327

This commit is contained in:
pppscn 2023-07-30 10:22:35 +08:00
parent 6fe7b41baa
commit 852d327076

View File

@ -50,8 +50,7 @@ class WebhookUtils {
val deviceMark: String = SettingUtils.extraDeviceMark val deviceMark: String = SettingUtils.extraDeviceMark
val appVersion: String = AppUtils.getAppVersionName() val appVersion: String = AppUtils.getAppVersionName()
val simInfo: String = msgInfo.simInfo val simInfo: String = msgInfo.simInfo
@SuppressLint("SimpleDateFormat") val receiveTime = val receiveTimeTag = Regex("\\[receive_time(:(.*?))?]")
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()) //smsVo.getDate()
var sign = "" var sign = ""
if (!TextUtils.isEmpty(setting.secret)) { if (!TextUtils.isEmpty(setting.secret)) {
@ -100,10 +99,9 @@ class WebhookUtils {
.replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8")) .replace("[app_version]", URLEncoder.encode(appVersion, "UTF-8"))
.replace("[title]", URLEncoder.encode(simInfo, "UTF-8")) .replace("[title]", URLEncoder.encode(simInfo, "UTF-8"))
.replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8")) .replace("[card_slot]", URLEncoder.encode(simInfo, "UTF-8"))
.replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8")) .replace(receiveTimeTag) {
.replace(Regex("\\[receive_time:(.*?)\\]")) { val format = it.groups[2]?.value
val format = it.groups[1]?.value ?: "" URLEncoder.encode(formatDateTime(msgInfo.date, format), "UTF-8")
formatDateTime(format)
} }
.replace("\n", "%0A") .replace("\n", "%0A")
if (!TextUtils.isEmpty(setting.secret)) { if (!TextUtils.isEmpty(setting.secret)) {
@ -126,10 +124,9 @@ class WebhookUtils {
.replace("[app_version]", appVersion) .replace("[app_version]", appVersion)
.replace("[title]", escapeJson(simInfo)) .replace("[title]", escapeJson(simInfo))
.replace("[card_slot]", escapeJson(simInfo)) .replace("[card_slot]", escapeJson(simInfo))
.replace("[receive_time]", receiveTime) .replace(receiveTimeTag) {
.replace(Regex("\\[receive_time:(.*?)\\]")) { val format = it.groups[2]?.value
val format = it.groups[1]?.value ?: "" formatDateTime(msgInfo.date, format)
formatDateTime(format)
} }
.replace("[timestamp]", timestamp.toString()) .replace("[timestamp]", timestamp.toString())
.replace("[sign]", sign) .replace("[sign]", sign)
@ -162,10 +159,9 @@ class WebhookUtils {
.replace("[app_version]", appVersion) .replace("[app_version]", appVersion)
.replace("[title]", simInfo) .replace("[title]", simInfo)
.replace("[card_slot]", simInfo) .replace("[card_slot]", simInfo)
.replace("[receive_time]", receiveTime) .replace(receiveTimeTag) { t ->
.replace(Regex("\\[receive_time:(.*?)\\]")) { val format = t.groups[2]?.value
val format = it.groups[1]?.value ?: "" formatDateTime(msgInfo.date, format)
formatDateTime(format)
} }
.replace("[timestamp]", timestamp.toString()) .replace("[timestamp]", timestamp.toString())
.replace("[sign]", sign) .replace("[sign]", sign)
@ -219,10 +215,10 @@ class WebhookUtils {
} }
@SuppressLint("SimpleDateFormat") @SuppressLint("SimpleDateFormat")
fun formatDateTime(format: String): String { fun formatDateTime(currentTime: Date, format: String?): String {
val currentTime = Date() val actualFormat = format?.removePrefix(":") ?: "yyyy-MM-dd HH:mm:ss"
val dateFormat = SimpleDateFormat(format) val dateFormat = SimpleDateFormat(actualFormat)
dateFormat.timeZone = TimeZone.getTimeZone("UTC") // Optional: Set the desired timezone here dateFormat.timeZone = TimeZone.getTimeZone("UTC")
return dateFormat.format(currentTime) return dateFormat.format(currentTime)
} }