@@ -51,118 +51,118 @@ async def wrapper(*args, **kwargs):
5151 # 获取请求的ip及ip归属区域
5252 oper_ip = request .headers .get ('remote_addr' ) if request .headers .get ('is_browser' ) == 'no' else request .headers .get ('X-Forwarded-For' )
5353 oper_location = '内网IP'
54- try :
55- if oper_ip != '127.0.0.1' and oper_ip != 'localhost' :
56- ip_result = requests .get (f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ oper_ip } ' )
57- if ip_result .status_code == 200 :
58- prov = ip_result .json ().get ('data' ).get ('prov' )
59- city = ip_result .json ().get ('data' ).get ('city' )
60- if prov or city :
61- oper_location = f'{ prov } -{ city } '
54+ if AppConfig .app_ip_location_query :
55+ try :
56+ if oper_ip != '127.0.0.1' and oper_ip != 'localhost' :
57+ ip_result = requests .get (f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ oper_ip } ' )
58+ if ip_result .status_code == 200 :
59+ prov = ip_result .json ().get ('data' ).get ('prov' )
60+ city = ip_result .json ().get ('data' ).get ('city' )
61+ if prov or city :
62+ oper_location = f'{ prov } -{ city } '
63+ else :
64+ oper_location = '未知'
6265 else :
6366 oper_location = '未知'
64- else :
65- oper_location = '未知'
66- except Exception as e :
67- oper_location = '未知'
68- print (e )
69- finally :
70- # 根据不同的请求类型使用不同的方法获取请求参数
71- content_type = request .headers .get ("Content-Type" )
72- if content_type and ("multipart/form-data" in content_type or 'application/x-www-form-urlencoded' in content_type ):
73- payload = await request .form ()
74- oper_param = "\n " .join ([f"{ key } : { value } " for key , value in payload .items ()])
75- else :
76- payload = await request .body ()
77- oper_param = json .dumps (json .loads (str (payload , 'utf-8' )), ensure_ascii = False )
78- # 日志表请求参数字段长度最大为2000,因此在此处判断长度
79- if len (oper_param ) > 2000 :
80- oper_param = '请求参数过长'
67+ except Exception as e :
68+ oper_location = '未知'
69+ print (e )
70+ # 根据不同的请求类型使用不同的方法获取请求参数
71+ content_type = request .headers .get ("Content-Type" )
72+ if content_type and ("multipart/form-data" in content_type or 'application/x-www-form-urlencoded' in content_type ):
73+ payload = await request .form ()
74+ oper_param = "\n " .join ([f"{ key } : { value } " for key , value in payload .items ()])
75+ else :
76+ payload = await request .body ()
77+ oper_param = json .dumps (json .loads (str (payload , 'utf-8' )), ensure_ascii = False )
78+ # 日志表请求参数字段长度最大为2000,因此在此处判断长度
79+ if len (oper_param ) > 2000 :
80+ oper_param = '请求参数过长'
8181
82- # 获取操作时间
83- oper_time = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
84- # 此处在登录之前向原始函数传递一些登录信息,用于监测在线用户的相关信息
85- login_log = {}
86- if log_type == 'login' :
87- user_agent_info = parse (user_agent )
88- browser = f'{ user_agent_info .browser .family } '
89- system_os = f'{ user_agent_info .os .family } '
90- if user_agent_info .browser .version != ():
91- browser += f' { user_agent_info .browser .version [0 ]} '
92- if user_agent_info .os .version != ():
93- system_os += f' { user_agent_info .os .version [0 ]} '
94- login_log = dict (
95- ipaddr = oper_ip ,
96- login_location = oper_location ,
97- browser = browser ,
98- os = system_os ,
99- login_time = oper_time
100- )
101- kwargs ['form_data' ].login_info = login_log
102- # 调用原始函数
103- result = await func (* args , ** kwargs )
104- # 获取请求耗时
105- cost_time = float (time .time () - start_time ) * 100
106- # 判断请求是否来自api文档
107- request_from_swagger = request .headers .get ('referer' ).endswith ('docs' ) if request .headers .get ('referer' ) else False
108- request_from_redoc = request .headers .get ('referer' ).endswith ('redoc' ) if request .headers .get ('referer' ) else False
109- # 根据响应结果的类型使用不同的方法获取响应结果参数
110- if isinstance (result , JSONResponse ) or isinstance (result , ORJSONResponse ) or isinstance (result , UJSONResponse ):
111- result_dict = json .loads (str (result .body , 'utf-8' ))
82+ # 获取操作时间
83+ oper_time = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
84+ # 此处在登录之前向原始函数传递一些登录信息,用于监测在线用户的相关信息
85+ login_log = {}
86+ if log_type == 'login' :
87+ user_agent_info = parse (user_agent )
88+ browser = f'{ user_agent_info .browser .family } '
89+ system_os = f'{ user_agent_info .os .family } '
90+ if user_agent_info .browser .version != ():
91+ browser += f' { user_agent_info .browser .version [0 ]} '
92+ if user_agent_info .os .version != ():
93+ system_os += f' { user_agent_info .os .version [0 ]} '
94+ login_log = dict (
95+ ipaddr = oper_ip ,
96+ login_location = oper_location ,
97+ browser = browser ,
98+ os = system_os ,
99+ login_time = oper_time
100+ )
101+ kwargs ['form_data' ].login_info = login_log
102+ # 调用原始函数
103+ result = await func (* args , ** kwargs )
104+ # 获取请求耗时
105+ cost_time = float (time .time () - start_time ) * 100
106+ # 判断请求是否来自api文档
107+ request_from_swagger = request .headers .get ('referer' ).endswith ('docs' ) if request .headers .get ('referer' ) else False
108+ request_from_redoc = request .headers .get ('referer' ).endswith ('redoc' ) if request .headers .get ('referer' ) else False
109+ # 根据响应结果的类型使用不同的方法获取响应结果参数
110+ if isinstance (result , JSONResponse ) or isinstance (result , ORJSONResponse ) or isinstance (result , UJSONResponse ):
111+ result_dict = json .loads (str (result .body , 'utf-8' ))
112+ else :
113+ if request_from_swagger or request_from_redoc :
114+ result_dict = {}
112115 else :
113- if request_from_swagger or request_from_redoc :
114- result_dict = {}
116+ if result . status_code == 200 :
117+ result_dict = {'code' : result . status_code , 'message' : '获取成功' }
115118 else :
116- if result .status_code == 200 :
117- result_dict = {'code' : result .status_code , 'message' : '获取成功' }
118- else :
119- result_dict = {'code' : result .status_code , 'message' : '获取失败' }
120- json_result = json .dumps (dict (code = result_dict .get ('code' ), message = result_dict .get ('message' )), ensure_ascii = False )
121- # 根据响应结果获取响应状态及异常信息
122- status = 1
123- error_msg = ''
124- if result_dict .get ('code' ) == 200 :
125- status = 0
119+ result_dict = {'code' : result .status_code , 'message' : '获取失败' }
120+ json_result = json .dumps (dict (code = result_dict .get ('code' ), message = result_dict .get ('message' )), ensure_ascii = False )
121+ # 根据响应结果获取响应状态及异常信息
122+ status = 1
123+ error_msg = ''
124+ if result_dict .get ('code' ) == 200 :
125+ status = 0
126+ else :
127+ error_msg = result_dict .get ('message' )
128+ # 根据日志类型向对应的日志表插入数据
129+ if log_type == 'login' :
130+ # 登录请求来自于api文档时不记录登录日志,其余情况则记录
131+ if request_from_swagger or request_from_redoc :
132+ pass
126133 else :
127- error_msg = result_dict .get ('message' )
128- # 根据日志类型向对应的日志表插入数据
129- if log_type == 'login' :
130- # 登录请求来自于api文档时不记录登录日志,其余情况则记录
131- if request_from_swagger or request_from_redoc :
132- pass
133- else :
134- user = kwargs .get ('form_data' )
135- user_name = user .username
136- login_log ['user_name' ] = user_name
137- login_log ['status' ] = str (status )
138- login_log ['msg' ] = result_dict .get ('message' )
134+ user = kwargs .get ('form_data' )
135+ user_name = user .username
136+ login_log ['user_name' ] = user_name
137+ login_log ['status' ] = str (status )
138+ login_log ['msg' ] = result_dict .get ('message' )
139139
140- LoginLogService .add_login_log_services (query_db , LogininforModel (** login_log ))
141- else :
142- current_user = await get_current_user (request , token , query_db )
143- oper_name = current_user .user .user_name
144- dept_name = current_user .dept .dept_name if current_user .dept else None
145- operation_log = dict (
146- title = title ,
147- business_type = business_type ,
148- method = func_path ,
149- request_method = request_method ,
150- operator_type = operator_type ,
151- oper_name = oper_name ,
152- dept_name = dept_name ,
153- oper_url = oper_url ,
154- oper_ip = oper_ip ,
155- oper_location = oper_location ,
156- oper_param = oper_param ,
157- json_result = json_result ,
158- status = status ,
159- error_msg = error_msg ,
160- oper_time = oper_time ,
161- cost_time = cost_time
162- )
163- OperationLogService .add_operation_log_services (query_db , OperLogModel (** operation_log ))
140+ LoginLogService .add_login_log_services (query_db , LogininforModel (** login_log ))
141+ else :
142+ current_user = await get_current_user (request , token , query_db )
143+ oper_name = current_user .user .user_name
144+ dept_name = current_user .dept .dept_name if current_user .dept else None
145+ operation_log = dict (
146+ title = title ,
147+ business_type = business_type ,
148+ method = func_path ,
149+ request_method = request_method ,
150+ operator_type = operator_type ,
151+ oper_name = oper_name ,
152+ dept_name = dept_name ,
153+ oper_url = oper_url ,
154+ oper_ip = oper_ip ,
155+ oper_location = oper_location ,
156+ oper_param = oper_param ,
157+ json_result = json_result ,
158+ status = status ,
159+ error_msg = error_msg ,
160+ oper_time = oper_time ,
161+ cost_time = cost_time
162+ )
163+ OperationLogService .add_operation_log_services (query_db , OperLogModel (** operation_log ))
164164
165- return result
165+ return result
166166
167167 return wrapper
168168
0 commit comments