BHYG
BHYG copied to clipboard
feat: display alert on special events
Summary:
When special events occur (e.g., triggering risk control, encountering errors), it would be beneficial to display a MessageBox or Notification to alert the user. This would improve the user experience by providing immediate feedback and necessary information about the event.
Description:
Special events such as risk control triggers or errors are critical moments that require the user's attention. Displaying a MessageBox or Notification would ensure that the user is promptly informed and can take appropriate action. This feature should be implemented in a cross-platform manner to support users on various operating systems.
Sample Code:
import sys
if sys.platform == "win32":
import ctypes
from win10toast import ToastNotifier
def show_message_box(title, message):
ctypes.windll.user32.MessageBoxW(0, message, title, 1)
def show_notification(title, message):
toaster = ToastNotifier()
toaster.show_toast(title, message, duration=10)
elif sys.platform == "darwin":
import subprocess
def show_message_box(title, message):
script = f'display dialog "{message}" with title "{title}" buttons {{"OK"}} default button "OK"'
subprocess.run(["osascript", "-e", script])
def show_notification(title, message):
script = f'display notification "{message}" with title "{title}"'
subprocess.run(["osascript", "-e", script])
else: # Assume Linux
from gi.repository import Notify
def show_message_box(title, message):
Notify.init("App")
notification = Notify.Notification.new(title, message)
notification.show()
def show_notification(title, message):
Notify.init("App")
notification = Notify.Notification.new(title, message)
notification.show()
# Example usage:
event = "error" # This can be any special event like "risk_control" or "error" or something else.
if event == "risk_control":
#show_message_box("触发风控", "类型:手机号验证,请尽快查看")
show_notification("触发风控", "类型:手机号验证,请尽快查看")
elif event == "error":
#show_message_box("出现错误", "发生错误,请尝试重启应用")
show_notification("出现错误", "发生错误,请尝试重启应用")
#...
Additional Notes:
- This implementation uses
ctypes
andwin10toast
for Windows,osascript
for macOS, andgi.repository.Notify
for Linux. - The MessageBox provides a blocking alert that requires user acknowledgment, while the Notification provides a non-blocking alert.
Benefits:
- Immediate user feedback on critical events.
- Enhanced user experience through timely and clear notifications.