Test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/5/22 13:58
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: Test.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.views.generic.base import View
  15. '''
  16. http://192.168.136.40:8077/Test
  17. '''
  18. from Object.ResponseObject import ResponseObject
  19. from Ansjer.config import BASE_DIR
  20. import json
  21. from alipay import AliPay
  22. import time
  23. # 测试接口sdk
  24. class Test(View):
  25. def get(self, request, *args, **kwargs):
  26. return self.do_gcm_push(request)
  27. def do_gcm_push(self, request):
  28. import json
  29. import requests
  30. response = ResponseObject()
  31. rg_id = request.GET.get('rg_id', '')
  32. serverKey = request.GET.get('serverKey', '')
  33. if not rg_id or not serverKey:
  34. return response.json(444)
  35. data = {
  36. "received_at": "1",
  37. "msg": "1",
  38. "uid": "1",
  39. "alert": "1",
  40. "sound": "1",
  41. "event_time": "1",
  42. "event_type": "1",
  43. }
  44. json_data = {
  45. "collapse_key": "WhatYouWant",
  46. "data": data,
  47. "delay_while_idle": False,
  48. "time_to_live": 3600,
  49. "registration_ids": [rg_id]
  50. }
  51. url = 'https://android.googleapis.com/gcm/send'
  52. data = json.dumps(json_data).encode('utf-8')
  53. headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey}
  54. req = requests.post(url, data, headers=headers)
  55. response = ResponseObject()
  56. return response.json(0)
  57. def jgPush(self, request):
  58. response = ResponseObject()
  59. devToken = request.GET.get('devToken', '')
  60. app_key = request.GET.get('app_key', '')
  61. master_secret = request.GET.get('master_secret', '')
  62. import jpush as jpush
  63. # 此处换成各自的app_key和master_secret
  64. _jpush = jpush.JPush(app_key, master_secret)
  65. push = _jpush.create_push()
  66. # if you set the logging level to "DEBUG",it will show the debug logging.
  67. _jpush.set_logging("DEBUG")
  68. # push.audience = jpush.all_
  69. push.audience = jpush.registration_id(devToken)
  70. push.notification = jpush.notification(alert="hello python jpush api")
  71. push.platform = jpush.all_
  72. try:
  73. res = push.send()
  74. except Exception as e:
  75. print("Exception")
  76. return response.json(10, repr(e))
  77. return response.json(0)
  78. def post(self, request, *args, **kwargs):
  79. response = ResponseObject()
  80. data = request.POST.dict()
  81. signature = data["sign"]
  82. data.pop('sign')
  83. print(json.dumps(data))
  84. print(signature)
  85. # verify
  86. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  87. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  88. alipay = AliPay(
  89. appid="2016092200569234",
  90. app_notify_url=None, # the default notify path
  91. app_private_key_string=app_private_key_string,
  92. alipay_public_key_string=alipay_public_key_string,
  93. sign_type="RSA2", # RSA or RSA2
  94. debug=False # False by default
  95. )
  96. success = alipay.verify(data, signature)
  97. if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
  98. print("trade succeed")
  99. return response.json(0, signature)
  100. # 修改 资源改变
  101. def put(self, request):
  102. response = ResponseObject()
  103. return response.json(0, '')
  104. # 修改 属性改变
  105. def PATCH(self, request):
  106. response = ResponseObject()
  107. return response.json(0)
  108. # 删除
  109. def delete(self, request):
  110. response = ResponseObject()
  111. return response.json(0)
  112. def validation(self, request_dict, *args, **kwargs):
  113. response = ResponseObject()
  114. return response.json(0)