95 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
	
| import os
 | |
| from rest_framework.response import Response
 | |
| from rest_framework.views import APIView
 | |
| from rest_framework.parsers import MultiPartParser
 | |
| from rest_framework.permissions import IsAuthenticated
 | |
| from PIL import Image
 | |
| from django.conf import settings
 | |
| from rest_framework import status
 | |
| from datetime import datetime
 | |
| import cv2
 | |
| from server.settings import BASE_DIR
 | |
| import numpy as np
 | |
| 
 | |
| # class UploadFileView(APIView):
 | |
| #     permission_classes = [IsAuthenticated]
 | |
| #     parser_classes = (MultiPartParser,)
 | |
| 
 | |
| #     def post(self, request, *args, **kwargs):
 | |
| #         fileobj = request.FILES['file']
 | |
| #         file_name = fileobj.name.encode('utf-8').decode('utf-8')
 | |
| #         file_name_new = str(uuid.uuid1()) + '.' + file_name.split('.')[-1]
 | |
| #         subfolder = os.path.join('media', datetime.now().strftime("%Y%m%d"))
 | |
| #         if not os.path.exists(subfolder):
 | |
| #             os.mkdir(subfolder)
 | |
| #         file_path = os.path.join(subfolder, file_name_new)
 | |
| #         file_path = file_path.replace('\\', '/')
 | |
| #         with open(file_path, 'wb') as f:
 | |
| #             for chunk in fileobj.chunks():
 | |
| #                 f.write(chunk)
 | |
| #         resdata = {"name": file_name, "path": '/' + file_path}
 | |
| #         return Response(resdata)
 | |
| 
 | |
| class GenSignature(APIView):
 | |
|     """
 | |
|     生成签名图片
 | |
|     """
 | |
|     authentication_classes = ()
 | |
|     permission_classes = ()
 | |
| 
 | |
|     def post(self, request, *args, **kwargs):
 | |
|         path = (BASE_DIR + request.data['path']).replace('\\', '/')
 | |
|         try:
 | |
|             image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
 | |
|             size = image.shape
 | |
|             width = size[0]  # 宽度
 | |
|             height = size[1]  # 高度
 | |
|             if size[2] != 4:  # 判断
 | |
|                 background = np.zeros((size[0], size[1], 4))
 | |
|                 for yh in range(height):
 | |
|                     for xw in range(width):
 | |
|                         background[xw, yh, :3] = image[xw, yh]
 | |
|                         background[xw, yh, 3] = 255
 | |
|                 image = background
 | |
|             size = image.shape
 | |
|             for i in range(size[0]):
 | |
|                 for j in range(size[1]):
 | |
|                     if image[i][j][0]>100 and image[i][j][1]>100 and image[i][j][2]>100:
 | |
|                         image[i][j][3] = 0
 | |
|                     else:
 | |
|                         image[i][j][0],image[i][j][1],image[i][j][2] = 0,0,0
 | |
|             ext = os.path.splitext(path)
 | |
|             new_path = ext[0] + '.png'
 | |
|             cv2.imwrite(new_path, image)
 | |
|             return Response({'path':new_path.replace(BASE_DIR, '')}, status=status.HTTP_200_OK)
 | |
|         except:
 | |
|             return Response('签名照处理失败,请重新上传',
 | |
|                 status=status.HTTP_400_BAD_REQUEST)
 | |
|         
 | |
| 
 | |
| import time
 | |
| class UpdateDevelop(APIView):
 | |
|     """
 | |
|     更新开发服务器
 | |
|     """
 | |
|     authentication_classes = ()
 | |
|     permission_classes = ()
 | |
| 
 | |
|     def post(self, request, *args, **kwargs):
 | |
|         import os
 | |
|         # 更新后端
 | |
|         os.chdir('/home/hberp')
 | |
|         ret = os.popen('git pull https://caoqianming%40foxmail.com:9093qqww@e.coding.net/ctcdevteam/hberp/hberp.git develop')
 | |
|         # 奇怪的处理
 | |
|         os.chdir('/home/hberp/hb_server/vuedist')
 | |
|         os.popen('cp index.html indexbak')
 | |
|         time.sleep(1000)
 | |
|         os.popen('rm -rf index.html')
 | |
|         time.sleep(1000)
 | |
|         os.popen('mv -f indexbak index.html')
 | |
|         # 打包前端
 | |
|         # os.chdir('/home/hberp/hb_client')
 | |
|         # os.system('npm run build:prod')
 | |
|         # os.system('\cp -rf /home/hberp/hb_client/dist/* /home/hberp/hb_server/vuedist')
 | |
|         return Response(ret.read())
 |