peng 2 năm trước cách đây
mục cha
commit
6004edf90a
2 tập tin đã thay đổi với 8 bổ sung42 xóa
  1. 0 36
      background/serializers.py
  2. 8 6
      background/views.py

+ 0 - 36
background/serializers.py

@@ -5,42 +5,6 @@ from rest_framework.serializers import ModelSerializer, SerializerMethodField
 from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo
 
 
-class ReadWriteSerializerMethodField(SerializerMethodField):
-    """
-    支持可读写的SerializerMethodField
-    可实现Model字段和Serializer字段更加灵活地解绑
-    通过实现get_xxxfield方法,实现从Model的某个字段读值映射到Serializer对应字段
-    通过实现set_xxxfield方法,实现从Serializer字段回填值到Model对应字段
-    """
-
-    def __init__(self, method_name=None, write_method_name=None, **kwargs):
-        self.method_name = method_name
-        self.write_method_name = write_method_name
-        kwargs["source"] = "*"
-        super(SerializerMethodField, self).__init__(**kwargs)
-
-    def bind(self, field_name, parent):
-        # 绑定读函数 get_{field_name} 和写函数 set_{field_name}
-        default_method_name = f"get_{field_name}"
-        default_write_method_name = f"set_{field_name}"
-
-        if self.method_name is None:
-            self.method_name = default_method_name
-        if self.write_method_name is None:
-            self.write_method_name = default_write_method_name
-        super(SerializerMethodField, self).bind(field_name, parent)
-
-    def to_representation(self, value):
-        # 读取过程hook
-        method = getattr(self.parent, self.method_name)
-        return method(value)
-
-    def to_internal_value(self, data):
-        # 写入过程hook
-        method = getattr(self.parent, self.write_method_name)
-        return method(data)
-
-
 class ProductInfoSerializer(ModelSerializer):
     class Meta:
         model = ProductInfo

+ 8 - 6
background/views.py

@@ -8,7 +8,7 @@ from rest_framework.views import APIView
 from rest_framework.viewsets import ModelViewSet
 
 from background.Object import AmazonS3Util, RedisObject, TokenObject
-# from background.author import MyAuthentication
+from background.author import MyAuthentication
 from background.serializers import ProductInfoSerializer, VideoInSerializer, QuickStartInfoSerializer, \
     UpgradeFirmwareInfoSerializer
 from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo, WechatUserInfo
@@ -21,18 +21,18 @@ bucket = 'ansjerfilemanager'
 class ProductInfoSet(ModelViewSet):
     queryset = ProductInfo.objects.all()
     serializer_class = ProductInfoSerializer
-    # authentication_classes = [MyAuthentication, ]
+    authentication_classes = [MyAuthentication, ]
 
 
 class VideoInfoSet(ModelViewSet):
-    # authentication_classes = [MyAuthentication, ]
+    authentication_classes = [MyAuthentication, ]
     queryset = VideoInfo.objects.all()
     serializer_class = VideoInSerializer
     filterset_fields = ['id', 'product_info_id', 'title']
 
 
 class QuickStartInfoSet(ModelViewSet):
-    # authentication_classes = [MyAuthentication, ]
+    authentication_classes = [MyAuthentication, ]
     queryset = QuickStartInfo.objects.all()
     serializer_class = QuickStartInfoSerializer
     filterset_fields = ['id', 'product_info_id', 'title']
@@ -62,8 +62,8 @@ class UpgradeFirmwareInfoSet(ModelViewSet):
     queryset = UpgradeFirmwareInfo.objects.all()
     serializer_class = UpgradeFirmwareInfoSerializer
     filterset_fields = ['id', 'product_info_id', 'title']
+    authentication_classes = [MyAuthentication, ]
 
-    # authentication_classes = [MyAuthentication, ]
     def create(self, request, *args, **kwargs):
         link = request.data.get('link')
         file_key = 'vsees/upgrade_firmware_file/{}'.format(link)
@@ -137,6 +137,7 @@ class WechatLoginView(APIView):
             return Response({'code': 444, 'result': {'error_msg': '缺少参数'}})
         # 验证state,获取token
         access_token = cls.get_access_token(state)
+
         if not access_token:
             return Response({'code': 120, 'result': {'error_msg': '获取token失败'}})
         data = {
@@ -186,7 +187,8 @@ class WechatLoginView(APIView):
         data = token_response.json()
         if data.get('errcode') == 0:
             access_token = data['access_token']
-            redis_client.set_data('enterprise_wechat_access_token', access_token)
+            expires_in = data['expires_in']
+            redis_client.set_data('enterprise_wechat_access_token', access_token, expires_in)
             return access_token
         else:
             return False