From 4dd85fe4bd598c10e3b1210f9f32a749eea33284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Wed, 3 Jun 2026 10:38:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=83=E5=8B=A4=E6=9C=BA=E4=BA=BA=E8=84=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- attendance_iot/models/attendance_iot_user.py | 30 +++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/attendance_iot/models/attendance_iot_user.py b/attendance_iot/models/attendance_iot_user.py index e0b36edaa..50e715a5d 100644 --- a/attendance_iot/models/attendance_iot_user.py +++ b/attendance_iot/models/attendance_iot_user.py @@ -58,11 +58,16 @@ class AttendanceIotUser(models.Model): 'user_id': f'{r.employee_id.mobile_phone}', 'name': r.employee_id.name } - if r.employee_id.avatar_image: - raw = r.employee_id.avatar_image - b64 = base64.b64encode(raw).decode('ascii') - # 设备要求:base64 字符串 + URL 编码,不要 data:image/... 前缀 - face_template = urllib.parse.quote(b64, encoding='utf-8') + if r.employee_id.compress_image: + raw = r.employee_id.compress_image + # compress_image 可能是原图 bytes,也可能是 base64 bytes,需要兼容 + try: + b64 = raw.decode('ascii') # 已经是 base64 文本 + except UnicodeDecodeError: + b64 = base64.b64encode(raw).decode('ascii') # 原图 bytes,编码 + # 设备要求:data:image/jpeg;base64, + base64 再 URL 编码 + face_template = urllib.parse.quote( + f"data:image/jpeg;base64,{b64}", encoding='utf-8') add_value.update({'face_template': face_template}) self.env['iot.message'].create([{ 'name': '增加用户', @@ -84,11 +89,16 @@ class AttendanceIotUser(models.Model): 'user_id': f'{record.employee_id.mobile_phone}', 'name': record.employee_id.name } - if record.employee_id.avatar_image: - raw = record.employee_id.avatar_image - b64 = base64.b64encode(raw).decode('ascii') - # 设备要求:base64 字符串 + URL 编码,不要 data:image/... 前缀 - face_template = urllib.parse.quote(b64, encoding='utf-8') + if record.employee_id.compress_image: + raw = record.employee_id.compress_image + # compress_image 可能是原图 bytes,也可能是 base64 bytes,需要兼容 + try: + b64 = raw.decode('ascii') # 已经是 base64 文本 + except UnicodeDecodeError: + b64 = base64.b64encode(raw).decode('ascii') # 原图 bytes,编码 + # 设备要求:data:image/jpeg;base64, + base64 再 URL 编码 + face_template = urllib.parse.quote( + f"data:image/jpeg;base64,{b64}", encoding='utf-8') update_value.update({'face_template': face_template}) _logger.info(f"action_update_iot_info--{update_value}") self.env['iot.message'].create([{