From 963e56a2321bf08a92c5688fb6dc49c9de5e7903 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Fri, 15 Aug 2025 21:50:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=AE=9A=E4=BD=8D?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/chat_api/SystemFunctionHandler.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift index 35d553698..6de9c42ce 100644 --- a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift +++ b/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() From 3fa9c2874876d0f877b7641ea60d4e13eb108ced Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sat, 16 Aug 2025 11:00:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=8E=86?= =?UTF-8?q?=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat_api/SystemFunctionHandler.swift | 199 +++++++++++++++--- 1 file changed, 169 insertions(+), 30 deletions(-) diff --git a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift index 6de9c42ce..f16aa088d 100644 --- a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift +++ b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift @@ -729,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: "事件存储未初始化") - } - - // 请求日历权限 - let status = await requestCalendarAccess() - guard status == .authorized else { - 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: "时间格式错误") - } - - // 创建提醒 - let reminder = EKReminder(eventStore: eventStore) - reminder.title = title - reminder.notes = content - reminder.calendar = eventStore.defaultCalendarForNewReminders() - - let alarm = EKAlarm(absoluteDate: date) - reminder.addAlarm(alarm) + Log.d(TAG, "设置提醒: $title, 时间: $timeStr") - do { - try eventStore.save(reminder, commit: true) - return Result(success: true, message: "提醒设置成功") - } catch { - return Result(success: false, message: "设置提醒失败:\(error.localizedDescription)") + 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}" + ) } } @@ -773,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 {