From 32e82441c2d855a7dc2a2278d14e70f0efc8047b Mon Sep 17 00:00:00 2001 From: jayhgq Date: Mon, 9 Sep 2024 23:30:04 +0800 Subject: [PATCH] =?UTF-8?q?1.api=E4=B8=AD=E6=B7=BB=E5=8A=A0Base64=E7=AE=97?= =?UTF-8?q?=E6=B3=95=202.apps=E4=B8=AD=E6=B7=BB=E5=8A=A0home=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=B5=8B=E8=AF=95=E7=AE=97=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=90=8E=E9=9D=A2=E5=B0=86=E9=A6=96=E9=A1=B5=E5=86=99=E5=88=B0?= =?UTF-8?q?=E6=AD=A4=E5=A4=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMS_Django_Backend/settings.py | 1 + CMS_Django_Backend/urls.py | 8 ++++--- apps/api/common.py | 36 +++++++++++++++++++++++++++++++- apps/home/__init__.py | 0 apps/home/admin.py | 3 +++ apps/home/apps.py | 6 ++++++ apps/home/migrations/__init__.py | 0 apps/home/models.py | 3 +++ apps/home/tests.py | 3 +++ apps/home/views.py | 14 +++++++++++++ 10 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 apps/home/__init__.py create mode 100644 apps/home/admin.py create mode 100644 apps/home/apps.py create mode 100644 apps/home/migrations/__init__.py create mode 100644 apps/home/models.py create mode 100644 apps/home/tests.py create mode 100644 apps/home/views.py diff --git a/CMS_Django_Backend/settings.py b/CMS_Django_Backend/settings.py index ef4ed54..b510628 100644 --- a/CMS_Django_Backend/settings.py +++ b/CMS_Django_Backend/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'apps.auth.apps.AuthConfig', 'apps.api.apps.ApiConfig', + 'apps.home.apps.HomeConfig', ] MIDDLEWARE = [ diff --git a/CMS_Django_Backend/urls.py b/CMS_Django_Backend/urls.py index 6d2af97..1aeb07e 100644 --- a/CMS_Django_Backend/urls.py +++ b/CMS_Django_Backend/urls.py @@ -18,11 +18,13 @@ from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static +from apps.home import views as home_views urlpatterns = [ # path('admin/', admin.site.urls), - # path('login', include('apps.login.urls')) - # path('auth', include('apps.auth.urls')) + # path('login', include('apps.login.urls')), + # path('auth', include('apps.auth.urls')), + path('home/', home_views.home, name='home'), path('api/', include('apps.api.urls')), - path('auth/', include('apps.auth.urls')) + path('auth/', include('apps.auth.urls')), ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) diff --git a/apps/api/common.py b/apps/api/common.py index 9a6c345..0d8d0a1 100644 --- a/apps/api/common.py +++ b/apps/api/common.py @@ -48,4 +48,38 @@ class CaesarCypherClass: class Base64CypherClass: """ Base64的加解密算法,最简单的加密方式,可加密短的文字、小图片、小文件,图片文件大小不宜超过10M - """ \ No newline at end of file + """ + + def __init__(self, *args, **kwargs): + import importlib + self.base64 = importlib.import_module('base64') + self.os = importlib.import_module('os') + self.time = importlib.import_module('time') + + @staticmethod + def base64_encode_str(self, s): + return self.base64.b64encode(s.encode('utf-8')) + + @staticmethod + def base64_decode_str(self, s): + return self.base64.b64decode(s).decode('utf-8') + + @staticmethod + def base64_encode_pic(self, pic): + if self.os.path.exists(pic): + with open(pic, 'rb') as f: + read_pic = open(pic, 'rb') + read_data = read_pic.read() + read_pic.close() + return self.base64.b64encode(read_data) + else: + return "图片路径不存在" + + @staticmethod + def base64_decode_pic(self, pic_bs64): + if self.os.path.exists(f"upload/temp/pic{int(self.time.time())}.jpg"): + self.os.remove(f"upload/temp/pic{int(self.time.time())}.jpg") + with open(f"upload/temp/pic{int(self.time.time())}.jpg", 'wb') as f: + f.write(self.base64.b64decode(pic_bs64)) + + return 'upload/temp/pic.jpg' diff --git a/apps/home/__init__.py b/apps/home/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/home/admin.py b/apps/home/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/apps/home/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/apps/home/apps.py b/apps/home/apps.py new file mode 100644 index 0000000..c11732f --- /dev/null +++ b/apps/home/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class HomeConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'apps.home' diff --git a/apps/home/migrations/__init__.py b/apps/home/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/home/models.py b/apps/home/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/apps/home/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/apps/home/tests.py b/apps/home/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/apps/home/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/apps/home/views.py b/apps/home/views.py new file mode 100644 index 0000000..231b8bf --- /dev/null +++ b/apps/home/views.py @@ -0,0 +1,14 @@ +from django.shortcuts import render, HttpResponse +from apps.api.common import CaesarCypherClass, Base64CypherClass + + +# Create your views here. +def home(request): + bs64 = Base64CypherClass() + s = request.GET.get('s') + s_encode = bs64.base64_encode_pic(bs64, s) + if s_encode != "图片路径不存在": + s_decode = bs64.base64_decode_pic(bs64, s_encode) + else: + s_decode = '' + return HttpResponse(f"加密:{s_encode}\n解密:{s_decode}")