Test.py 7.3 KB

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