Test.py 3.9 KB

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