소스 검색

后台设备音频管理

linhaohong 5 달 전
부모
커밋
e307c17c69
1개의 변경된 파일200개의 추가작업 그리고 1개의 파일을 삭제
  1. 200 1
      AdminController/DeviceManagementController.py

+ 200 - 1
AdminController/DeviceManagementController.py

@@ -20,7 +20,7 @@ from Ansjer.config import SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_US
 from Model.models import Device_Info, UidSetModel, LogModel, UID_Bucket, Unused_Uid_Meal, StsCrdModel, \
     VodHlsModel, ExperienceContextModel, DeviceTypeModel, UidUserModel, ExperienceAiModel, AiService, \
     AppBundle, App_Info, AppDeviceType, DeviceNameLanguage, UIDCompanySerialModel, UidPushModel, \
-    CustomCustomerOrderInfo, CustomCustomerDevice, DeviceVersionInfo
+    CustomCustomerOrderInfo, CustomCustomerDevice, DeviceVersionInfo, VoicePromptModel, DeviceAlgorithmExplain
 from Object.AWS.AmazonS3Util import AmazonS3Util
 from Object.Enums.RedisKeyConstant import RedisKeyConstant
 from Object.RedisObject import RedisObject
@@ -116,6 +116,18 @@ class DeviceManagement(View):
                 return self.edit_device_ver_info(request_dict, response)
             elif operation == 'delDeviceVerInfo':  # 删除设备型号版本
                 return self.del_device_ver_info(request_dict, response)
+
+            # 设备语音设置
+            elif operation == 'getDeviceVoice':  # 获取设备音频
+                return self.get_device_voice(request_dict, response)
+            elif operation == 'algorithmTypeList':  # 添加设备型号版本
+                return self.algorithm_type_list(response)
+            elif operation == 'addDeviceVoice':  # 获取设备音频列表
+                return self.add_device_voice(request, request_dict, response)
+            elif operation == 'editDeviceVoice':
+                return self.edit_device_voice(request_dict, response)
+            elif operation == 'delDeviceVoice':
+                return self.del_device_voice(request_dict, response)
             else:
                 return response.json(444, 'operation')
 
@@ -1664,8 +1676,195 @@ class DeviceManagement(View):
         device_version_info_qs.delete()
         return response.json(0)
 
+    @staticmethod
+    def get_device_voice(request_dict, response):
+        """
+        获取设备音频
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        title = request_dict.get('title', None)
+        voice_type = request_dict.get('voiceType', None)
+        classification = request_dict.get('classification', None)
+        uid = request_dict.get('uid', None)
+        algorithm_type = request_dict.get('algorithmType', None)
+        status = request_dict.get('status', None)
+        page = request_dict.get('page', 1)
+        page_size = request_dict.get('pageSize', 20)
+        try:
+            voice_prompt_qs = VoicePromptModel.objects.all()
+            if title:
+                voice_prompt_qs = voice_prompt_qs.filter(title__icontains=title)
+            if voice_type:
+                voice_prompt_qs = voice_prompt_qs.filter(type=voice_type)
+            if classification:
+                voice_prompt_qs = voice_prompt_qs.filter(classification=classification)
+            if uid:
+                voice_prompt_qs = voice_prompt_qs.filter(uid=uid)
+            if algorithm_type:
+                voice_prompt_qs = voice_prompt_qs.filter(algorithm_type=algorithm_type)
+            if status:
+                voice_prompt_qs = voice_prompt_qs.filter(status=status)
 
+            # 分页
+            paginator = Paginator(voice_prompt_qs.order_by("-add_time"), page_size)  # 每页显示 page_size 条
+            voice_prompt_page = paginator.get_page(page)  # 获取当前页的数据
 
+            # 上传文件到阿里云OSS
+            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+            bucket = oss2.Bucket(auth, 'https://oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
+            voice_prompt_list = []
+            for voice_prompt in voice_prompt_page:
+                filename = voice_prompt.filename
+                obj = 'voice_prompt/system/' + filename
+                filename_url = bucket.sign_url('GET', obj, 3600)
+                voice_prompt_list.append({
+                    "id": voice_prompt.id,
+                    "title": voice_prompt.title,
+                    "type": voice_prompt.type,
+                    "classification": voice_prompt.classification,
+                    "filename": filename,
+                    "filenameUrl": filename_url,
+                    "language": voice_prompt.language,
+                    "status": voice_prompt.status,
+                    "addTime": voice_prompt.add_time,
+                    "algorithmType": voice_prompt.algorithm_type,
+                    "uid": voice_prompt.uid,
+                })
+            return response.json(0, {'list': voice_prompt_list, 'total': paginator.count})
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
+    @staticmethod
+    def algorithm_type_list(response):
+        """
+        算法类型列表
+        @param response:
+        @return:
+        """
+        try:
+            device_algorithm_explain_qs = DeviceAlgorithmExplain.objects.filter(lang='cn').values('title', 'algorithm_type_id')
+            return response.json(0, list(device_algorithm_explain_qs))
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def add_device_voice(request, request_dict, response):
+        """
+        添加设备音频
+        @param request
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        title = request_dict.get('title', None)
+        voice_type = request_dict.get('voiceType', None)
+        classification = request_dict.get('classification', None)
+        algorithm_type = request_dict.get('algorithmType', None)
+        status = request_dict.get('status', None)
+        language = request_dict.get('language', None)
+        uid = request_dict.get('uid', None)
+        channel = request_dict.get('channel', 1)
+        voice_file = request.FILES.get('voiceFile', None)
+        if not all([title, voice_type, classification, algorithm_type, status, language, voice_file]):
+            return response.json(444)
+        try:
+            classification = int(classification)
+            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+            bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
+            filename = CommonService.createOrderID() + ".g711a"
+            voice_prompt_dict = {
+                "title": title,
+                "type": voice_type,
+                "classification": classification,
+                "filename": filename,
+                "language": language,
+                "status": status,
+                "algorithm_type": algorithm_type,
+                "add_time": int(time.time())
+            }
+            if uid and classification == 1:
+                voice_prompt_dict["uid"] = uid
+                obj = 'voice_prompt/system/' + filename
+                bucket.put_object(obj, voice_file)
+            elif uid and classification == 0:
+                return response.json(178)
+            else:
+                obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
+                bucket.put_object(obj, voice_file)
+            VoicePromptModel.objects.create(**voice_prompt_dict)
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def edit_device_voice(request_dict, response):
+        """
+        添加设备音频
+        @param request:
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        device_voice_id = request_dict.get('deviceVoiceId', None)
+        title = request_dict.get('title', None)
+        voice_type = request_dict.get('voiceType', None)
+        classification = request_dict.get('classification', None)
+        algorithm_type = request_dict.get('algorithmType', None)
+        status = request_dict.get('status', None)
+        language = request_dict.get('language', None)
+        try:
+            voice_prompt = VoicePromptModel.objects.get(pk=device_voice_id)
+            if title:
+                voice_prompt.title = title
+            if voice_type:
+                voice_prompt.type = voice_type
+            if classification:
+                voice_prompt.classification = classification
+            if algorithm_type:
+                voice_prompt.algorithm_type = algorithm_type
+            if status:
+                voice_prompt.status = status
+            if language:
+                voice_prompt.language = language
+            voice_prompt.save()
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+
+    @staticmethod
+    def del_device_voice(request_dict, response):
+        """
+        删除设备音频
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        device_voice_id = request_dict.get('deviceVoiceId', None)
+        try:
+            voice_prompt_qs = VoicePromptModel.objects.filter(id=device_voice_id)
+            filename = voice_prompt_qs[0].filename
+            classification = voice_prompt_qs[0].classification
+            uid = voice_prompt_qs[0].uid
+            channel = voice_prompt_qs[0].channel
+            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+            bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
+            if classification == 1 and uid:
+                obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
+                bucket.delete_object(obj)
+            else:
+                obj = 'voice_prompt/system/' + filename
+                bucket.delete_object(obj)
+            voice_prompt_qs.delete()
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))