1 Commits

Author SHA1 Message Date
0c98628d03 Merge pull request 'dev-2025' (#2) from dev-2025 into main
Reviewed-on: #2
2025-04-11 16:37:08 +08:00
21 changed files with 161 additions and 792 deletions

View File

@@ -1,110 +0,0 @@
# 钉钉通知集成说明
## 功能概述
本项目已成功集成钉钉机器人通知功能,与现有的 ServerChan3 通知系统并存,提供多渠道消息通知能力。
## 新增文件
### 1. 配置类
- `src/main/kotlin/com/pomelotea/hoperun/sign/config/DingTalkConfig.kt` - 钉钉配置类
### 2. 数据模型
- `src/main/kotlin/com/pomelotea/hoperun/sign/api/model/DingTalkNotifyRequest.kt` - 钉钉消息请求/响应模型
### 3. 通知助手
- `src/main/kotlin/com/pomelotea/hoperun/sign/notify/DingTalkNotifyHelper.kt` - 钉钉通知助手类
### 4. 统一通知服务
- `src/main/kotlin/com/pomelotea/hoperun/sign/service/NotificationService.kt` - 统一管理多种通知方式
## 配置说明
### application-dev.yml 配置
```yaml
dingtalk:
enabled: false # 是否启用钉钉通知
webhook: https://oapi.dingtalk.com/robot/send?access_token=YOUR_ACCESS_TOKEN
secret: YOUR_SECRET # 钉钉机器人密钥(可选)
```
### 获取钉钉机器人信息
1. 在钉钉群中添加"自定义机器人"
2. 安全设置选择"加签",获取密钥
3. 获取 Webhook 地址
4. 将配置信息填入 `application-dev.yml`
## API 接口
### 测试接口
- `GET /api/daka/test/dingtalk` - 测试钉钉通知
- `GET /api/daka/test/serverchan` - 测试ServerChan3通知
- `GET /api/daka/test/all` - 测试所有通知方式
### 使用示例
```bash
# 测试钉钉通知
curl http://localhost:8982/api/daka/test/dingtalk
# 测试ServerChan3通知
curl http://localhost:8982/api/daka/test/serverchan
# 测试所有通知
curl http://localhost:8982/api/daka/test/all
```
## 编程接口
### NotificationService 使用
```kotlin
@Autowired
private lateinit var notificationService: NotificationService
// 发送所有类型的通知
notificationService.sendAllNotifications("标题", "内容")
// 只发送钉钉通知
val response = notificationService.sendDingTalkNotification("标题", "内容")
// 只发送ServerChan3通知
val response = notificationService.sendServerChanNotification("标题", "内容")
// 发送钉钉文本消息
val response = notificationService.sendDingTalkText("纯文本消息")
// 发送钉钉Markdown消息
val response = notificationService.sendDingTalkMarkdown("标题", "**Markdown** 内容")
```
## 功能特性
### 消息类型支持
- **文本消息**: 简单文本内容
- **Markdown消息**: 支持丰富的格式化内容
### 安全特性
- 支持钉钉机器人加签验证
- 自动URL签名生成
- 错误处理和日志记录
### 智能切换
- 自动检测配置有效性
- 支持同时启用多种通知方式
- 配置错误时自动跳过对应通知
## 使用建议
1. **开发测试**: 先使用测试接口验证配置正确性
2. **生产部署**: 确保 `enabled: true` 并填入正确的 webhook 和 secret
3. **消息格式**: 建议使用 Markdown 格式,支持更丰富的展示效果
4. **监控日志**: 关注应用日志,及时发现通知发送问题
## 注意事项
1. 确保钉钉机器人在群中有发送消息权限
2. Webhook 地址不要泄露到公开仓库
3. 消息发送频率不要超过钉钉限制通常每分钟最多20条
4. 建议在生产环境中配置错误重试机制

View File

@@ -1,4 +1,4 @@
FROM eclipse-temurin:8-jdk-alpine FROM openjdk:8-jre
LABEL name="hoperun-custom-sign" LABEL name="hoperun-custom-sign"
MAINTAINER li@2ha.me MAINTAINER li@2ha.me
WORKDIR / WORKDIR /

56
pom.xml
View File

@@ -21,7 +21,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style> <kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget> <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<kotlin.version>2.2.0</kotlin.version> <kotlin.version>2.0.20</kotlin.version>
</properties> </properties>
<build> <build>
@@ -44,6 +44,26 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.jetbrains.kotlin</groupId>
@@ -69,34 +89,6 @@
<jvmTarget>1.8</jvmTarget> <jvmTarget>1.8</jvmTarget>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
@@ -131,12 +123,6 @@
<artifactId>jsoup</artifactId> <artifactId>jsoup</artifactId>
<version>1.15.3</version> <version>1.15.3</version>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<distributionManagement> <distributionManagement>

View File

@@ -131,11 +131,6 @@ under the License.
<passphrase>optional; leave empty if not used.</passphrase> <passphrase>optional; leave empty if not used.</passphrase>
</server> </server>
--> -->
<server>
<id>mirror-all</id>
<username>jimlee</username>
<password>wysnih-dybbyQ-0rigti</password>
</server>
<server> <server>
<id>2ha</id> <id>2ha</id>
<username>jimlee</username> <username>jimlee</username>
@@ -181,7 +176,7 @@ under the License.
<id>mirror-all</id> <id>mirror-all</id>
<mirrorOf>*</mirrorOf> <mirrorOf>*</mirrorOf>
<name>2ha mirror</name> <name>2ha mirror</name>
<url>https://maven.2ha.me/repository/maven-public/</url> <url>https://2ha.me:18082/repository/maven-public/</url>
</mirror> </mirror>
</mirrors> </mirrors>
@@ -277,7 +272,7 @@ under the License.
<repository> <repository>
<id>2ha</id> <id>2ha</id>
<name>2ha.me nexus private</name> <name>2ha.me nexus private</name>
<url>https://maven.2ha.me/repository/maven-public/</url> <url>https://2ha.me:18082/repository/maven-public/</url>
<releases> <releases>
<enabled>true</enabled> <enabled>true</enabled>
</releases> </releases>
@@ -289,12 +284,12 @@ under the License.
<repository> <repository>
<id>maven-releases</id> <id>maven-releases</id>
<name>Nexus Release Repository</name> <name>Nexus Release Repository</name>
<url>https://maven.2ha.me/repository/private/</url> <url>https://2ha.me:18082/repository/private/</url>
</repository> </repository>
<repository> <repository>
<id>maven-snapshots</id> <id>maven-snapshots</id>
<name>Nexus Snapshot Repository</name> <name>Nexus Snapshot Repository</name>
<url>https://maven.2ha.me/repository/private/</url> <url>https://2ha.me:18082/repository/private/</url>
</repository> </repository>
</repositories> </repositories>
</profile> </profile>

View File

@@ -1,9 +1,9 @@
package com.pomelotea.hoperun.sign package com.pomelotea.hoperun.sign
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.boot.SpringApplication import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@@ -15,9 +15,29 @@ import java.util.*
* 启动入口 * 启动入口
**/ **/
@SpringBootApplication @SpringBootApplication
@ConfigurationPropertiesScan
open class DakaApplication open class DakaApplication
val <T : Any> T.logger: Logger
get() = LoggerFactory.getLogger(this::class.java)
val holidays: List<String> = listOf(
// 元旦
"2025-01-01",
// 春节
"2025-01-28", "2025-01-29", "2025-01-30", "2025-01-31", "2025-02-01", "2025-02-02", "2025-02-03", "2025-02-04",
// 清明
"2025-04-04", "2025-04-05", "2025-04-06",
// 劳动节
"2025-05-01", "2025-05-02", "2025-05-05", "2025-05-04", "2025-05-05",
// 端午节
"2025-05-31", "2025-06-01", "2025-06-02",
// 中秋
// 国庆
"2025-10-01", "2025-10-02", "2025-10-03", "2025-10-04", "2025-10-05", "2025-10-06", "2025-10-07", "2025-10-08")
// 周末要上班的日期
val workdays: List<String> = listOf("2025-01-26", "2025-02-08", "2025-04-27", "2025-10-11", "2025-09-28", "2025-10-12")
private val logger = LoggerFactory.getLogger("APPLICATION-STARTER") private val logger = LoggerFactory.getLogger("APPLICATION-STARTER")
fun main(args: Array<String>) { fun main(args: Array<String>) {
SpringApplication.run(DakaApplication::class.java, *args) SpringApplication.run(DakaApplication::class.java, *args)

View File

@@ -8,15 +8,12 @@ import com.pomelotea.hoperun.sign.config.HoperunUserConfig.deviceMap
import com.pomelotea.hoperun.sign.config.HoperunUserConfig.getUserConfig import com.pomelotea.hoperun.sign.config.HoperunUserConfig.getUserConfig
import com.pomelotea.hoperun.sign.config.HoperunUserConfig.userConfigMap import com.pomelotea.hoperun.sign.config.HoperunUserConfig.userConfigMap
import com.pomelotea.hoperun.sign.config.UserConfig import com.pomelotea.hoperun.sign.config.UserConfig
import com.pomelotea.hoperun.sign.holiday.HolidayService
import com.pomelotea.hoperun.sign.service.NotificationService
import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler
import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.dakaQueue import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.dakaQueue
import okhttp3.FormBody import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@@ -32,15 +29,13 @@ import java.util.*
@RestController @RestController
@RequestMapping("/api/daka") @RequestMapping("/api/daka")
class HoperunSignController( class HoperunSignController {
@field:Autowired private val notificationService: NotificationService,
@field:Autowired private val holidayService: HolidayService
) {
init { init {
AutoDakaScheduler(notificationService, holidayService) AutoDakaScheduler()
// AutoRenewSessionScheduler()
val yxl = UserConfig( val yxl = UserConfig(
project_id = "U2103S000112", project_id = "U2103S000112",
projectcode = "U2103S000112", projectcode = "U2103S000112",
@@ -50,11 +45,17 @@ class HoperunSignController(
userConfigMap["16638"] = yxl userConfigMap["16638"] = yxl
} }
// @GetMapping("/username/{employeeNo}/{checked}/{jsessionId}")
@GetMapping("/username/{employeeNo}/{checked}") @GetMapping("/username/{employeeNo}/{checked}")
fun getUsername( fun getUsername(
@PathVariable employeeNo: String, @PathVariable employeeNo: String,
@PathVariable checked: Boolean @PathVariable checked: Boolean
): WebResult<LoginResponse?> { ): WebResult<LoginResponse?> {
/*
if (jsessionId != null) {
getJsessionIdAutoLogin(employeeNo, jsessionId)
}*/
val userConfig = getUserConfig(employeeNo).let { val userConfig = getUserConfig(employeeNo).let {
defaultLogin(employeeNo) defaultLogin(employeeNo)
getUserConfig(employeeNo) getUserConfig(employeeNo)

View File

@@ -1,25 +0,0 @@
package com.pomelotea.hoperun.sign.api.model
data class DingTalkNotifyRequest(
val msgtype: String = "text",
val text: TextContent
)
data class TextContent(
val content: String
)
data class DingTalkNotifyResponse(
val errcode: Int,
val errmsg: String
)
data class MarkdownContent(
val title: String,
val text: String
)
data class DingTalkMarkdownRequest(
val msgtype: String = "markdown",
val markdown: MarkdownContent
)

View File

@@ -1,14 +0,0 @@
package com.pomelotea.hoperun.sign.api.model
data class ScNotifyRequest(
val title: String,
val desp: String?,
val tags: String? = null,
val short: String? = null
)
data class ScNotifyResponse(
val code: Int,
val message: String,
val data: String?
)

View File

@@ -7,14 +7,18 @@ import com.pomelotea.hoperun.sign.api.HoperunDakaRequest
import com.pomelotea.hoperun.sign.config.HoperunUserConfig import com.pomelotea.hoperun.sign.config.HoperunUserConfig
import com.pomelotea.hoperun.sign.config.HoperunUserConfig.getUserConfig import com.pomelotea.hoperun.sign.config.HoperunUserConfig.getUserConfig
import com.pomelotea.hoperun.sign.config.UserConfig import com.pomelotea.hoperun.sign.config.UserConfig
import com.pomelotea.hoperun.sign.holidays
import com.pomelotea.hoperun.sign.workdays
import okhttp3.FormBody import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import java.time.DayOfWeek
import java.time.Duration import java.time.Duration
import java.time.LocalDate import java.time.LocalDate
import java.time.ZoneOffset import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.* import java.util.*
/** /**
@@ -34,6 +38,7 @@ val client = OkHttpClient()
.newBuilder() .newBuilder()
.connectTimeout(Duration.ofSeconds(10)) .connectTimeout(Duration.ofSeconds(10))
.callTimeout(Duration.ofSeconds(10)) .callTimeout(Duration.ofSeconds(10))
// .addInterceptor(LogInterceptor())
.build() .build()
val sessionMap: MutableMap<String, String?> = HashMap() val sessionMap: MutableMap<String, String?> = HashMap()
@@ -52,9 +57,26 @@ fun getNowDateyyyy_MM_dd(): String {
return "${localDate.year}-${pad(localDate.monthValue)}-${pad(localDate.dayOfMonth)}" return "${localDate.year}-${pad(localDate.monthValue)}-${pad(localDate.dayOfMonth)}"
} }
fun getYesterdayyyyy_MM_dd(): String {
val localDate = LocalDate.now()
localDate.plusDays(-1)
return "${localDate.year}-${pad(localDate.monthValue)}-${pad(localDate.dayOfMonth)}"
}
private fun pad(num: Int): String { private fun pad(num: Int): String {
return if (num < 10) "0$num" else "$num" return if (num < 10) "0$num" else "$num"
} }
fun isWeekend(dakaDate: String): Boolean {
val date = LocalDate.parse(dakaDate, DateTimeFormatter.ISO_LOCAL_DATE)
return date.dayOfWeek == DayOfWeek.SATURDAY || date.dayOfWeek == DayOfWeek.SUNDAY
}
fun isWeekday(dakaDate: String): Boolean {
val date = LocalDate.parse(dakaDate, DateTimeFormatter.ISO_LOCAL_DATE)
return date.dayOfWeek == DayOfWeek.MONDAY || date.dayOfWeek == DayOfWeek.TUESDAY
|| date.dayOfWeek == DayOfWeek.WEDNESDAY || date.dayOfWeek == DayOfWeek.THURSDAY || date.dayOfWeek == DayOfWeek.FRIDAY
}
fun beginTime(employeeNo: String, date: String, time: String, jsessionId: String = sessionMap.get(employeeNo)!!): DakaResponse { fun beginTime(employeeNo: String, date: String, time: String, jsessionId: String = sessionMap.get(employeeNo)!!): DakaResponse {
val dakaRequest = Request.Builder() val dakaRequest = Request.Builder()
@@ -161,3 +183,6 @@ fun randowSecond(): String {
return if (second < 10) "0$second" else "$second" return if (second < 10) "0$second" else "$second"
} }
fun isNeedDaka(dakaDate: String): Boolean {
return workdays.contains(dakaDate) || (isWeekday(dakaDate) && !holidays.contains(dakaDate))
}

View File

@@ -1,12 +0,0 @@
package com.pomelotea.hoperun.sign.config
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component
@Component
@ConfigurationProperties(prefix = "dingtalk")
class DingTalkConfig {
lateinit var webhook: String
lateinit var secret: String
var enabled: Boolean = false
}

View File

@@ -1,11 +0,0 @@
package com.pomelotea.hoperun.sign.config
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component
@Component
@ConfigurationProperties(prefix = "sc3")
class ServerChan3Config {
lateinit var uid: String
lateinit var sendKey: String
}

View File

@@ -1,23 +0,0 @@
package com.pomelotea.hoperun.sign.holiday
import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties(prefix = "holiday")
data class HolidayConfig(
var enabled: Boolean = true,
var sources: List<String> = listOf(
"https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/{year}.json"
),
var proxySources: List<String> = listOf(
"https://ghproxy.com/https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/{year}.json"
),
var cacheDir: String = "./data/holiday",
var cacheTtlDays: Long = 30,
var yearsAhead: Int = 1,
var fallback: Fallback = Fallback()
) {
data class Fallback(
var holidays: List<String> = emptyList(),
var workdays: List<String> = emptyList()
)
}

View File

@@ -1,17 +0,0 @@
package com.pomelotea.hoperun.sign.holiday
import com.alibaba.fastjson.annotation.JSONField
import java.time.Instant
data class HolidayEntry(
val name: String? = null,
val date: String? = null,
@field:JSONField(name = "isOffDay")
val isOffDay: Boolean? = null
)
data class HolidayCalendar(
val holidays: Set<String>,
val workdays: Set<String>,
val updatedAt: Instant
)

View File

@@ -1,231 +0,0 @@
package com.pomelotea.hoperun.sign.holiday
import com.alibaba.fastjson.JSON
import okhttp3.OkHttpClient
import okhttp3.Request
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.DayOfWeek
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.concurrent.ConcurrentHashMap
import javax.annotation.PostConstruct
@Service
class HolidayService(
private val config: HolidayConfig
) {
private val logger = LoggerFactory.getLogger(HolidayService::class.java)
private val cache = ConcurrentHashMap<Int, HolidayCalendar>()
private val dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE
private val httpClient = OkHttpClient.Builder()
.connectTimeout(Duration.ofSeconds(10))
.callTimeout(Duration.ofSeconds(10))
.build()
@PostConstruct
fun preload() {
if (!config.enabled) {
return
}
val now = LocalDate.now()
val endYear = now.year + config.yearsAhead
logger.info("节假日配置预加载开始: years={}..{}", now.year, endYear)
for (year in now.year..endYear) {
runCatching { getCalendar(year) }
.onFailure { logger.warn("节假日配置预加载失败: year={}, err={}", year, it.message) }
}
}
fun isWorkingDay(dateStr: String): Boolean {
val date = LocalDate.parse(dateStr, dateFormatter)
return isWorkingDay(date)
}
fun isWorkingDay(date: LocalDate): Boolean {
if (!config.enabled) {
return defaultIsWorkingDay(date)
}
val dateKey = date.format(dateFormatter)
val calendar = getCalendar(date.year)
if (calendar.workdays.contains(dateKey)) {
return true
}
if (calendar.holidays.contains(dateKey)) {
return false
}
return defaultIsWorkingDay(date)
}
private fun defaultIsWorkingDay(date: LocalDate): Boolean {
return date.dayOfWeek != DayOfWeek.SATURDAY && date.dayOfWeek != DayOfWeek.SUNDAY
}
private fun getCalendar(year: Int): HolidayCalendar {
val now = Instant.now()
val cached = cache[year]
if (cached != null && !isExpired(cached.updatedAt, now)) {
return cached
}
val remoteResult = fetchRemote(year)
if (remoteResult != null) {
val calendar = applyFallback(parse(remoteResult.body)).copy(updatedAt = now)
cache[year] = calendar
writeCache(year, remoteResult.body)
logger.info(
"节假日配置加载成功(远程): year={}, url={}, holidays={}, workdays={}",
year,
remoteResult.url,
calendar.holidays.size,
calendar.workdays.size
)
return calendar
}
if (cached != null) {
val refreshed = cached.copy(updatedAt = now)
cache[year] = refreshed
logger.warn(
"节假日远程刷新失败,继续使用内存缓存: year={}, holidays={}, workdays={}",
year,
refreshed.holidays.size,
refreshed.workdays.size
)
return refreshed
}
val cachedRaw = readCache(year)
if (cachedRaw != null) {
val calendar = applyFallback(parse(cachedRaw)).copy(updatedAt = now)
cache[year] = calendar
logger.info(
"节假日配置加载成功(本地缓存): year={}, path={}, holidays={}, workdays={}",
year,
cachePath(year).toString(),
calendar.holidays.size,
calendar.workdays.size
)
return calendar
}
logger.warn("节假日配置缺失仅使用fallback: year={}", year)
val fallbackCalendar = applyFallback(HolidayCalendar(emptySet(), emptySet(), now))
cache[year] = fallbackCalendar
return fallbackCalendar
}
private fun isExpired(updatedAt: Instant, now: Instant): Boolean {
if (config.cacheTtlDays <= 0) {
return false
}
return Duration.between(updatedAt, now).toDays() >= config.cacheTtlDays
}
private fun fetchRemote(year: Int): FetchResult? {
val urls = buildUrls(year)
for (url in urls) {
try {
val request = Request.Builder().url(url).get().build()
httpClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
logger.warn("节假日配置拉取失败: url={}, code={}", url, response.code)
return@use
}
val body = response.body?.string()
if (!body.isNullOrBlank()) {
return FetchResult(url = url, body = body)
}
logger.warn("节假日配置拉取为空: url={}", url)
}
} catch (ex: Exception) {
logger.warn("节假日配置拉取异常: url={}, err={}", url, ex.message)
}
}
return null
}
private fun buildUrls(year: Int): List<String> {
if (config.sources.isEmpty() && config.proxySources.isEmpty()) {
return emptyList()
}
val urls = ArrayList<String>()
config.sources.forEach { urls.add(it.replace("{year}", year.toString())) }
config.proxySources.forEach { urls.add(it.replace("{year}", year.toString())) }
return urls
}
private fun parse(raw: String): HolidayCalendar {
val entries = JSON.parseArray(raw, HolidayEntry::class.java) ?: emptyList()
val holidays = LinkedHashSet<String>()
val workdays = LinkedHashSet<String>()
for (entry in entries) {
val date = entry.date?.trim()
if (date.isNullOrEmpty()) {
continue
}
when (entry.isOffDay) {
true -> holidays.add(date)
false -> workdays.add(date)
else -> {}
}
}
return HolidayCalendar(holidays, workdays, Instant.now())
}
private fun applyFallback(calendar: HolidayCalendar): HolidayCalendar {
if (config.fallback.holidays.isEmpty() && config.fallback.workdays.isEmpty()) {
return calendar
}
val holidays = calendar.holidays.toMutableSet()
val workdays = calendar.workdays.toMutableSet()
for (date in config.fallback.holidays) {
holidays.add(date)
workdays.remove(date)
}
for (date in config.fallback.workdays) {
workdays.add(date)
holidays.remove(date)
}
return calendar.copy(holidays = holidays, workdays = workdays)
}
private fun cachePath(year: Int): Path {
return Paths.get(config.cacheDir).resolve("$year.json")
}
private fun writeCache(year: Int, raw: String) {
try {
val path = cachePath(year)
Files.createDirectories(path.parent)
Files.write(path, raw.toByteArray(StandardCharsets.UTF_8))
logger.info("节假日缓存写入成功: year={}, path={}", year, path.toString())
} catch (ex: Exception) {
logger.warn("节假日缓存写入失败: year={}, err={}", year, ex.message)
}
}
private fun readCache(year: Int): String? {
return try {
val path = cachePath(year)
if (!Files.exists(path)) {
return null
}
String(Files.readAllBytes(path), StandardCharsets.UTF_8)
} catch (ex: Exception) {
logger.warn("节假日缓存读取失败: year={}, err={}", year, ex.message)
null
}
}
private data class FetchResult(
val url: String,
val body: String
)
}

View File

@@ -1,100 +0,0 @@
package com.pomelotea.hoperun.sign.notify
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.pomelotea.hoperun.sign.api.model.DingTalkMarkdownRequest
import com.pomelotea.hoperun.sign.api.model.DingTalkNotifyRequest
import com.pomelotea.hoperun.sign.api.model.DingTalkNotifyResponse
import com.pomelotea.hoperun.sign.api.model.MarkdownContent
import com.pomelotea.hoperun.sign.common.client
import com.pomelotea.hoperun.sign.config.DingTalkConfig
import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.logger
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.springframework.stereotype.Service
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.Base64
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import kotlin.experimental.and
@Service
open class DingTalkNotifyHelper(
private val dingTalkConfig: DingTalkConfig
) {
fun sendText(content: String): DingTalkNotifyResponse {
if (!dingTalkConfig.enabled) {
logger.info("钉钉通知未启用,跳过发送消息")
return DingTalkNotifyResponse(0, "钉钉通知未启用")
}
val request = DingTalkNotifyRequest(
text = com.pomelotea.hoperun.sign.api.model.TextContent(content)
)
return sendRequest(request)
}
fun sendMarkdown(title: String, content: String): DingTalkNotifyResponse {
if (!dingTalkConfig.enabled) {
logger.info("钉钉通知未启用,跳过发送消息")
return DingTalkNotifyResponse(0, "钉钉通知未启用")
}
val request = DingTalkMarkdownRequest(
markdown = MarkdownContent(title, content)
)
return sendRequest(request)
}
private fun sendRequest(request: Any): DingTalkNotifyResponse {
try {
val url = generateSignedUrl()
val jsonBody = JSON.toJSONString(request)
logger.info("发送钉钉消息: $jsonBody")
val httpRequest = Request.Builder()
.url(url)
.post(jsonBody.toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull()))
.build()
val response = client.newCall(httpRequest).execute()
val responseBody = response.body?.string()
logger.info("钉钉响应: $responseBody")
return JSONObject.parseObject(responseBody, DingTalkNotifyResponse::class.java)
} catch (e: Exception) {
logger.error("发送钉钉消息失败", e)
return DingTalkNotifyResponse(-1, "发送钉钉消息失败: ${e.message}")
}
}
private fun generateSignedUrl(): String {
val webhook = dingTalkConfig.webhook
val secret = dingTalkConfig.secret
if (secret.isEmpty()) {
return webhook
}
val timestamp = System.currentTimeMillis()
val sign = generateSign(timestamp.toString(), secret)
return "$webhook&timestamp=$timestamp&sign=$sign"
}
private fun generateSign(timestamp: String, secret: String): String {
val stringToSign = "$timestamp\n$secret"
val mac = Mac.getInstance("HmacSHA256")
mac.init(SecretKeySpec(secret.toByteArray(StandardCharsets.UTF_8), "HmacSHA256"))
val signData = mac.doFinal(stringToSign.toByteArray(StandardCharsets.UTF_8))
val sign = Base64.getEncoder().encodeToString(signData)
return URLEncoder.encode(sign, StandardCharsets.UTF_8.name())
}
}

View File

@@ -1,31 +0,0 @@
package com.pomelotea.hoperun.sign.notify
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.pomelotea.hoperun.sign.api.model.ScNotifyRequest
import com.pomelotea.hoperun.sign.api.model.ScNotifyResponse
import com.pomelotea.hoperun.sign.common.client
import com.pomelotea.hoperun.sign.config.ServerChan3Config
import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.logger
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.springframework.stereotype.Service
@Service
open class ServerChan3NotifyHelper(
private val serverChan3Config: ServerChan3Config
) {
fun push(req: ScNotifyRequest): ScNotifyResponse {
val notifyRequest = Request.Builder()
.url("https://${serverChan3Config.uid}.push.ft07.com/send/${serverChan3Config.sendKey}.send")
.post(
JSON.toJSONString(req)
.toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
)
.build()
val result: String? = client.newCall(notifyRequest).execute().body?.string()
return JSONObject.parseObject(result, ScNotifyResponse::class.java)
}
}

View File

@@ -4,11 +4,6 @@ import com.pomelotea.hoperun.sign.api.DakaResponse
import com.pomelotea.hoperun.sign.common.* import com.pomelotea.hoperun.sign.common.*
import com.pomelotea.hoperun.sign.config.HoperunUserConfig import com.pomelotea.hoperun.sign.config.HoperunUserConfig
import com.pomelotea.hoperun.sign.config.UserConfig import com.pomelotea.hoperun.sign.config.UserConfig
import com.pomelotea.hoperun.sign.holiday.HolidayService
import com.pomelotea.hoperun.sign.service.NotificationService
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.time.LocalDateTime import java.time.LocalDateTime
@@ -26,10 +21,7 @@ import java.util.concurrent.TimeUnit
* date 2023-03-22 14:50 * date 2023-03-22 14:50
* 自动打卡定时任务 * 自动打卡定时任务
**/ **/
open class AutoDakaScheduler( class AutoDakaScheduler {
private val notificationService: NotificationService,
private val holidayService: HolidayService
) {
val timeThreadPool: ScheduledExecutorService = Executors.newScheduledThreadPool(1) val timeThreadPool: ScheduledExecutorService = Executors.newScheduledThreadPool(1)
val schedulerThreadPool: ScheduledExecutorService = Executors.newScheduledThreadPool(10) val schedulerThreadPool: ScheduledExecutorService = Executors.newScheduledThreadPool(10)
@@ -45,7 +37,7 @@ open class AutoDakaScheduler(
private fun addAutoDakaScheduled(dakaDate: String = getNowDateyyyy_MM_dd()) { private fun addAutoDakaScheduled(dakaDate: String = getNowDateyyyy_MM_dd()) {
// 调休的工作日 // 调休的工作日
if (holidayService.isWorkingDay(dakaDate)) { if (isNeedDaka(dakaDate)) {
HoperunUserConfig.userConfigMap.forEach { (_, v) -> HoperunUserConfig.userConfigMap.forEach { (_, v) ->
if (v.autoDaka) { if (v.autoDaka) {
// 没有当日的打卡信息才插入 // 没有当日的打卡信息才插入
@@ -53,19 +45,6 @@ open class AutoDakaScheduler(
if (daka == null) { if (daka == null) {
daka = generateDakaInfo(v, dakaDate) daka = generateDakaInfo(v, dakaDate)
dakaQueue.add(daka) dakaQueue.add(daka)
notificationService.sendDingTalkNotification(
"#### 【 $dakaDate 】添加打卡任务!",
"""
#### 【 $dakaDate 】添加打卡任务!
**************************************************
##### 工号: ${daka.employeeNo}
##### 上班卡: ${daka.beginTime}
##### 下班卡: ${daka.endTime}
**************************************************
"""
)
logger.info("添加打卡任务: ${v.username}, $daka") logger.info("添加打卡任务: ${v.username}, $daka")
} }
} }
@@ -94,9 +73,11 @@ open class AutoDakaScheduler(
schedulerThreadPool.schedule({ endDaka(it) }, endSeconds, TimeUnit.SECONDS) schedulerThreadPool.schedule({ endDaka(it) }, endSeconds, TimeUnit.SECONDS)
} }
} }
// schedulerThreadPool.schedule({beginDaka(it)}, 5, TimeUnit.SECONDS)
// schedulerThreadPool.schedule({ endDaka(it) }, 10, TimeUnit.SECONDS)
} }
dakaQueue.removeIf { it.dakaDate.compareTo(dakaDate) < 0 } dakaQueue.removeIf { it.dakaDate.compareTo(dakaDate) == -1 }
printScheduler() printScheduler()
dakaQueue.forEach { dakaQueue.forEach {
logger.info("Task: ${it.toCsv()}") logger.info("Task: ${it.toCsv()}")
@@ -115,28 +96,7 @@ open class AutoDakaScheduler(
beginTime(employeeNo = daka.employeeNo, date = daka.dakaDate, time = daka.beginTime) beginTime(employeeNo = daka.employeeNo, date = daka.dakaDate, time = daka.beginTime)
if (resp.result != "success") { if (resp.result != "success") {
logger.error("打上班卡失败") logger.error("打上班卡失败")
notificationService.sendDingTalkNotification(
title = "${daka.dakaDate} 】打上班卡失败",
"""
#### 【 ${daka.dakaDate} 】打上班卡失败!
**************************************************
##### 工号: ${daka.employeeNo}
##### 上班卡: ${daka.beginTime}
##### 下班卡: ${daka.endTime}
**************************************************
"""
)
} else { } else {
notificationService.sendDingTalkNotification(
title = "打上班卡成功:${daka.employeeNo}:DATE:${daka.dakaDate}:TIME:${daka.beginTime}",
"""
#### 【 ${daka.dakaDate} 】打上班卡成功!
**************************************************
##### 工号: ${daka.employeeNo}
##### 时间: ${daka.beginTime}
**************************************************
"""
)
logger.info("打上班卡成功") logger.info("打上班卡成功")
} }
} catch (e: Exception) { } catch (e: Exception) {
@@ -156,26 +116,7 @@ open class AutoDakaScheduler(
val resp: DakaResponse = endTime(employeeNo = daka.employeeNo, date = daka.dakaDate, time = daka.endTime) val resp: DakaResponse = endTime(employeeNo = daka.employeeNo, date = daka.dakaDate, time = daka.endTime)
if (resp.result != "success") { if (resp.result != "success") {
logger.error("打下班卡失败") logger.error("打下班卡失败")
notificationService.sendDingTalkNotification(
title = "打下班卡失败:${daka.employeeNo}:DATE:${daka.dakaDate}:TIME:${daka.endTime}",
"""
#### 【 ${daka.dakaDate} 】打下班卡失败!
**************************************************
##### 工号: ${daka.employeeNo}
##### 上班卡: ${daka.beginTime}
##### 下班卡: ${daka.endTime}
**************************************************
""" )
} else { } else {
notificationService.sendDingTalkNotification(
title = "打下班卡成功:${daka.employeeNo}:DATE:${daka.dakaDate}:TIME:${daka.endTime}",
"""
#### 【 ${daka.dakaDate} 】打下班卡成功!
**************************************************
##### 工号: ${daka.employeeNo}
##### 时间: ${daka.endTime}
**************************************************
""" )
logger.info("打下班卡成功") logger.info("打下班卡成功")
} }
} catch (e: Exception) { } catch (e: Exception) {

View File

@@ -0,0 +1,70 @@
package com.pomelotea.hoperun.sign.scheduler
import com.pomelotea.hoperun.sign.common.client
import com.pomelotea.hoperun.sign.common.sessionMap
import com.pomelotea.hoperun.sign.logger
import okhttp3.Request
import org.jsoup.Jsoup
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
/**
*
* @version 0.0.1
* @author jimlee
* date 2023-03-23 08:54
*
**/
class AutoRenewSessionScheduler {
val schedulerThreadPool: ScheduledExecutorService = Executors.newScheduledThreadPool(1)
init {
schedulerThreadPool.schedule({renewSession()}, 2, TimeUnit.MINUTES)
}
private fun renewSession() {
val iterator = sessionMap.iterator()
while (iterator.hasNext()) {
val item = iterator.next()
item.value?.let {
val result = index(it)
if (result != null) {
logger.info("[RENEW-SESSION]:SUCCESS:USER:$result")
} else {
logger.info("[REMOVE-SESSION]:USER:$result")
iterator.remove()
}
}
}
schedulerThreadPool.schedule({renewSession()}, (30 + Random().nextInt(30)).toLong(), TimeUnit.MINUTES)
}
private fun index(jsessionId: String): String? {
val indexRequest = Request.Builder()
.url("http://pom.hoperun.com:8187/attm/attence/getInfo")
.get()
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.addHeader(
"User-Agent",
"Qing/0.9.113;iOS 16.3.1;Apple;iPhone13,2;deviceId:a8baf66f-fdeb-4f4d-b1e5-9fafcd5045b6;deviceName:iOS;clientId:10200;os:iOS 16.3.1;brand:Apple;model:iPhone13,2;lang:zh-CN;fontNum:0;fontScale:1.0;ver:10.7.14;Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
)
.addHeader("accept", "*/*")
.addHeader("Origin", "http://pom.hoperun.com:8187")
.addHeader("Referer", "http://pom.hoperun.com:8187/attm/attence/getInfo")
.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.build()
val indexResp = client.newCall(indexRequest).execute()
val indexHtml = indexResp.body?.string()
val username = try {
Jsoup.parse(indexHtml!!).select("#attendance-detail-content > div.container.none-padding > div > div:nth-child(1) > div:nth-child(4)")
.text()
} catch (e: Exception) {
null
}
return username
}
}

View File

@@ -1,70 +0,0 @@
package com.pomelotea.hoperun.sign.service
import com.pomelotea.hoperun.sign.api.model.ScNotifyRequest
import com.pomelotea.hoperun.sign.api.model.ScNotifyResponse
import com.pomelotea.hoperun.sign.api.model.DingTalkNotifyResponse
import com.pomelotea.hoperun.sign.config.DingTalkConfig
import com.pomelotea.hoperun.sign.config.ServerChan3Config
import com.pomelotea.hoperun.sign.notify.DingTalkNotifyHelper
import com.pomelotea.hoperun.sign.notify.ServerChan3NotifyHelper
import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.logger
import org.springframework.stereotype.Service
@Service
open class NotificationService(
private val serverChan3NotifyHelper: ServerChan3NotifyHelper,
private val dingTalkNotifyHelper: DingTalkNotifyHelper,
private val serverChan3Config: ServerChan3Config,
private val dingTalkConfig: DingTalkConfig
) {
fun sendAllNotifications(title: String, content: String) {
// 发送 ServerChan3 通知
try {
val scResponse = sendServerChanNotification(title, content)
logger.info("ServerChan3 通知发送结果: ${scResponse.message}")
} catch (e: Exception) {
logger.error("ServerChan3 通知发送失败", e)
}
// 发送钉钉通知
try {
val dtResponse = sendDingTalkNotification(title, content)
logger.info("钉钉通知发送结果: ${dtResponse.errmsg}")
} catch (e: Exception) {
logger.error("钉钉通知发送失败", e)
}
}
fun sendServerChanNotification(title: String, content: String): ScNotifyResponse {
val request = ScNotifyRequest(title = title, desp = content)
return serverChan3NotifyHelper.push(request)
}
fun sendDingTalkNotification(title: String, content: String): DingTalkNotifyResponse {
// 如果内容包含markdown格式使用markdown类型消息
return if (content.contains("**") || content.contains("#") || content.contains("*")) {
dingTalkNotifyHelper.sendMarkdown(title, content)
} else {
dingTalkNotifyHelper.sendText("$title\n\n$content")
}
}
fun sendDingTalkText(text: String): DingTalkNotifyResponse {
return dingTalkNotifyHelper.sendText(text)
}
fun sendDingTalkMarkdown(title: String, content: String): DingTalkNotifyResponse {
return dingTalkNotifyHelper.sendMarkdown(title, content)
}
fun isServerChanEnabled(): Boolean {
return serverChan3Config.uid.isNotEmpty() && serverChan3Config.sendKey.isNotEmpty()
}
fun isDingTalkEnabled(): Boolean {
return dingTalkConfig.enabled &&
dingTalkConfig.webhook.isNotEmpty() &&
!dingTalkConfig.webhook.contains("YOUR_ACCESS_TOKEN")
}
}

View File

@@ -9,11 +9,3 @@ hoperun:
latitudeShort: "30.140219809955912" latitudeShort: "30.140219809955912"
qingUa: "Qing/0.9.113" qingUa: "Qing/0.9.113"
sc3:
uid: 7248
send-key: sctp7248ta-yehg0lpo6cr9xl6ikqwbpn4l
dingtalk:
enabled: true
webhook: https://oapi.dingtalk.com/robot/send?access_token=6925880a1b7379b2fb393b5336dd75155f37189a7912981b568b08316bfd7b9e
secret:

View File

@@ -37,20 +37,3 @@ hoperun:
"projectcode": "U2103S000112" "projectcode": "U2103S000112"
"projectname": "JRKF-浙江网商-技术服务外包" "projectname": "JRKF-浙江网商-技术服务外包"
"device": "Android 12;Redmi;M2007J3SC;deviceId:OAIDe7fa6084205e9a22d8f6f71bc91893ff;deviceName:Android" "device": "Android 12;Redmi;M2007J3SC;deviceId:OAIDe7fa6084205e9a22d8f6f71bc91893ff;deviceName:Android"
sc3:
uid: 7248
sendKey: sctp7248ta-yehg0lpo6cr9xl6ikqwbpn4l
holiday:
enabled: true
sources:
- https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/{year}.json
proxy-sources:
- https://ghproxy.com/https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/{year}.json
cache-dir: ./data/holiday
cache-ttl-days: 30
years-ahead: 1
fallback:
holidays: []
workdays: []