| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 | 
							- #!/usr/bin/env python3
 
- # -*- coding: utf-8 -*-
 
- import time
 
- import oss2
 
- from django.db.models import Q
 
- from django.views import View
 
- from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
 
- from Model.models import VoicePromptModel, UidChannelSetModel, Device_Info
 
- from Object.ResponseObject import ResponseObject
 
- from Object.TokenObject import TokenObject
 
- from Service.CommonService import CommonService
 
- from Service.ModelService import ModelService
 
- class VoicePromptView(View):
 
-     def get(self, request, *args, **kwargs):
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.GET
 
-         operation = kwargs.get('operation', None)
 
-         return self.validate(request, request_dict, operation)
 
-     def post(self, request, *args, **kwargs):
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.POST
 
-         operation = kwargs.get('operation', None)
 
-         return self.validate(request, request_dict, operation)
 
-     def validate(self, request, request_dict, operation):
 
-         token = request_dict.get('token', None)
 
-         print(token)
 
-         lang = request_dict.get('lang', None)
 
-         response = ResponseObject(lang=lang)
 
-         token = TokenObject(token)
 
-         if token.code != 0:
 
-             return response.json(token.code)
 
-         if operation == 'getUploadUrl':
 
-             return self.get_upload_url(request_dict, response)
 
-         elif operation == 'add':
 
-             return self.do_add(request_dict, response)
 
-         elif operation == 'delete':
 
-             return self.do_delete(token.userID, request_dict, response)
 
-         elif operation == 'query':
 
-             return self.do_query(request_dict, response)
 
-         elif operation == 'update':
 
-             return self.do_update(token.userID, request_dict, response)
 
-         elif operation == 'adminGetUploadUrl':
 
-             return self.admin_get_upload_url(token.userID, request_dict, response)
 
-         elif operation == 'adminAdd':
 
-             return self.do_admin_add(token.userID, request_dict, response)
 
-         elif operation == 'adminQuery':
 
-             return self.do_admin_query(token.userID, request_dict, response)
 
-         elif operation == 'adminUpdate':
 
-             return self.do_admin_update(token.userID, request_dict, response)
 
-         elif operation == 'adminDelete':
 
-             return self.do_admin_delete(token.userID, request_dict, response)
 
-         elif operation == 'uploadSystemVoice':
 
-             return self.upload_system_voice(request, request_dict, response)
 
-         else:
 
-             return response.json(404)
 
-     def get_upload_url(self, request_dict, response):
 
-         upload_type = request_dict.get('upload_type', None)
 
-         uid = request_dict.get('uid', None)
 
-         channel = request_dict.get('channel', None)
 
-         type = request_dict.get('type', None)
 
-         algorithm_type = int(request_dict.get('algorithmType', 99))
 
-         if upload_type and uid and channel:
 
-             vp_qs = VoicePromptModel.objects.filter(uid=uid, channel=channel, type=type)
 
-             vp_qs = vp_qs.filter(algorithm_type=algorithm_type)
 
-             count = vp_qs.count()
 
-             if count >= 3:
 
-                 return response.json(201)
 
-             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-             name = CommonService.createOrderID()
 
-             filename = str(name) + '.' + upload_type
 
-             obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
 
-             url = bucket.sign_url('PUT', obj, 7200)
 
-             return response.json(0, {'put_url': url, 'filename': filename})
 
-         else:
 
-             return response.json(444)
 
-     def do_add(self, request_dict, response):
 
-         filename = request_dict.get('filename', None)
 
-         title = request_dict.get('title', None)
 
-         type = request_dict.get('type', None)
 
-         lang = request_dict.get('lang', '')
 
-         uid = request_dict.get('uid', None)
 
-         channel = request_dict.get('channel', None)
 
-         algorithm_type = request_dict.get('algorithmType', None)
 
-         if filename and title and type and uid and channel:
 
-             voice_prompt = VoicePromptModel()
 
-             voice_prompt.filename = filename
 
-             voice_prompt.title = title
 
-             voice_prompt.type = type
 
-             voice_prompt.language = lang
 
-             voice_prompt.classification = 1
 
-             voice_prompt.uid = uid
 
-             voice_prompt.channel = channel
 
-             voice_prompt.add_time = int(time.time())
 
-             if algorithm_type:
 
-                 voice_prompt.algorithm_type = int(algorithm_type)
 
-             voice_prompt.save()
 
-             res = {
 
-                 'id': voice_prompt.id,
 
-                 'filename': filename,
 
-                 'title': title,
 
-                 'type': type,
 
-             }
 
-             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-             filename = res['filename']
 
-             obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
 
-             url = bucket.sign_url('GET', obj, 3600)
 
-             res['url'] = url
 
-             del res['filename']
 
-             return response.json(0, res)
 
-         else:
 
-             return response.json(444)
 
-     def do_update(self, userID, request_dict, response):
 
-         id = request_dict.get('id', None)
 
-         title = request_dict.get('title', None)
 
-         if id and title:
 
-             voice_qs = VoicePromptModel.objects.filter(id=id)
 
-             if voice_qs.exists():
 
-                 uid = voice_qs[0].uid
 
-                 device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
 
-                 if device_qs.exists():
 
-                     voice_qs.update(title=title)
 
-                     return response.json(0)
 
-                 else:
 
-                     return response.json(404)
 
-             else:
 
-                 return response.json(173)
 
-         else:
 
-             return response.json(444)
 
-     def do_delete(self, userID, request_dict, response):
 
-         id = request_dict.get('id', None)
 
-         ids = request_dict.get('ids', None)
 
-         if id:
 
-             voice_qs = VoicePromptModel.objects.filter(id=id)
 
-         elif ids:
 
-             voice_qs = VoicePromptModel.objects.filter(id__in=ids.split(','))
 
-         if not voice_qs.exists():
 
-             return response.json(14)
 
-         for voice in voice_qs:
 
-             uid = voice.uid
 
-             device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
 
-             if device_qs.exists():
 
-                 channel = voice.channel
 
-                 filename = voice.filename
 
-                 voice_qs.delete()
 
-                 auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-                 bucket = oss2.Bucket(auth, 'http://oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-                 obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
 
-                 bucket.delete_object(obj)
 
-                 return response.json(0)
 
-             else:
 
-                 return response.json(404)
 
-         else:
 
-             return response.json(444)
 
-     def do_query(self, request_dict, response):
 
-         lang = request_dict.get('lang', 'en')
 
-         uid = request_dict.get('uid', None)
 
-         channel = request_dict.get('channel', None)
 
-         # 个性语音增加算法类型区分默认0兼容老版本
 
-         algorithm_type = int(request_dict.get('algorithmType', 99))
 
-         if uid and channel and lang:
 
-             voice_qs = VoicePromptModel.objects.filter(uid=uid, channel=channel, classification=1,
 
-                                                        algorithm_type=algorithm_type)
 
-             system_qs = VoicePromptModel.objects.filter(classification=0, language=lang, status=1,
 
-                                                         algorithm_type=algorithm_type)
 
-             if not system_qs.exists() and lang != 'en':
 
-                 system_qs = VoicePromptModel.objects.filter(classification=0, language='en', status=1,
 
-                                                             algorithm_type=algorithm_type)
 
-             if lang == 'cn' and algorithm_type == 10:
 
-                 device_type = Device_Info.objects.filter(UID=uid).values('Type').first()['Type']
 
-                 system_new_qs = system_qs.filter(Q(device_types__contains=[device_type]))
 
-                 if system_new_qs.exists():
 
-                     system_qs = system_new_qs
 
-                 else:
 
-                     system_qs = system_qs.filter(Q(device_types=[]))
 
-             channel_qs = UidChannelSetModel.objects.filter(uid__uid=uid, channel=channel,
 
-                                                            algorithm_type=algorithm_type)
 
-             res = {
 
-                 'enter_voice': {},
 
-                 'leave_voice': {},
 
-                 'voice_status': 0,
 
-                 'voice_mute': 0
 
-             }
 
-             enter_voice_id = 0
 
-             leave_voice_id = 0
 
-             if channel_qs.exists():
 
-                 channel_qs = channel_qs.values('voice_prompt_enter', 'voice_prompt_leave', 'voice_prompt_status',
 
-                                                'voice_prompt_intelligent_mute', 'voice_start_x', 'voice_start_y',
 
-                                                'voice_end_x', 'voice_end_y', 'voice_start_time', 'voice_end_time',
 
-                                                'voice_repeat_day', 'voice_direction')
 
-                 print(channel_qs)
 
-                 enter_voice_id = int(channel_qs[0]['voice_prompt_enter'])
 
-                 leave_voice_id = int(channel_qs[0]['voice_prompt_leave'])
 
-                 res['voice_status'] = channel_qs[0]['voice_prompt_status']
 
-                 res['voice_mute'] = channel_qs[0]['voice_prompt_intelligent_mute']
 
-                 res['start_x'] = channel_qs[0]['voice_start_x']
 
-                 res['start_y'] = channel_qs[0]['voice_start_y']
 
-                 res['end_x'] = channel_qs[0]['voice_end_x']
 
-                 res['end_y'] = channel_qs[0]['voice_end_y']
 
-                 res['start_time'] = channel_qs[0]['voice_start_time']
 
-                 res['end_time'] = channel_qs[0]['voice_end_time']
 
-                 res['repeat_day'] = channel_qs[0]['voice_repeat_day']
 
-                 res['direction'] = channel_qs[0]['voice_direction']
 
-             enter_systems = []
 
-             leave_systems = []
 
-             enter_customs = []
 
-             leave_customs = []
 
-             res['system'] = {}
 
-             res['custom'] = {}
 
-             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-             bucket = oss2.Bucket(auth, 'https://oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-             if system_qs.exists():
 
-                 system_qs = system_qs.values('id', 'title', 'filename', 'type')
 
-                 for system in system_qs:
 
-                     filename = system['filename']
 
-                     obj = 'voice_prompt/system/' + filename
 
-                     url = bucket.sign_url('GET', obj, 3600)
 
-                     system['url'] = url
 
-                     del system['filename']
 
-                     if system['type'] == 0:
 
-                         enter_systems.append(system)
 
-                         if enter_voice_id == system['id']:
 
-                             res['enter_voice'] = system
 
-                     elif system['type'] == 1:
 
-                         leave_systems.append(system)
 
-                         if leave_voice_id == system['id']:
 
-                             res['leave_voice'] = system
 
-             if voice_qs.exists():
 
-                 voice_qs = voice_qs.values('id', 'title', 'filename', 'type')
 
-                 for voice in voice_qs:
 
-                     filename = voice['filename']
 
-                     obj = 'voice_prompt/' + uid + '/' + channel + '/' + filename
 
-                     url = bucket.sign_url('GET', obj, 3600)
 
-                     voice['url'] = url
 
-                     del voice['filename']
 
-                     if voice['type'] == 0:
 
-                         enter_customs.append(voice)
 
-                         if enter_voice_id == voice['id']:
 
-                             res['enter_voice'] = voice
 
-                     elif voice['type'] == 1:
 
-                         leave_customs.append(voice)
 
-                         if leave_voice_id == voice['id']:
 
-                             res['leave_voice'] = voice
 
-             res['system']['enter'] = enter_systems
 
-             res['system']['leave'] = leave_systems
 
-             res['custom']['enter'] = enter_customs
 
-             res['custom']['leave'] = leave_customs
 
-             return response.json(0, res)
 
-         else:
 
-             return response.json(444)
 
-     def admin_get_upload_url(self, userID, request_dict, response):
 
-         own_perm = ModelService.check_perm(userID, 10)
 
-         if not own_perm:
 
-             return response.json(404)
 
-         upload_type = request_dict.get('upload_type', None)
 
-         if upload_type:
 
-             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-             name = CommonService.createOrderID()
 
-             filename = str(name) + '.' + upload_type
 
-             obj = 'voice_prompt/system/' + filename
 
-             url = bucket.sign_url('PUT', obj, 7200)
 
-             return response.json(0, {'put_url': url, 'filename': filename})
 
-         else:
 
-             return response.json(444)
 
-     @classmethod
 
-     def upload_system_voice(cls, request, request_dict, response):
 
-         """
 
-         上传系统个性化语音(系统默认以及算法小店默认提示音)
 
-         个性化语音有两个功能涉及使用一个是设备个性化语音,一个是算法小店语音
 
-         此接口上传个性化语音文件
 
-         参数algorithmType不为空或者不等于99(默认值)则系统保存的是算法小店的个性化语音
 
-         """
 
-         voice_file = request.FILES.get('voiceFile', None)
 
-         algorithm_type = int(request_dict.get('algorithmType', 99))
 
-         voice_title = request_dict.get('voiceTitle', None)
 
-         language = request_dict.get('language', None)
 
-         if not all([voice_file, algorithm_type, voice_title, language]):
 
-             return response.json(444)
 
-         try:
 
-             filename = CommonService.createOrderID()  # 文件名时间+随机数生成
 
-             file_ext = '.' + voice_file.name.split('.')[-1]
 
-             now_time = int(time.time())
 
-             key = 'voice_prompt/system/' + filename + file_ext
 
-             # 上传文件到阿里云OSS
 
-             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-             bucket.put_object(key=key, data=voice_file)
 
-             voice_type = 1 if algorithm_type < 99 else 0  # 算法小店提示音默认判断type=1才会显示
 
-             voice_vo = {
 
-                 'title': voice_title,
 
-                 'filename': filename + file_ext,
 
-                 'add_time': now_time,
 
-                 'classification': 0,
 
-                 'algorithm_type': algorithm_type,
 
-                 'language': language,
 
-                 'type': voice_type
 
-             }
 
-             VoicePromptModel.objects.create(**voice_vo)
 
-             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)))
 
-     def do_admin_add(self, userID, request_dict, response):
 
-         own_perm = ModelService.check_perm(userID, 10)
 
-         if not own_perm:
 
-             return response.json(404)
 
-         filename = request_dict.get('filename', None)
 
-         title = request_dict.get('title', None)
 
-         type = request_dict.get('type', None)
 
-         lang = request_dict.get('lang', '')
 
-         if filename and title and type:
 
-             voice_prompt = VoicePromptModel()
 
-             voice_prompt.filename = filename
 
-             voice_prompt.title = title
 
-             voice_prompt.type = type
 
-             voice_prompt.language = lang
 
-             voice_prompt.classification = 0
 
-             voice_prompt.add_time = int(time.time())
 
-             voice_prompt.status = 0
 
-             voice_prompt.save()
 
-             return response.json(0)
 
-         else:
 
-             return response.json(444)
 
-     def do_admin_query(self, userID, request_dict, response):
 
-         own_perm = ModelService.check_perm(userID, 10)
 
-         if not own_perm:
 
-             return response.json(404)
 
-         type = request_dict.get('type', 0)
 
-         page = request_dict.get('page', None)
 
-         line = request_dict.get('line', None)
 
-         if page is None or line is None:
 
-             return response.json(444)
 
-         voice_qs = VoicePromptModel.objects.filter(classification=0, type=type)
 
-         print(voice_qs)
 
-         res = {
 
-             'count': 0
 
-         }
 
-         if voice_qs.exists():
 
-             page = int(page)
 
-             line = int(line)
 
-             start = (page - 1) * line
 
-             end = start + line
 
-             count = voice_qs.count()
 
-             res['count'] = count
 
-             voice_qs = voice_qs.values('id', 'title', 'type', 'language', 'status', 'filename')[start:end]
 
-             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
 
-             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
 
-             for item in voice_qs:
 
-                 filename = item['filename']
 
-                 obj = 'voice_prompt/system/' + filename
 
-                 url = bucket.sign_url('GET', obj, 3600)
 
-                 item['url'] = url
 
-                 del item['filename']
 
-             res['data'] = list(voice_qs)
 
-             return response.json(0, res)
 
-         else:
 
-             res['data'] = []
 
-             return response.json(0, res)
 
-     def do_admin_update(self, userID, request_dict, response):
 
-         own_perm = ModelService.check_perm(userID, 10)
 
-         if not own_perm:
 
-             return response.json(404)
 
-         id = request_dict.get('id', None)
 
-         status = request_dict.get('status', None)
 
-         title = request_dict.get('title', None)
 
-         if id and status and title:
 
-             VoicePromptModel.objects.filter(id=id, classification=0).update(status=status, title=title)
 
-             return response.json(0)
 
-         else:
 
-             return response.json(444)
 
-     def do_admin_delete(self, userID, request_dict, response):
 
-         own_perm = ModelService.check_perm(userID, 10)
 
-         if not own_perm:
 
-             return response.json(404)
 
-         id = request_dict.get('id', None)
 
-         if id:
 
-             VoicePromptModel.objects.filter(id=id, classification=0).delete()
 
-             return response.json(0)
 
-         else:
 
-             return response.json(444)
 
 
  |