From 74087493bbb53f71c7df4efd0c05f95503ccf06c Mon Sep 17 00:00:00 2001 From: ivan deng Date: Sun, 21 May 2023 00:53:29 +0800 Subject: [PATCH] =?UTF-8?q?common=20=E4=B8=AD=E5=A2=9E=E5=8A=A0=20ua=5Ftyp?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_common/controllers/main.py | 41 ++---------------------- app_common/models/__init__.py | 2 ++ app_common/models/ir_http.py | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 app_common/models/ir_http.py diff --git a/app_common/controllers/main.py b/app_common/controllers/main.py index e0aee2cb..c4e4fc3f 100644 --- a/app_common/controllers/main.py +++ b/app_common/controllers/main.py @@ -6,6 +6,8 @@ import requests from math import radians, cos, sin, asin, sqrt from ..lib.user_agents import parse +from ..models.ir_http import get_ua_type + from odoo import api, http, SUPERUSER_ID, _ from odoo import http, exceptions @@ -36,44 +38,7 @@ class AppController(http.Controller): return request.make_response(ustr, [('Content-Type', 'text/html')]) def get_ua_type(self): - ua = request.httprequest.headers.get('User-Agent') - # 临时用 agent 处理,后续要前端中正确处理或者都从后台来 - # 微信浏览器 - # MicroMessenger: Mozilla/5.0 (Linux; Android 10; ELE-AL00 Build/HUAWEIELE-AL00; wv) - # AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 - # MQQBrowser/6.2 TBS/045525 Mobile Safari/537.36 MMWEBID/3135 MicroMessenger/8.0.2.1860(0x2800023B) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 - # 微信浏览器,开发工具,网页 iphone - # ,Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 - # wechatdevtools/1.03.2011120 MicroMessenger/7.0.4 Language/zh_CN webview/16178807094901773 - # webdebugger port/27772 token/b91f4a234b918f4e2a5d1a835a09c31e - - # 微信小程序 - # MicroMessenger: Mozilla/5.0 (Linux; Android 10; ELE-AL00 Build/HUAWEIELE-AL00; wv) - # AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2767 MMWEBSDK/20210302 Mobile Safari/537.36 MMWEBID/6689 MicroMessenger/8.0.2.1860(0x2800023B) Process/appbrand2 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 - # MiniProgramEnv/android - # 微信浏览器,开发工具,小程序,iphone - # Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 - # wechatdevtools/1.03.2011120 MicroMessenger/7.0.4 Language/zh_CN webview/ - # 微信内,iphone web - # Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 - # MicroMessenger/8.0.3(0x1800032a) NetType/WIFI Language/zh_CN - # 安卓app,h5 - # ELE-AL00(Android/10) (cn.erpapp.o20sticks.App/13.20.12.09) Weex/0.26.0 1080x2265 - - utype = 'web' - # todo: 引入现成 py lib,处理企业微信 - if 'MicroMessenger' in ua and 'webdebugger' not in ua and ('MiniProgramEnv' in ua or 'wechatdevtools' in ua): - # 微信小程序及开发者工具 - utype = 'wxapp' - elif 'MicroMessenger' in ua: - # 微信浏览器 - utype = 'wxweb' - elif 'cn.erpapp.o20sticks.App' in ua: - # 安卓app - utype = 'native_android' - # _logger.warning('=========get ua %s,%s' % (utype, ua)) - return utype - + return get_ua_type() def haversine(lon1, lat1, lon2, lat2): # 计算地图上两点的距离 diff --git a/app_common/models/__init__.py b/app_common/models/__init__.py index 2619b0b3..c2e3502c 100644 --- a/app_common/models/__init__.py +++ b/app_common/models/__init__.py @@ -28,6 +28,8 @@ from . import ir_cron from . import res_users from . import ir_mail_server from . import mail_mail +from . import ir_http + diff --git a/app_common/models/ir_http.py b/app_common/models/ir_http.py new file mode 100644 index 00000000..833d433c --- /dev/null +++ b/app_common/models/ir_http.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + + +from odoo import models +from odoo.http import request + + +class IrHttp(models.AbstractModel): + _inherit = 'ir.http' + + def session_info(self): + result = super(IrHttp, self).session_info() + result['ua_type'] = self.get_ua_type() + return result + + def get_ua_type(self): + return get_ua_type() + +def get_ua_type(): + ua = request.httprequest.headers.get('User-Agent') + # 临时用 agent 处理,后续要前端中正确处理或者都从后台来 + # 微信浏览器 + # MicroMessenger: Mozilla/5.0 (Linux; Android 10; ELE-AL00 Build/HUAWEIELE-AL00; wv) + # AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 + # MQQBrowser/6.2 TBS/045525 Mobile Safari/537.36 MMWEBID/3135 MicroMessenger/8.0.2.1860(0x2800023B) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 + # 微信浏览器,开发工具,网页 iphone + # ,Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 + # wechatdevtools/1.03.2011120 MicroMessenger/7.0.4 Language/zh_CN webview/16178807094901773 + # webdebugger port/27772 token/b91f4a234b918f4e2a5d1a835a09c31e + + # 微信小程序 + # MicroMessenger: Mozilla/5.0 (Linux; Android 10; ELE-AL00 Build/HUAWEIELE-AL00; wv) + # AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2767 MMWEBSDK/20210302 Mobile Safari/537.36 MMWEBID/6689 MicroMessenger/8.0.2.1860(0x2800023B) Process/appbrand2 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 + # MiniProgramEnv/android + # 微信浏览器,开发工具,小程序,iphone + # Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 + # wechatdevtools/1.03.2011120 MicroMessenger/7.0.4 Language/zh_CN webview/ + # 微信内,iphone web + # Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 + # MicroMessenger/8.0.3(0x1800032a) NetType/WIFI Language/zh_CN + # 安卓app,h5 + # ELE-AL00(Android/10) (cn.erpapp.o20sticks.App/13.20.12.09) Weex/0.26.0 1080x2265 + + # web 表示普通浏览器,后续更深入处理 + utype = 'web' + # todo: 引入现成 py lib,处理企业微信 + if 'MicroMessenger' in ua and 'webdebugger' not in ua and ('MiniProgramEnv' in ua or 'wechatdevtools' in ua): + # 微信小程序及开发者工具 + utype = 'wxapp' + elif 'MicroMessenger' in ua: + # 微信浏览器 + utype = 'wxweb' + elif 'cn.erpapp.o20sticks.App' in ua: + # 安卓app + utype = 'native_android' + # _logger.warning('=========get ua %s,%s' % (utype, ua)) + return utype