Test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "key1": "abc",
  37. "key2": "def"
  38. }
  39. json_data = {
  40. "collapse_key": "WhatYouWant",
  41. "data": data,
  42. "delay_while_idle": False,
  43. "time_to_live": 3600,
  44. "registration_ids": [rg_id]
  45. }
  46. url = 'https://android.googleapis.com/gcm/send'
  47. data = json.dumps(json_data).encode('utf-8')
  48. headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey}
  49. req = requests.post(url, data, headers=headers)
  50. response = ResponseObject()
  51. return response.json(0)
  52. def jgPush(self, request):
  53. response = ResponseObject()
  54. devToken = request.GET.get('devToken', '')
  55. app_key = request.GET.get('app_key', '')
  56. master_secret = request.GET.get('master_secret', '')
  57. import jpush as jpush
  58. # 此处换成各自的app_key和master_secret
  59. _jpush = jpush.JPush(app_key, master_secret)
  60. push = _jpush.create_push()
  61. # if you set the logging level to "DEBUG",it will show the debug logging.
  62. _jpush.set_logging("DEBUG")
  63. # push.audience = jpush.all_
  64. push.audience = jpush.registration_id(devToken)
  65. push.notification = jpush.notification(alert="hello python jpush api")
  66. push.platform = jpush.all_
  67. try:
  68. res = push.send()
  69. except Exception as e:
  70. print("Exception")
  71. return response.json(10, repr(e))
  72. return response.json(0)
  73. def post(self, request, *args, **kwargs):
  74. response = ResponseObject()
  75. data = request.POST.dict()
  76. signature = data["sign"]
  77. data.pop('sign')
  78. print(json.dumps(data))
  79. print(signature)
  80. # verify
  81. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  82. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  83. alipay = AliPay(
  84. appid="2016092200569234",
  85. app_notify_url=None, # the default notify path
  86. app_private_key_string=app_private_key_string,
  87. alipay_public_key_string=alipay_public_key_string,
  88. sign_type="RSA2", # RSA or RSA2
  89. debug=False # False by default
  90. )
  91. success = alipay.verify(data, signature)
  92. if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
  93. print("trade succeed")
  94. return response.json(0, signature)
  95. # 修改 资源改变
  96. def put(self, request):
  97. response = ResponseObject()
  98. return response.json(0, '')
  99. # 修改 属性改变
  100. def PATCH(self, request):
  101. response = ResponseObject()
  102. return response.json(0)
  103. # 删除
  104. def delete(self, request):
  105. response = ResponseObject()
  106. return response.json(0)
  107. def validation(self, request_dict, *args, **kwargs):
  108. response = ResponseObject()
  109. return response.json(0)