VoicePromptController.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. if upload_type and uid and channel:
  59. count = VoicePromptModel.objects.filter(uid=uid, channel=channel, type=type).count()
  60. if count >= 3:
  61. return response.json(201)
  62. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  63. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  64. name = CommonService.createOrderID()
  65. filename = str(name) + '.' + upload_type
  66. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  67. url = bucket.sign_url('PUT', obj, 7200)
  68. return response.json(0, {'put_url': url, 'filename': filename})
  69. else:
  70. return response.json(444)
  71. def do_add(self, request_dict, response):
  72. filename = request_dict.get('filename', None)
  73. title = request_dict.get('title', None)
  74. type = request_dict.get('type', None)
  75. lang = request_dict.get('lang', '')
  76. uid = request_dict.get('uid', None)
  77. channel = request_dict.get('channel', None)
  78. if filename and title and type and uid and channel:
  79. voice_prompt = VoicePromptModel()
  80. voice_prompt.filename = filename
  81. voice_prompt.title = title
  82. voice_prompt.type = type
  83. voice_prompt.language = lang
  84. voice_prompt.classification = 1
  85. voice_prompt.uid = uid
  86. voice_prompt.channel = channel
  87. voice_prompt.add_time = int(time.time())
  88. voice_prompt.save()
  89. res = {
  90. 'id': voice_prompt.id,
  91. 'filename': filename,
  92. 'title': title,
  93. 'type': type,
  94. }
  95. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  96. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  97. filename = res['filename']
  98. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  99. url = bucket.sign_url('GET', obj, 3600)
  100. res['url'] = url
  101. del res['filename']
  102. return response.json(0, res)
  103. else:
  104. return response.json(444)
  105. def do_update(self, userID, request_dict, response):
  106. id = request_dict.get('id', None)
  107. title = request_dict.get('title', None)
  108. if id and title:
  109. voice_qs = VoicePromptModel.objects.filter(id=id)
  110. if voice_qs.exists():
  111. uid = voice_qs[0].uid
  112. device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
  113. if device_qs.exists():
  114. voice_qs.update(title=title)
  115. return response.json(0)
  116. else:
  117. return response.json(404)
  118. else:
  119. return response.json(173)
  120. else:
  121. return response.json(444)
  122. def do_delete(self, userID, request_dict, response):
  123. id = request_dict.get('id', None)
  124. if id:
  125. voice_qs = VoicePromptModel.objects.filter(id=id)
  126. if voice_qs.exists():
  127. uid = voice_qs[0].uid
  128. device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
  129. if device_qs.exists():
  130. channel = voice_qs[0].channel
  131. filename = voice_qs[0].filename
  132. voice_qs.delete()
  133. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  134. bucket = oss2.Bucket(auth, 'http://oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  135. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  136. bucket.delete_object(obj)
  137. return response.json(0)
  138. else:
  139. return response.json(404)
  140. else:
  141. return response.json(173)
  142. else:
  143. return response.json(444)
  144. def do_query(self, request_dict, response):
  145. lang = request_dict.get('lang', None)
  146. uid = request_dict.get('uid', None)
  147. channel = request_dict.get('channel', None)
  148. if uid and channel and lang:
  149. voice_qs = VoicePromptModel.objects.filter(uid=uid, channel=channel, classification=1)
  150. system_qs = VoicePromptModel.objects.filter(classification=0, language=lang, status=1)
  151. channel_qs = UidChannelSetModel.objects.filter(uid__uid=uid, channel=channel)
  152. res = {
  153. 'enter_voice': {},
  154. 'leave_voice': {},
  155. 'voice_status': 0,
  156. 'voice_mute': 0
  157. }
  158. enter_voice_id = 0
  159. leave_voice_id = 0
  160. if channel_qs.exists():
  161. channel_qs = channel_qs.values('voice_prompt_enter', 'voice_prompt_leave', 'voice_prompt_status',
  162. 'voice_prompt_intelligent_mute', 'voice_start_x', 'voice_start_y',
  163. 'voice_end_x', 'voice_end_y', 'voice_start_time', 'voice_end_time',
  164. 'voice_repeat_day', 'voice_direction')
  165. print(channel_qs)
  166. enter_voice_id = int(channel_qs[0]['voice_prompt_enter'])
  167. leave_voice_id = int(channel_qs[0]['voice_prompt_leave'])
  168. res['voice_status'] = channel_qs[0]['voice_prompt_status']
  169. res['voice_mute'] = channel_qs[0]['voice_prompt_intelligent_mute']
  170. res['start_x'] = channel_qs[0]['voice_start_x']
  171. res['start_y'] = channel_qs[0]['voice_start_y']
  172. res['end_x'] = channel_qs[0]['voice_end_x']
  173. res['end_y'] = channel_qs[0]['voice_end_y']
  174. res['start_time'] = channel_qs[0]['voice_start_time']
  175. res['end_time'] = channel_qs[0]['voice_end_time']
  176. res['repeat_day'] = channel_qs[0]['voice_repeat_day']
  177. res['direction'] = channel_qs[0]['voice_direction']
  178. enter_systems = []
  179. leave_systems = []
  180. enter_customs = []
  181. leave_customs = []
  182. res['system'] = {}
  183. res['custom'] = {}
  184. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  185. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  186. if system_qs.exists():
  187. system_qs = system_qs.values('id', 'title', 'filename', 'type')
  188. for system in system_qs:
  189. filename = system['filename']
  190. obj = 'voice_prompt/system/' + filename
  191. url = bucket.sign_url('GET', obj, 3600)
  192. system['url'] = url
  193. del system['filename']
  194. if system['type'] == 0:
  195. enter_systems.append(system)
  196. if enter_voice_id == system['id']:
  197. res['enter_voice'] = system
  198. elif system['type'] == 1:
  199. leave_systems.append(system)
  200. if leave_voice_id == system['id']:
  201. res['leave_voice'] = system
  202. if voice_qs.exists():
  203. voice_qs = voice_qs.values('id', 'title', 'filename', 'type')
  204. for voice in voice_qs:
  205. filename = voice['filename']
  206. obj = 'voice_prompt/' + uid + '/' + channel + '/' + filename
  207. url = bucket.sign_url('GET', obj, 3600)
  208. voice['url'] = url
  209. del voice['filename']
  210. if voice['type'] == 0:
  211. enter_customs.append(voice)
  212. if enter_voice_id == voice['id']:
  213. res['enter_voice'] = voice
  214. elif voice['type'] == 1:
  215. leave_customs.append(voice)
  216. if leave_voice_id == voice['id']:
  217. res['leave_voice'] = voice
  218. res['system']['enter'] = enter_systems
  219. res['system']['leave'] = leave_systems
  220. res['custom']['enter'] = enter_customs
  221. res['custom']['leave'] = leave_customs
  222. return response.json(0, res)
  223. else:
  224. return response.json(444)
  225. def admin_get_upload_url(self, userID, request_dict, response):
  226. own_perm = ModelService.check_perm(userID, 10)
  227. if not own_perm:
  228. return response.json(404)
  229. upload_type = request_dict.get('upload_type', None)
  230. if upload_type:
  231. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  232. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  233. name = CommonService.createOrderID()
  234. filename = str(name) + '.' + upload_type
  235. obj = 'voice_prompt/system/' + filename
  236. url = bucket.sign_url('PUT', obj, 7200)
  237. return response.json(0, {'put_url': url, 'filename': filename})
  238. else:
  239. return response.json(444)
  240. def do_admin_add(self, userID, request_dict, response):
  241. own_perm = ModelService.check_perm(userID, 10)
  242. if not own_perm:
  243. return response.json(404)
  244. filename = request_dict.get('filename', None)
  245. title = request_dict.get('title', None)
  246. type = request_dict.get('type', None)
  247. lang = request_dict.get('lang', '')
  248. if filename and title and type:
  249. voice_prompt = VoicePromptModel()
  250. voice_prompt.filename = filename
  251. voice_prompt.title = title
  252. voice_prompt.type = type
  253. voice_prompt.language = lang
  254. voice_prompt.classification = 0
  255. voice_prompt.add_time = int(time.time())
  256. voice_prompt.status = 0
  257. voice_prompt.save()
  258. return response.json(0)
  259. else:
  260. return response.json(444)
  261. def do_admin_query(self, userID, request_dict, response):
  262. own_perm = ModelService.check_perm(userID, 10)
  263. if not own_perm:
  264. return response.json(404)
  265. type = request_dict.get('type', 0)
  266. page = request_dict.get('page', None)
  267. line = request_dict.get('line', None)
  268. if page is None or line is None:
  269. return response.json(444)
  270. voice_qs = VoicePromptModel.objects.filter(classification=0, type=type)
  271. print(voice_qs)
  272. res = {
  273. 'count': 0
  274. }
  275. if voice_qs.exists():
  276. page = int(page)
  277. line = int(line)
  278. start = (page - 1) * line
  279. end = start + line
  280. count = voice_qs.count()
  281. res['count'] = count
  282. voice_qs = voice_qs.values('id', 'title', 'type', 'language', 'status', 'filename')[start:end]
  283. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  284. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  285. for item in voice_qs:
  286. filename = item['filename']
  287. obj = 'voice_prompt/system/' + filename
  288. url = bucket.sign_url('GET', obj, 3600)
  289. item['url'] = url
  290. del item['filename']
  291. res['data'] = list(voice_qs)
  292. return response.json(0, res)
  293. else:
  294. res['data'] = []
  295. return response.json(0, res)
  296. def do_admin_update(self, userID, request_dict, response):
  297. own_perm = ModelService.check_perm(userID, 10)
  298. if not own_perm:
  299. return response.json(404)
  300. id = request_dict.get('id', None)
  301. status = request_dict.get('status', None)
  302. title = request_dict.get('title', None)
  303. if id and status and title:
  304. VoicePromptModel.objects.filter(id=id, classification=0).update(status=status, title=title)
  305. return response.json(0)
  306. else:
  307. return response.json(444)
  308. def do_admin_delete(self, userID, request_dict, response):
  309. own_perm = ModelService.check_perm(userID, 10)
  310. if not own_perm:
  311. return response.json(404)
  312. id = request_dict.get('id', None)
  313. if id:
  314. VoicePromptModel.objects.filter(id=id, classification=0).delete()
  315. return response.json(0)
  316. else:
  317. return response.json(444)