jiaweiqi 3 년 전
부모
커밋
051f28abfd
5개의 변경된 파일59개의 추가작업 그리고 2개의 파일을 삭제
  1. 1 0
      apps/activity/urls.py
  2. 34 0
      apps/activity/views.py
  3. 1 1
      uis/layuiadmin/layui/ueditor/umeditor.config.js
  4. 4 1
      uis/views/activity/edit.html
  5. 19 0
      util/file_operation.py

+ 1 - 0
apps/activity/urls.py

@@ -27,5 +27,6 @@ urlpatterns = (
     url(r'^activity/signs_save/$', activity_signs_save),
     url(r'^activity/coupon_save/$', activity_coupon_save),
     url(r'^activity/code/$', activity_code),
+    url(r'^ueditor/images/$', ueditor_image),
 )
 

+ 34 - 0
apps/activity/views.py

@@ -23,6 +23,8 @@ from apps.account.models import Branch
 from apps.WechatApplet.models import WechatApplet
 from .base import OrderUpdate
 from util.format import Formater
+from util.file_operation import PathAndRename, resizePicture
+from django.http import JsonResponse as DJR
 
 
 @token_required
@@ -415,3 +417,35 @@ def activity_code(request):
     except Exception as e:
         traceback.print_exc()
         return JSONError(u'获取失败!')
+
+
+def ueditor_image(request):
+    try:
+        upload_path = PathAndRename("upload/")
+
+        data = {}
+        if request.FILES.getlist('upfile'):
+            document = request.FILES.getlist('upfile')[0]
+            type = document.name.split('.')[-1]
+            name = timezone.now().strftime('%Y%m%d%H%M%S%f')
+            filename = "%s-%s.%s" % (
+                upload_path.path, name, type)
+            filename = filename.lower()
+            full_filename = "%s/%s" % (settings.MEDIA_ROOT, filename)
+            with open(full_filename, 'wb+') as destination:
+                for chunk in document.chunks():
+                    destination.write(chunk)
+
+            resizePicture(full_filename, 500)
+            data = {
+                "state": "SUCCESS",
+                "original": "%s.%s" % (name, type),
+                "title": "%s.%s" % (name, type),
+                "url": 'https://jpm.zzly.vip' + settings.MEDIA_URL + filename
+            }
+        return DJR(data, safe=False)
+    except CustomError as e:
+        return JSONError(e.get_error_msg())
+    except Exception as e:
+        traceback.print_exc()
+        return JSONError(u'图片上传失败!')

+ 1 - 1
uis/layuiadmin/layui/ueditor/umeditor.config.js

@@ -132,7 +132,7 @@
      */
     window.UMEDITOR_CONFIG = {
 
-        convertImageToBase64Enable: true,
+        convertImageToBase64Enable: false,
 
         //为编辑器实例添加一个路径,这个不能被注释
         UMEDITOR_HOME_URL : URL

+ 4 - 1
uis/views/activity/edit.html

@@ -110,7 +110,10 @@
     ,form = layui.form;
     var id = layui.view.getParameterByName('id');
 
-    var um = UM.getEditor('myEditor');
+    var um = UM.getEditor('myEditor', {
+        imageUrl: '/activity/ueditor/images/',
+        imagePath: "",
+    });
     um.setWidth('100%');
     $(".edui-body-container").css("width", "98%");
 

+ 19 - 0
util/file_operation.py

@@ -5,6 +5,25 @@ import requests
 from django.conf import settings
 from django.utils import timezone
 from django.utils.deconstruct import deconstructible
+from PIL import Image
+
+
+def resizePicture(file, width):
+    '''如果图片宽度或高度超过width,将图片宽度或高度修改为width'''
+    try:
+        img = Image.open(file)
+        w, h = img.size
+        if w > width or h > width:
+            if w > h:
+                size = (width, int(h * width / w))
+            elif w < h:
+                size = (int(w * width / h), width)
+            else:
+                size = (width, width)
+            img = img.resize(size)
+            img.save(file)
+    except Exception as e:
+        pass
 
 
 def UploadFile(file, upload_path):