|
@@ -9,6 +9,9 @@
|
|
|
import logging
|
|
|
import time
|
|
|
|
|
|
+import boto3
|
|
|
+import botocore
|
|
|
+from botocore import client
|
|
|
from django.db import transaction
|
|
|
from django.db.models import Q
|
|
|
from django.views import View
|
|
@@ -18,6 +21,10 @@ from Model.models import DeviceSharePermission, DeviceChannelUserSet, DeviceChan
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Service.UserDeviceService import UserDeviceService
|
|
|
+from Ansjer.config import CONFIG_CN, CONFIG_INFO, CONFIG_TEST, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, \
|
|
|
+ AWS_SES_ACCESS_REGION, AWS_IOT_SES_ACCESS_CHINA_REGION
|
|
|
+from Model.models import DeviceWallpaper
|
|
|
+from Object.AWS.AmazonS3Util import AmazonS3Util
|
|
|
|
|
|
LOGGER = logging.getLogger('info')
|
|
|
|
|
@@ -46,6 +53,16 @@ class UserDeviceShareView(View):
|
|
|
return self.user_channel_permission_save(request_dict, response)
|
|
|
elif operation == 'permissions-test':
|
|
|
return self.synch_share_device_permission(response)
|
|
|
+ elif operation == 'getWallpaperList':
|
|
|
+ return self.get_wallpaper_list(request_dict, response)
|
|
|
+ elif operation == 'getUploadWallpaper':
|
|
|
+ return self.get_upload_wallpaper(request_dict, response)
|
|
|
+ elif operation == 'notifyUploadWallpaper':
|
|
|
+ return self.notify_upload_wallpaper(request_dict, response)
|
|
|
+ elif operation == 'delWallpaper':
|
|
|
+ return self.del_wallpaper(request_dict, response)
|
|
|
+ elif operation == 'selectWallpaper':
|
|
|
+ return self.select_wallpaper(request_dict, response)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
@@ -88,7 +105,7 @@ class UserDeviceShareView(View):
|
|
|
return response.json(0, data)
|
|
|
except Exception as e:
|
|
|
LOGGER.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@classmethod
|
|
|
def user_channel_permission_save(cls, request_dict, response):
|
|
@@ -137,7 +154,7 @@ class UserDeviceShareView(View):
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
LOGGER.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def qrcode_share_channel_permission_save(user_id, uid):
|
|
@@ -193,3 +210,262 @@ class UserDeviceShareView(View):
|
|
|
for item in device_info_qs:
|
|
|
UserDeviceShareView.qrcode_share_channel_permission_save(item['userID_id'], item['UID'])
|
|
|
return response.json(0)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_wallpaper_list(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 获取设备壁纸列表
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ device_type = int(request_dict.get('deviceType', None))
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ channel = int(request_dict.get('channel', 1))
|
|
|
+ LOGGER.info('获取设备壁纸列表参数:{}'.format(request_dict))
|
|
|
+ if not all([device_type, uid]):
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ # 查询用户自定义壁纸
|
|
|
+ user_wallpaper_qs = DeviceWallpaper.objects.filter(channel=channel, uid=uid,
|
|
|
+ device_type=device_type, parent_id=0, status=1)
|
|
|
+
|
|
|
+ # 查询系统默认壁纸
|
|
|
+ def_wallpaper_qs = DeviceWallpaper.objects.filter(classification=1, device_type=device_type, uid='',
|
|
|
+ status=1)
|
|
|
+
|
|
|
+ # 查询用户选中的壁纸
|
|
|
+ user_checked_qs = DeviceWallpaper.objects.filter(channel=channel, uid=uid,
|
|
|
+ device_type=device_type, parent_id__gt=0, status=1)
|
|
|
+ checked_id = user_checked_qs[0].parent_id if user_checked_qs.exists() else 0
|
|
|
+
|
|
|
+ wallpaper_list = []
|
|
|
+ if def_wallpaper_qs.exists() or user_wallpaper_qs.exists():
|
|
|
+ # 初始化存储桶客户端
|
|
|
+ if CONFIG_CN == CONFIG_INFO or CONFIG_TEST == CONFIG_INFO:
|
|
|
+ s3 = boto3.client(
|
|
|
+ 's3',
|
|
|
+ aws_access_key_id=AWS_ACCESS_KEY_ID[0],
|
|
|
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
|
|
|
+ config=botocore.client.Config(signature_version='s3v4'),
|
|
|
+ region_name='cn-northwest-1'
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ s3 = boto3.client(
|
|
|
+ 's3',
|
|
|
+ aws_access_key_id=AWS_ACCESS_KEY_ID[1],
|
|
|
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
|
|
|
+ config=botocore.client.Config(signature_version='s3v4'),
|
|
|
+ region_name='us-east-1'
|
|
|
+ )
|
|
|
+
|
|
|
+ # 处理系统默认壁纸和用户自定义壁纸
|
|
|
+ all_wallpapers_qs = def_wallpaper_qs.union(user_wallpaper_qs)
|
|
|
+ for item in all_wallpapers_qs:
|
|
|
+ obj_key = item.obj_prefix + item.obj_name
|
|
|
+ params = {
|
|
|
+ 'Key': obj_key,
|
|
|
+ 'Bucket': 'ansjerfilemanager',
|
|
|
+ }
|
|
|
+ response_url = s3.generate_presigned_url(
|
|
|
+ 'get_object', Params=params, ExpiresIn=3600)
|
|
|
+ wallpaper = {
|
|
|
+ 'id': item.id,
|
|
|
+ 'url': response_url,
|
|
|
+ 'state': 1 if checked_id == item.id else 0,
|
|
|
+ 'classification': item.classification
|
|
|
+ }
|
|
|
+ wallpaper_list.append(wallpaper)
|
|
|
+
|
|
|
+ return response.json(0, {'wallpapers': wallpaper_list})
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error('查询设备壁纸异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(5)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_upload_wallpaper(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 获取设备壁纸上传链接
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ channel = int(request_dict.get('channel', 1))
|
|
|
+
|
|
|
+ if not uid:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ # 初始化存储桶客户端
|
|
|
+ if CONFIG_CN == CONFIG_INFO or CONFIG_TEST == CONFIG_INFO:
|
|
|
+ s3 = boto3.client(
|
|
|
+ 's3',
|
|
|
+ aws_access_key_id=AWS_ACCESS_KEY_ID[0],
|
|
|
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
|
|
|
+ config=botocore.client.Config(signature_version='s3v4'),
|
|
|
+ region_name='cn-northwest-1'
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ s3 = boto3.client(
|
|
|
+ 's3',
|
|
|
+ aws_access_key_id=AWS_ACCESS_KEY_ID[1],
|
|
|
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
|
|
|
+ config=botocore.client.Config(signature_version='s3v4'),
|
|
|
+ region_name='us-east-1'
|
|
|
+ )
|
|
|
+
|
|
|
+ # 生成唯一的文件名
|
|
|
+ file_name = f"{int(time.time())}.png"
|
|
|
+ obj_key = f"app/static/device-wallpaper/{uid}/{file_name}"
|
|
|
+
|
|
|
+ # 生成预签名的 URL
|
|
|
+ presigned_url = s3.generate_presigned_url(
|
|
|
+ 'put_object',
|
|
|
+ Params={
|
|
|
+ 'Bucket': 'ansjerfilemanager',
|
|
|
+ 'Key': obj_key,
|
|
|
+ },
|
|
|
+ ExpiresIn=600
|
|
|
+ )
|
|
|
+
|
|
|
+ # 返回预签名的URL和字段
|
|
|
+ return response.json(0, {
|
|
|
+ 'uploadUrl': presigned_url,
|
|
|
+ 'fileName': file_name,
|
|
|
+ })
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error('获取上传链接异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(5)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def notify_upload_wallpaper(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 确认壁纸上传成功,创建DeviceWallpaper
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ file_name = request_dict.get('fileName', None)
|
|
|
+ channel = int(request_dict.get('channel', 1))
|
|
|
+
|
|
|
+ if not all([uid, file_name]):
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ device_info_qs = Device_Info.objects.filter(UID=uid).values('id', 'Type')
|
|
|
+ device_type = device_info_qs[0]['Type'] if device_info_qs.exists() else ''
|
|
|
+
|
|
|
+ DeviceWallpaper.objects.create(
|
|
|
+ device_type=device_type,
|
|
|
+ storage_type=1,
|
|
|
+ channel=channel,
|
|
|
+ uid=uid,
|
|
|
+ obj_prefix=f'app/static/device-wallpaper/{uid}/',
|
|
|
+ obj_name=file_name,
|
|
|
+ classification=2, # 自定义壁纸分类
|
|
|
+ status=1,
|
|
|
+ parent_id=0,
|
|
|
+ created_time=int(time.time())
|
|
|
+ )
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error('壁纸创建失败:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(5)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def del_wallpaper(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 删除设备壁纸
|
|
|
+ """
|
|
|
+ wallpaper_id = request_dict.get('wallpaperId', None)
|
|
|
+ if not all([wallpaper_id]):
|
|
|
+ return response.json(444)
|
|
|
+ try:
|
|
|
+ wallpaper_id = int(wallpaper_id)
|
|
|
+ if DeviceWallpaper.objects.filter(id=wallpaper_id, classification=1).exists():
|
|
|
+ return response.json(176, "系统图片不能删除")
|
|
|
+ device_wallpaper_qs = DeviceWallpaper.objects.filter(id=wallpaper_id)
|
|
|
+ device_wallpaper = device_wallpaper_qs.values("obj_prefix", "obj_name", "uid", "parent_id")
|
|
|
+ uid = device_wallpaper[0]['uid']
|
|
|
+ obj_name = device_wallpaper[0]['obj_name']
|
|
|
+
|
|
|
+ # 使用中壁纸被删除
|
|
|
+ use_device_wallpaper = DeviceWallpaper.objects.filter(uid=uid, parent_id__gt=0).first()
|
|
|
+ if use_device_wallpaper and use_device_wallpaper.parent_id == wallpaper_id:
|
|
|
+ system_device_wallpaper = DeviceWallpaper.objects.filter(id=1).first()
|
|
|
+ use_device_wallpaper.parent_id = 1
|
|
|
+ use_device_wallpaper.storage_type = system_device_wallpaper.storage_type
|
|
|
+ use_device_wallpaper.obj_name = system_device_wallpaper.obj_name
|
|
|
+ use_device_wallpaper.obj_prefix = system_device_wallpaper.obj_prefix
|
|
|
+ use_device_wallpaper.classification = system_device_wallpaper.classification
|
|
|
+ use_device_wallpaper.channel = system_device_wallpaper.channel
|
|
|
+ use_device_wallpaper.save()
|
|
|
+
|
|
|
+ if CONFIG_CN == CONFIG_INFO or CONFIG_TEST == CONFIG_INFO:
|
|
|
+ s3 = AmazonS3Util(
|
|
|
+ AWS_ACCESS_KEY_ID[0], AWS_SECRET_ACCESS_KEY[0], 'cn-northwest-1'
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ s3 = AmazonS3Util(
|
|
|
+ AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], 'us-east-1'
|
|
|
+ )
|
|
|
+
|
|
|
+ s3.delete_obj("ansjerfilemanager", f"app/static/device-wallpaper/{uid}/{obj_name}")
|
|
|
+ device_wallpaper_qs.delete()
|
|
|
+
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.info('删除壁纸异常:{}'.format(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def select_wallpaper(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 用户选取壁纸
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ wallpaper_id = request_dict.get('wallpaperId', None)
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ if not all([wallpaper_id, uid]):
|
|
|
+ return response.json(444)
|
|
|
+ device_wallpaper = DeviceWallpaper.objects.filter(id=wallpaper_id).values("device_type", "storage_type",
|
|
|
+ "obj_prefix",
|
|
|
+ "obj_name",
|
|
|
+ "classification",
|
|
|
+ "channel").first()
|
|
|
+ device_wallpaper_qs = DeviceWallpaper.objects.filter(uid=uid, parent_id__gt=0)
|
|
|
+ if device_wallpaper_qs.exists():
|
|
|
+ device_wallpaper_qs.update(
|
|
|
+ storage_type=device_wallpaper["storage_type"],
|
|
|
+ classification=device_wallpaper["classification"],
|
|
|
+ obj_prefix=device_wallpaper["obj_prefix"],
|
|
|
+ obj_name=device_wallpaper["obj_name"],
|
|
|
+ parent_id=wallpaper_id
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ DeviceWallpaper.objects.create(
|
|
|
+ device_type=device_wallpaper["device_type"],
|
|
|
+ storage_type=device_wallpaper["storage_type"],
|
|
|
+ obj_name=device_wallpaper["obj_name"],
|
|
|
+ obj_prefix=device_wallpaper["obj_prefix"],
|
|
|
+ classification=device_wallpaper["classification"],
|
|
|
+ uid=uid,
|
|
|
+ channel=device_wallpaper["channel"],
|
|
|
+ status=1,
|
|
|
+ parent_id=wallpaper_id,
|
|
|
+ created_time=int(time.time())
|
|
|
+ )
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error('用户选取壁纸异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(5)
|