Test.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. import os
  16. '''
  17. http://192.168.136.40:8077/Test
  18. '''
  19. from Object.ResponseObject import ResponseObject
  20. from Ansjer.config import BASE_DIR
  21. import json
  22. from alipay import AliPay
  23. import time
  24. import apns2
  25. # 测试接口sdk
  26. class Test(View):
  27. def get(self, request, *args, **kwargs):
  28. request_dict = request.GET
  29. return self.do_apns(request_dict)
  30. # return self.do_get_putOss_url(request.GET)
  31. # return self.do_gcm_push(request)
  32. def do_apns(self,request_dict):
  33. token_val = request_dict.get('token_val',None)
  34. pem_path = os.path.join(BASE_DIR,'Ansjer/file/apns_pem/apns-dev.pem')
  35. print(pem_path)
  36. response = ResponseObject()
  37. # apns_config = {
  38. # 'appbundleId': {'pem_path': 'xxxx', 'topic': 'topic', 'password': 'password'}
  39. # }
  40. try:
  41. # daytime = time.strftime("%Y%m%d%H%M", time.localtime(1547256103))
  42. # print(daytime)
  43. # pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns-dev.pem')
  44. # cli = apns2.APNSClient(mode="dev", client_cert=pem_path, password='111111')
  45. cli = apns2.APNSClient(mode="prod", client_cert=pem_path, password='1234')
  46. # cli = apns2.APNSClient(mode="dev", client_cert=pem_path, password='1234')
  47. now_time = int(time.time())
  48. push_data = {"alert": "Motion ", "event_time": now_time, "event_type": '51', "msg": "",
  49. "received_at": now_time, "sound": "sound.aif", "uid": "XFDJUHUIOKJHYTGSFFDR"}
  50. alert = apns2.PayloadAlert(body=push_data, title="title!")
  51. payload = apns2.Payload(alert=alert)
  52. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  53. res = cli.push(n=n, device_token=token_val, topic=pem_path)
  54. # assert res.status_code == 200, res.reason
  55. # assert res.apns_id
  56. if res.status_code == 200:
  57. return response.json(0)
  58. else:
  59. return response.json(404, res.reason)
  60. except Exception as e:
  61. return response.json(10, repr(e))
  62. def do_get_putOss_url(self, request_dict):
  63. import oss2
  64. obj_name = request_dict.get('obj_name','')
  65. # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  66. auth = oss2.Auth('LTAIyMkGfEdogyL9', '71uIjpsqVOmF7DAITRyRuc259jHOjO')
  67. # Endpoint以杭州为例,其它Region请按实际情况填写。
  68. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  69. # 设置此签名URL在60秒内有效。
  70. url = bucket.sign_url('PUT', obj_name, 7200)
  71. response = ResponseObject()
  72. return response.json(0, url)
  73. def do_gcm_push(self, request):
  74. import json
  75. import requests
  76. response = ResponseObject()
  77. rg_id = request.GET.get('rg_id', '')
  78. serverKey = request.GET.get('serverKey', '')
  79. if not rg_id or not serverKey:
  80. return response.json(444)
  81. data = {
  82. "received_at": "1",
  83. "msg": "1",
  84. "uid": "1",
  85. "alert": "1",
  86. "sound": "1",
  87. "event_time": "1",
  88. "event_type": "1",
  89. }
  90. json_data = {
  91. "collapse_key": "WhatYouWant",
  92. "data": data,
  93. "delay_while_idle": False,
  94. "time_to_live": 3600,
  95. "registration_ids": [rg_id]
  96. }
  97. url = 'https://android.googleapis.com/gcm/send'
  98. data = json.dumps(json_data).encode('utf-8')
  99. headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey}
  100. req = requests.post(url, data, headers=headers)
  101. response = ResponseObject()
  102. return response.json(0)
  103. def jgPush(self, request):
  104. response = ResponseObject()
  105. devToken = request.GET.get('devToken', '')
  106. app_key = request.GET.get('app_key', '')
  107. master_secret = request.GET.get('master_secret', '')
  108. import jpush as jpush
  109. # 此处换成各自的app_key和master_secret
  110. _jpush = jpush.JPush(app_key, master_secret)
  111. push = _jpush.create_push()
  112. # if you set the logging level to "DEBUG",it will show the debug logging.
  113. _jpush.set_logging("DEBUG")
  114. # push.audience = jpush.all_
  115. push.audience = jpush.registration_id(devToken)
  116. push.notification = jpush.notification(alert="hello python jpush api")
  117. push.platform = jpush.all_
  118. try:
  119. res = push.send()
  120. except Exception as e:
  121. print("Exception")
  122. return response.json(10, repr(e))
  123. return response.json(0)
  124. def post(self, request, *args, **kwargs):
  125. response = ResponseObject()
  126. data = request.POST.dict()
  127. signature = data["sign"]
  128. data.pop('sign')
  129. print(json.dumps(data))
  130. print(signature)
  131. # verify
  132. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  133. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  134. alipay = AliPay(
  135. appid="2016092200569234",
  136. app_notify_url=None, # the default notify path
  137. app_private_key_string=app_private_key_string,
  138. alipay_public_key_string=alipay_public_key_string,
  139. sign_type="RSA2", # RSA or RSA2
  140. debug=False # False by default
  141. )
  142. success = alipay.verify(data, signature)
  143. if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
  144. print("trade succeed")
  145. return response.json(0, signature)
  146. # 修改 资源改变
  147. def put(self, request):
  148. response = ResponseObject()
  149. return response.json(0, '')
  150. # 修改 属性改变
  151. def PATCH(self, request):
  152. response = ResponseObject()
  153. return response.json(0)
  154. # 删除
  155. def delete(self, request):
  156. response = ResponseObject()
  157. return response.json(0)
  158. def validation(self, request_dict, *args, **kwargs):
  159. response = ResponseObject()
  160. return response.json(0)