wechat.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # coding=utf-8
  2. import requests
  3. import json
  4. from utils.exceptions import CustomError
  5. class WeChat(object):
  6. @staticmethod
  7. def getPreAuthCode(component_appid, component_access_token):
  8. """
  9. 第三方平台 获得预授权码
  10. 结果{"pre_auth_code": "Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw", "expires_in": 600}
  11. """
  12. url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' + component_access_token
  13. param = {
  14. 'component_appid': component_appid,
  15. }
  16. result = requests.post(url, data=json.dumps(param))
  17. result = result.json()
  18. if 'errcode' in result and result['errcode']:
  19. raise CustomError(u'微信请求失败' + str(result['errcode']) + ' :' + result['errmsg'])
  20. return result
  21. @staticmethod
  22. def getComponentAccessToken(component_appid, component_appsecret, component_verify_ticket):
  23. """第三方平台 获得access_token"""
  24. url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token'
  25. param = {
  26. 'component_appid': component_appid,
  27. 'component_appsecret': component_appsecret,
  28. 'component_verify_ticket': component_verify_ticket
  29. }
  30. result = requests.post(url, data=json.dumps(param))
  31. result = result.json()
  32. if 'errcode' in result and result['errcode']:
  33. raise CustomError(u'微信请求失败'+ str(result['errcode']) + ' :' + result['errmsg'])
  34. return result
  35. @staticmethod
  36. def getAuthorizationInfo(component_appid, authorization_code, component_access_token):
  37. """第三方平台 获得授权信息"""
  38. url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' + component_access_token
  39. param = {
  40. 'component_appid': component_appid,
  41. 'authorization_code': authorization_code,
  42. }
  43. result = requests.post(url, data=json.dumps(param))
  44. result = result.json()
  45. if 'errcode' in result and result['errcode']:
  46. raise CustomError(u'微信请求失败'+ str(result['errcode']) + ' :' + result['errmsg'])
  47. return result
  48. @staticmethod
  49. def getAuthorizerInfo(component_appid, authorizer_appid, component_access_token):
  50. """第三方平台 获得授权方信息"""
  51. url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=' + component_access_token
  52. param = {
  53. 'component_appid': component_appid,
  54. 'authorizer_appid': authorizer_appid,
  55. }
  56. result = requests.post(url, data=json.dumps(param))
  57. result = result.json()
  58. if 'errcode' in result and result['errcode']:
  59. raise CustomError(u'微信请求失败'+ str(result['errcode']) + ' :' + result['errmsg'])
  60. return result
  61. @staticmethod
  62. def getAuthorizerAccessToken(component_appid, authorizer_appid, authorizer_refresh_token, component_access_token):
  63. """第三方平台 获得授权方 access_token"""
  64. url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' + component_access_token
  65. param = {
  66. 'component_appid': component_appid,
  67. 'authorizer_appid': authorizer_appid,
  68. 'authorizer_refresh_token': authorizer_refresh_token
  69. }
  70. result = requests.post(url, data=json.dumps(param))
  71. result = result.json()
  72. if 'errcode' in result and result['errcode']:
  73. raise CustomError(u'微信请求失败'+ str(result['errcode']) + ' :' + result['errmsg'])
  74. return result
  75. @staticmethod
  76. def getCodeTemplateList(component_access_token):
  77. '''获取代码模版列表'''
  78. url = 'https://api.weixin.qq.com/wxa/gettemplatelist?access_token=' + component_access_token
  79. result = requests.get(url)
  80. result = result.json()
  81. if 'errcode' in result and result['errcode']:
  82. raise CustomError(u'微信请求失败'+ str(result['errcode']) + ' :' + result['errmsg'])
  83. return result['template_list']
  84. @staticmethod
  85. def commitCode(access_token, template_id, user_version, user_desc):
  86. '''小程序提交代码'''
  87. url = 'https://api.weixin.qq.com/wxa/commit?access_token=' + access_token
  88. param = {
  89. 'template_id': template_id,
  90. 'ext_json': "{\"extEnable\": false,\"extAppid\": \"\"}",
  91. 'user_version': user_version,
  92. 'user_desc': 'test'
  93. }
  94. result = requests.post(url, data=json.dumps(param))
  95. result = result.json()
  96. if 'errcode' in result and result['errcode']:
  97. raise CustomError(u'微信请求失败'+ str(result['errcode']) + ' :' + result['errmsg'])
  98. return result
  99. @staticmethod
  100. def submitAuditCode(access_token):
  101. '''将上传的代码提交审核'''
  102. url = 'https://api.weixin.qq.com/wxa/submit_audit?access_token=' + access_token
  103. result = requests.post(url)
  104. result = result.json()
  105. if 'errcode' in result and result['errcode']:
  106. raise CustomError(u'微信请求失败' + str(result['errcode']) + ' :' + result['errmsg'])
  107. return result
  108. @staticmethod
  109. def getLastSubmitAuditCodeStatus(access_token):
  110. '''查询最新一次提交审核代码的审核状态'''
  111. url = 'https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=' + access_token
  112. result = requests.get(url)
  113. result = result.json()
  114. if 'errcode' in result and result['errcode']:
  115. raise CustomError(u'微信请求失败' + str(result['errcode']) + ' :' + result['errmsg'])
  116. return result
  117. @staticmethod
  118. def modify_domain(access_token, action, requestdomain, wsrequestdomain, uploaddomain, downloaddomain):
  119. '''设置小程序服务器域名'''
  120. url = 'https://api.weixin.qq.com/wxa/modify_domain?access_token=' + access_token
  121. param = {
  122. "action": action,
  123. "requestdomain": requestdomain,
  124. "wsrequestdomain": wsrequestdomain,
  125. "uploaddomain": uploaddomain,
  126. "downloaddomain": downloaddomain
  127. }
  128. result = requests.post(url, data=json.dumps(param))
  129. result = result.json()
  130. if 'errcode' in result and result['errcode']:
  131. raise CustomError(u'微信请求失败' + str(result['errcode']) + ' :' + result['errmsg'])
  132. return result
  133. @staticmethod
  134. def code2Session(appid, secret, code):
  135. '''登录凭证校验'''
  136. url = 'https://api.weixin.qq.com/sns/jscode2session?appid='+ appid + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code'
  137. result = requests.get(url)
  138. result = result.json()
  139. if 'errcode' in result and result['errcode']:
  140. raise CustomError(u'微信请求失败' + str(result['errcode']) + ':' + result['errmsg'])
  141. return result
  142. @staticmethod
  143. def getAccessToken(appid, secret,):
  144. '''获取小程序验证token'''
  145. url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+ appid + '&secret=' + secret
  146. result = requests.get(url)
  147. result = result.json()
  148. if 'errcode' in result and result['errcode']:
  149. raise CustomError(u'微信请求失败' + str(result['errcode']) + ':' + result['errmsg'])
  150. return result
  151. @staticmethod
  152. def addPlugin(access_token):
  153. '''小程序插件管理'''
  154. url = 'https://api.weixin.qq.com/wxa/plugin?access_token=' + access_token
  155. param = {
  156. 'action': 'apply',
  157. 'plugin_appid': 'wxfa43a4a7041a84de'
  158. }
  159. result = requests.post(url, data=json.dumps(param))
  160. result = result.json()
  161. if 'errcode' in result and result['errcode'] and str(result['errcode']) != '89237':
  162. raise CustomError(u'微信请求失败' + str(result['errcode']) + ':' + result['errmsg'])
  163. return result
  164. @staticmethod
  165. def getTemplateList(access_token):
  166. '''获取当前帐号下的个人模板列表'''
  167. url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=' + access_token
  168. result = requests.get(url)
  169. result = result.json()
  170. if 'errcode' in result and result['errcode']:
  171. raise CustomError(u'微信请求失败' + str(result['errcode']) + ':' + result['errmsg'])
  172. return result['data']
  173. @staticmethod
  174. def sendSubscribeMessage(access_token, openid, template_id, page, data):
  175. '''发送订阅消息'''
  176. url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + access_token
  177. param = {
  178. 'touser': openid,
  179. 'template_id': template_id,
  180. 'page': page,
  181. 'data': data
  182. }
  183. result = requests.post(url, data=json.dumps(param))
  184. # print('-------------------------')
  185. # result = result.json()
  186. # print(result)
  187. #result = requests.post(url, data=json.dumps(param))
  188. #result = result.json()
  189. #if 'errcode' in result and result['errcode'] and str(result['errcode']) != '89237':
  190. # raise CustomError(u'微信请求失败' + str(result['errcode']) + ':' + result['errmsg'])
  191. #return result
  192. @staticmethod
  193. def releaseCode(access_token):
  194. '''已审核代码发布'''
  195. url = 'https://api.weixin.qq.com/wxa/release?access_token=' + access_token
  196. result = requests.post(url, data=json.dumps({}))
  197. result = result.json()
  198. if 'errcode' in result and result['errcode']:
  199. raise CustomError(u'微信请求失败' + str(result['errcode']) + ' :' + result['errmsg'])
  200. return result