Test.py 7.4 KB

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