VoicePromptController.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import time
  4. import oss2
  5. from django.views import View
  6. from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
  7. from Model.models import VoicePromptModel, UidChannelSetModel, Device_Info
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. from Service.ModelService import ModelService
  12. class VoicePromptView(View):
  13. def get(self, request, *args, **kwargs):
  14. request.encoding = 'utf-8'
  15. request_dict = request.GET
  16. operation = kwargs.get('operation', None)
  17. return self.validate(request_dict, operation)
  18. def post(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. request_dict = request.POST
  21. operation = kwargs.get('operation', None)
  22. return self.validate(request_dict, operation)
  23. def validate(self, request_dict, operation):
  24. token = request_dict.get('token', None)
  25. print(token)
  26. lang = request_dict.get('lang', None)
  27. response = ResponseObject(lang=lang)
  28. token = TokenObject(token)
  29. if token.code != 0:
  30. return response.json(token.code)
  31. if operation == 'getUploadUrl':
  32. return self.get_upload_url(request_dict, response)
  33. elif operation == 'add':
  34. return self.do_add(request_dict, response)
  35. elif operation == 'delete':
  36. return self.do_delete(token.userID, request_dict, response)
  37. elif operation == 'query':
  38. return self.do_query(request_dict, response)
  39. elif operation == 'update':
  40. return self.do_update(token.userID, request_dict, response)
  41. elif operation == 'adminGetUploadUrl':
  42. return self.admin_get_upload_url(token.userID, request_dict, response)
  43. elif operation == 'adminAdd':
  44. return self.do_admin_add(token.userID, request_dict, response)
  45. elif operation == 'adminQuery':
  46. return self.do_admin_query(token.userID, request_dict, response)
  47. elif operation == 'adminUpdate':
  48. return self.do_admin_update(token.userID, request_dict, response)
  49. elif operation == 'adminDelete':
  50. return self.do_admin_delete(token.userID, request_dict, response)
  51. else:
  52. return response.json(404)
  53. def get_upload_url(self, request_dict, response):
  54. upload_type = request_dict.get('upload_type', None)
  55. uid = request_dict.get('uid', None)
  56. channel = request_dict.get('channel', None)
  57. type = request_dict.get('type', None)
  58. algorithm_type = request_dict.get('algorithmType', None)
  59. if upload_type and uid and channel:
  60. vp_qs = VoicePromptModel.objects.filter(uid=uid, channel=channel, type=type)
  61. if algorithm_type:
  62. vp_qs.filter(algorithm_type=int(algorithm_type))
  63. count = vp_qs.count()
  64. if count >= 3:
  65. return response.json(201)
  66. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  67. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  68. name = CommonService.createOrderID()
  69. filename = str(name) + '.' + upload_type
  70. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  71. url = bucket.sign_url('PUT', obj, 7200)
  72. return response.json(0, {'put_url': url, 'filename': filename})
  73. else:
  74. return response.json(444)
  75. def do_add(self, request_dict, response):
  76. filename = request_dict.get('filename', None)
  77. title = request_dict.get('title', None)
  78. type = request_dict.get('type', None)
  79. lang = request_dict.get('lang', '')
  80. uid = request_dict.get('uid', None)
  81. channel = request_dict.get('channel', None)
  82. algorithm_type = request_dict.get('algorithmType', None)
  83. if filename and title and type and uid and channel:
  84. voice_prompt = VoicePromptModel()
  85. voice_prompt.filename = filename
  86. voice_prompt.title = title
  87. voice_prompt.type = type
  88. voice_prompt.language = lang
  89. voice_prompt.classification = 1
  90. voice_prompt.uid = uid
  91. voice_prompt.channel = channel
  92. voice_prompt.add_time = int(time.time())
  93. if algorithm_type:
  94. voice_prompt.algorithm_type = int(algorithm_type)
  95. voice_prompt.save()
  96. res = {
  97. 'id': voice_prompt.id,
  98. 'filename': filename,
  99. 'title': title,
  100. 'type': type,
  101. }
  102. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  103. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  104. filename = res['filename']
  105. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  106. url = bucket.sign_url('GET', obj, 3600)
  107. res['url'] = url
  108. del res['filename']
  109. return response.json(0, res)
  110. else:
  111. return response.json(444)
  112. def do_update(self, userID, request_dict, response):
  113. id = request_dict.get('id', None)
  114. title = request_dict.get('title', None)
  115. if id and title:
  116. voice_qs = VoicePromptModel.objects.filter(id=id)
  117. if voice_qs.exists():
  118. uid = voice_qs[0].uid
  119. device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
  120. if device_qs.exists():
  121. voice_qs.update(title=title)
  122. return response.json(0)
  123. else:
  124. return response.json(404)
  125. else:
  126. return response.json(173)
  127. else:
  128. return response.json(444)
  129. def do_delete(self, userID, request_dict, response):
  130. id = request_dict.get('id', None)
  131. ids = request_dict.get('ids', None)
  132. if id:
  133. voice_qs = VoicePromptModel.objects.filter(id=id)
  134. elif ids:
  135. voice_qs = VoicePromptModel.objects.filter(id__in=ids.split(','))
  136. if not voice_qs.exists():
  137. return response.json(14)
  138. for voice in voice_qs:
  139. uid = voice.uid
  140. device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
  141. if device_qs.exists():
  142. channel = voice.channel
  143. filename = voice.filename
  144. voice_qs.delete()
  145. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  146. bucket = oss2.Bucket(auth, 'http://oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  147. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  148. bucket.delete_object(obj)
  149. return response.json(0)
  150. else:
  151. return response.json(404)
  152. else:
  153. return response.json(444)
  154. def do_query(self, request_dict, response):
  155. lang = request_dict.get('lang', None)
  156. uid = request_dict.get('uid', None)
  157. channel = request_dict.get('channel', None)
  158. # 个性语音增加算法类型区分默认0兼容老版本
  159. algorithm_type = int(request_dict.get('algorithmType', 0))
  160. if uid and channel and lang:
  161. voice_qs = VoicePromptModel.objects.filter(uid=uid, channel=channel, classification=1,
  162. algorithm_type=algorithm_type)
  163. system_qs = VoicePromptModel.objects.filter(classification=0, language=lang, status=1,
  164. algorithm_type=algorithm_type)
  165. channel_qs = UidChannelSetModel.objects.filter(uid__uid=uid, channel=channel,
  166. algorithm_type=algorithm_type)
  167. res = {
  168. 'enter_voice': {},
  169. 'leave_voice': {},
  170. 'voice_status': 0,
  171. 'voice_mute': 0
  172. }
  173. enter_voice_id = 0
  174. leave_voice_id = 0
  175. if channel_qs.exists():
  176. channel_qs = channel_qs.values('voice_prompt_enter', 'voice_prompt_leave', 'voice_prompt_status',
  177. 'voice_prompt_intelligent_mute', 'voice_start_x', 'voice_start_y',
  178. 'voice_end_x', 'voice_end_y', 'voice_start_time', 'voice_end_time',
  179. 'voice_repeat_day', 'voice_direction')
  180. print(channel_qs)
  181. enter_voice_id = int(channel_qs[0]['voice_prompt_enter'])
  182. leave_voice_id = int(channel_qs[0]['voice_prompt_leave'])
  183. res['voice_status'] = channel_qs[0]['voice_prompt_status']
  184. res['voice_mute'] = channel_qs[0]['voice_prompt_intelligent_mute']
  185. res['start_x'] = channel_qs[0]['voice_start_x']
  186. res['start_y'] = channel_qs[0]['voice_start_y']
  187. res['end_x'] = channel_qs[0]['voice_end_x']
  188. res['end_y'] = channel_qs[0]['voice_end_y']
  189. res['start_time'] = channel_qs[0]['voice_start_time']
  190. res['end_time'] = channel_qs[0]['voice_end_time']
  191. res['repeat_day'] = channel_qs[0]['voice_repeat_day']
  192. res['direction'] = channel_qs[0]['voice_direction']
  193. enter_systems = []
  194. leave_systems = []
  195. enter_customs = []
  196. leave_customs = []
  197. res['system'] = {}
  198. res['custom'] = {}
  199. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  200. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  201. if system_qs.exists():
  202. system_qs = system_qs.values('id', 'title', 'filename', 'type')
  203. for system in system_qs:
  204. filename = system['filename']
  205. obj = 'voice_prompt/system/' + filename
  206. url = bucket.sign_url('GET', obj, 3600)
  207. system['url'] = url
  208. del system['filename']
  209. if system['type'] == 0:
  210. enter_systems.append(system)
  211. if enter_voice_id == system['id']:
  212. res['enter_voice'] = system
  213. elif system['type'] == 1:
  214. leave_systems.append(system)
  215. if leave_voice_id == system['id']:
  216. res['leave_voice'] = system
  217. if voice_qs.exists():
  218. voice_qs = voice_qs.values('id', 'title', 'filename', 'type')
  219. for voice in voice_qs:
  220. filename = voice['filename']
  221. obj = 'voice_prompt/' + uid + '/' + channel + '/' + filename
  222. url = bucket.sign_url('GET', obj, 3600)
  223. voice['url'] = url
  224. del voice['filename']
  225. if voice['type'] == 0:
  226. enter_customs.append(voice)
  227. if enter_voice_id == voice['id']:
  228. res['enter_voice'] = voice
  229. elif voice['type'] == 1:
  230. leave_customs.append(voice)
  231. if leave_voice_id == voice['id']:
  232. res['leave_voice'] = voice
  233. res['system']['enter'] = enter_systems
  234. res['system']['leave'] = leave_systems
  235. res['custom']['enter'] = enter_customs
  236. res['custom']['leave'] = leave_customs
  237. return response.json(0, res)
  238. else:
  239. return response.json(444)
  240. def admin_get_upload_url(self, userID, request_dict, response):
  241. own_perm = ModelService.check_perm(userID, 10)
  242. if not own_perm:
  243. return response.json(404)
  244. upload_type = request_dict.get('upload_type', None)
  245. if upload_type:
  246. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  247. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  248. name = CommonService.createOrderID()
  249. filename = str(name) + '.' + upload_type
  250. obj = 'voice_prompt/system/' + filename
  251. url = bucket.sign_url('PUT', obj, 7200)
  252. return response.json(0, {'put_url': url, 'filename': filename})
  253. else:
  254. return response.json(444)
  255. def do_admin_add(self, userID, request_dict, response):
  256. own_perm = ModelService.check_perm(userID, 10)
  257. if not own_perm:
  258. return response.json(404)
  259. filename = request_dict.get('filename', None)
  260. title = request_dict.get('title', None)
  261. type = request_dict.get('type', None)
  262. lang = request_dict.get('lang', '')
  263. if filename and title and type:
  264. voice_prompt = VoicePromptModel()
  265. voice_prompt.filename = filename
  266. voice_prompt.title = title
  267. voice_prompt.type = type
  268. voice_prompt.language = lang
  269. voice_prompt.classification = 0
  270. voice_prompt.add_time = int(time.time())
  271. voice_prompt.status = 0
  272. voice_prompt.save()
  273. return response.json(0)
  274. else:
  275. return response.json(444)
  276. def do_admin_query(self, userID, request_dict, response):
  277. own_perm = ModelService.check_perm(userID, 10)
  278. if not own_perm:
  279. return response.json(404)
  280. type = request_dict.get('type', 0)
  281. page = request_dict.get('page', None)
  282. line = request_dict.get('line', None)
  283. if page is None or line is None:
  284. return response.json(444)
  285. voice_qs = VoicePromptModel.objects.filter(classification=0, type=type)
  286. print(voice_qs)
  287. res = {
  288. 'count': 0
  289. }
  290. if voice_qs.exists():
  291. page = int(page)
  292. line = int(line)
  293. start = (page - 1) * line
  294. end = start + line
  295. count = voice_qs.count()
  296. res['count'] = count
  297. voice_qs = voice_qs.values('id', 'title', 'type', 'language', 'status', 'filename')[start:end]
  298. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  299. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  300. for item in voice_qs:
  301. filename = item['filename']
  302. obj = 'voice_prompt/system/' + filename
  303. url = bucket.sign_url('GET', obj, 3600)
  304. item['url'] = url
  305. del item['filename']
  306. res['data'] = list(voice_qs)
  307. return response.json(0, res)
  308. else:
  309. res['data'] = []
  310. return response.json(0, res)
  311. def do_admin_update(self, userID, request_dict, response):
  312. own_perm = ModelService.check_perm(userID, 10)
  313. if not own_perm:
  314. return response.json(404)
  315. id = request_dict.get('id', None)
  316. status = request_dict.get('status', None)
  317. title = request_dict.get('title', None)
  318. if id and status and title:
  319. VoicePromptModel.objects.filter(id=id, classification=0).update(status=status, title=title)
  320. return response.json(0)
  321. else:
  322. return response.json(444)
  323. def do_admin_delete(self, userID, request_dict, response):
  324. own_perm = ModelService.check_perm(userID, 10)
  325. if not own_perm:
  326. return response.json(404)
  327. id = request_dict.get('id', None)
  328. if id:
  329. VoicePromptModel.objects.filter(id=id, classification=0).delete()
  330. return response.json(0)
  331. else:
  332. return response.json(444)