Ver Fonte

修复消息列表全部已读异常问题

zhangdongming há 1 mês atrás
pai
commit
e815598243
1 ficheiros alterados com 41 adições e 11 exclusões
  1. 41 11
      Controller/EquipmentInfo.py

+ 41 - 11
Controller/EquipmentInfo.py

@@ -7,12 +7,13 @@ from django.views.generic.base import View
 
 from Model.models import Device_Info, Equipment_Info, UidPushModel
 from Model.models import Device_User
+from Ansjer.config import LOGGER
+from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
 from Service.CommonService import CommonService
 from Service.EquipmentInfoService import EquipmentInfoService
 from Service.ModelService import ModelService
-from Ansjer.config import LOGGER
 
 
 class EquipmentInfo(View):
@@ -38,15 +39,15 @@ class EquipmentInfo(View):
         operation = request_dict.get('operation', None)
         if not userID:
             return response.json(309)
-        if operation == 'query':  # 查询推送消息
+        if operation == 'query':    # 查询推送消息
             return self.query_info(request_dict, userID, response)
         elif operation == 'add':
             return self.add_info(request_dict, userID, response)
-        elif operation == 'update':  # 已读推送消息
+        elif operation == 'update':     # 已读推送消息
             return self.update_info(request_dict, userID, response)
-        elif operation == 'update-answer-status':  # 已接听一键通话消息
+        elif operation == 'update-answer-status':   # 已接听一键通话消息
             return self.update_answer_status(request_dict, userID, response)
-        elif operation == 'delete':  # 删除推送消息
+        elif operation == 'delete':     # 删除推送消息
             return self.delete_info(request_dict, response)
         elif operation == 'findByTime':
             return self.findByTime_info(request_dict, userID, response)
@@ -98,17 +99,21 @@ class EquipmentInfo(View):
     def update_info(request_dict, userID, response):
         is_update_all = int(request_dict.get('is_update_all', 0))
         event_type = request_dict.get('eventType', None)
+        redis_obj = RedisObject()
+
         if is_update_all == 1:
-            kwargs = {
-                'device_user_id': userID,
-                'status': 0
-            }
+            kwargs = {'device_user_id': userID, 'status': 0}
             if event_type:
                 kwargs['event_type'] = int(event_type)
             try:
                 EquipmentInfoService.all_read_equipment_info(**kwargs)
+
+                # 使用方法清除缓存
+                EquipmentInfo.clear_unread_message_cache(userID, redis_obj)
+
             except Exception as e:
-                print(repr(e))
+                error_line = e.__traceback__.tb_lineno
+                LOGGER.error(f'批量更新失败 user:{userID}, error_line:{error_line}, error_msg:{repr(e)}')
         else:
             id_list = request_dict.getlist('id[]', None)
             if id_list is None or len(id_list) < 1:
@@ -120,11 +125,36 @@ class EquipmentInfo(View):
                     ei_id = int(full_id[2:])
                     equipment_info_model = EquipmentInfoService.get_equipment_info_model_with_full_id(full_id)
                     equipment_info_model.objects.filter(id=ei_id).update(status=1)
+
+                    #  每条消息更新后也尝试清除缓存
+                    EquipmentInfo.clear_unread_message_cache(userID, redis_obj)
+
                 except Exception as e:
-                    LOGGER.info('equipment/info/update接口报错:{}'.format(repr(e)))
+                    error_line = e.__traceback__.tb_lineno
+                    LOGGER.error(f'单条更新失败 user:{userID}, error_line:{error_line}, error_msg:{repr(e)}')
+                    continue
         return response.json(0)
 
     @staticmethod
+    def clear_unread_message_cache(userID, redis_obj):
+        """
+        清除用户未读消息缓存
+        :param userID: 用户ID
+        :param redis_obj: RedisObject 实例
+        :return: True/False
+        """
+        push_message_key = f'PUSH:MESSAGE:USER:{userID}'
+        try:
+            if not redis_obj.del_data(key=push_message_key):
+                LOGGER.info(f'[CACHE] Failed to delete Redis key: {push_message_key}')
+                return False
+            LOGGER.info(f'[CACHE] Successfully deleted Redis key: {push_message_key}')
+            return True
+        except Exception as e:
+            error_line = e.__traceback__.tb_lineno
+            LOGGER.error(f'清除缓存失败 user:{userID}, error_line:{error_line}, error_msg:{repr(e)}')
+            return False
+    @staticmethod
     def update_answer_status(request_dict, user_id, response):
         """
         更新一键通话消息状态为已接听