DeviceShare.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2019/3/4 16:30
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: DeviceShare.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.views.generic.base import View
  15. from Model.models import Device_Info
  16. from Object.RedisObject import RedisObject
  17. from Object.ResponseObject import ResponseObject
  18. from Object.TokenObject import TokenObject
  19. import time
  20. from Ansjer.config import SERVER_DOMAIN
  21. import jwt
  22. class DeviceShareView(View):
  23. def get(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. operation = kwargs.get('operation')
  26. return self.validation(request.GET, operation)
  27. def post(self, request, *args, **kwargs):
  28. request.encoding = 'utf-8'
  29. operation = kwargs.get('operation')
  30. return self.validation(request.POST, operation)
  31. def validation(self, request_dict, operation):
  32. response = ResponseObject()
  33. token = request_dict.get('token', None)
  34. if token:
  35. tko = TokenObject(token)
  36. if tko.code == 0:
  37. if operation == 'generateTokenUrl':
  38. return self.do_generate_token_url(tko.userID, request_dict, response)
  39. else:
  40. return response.json(414)
  41. else:
  42. return response.json(tko.code)
  43. else:
  44. return response.json(444, {'param': 'token'})
  45. def do_generate_token_url(self, userID, request_dict, response):
  46. UID = request_dict.get('UID', None)
  47. dvqs = Device_Info.objects.filter(UID=UID, userID_id=userID, isShare=False)
  48. if dvqs.exists():
  49. data = {'sharer': userID, 'UID': UID}
  50. share_token = jwt.encode(data, 'jwt.#^^&', algorithm='HS256')
  51. share_url = '{SERVER_DOMAIN}deviceShare/confirm?shareToken={shareToken}' \
  52. .format(SERVER_DOMAIN=SERVER_DOMAIN, shareToken=share_token.decode('utf-8'))
  53. return response.json(0, {'share_url': share_url})
  54. else:
  55. return response.json(173)
  56. def do_confirm(self, userID, request_dict, response):
  57. shareToken = request_dict.get('shareToken', None)
  58. if shareToken is not None:
  59. data = jwt.decode(shareToken, 'jwt.#^^&', algorithms='HS256')
  60. sharer = data.get('sharer', None)
  61. UID = data.get('UID', None)
  62. dval = Device_Info.objects.filter(userID_id=userID, UID=UID)
  63. if not dval.exists():
  64. sharerDvqs = Device_Info.objects.filter(userID_id=sharer, UID=UID)
  65. if sharerDvqs.exists():
  66. pass
  67. else:
  68. return response.json(173)
  69. else:
  70. return response.json(174)
  71. else:
  72. return response.json(444, {'param': 'shareToken'})