Test.py 6.4 KB

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