today_ai_weather/app/controllers/application_controller.rb

81 lines
2.5 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class ApplicationController < ActionController::Base
include SeoConcern
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
before_action :log_browser_info
# allow_browser versions: :modern
allow_browser versions: :modern,
patterns: [
/Mobile Safari/,
/Chrome\/[\d.]+/,
/Quark\/[\d.]+/,
/HuaweiBrowser\/[\d.]+/,
/MiuiBrowser\/[\d.]+/,
/VivoBrowser\/[\d.]+/,
/OppoBrowser\/[\d.]+/,
/UCBrowser\/[\d.]+/,
/QQBrowser\/[\d.]+/,
/MicroMessenger\/[\d.]+/,
/Alipay/,
/BaiduBoxApp/,
/baiduboxapp/i,
/SogouMobile/,
/Weibo/,
/DingTalk/,
/ToutiaoMicroApp/,
/BytedanceWebview/,
/ArkWeb/
],
on_failure: -> (browser) {
Rails.logger.warn <<~BROWSER_INFO
Browser Blocked:
User Agent: #{browser.ua}
Name: #{browser.name}
Version: #{browser.version}
Platform: #{browser.platform.name}
Device: #{browser.device.name}
Mobile: #{browser.mobile?}
Modern: #{browser.modern?}
Bot: #{browser.bot?}
BROWSER_INFO
}
before_action :set_locale
after_action :track_action
def log_browser_info
# 构建详细的浏览器信息
browser_info = {
user_agent: request.user_agent,
path: request.path,
browser_name: browser.name,
browser_version: browser.version,
platform: browser.platform.name,
device: browser.device.name,
mobile: browser.mobile?,
tablet: browser.tablet?,
bot: browser.bot?,
matched_patterns: matched_browser_patterns,
timestamp: Time.current
}
# 使用 Rails.logger 记录信息both development and production
Rails.logger.info "Browser Info: #{browser_info.to_json}"
# 如果是被拦截的浏览器,记录额外信息
unless browser_allowed?
Rails.logger.warn "Browser Blocked: #{browser_info.to_json}"
end
end
protected
def track_action
ahoy.track "Viewed Application", request.path_parameters
end
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end