Browse Source

Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev

weicu
lxm 1 year ago
parent
commit
d0b68348db
  1. 194
      local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift

194
local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift

@ -447,10 +447,17 @@ private class GetCurrentLocationHandler: FunctionHandler {
return await withCheckedContinuation { continuation in
// 设置超时
let timeoutTask = Task {
try await Task.sleep(nanoseconds: 15_000_000_000) // 15秒
try await Task.sleep(seconds: 15) // 15秒
continuation.resume(returning: self.createErrorResponse(message: "位置获取超时,请检查网络连接后重试"))
}
// 检查位置服务是否可用
guard CLLocationManager.locationServicesEnabled() else {
locationCompletion?(Result(success: false, message: "位置服务未启用。请前往 设置 > 隐私与安全 > 定位服务 启用定位服务"))
locationCompletion = nil
return
}
LocationService.shared.getCurrentLocation { result in
timeoutTask.cancel()
@ -722,37 +729,85 @@ private class SetReminderHandler: FunctionHandler {
}
private func setReminder(title: String, content: String, time: String) async -> Result {
guard let eventStore = eventStore else {
return Result(success: false, message: "事件存储未初始化")
Log.d(TAG, "设置提醒: $title, 时间: $timeStr")
try {
// 解析时间字符串
val time = try {
val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault())
dateFormat.parse(timeStr)?.time ?: System.currentTimeMillis()
} catch (e: Exception) {
Log.e(TAG, "解析时间失败: $timeStr, ${e.message}")
return Result(
success = false,
message = "提醒时间格式无效,请使用'yyyy-MM-dd HH:mm'格式"
)
}
// 请求日历权限
let status = await requestCalendarAccess()
guard status == .authorized else {
return Result(success: false, message: "日历权限被拒绝")
// 检查日历权限
if (!checkPermissionAndGuide(Manifest.permission.WRITE_CALENDAR, "日历写入", "设置提醒")) {
return Result(
success = false,
message = "需要日历权限才能创建提醒,请在权限管理中开启日历权限"
)
}
// 解析时间
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
guard let date = formatter.date(from: time) else {
return Result(success: false, message: "时间格式错误")
// 直接使用ContentProvider写入日历
val endTime = time + 3600000 // 默认1小时后结束
// 获取默认日历ID
val calendarId = getDefaultCalendarId()
if (calendarId == -1L) {
Log.w(TAG, "未找到默认日历")
return tryCreateAlarm(title, content, timeStr)
}
// 创建提醒
let reminder = EKReminder(eventStore: eventStore)
reminder.title = title
reminder.notes = content
reminder.calendar = eventStore.defaultCalendarForNewReminders()
// 创建事件
val eventValues = ContentValues().apply {
put(CalendarContract.Events.CALENDAR_ID, calendarId)
put(CalendarContract.Events.TITLE, title)
put(CalendarContract.Events.DESCRIPTION, content)
put(CalendarContract.Events.DTSTART, time)
put(CalendarContract.Events.DTEND, endTime)
put(CalendarContract.Events.HAS_ALARM, 1)
put(CalendarContract.Events.EVENT_TIMEZONE, java.util.TimeZone.getDefault().id)
}
let alarm = EKAlarm(absoluteDate: date)
reminder.addAlarm(alarm)
val eventUri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, eventValues)
if (eventUri == null) {
Log.e(TAG, "插入日历事件失败")
return tryCreateAlarm(title, content, timeStr)
}
do {
try eventStore.save(reminder, commit: true)
return Result(success: true, message: "提醒设置成功")
} catch {
return Result(success: false, message: "设置提醒失败:\(error.localizedDescription)")
// 获取事件ID
val eventId = ContentUris.parseId(eventUri)
// 添加提醒(15分钟前提醒)
val reminderValues = ContentValues().apply {
put(CalendarContract.Reminders.EVENT_ID, eventId)
put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
put(CalendarContract.Reminders.MINUTES, 15) // 提前15分钟提醒
}
val reminderUri = context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues)
if (reminderUri != null) {
return Result(
success = true,
message = "已成功创建提醒: $title, 时间: $timeStr"
)
} else {
// 仍然创建了事件,但没有提醒
return Result(
success = true,
message = "已创建日历事件,但未设置提醒: $title"
)
}
} catch (e: Exception) {
Log.e(TAG, "设置提醒失败: ${e.message}", e)
return Result(
success = false,
message = "设置提醒失败: ${e.message}"
)
}
}
@ -766,6 +821,97 @@ private class SetReminderHandler: FunctionHandler {
}
}
// /**
// * 设置提醒
// * @param title 提醒标题
// * @param content 提醒内容(可选)
// * @param timeStr 提醒时间,格式为"yyyy-MM-dd HH:mm"
// * @return 操作结果
// */
// fun setReminder(title: String, content: String, timeStr: String): Result {
// Log.d(TAG, "设置提醒: $title, 时间: $timeStr")
// try {
// // 解析时间字符串
// val time = try {
// val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault())
// dateFormat.parse(timeStr)?.time ?: System.currentTimeMillis()
// } catch (e: Exception) {
// Log.e(TAG, "解析时间失败: $timeStr, ${e.message}")
// return Result(
// success = false,
// message = "提醒时间格式无效,请使用'yyyy-MM-dd HH:mm'格式"
// )
// }
// // 检查日历权限
// if (!checkPermissionAndGuide(Manifest.permission.WRITE_CALENDAR, "日历写入", "设置提醒")) {
// return Result(
// success = false,
// message = "需要日历权限才能创建提醒,请在权限管理中开启日历权限"
// )
// }
// // 直接使用ContentProvider写入日历
// val endTime = time + 3600000 // 默认1小时后结束
// // 获取默认日历ID
// val calendarId = getDefaultCalendarId()
// if (calendarId == -1L) {
// Log.w(TAG, "未找到默认日历")
// return tryCreateAlarm(title, content, timeStr)
// }
// // 创建事件
// val eventValues = ContentValues().apply {
// put(CalendarContract.Events.CALENDAR_ID, calendarId)
// put(CalendarContract.Events.TITLE, title)
// put(CalendarContract.Events.DESCRIPTION, content)
// put(CalendarContract.Events.DTSTART, time)
// put(CalendarContract.Events.DTEND, endTime)
// put(CalendarContract.Events.HAS_ALARM, 1)
// put(CalendarContract.Events.EVENT_TIMEZONE, java.util.TimeZone.getDefault().id)
// }
// val eventUri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, eventValues)
// if (eventUri == null) {
// Log.e(TAG, "插入日历事件失败")
// return tryCreateAlarm(title, content, timeStr)
// }
// // 获取事件ID
// val eventId = ContentUris.parseId(eventUri)
// // 添加提醒(15分钟前提醒)
// val reminderValues = ContentValues().apply {
// put(CalendarContract.Reminders.EVENT_ID, eventId)
// put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
// put(CalendarContract.Reminders.MINUTES, 15) // 提前15分钟提醒
// }
// val reminderUri = context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues)
// if (reminderUri != null) {
// return Result(
// success = true,
// message = "已成功创建提醒: $title, 时间: $timeStr"
// )
// } else {
// // 仍然创建了事件,但没有提醒
// return Result(
// success = true,
// message = "已创建日历事件,但未设置提醒: $title"
// )
// }
// } catch (e: Exception) {
// Log.e(TAG, "设置提醒失败: ${e.message}", e)
// return Result(
// success = false,
// message = "设置提醒失败: ${e.message}"
// )
// }
// }
/// 获取当前时间处理器
private class GetCurrentTimeHandler: FunctionHandler {
func handle(arguments: [String: Any]) async throws -> String {

Loading…
Cancel
Save