# WeChat-Django
[![PyPI](https://img.shields.io/pypi/v/wechat-django.svg)](https://pypi.org/project/wechat-django)
[![Build Status](https://travis-ci.org/Xavier-Lam/wechat-django.svg?branch=master)](https://travis-ci.org/Xavier-Lam/wechat-django)
[![Donate with Bitcoin](https://en.cryptobadges.io/badge/micro/1BdJG31zinrMFWxRt2utGBU2jdpv8xSgju)](https://en.cryptobadges.io/donate/1BdJG31zinrMFWxRt2utGBU2jdpv8xSgju)
**WeChat-Django**旨在为接入微信公众平台的django开发者提供便捷的微信及微信支付功能封装及基本的[**后台管理支持**](docs/admin.md).
项目官方地址: https://github.com/Xavier-Lam/wechat-django
本拓展基于[wechatpy](https://github.com/jxtech/wechatpy) ,支持的最低django版本为1.11. WeChat-Django只是一个预览版本,可能存在较多bug并且有api及数据结构变更可能,请密切关注[CHANGELOG](CHANGELOG.md)).
目录
======
- [功能](#%e5%8a%9f%e8%83%bd)
- [安装及配置](#%e5%ae%89%e8%a3%85%e5%8f%8a%e9%85%8d%e7%bd%ae)
- [初次安装](#%e5%88%9d%e6%ac%a1%e5%ae%89%e8%a3%85)
- [直接加入项目](#%e7%9b%b4%e6%8e%a5%e5%8a%a0%e5%85%a5%e9%a1%b9%e7%9b%ae)
- [更新](#%e6%9b%b4%e6%96%b0)
- [配置](#%e9%85%8d%e7%bd%ae)
- [日志](#%e6%97%a5%e5%bf%97)
- [注意事项](#%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9)
- [部分功能使用说明](#%e9%83%a8%e5%88%86%e5%8a%9f%e8%83%bd%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e)
- [网页授权](#%e7%bd%91%e9%a1%b5%e6%8e%88%e6%9d%83)
- [小程序授权](#%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%8e%88%e6%9d%83)
- [小程序信息加解密及用户数据更新](#%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%bf%a1%e6%81%af%e5%8a%a0%e8%a7%a3%e5%af%86%e5%8f%8a%e7%94%a8%e6%88%b7%e6%95%b0%e6%8d%ae%e6%9b%b4%e6%96%b0)
- [主动调用微信api](#%e4%b8%bb%e5%8a%a8%e8%b0%83%e7%94%a8%e5%be%ae%e4%bf%a1api)
- [自定义微信回复](#%e8%87%aa%e5%ae%9a%e4%b9%89%e5%be%ae%e4%bf%a1%e5%9b%9e%e5%a4%8d)
- [微信支付](#%e5%be%ae%e4%bf%a1%e6%94%af%e4%bb%98)
- [统一下单](#%e7%bb%9f%e4%b8%80%e4%b8%8b%e5%8d%95)
- [订单更新(回调)通知](#%e8%ae%a2%e5%8d%95%e6%9b%b4%e6%96%b0%e5%9b%9e%e8%b0%83%e9%80%9a%e7%9f%a5)
- [django-rest-framework](#django-rest-framework)
- [后台使用简介](#%e5%90%8e%e5%8f%b0%e4%bd%bf%e7%94%a8%e7%ae%80%e4%bb%8b)
- [示例项目](#%e7%a4%ba%e4%be%8b%e9%a1%b9%e7%9b%ae)
- [TODOS:](#todos)
- [计划的功能](#%e8%ae%a1%e5%88%92%e7%9a%84%e5%8a%9f%e8%83%bd)
- [已知bugs](#%e5%b7%b2%e7%9f%a5bugs)
- [Changelog](#changelog)
## 功能
* 公众号管理
* 同步用户及用户查看,备注,用户标签管理
* 菜单同步,查看及发布
* 同步公众号自动回复,管理自动回复,转发和自定义自动回复业务,接收消息日志
* 模板消息模板的同步及发送
* 永久素材,图文的同步及查看
* 微信网页授权
* [微信jsapi配置](docs/cookbook/web.md#jsapi)
* 主动调用微信api封装
* 微信支付api封装
* 微信支付订单管理及信号
* 后台权限管理
* django-rest-framework APIView兼容
* 迁移公众号自动回复/菜单/素材(不建议)
## 安装及配置
### 初次安装
1. 运行**pip install wechat-django[cryptography]** 或 **pip install wechat-django[pycrypto]** 安装
2. 在settings.py的**INSTALLED_APPS中添加wechat_django**
3. 运行**manage.py migrate wechat_django** 来更新数据库结构
4. 在urls.py 中引入wechat_django.sites.wechat.urls, 将其配置到urlpatterns中
5. 在settings.py中,设置`USE_TZ = True`
至此,您已可以开始轻松使用wechat_django.项目尚未提供具体的使用文档,如需客制化需求,烦请先阅读代码
### 直接加入项目
想使用最新特性或是自行编辑代码,可clone本项目后,采用pip install -e 直接安装到你的django项目目录
### 更新
1. 运行**pip install -U wechat-django**
2. 运行**python manage.py migrate** 来更新数据库结构
### 配置
一般而言,默认配置足以满足需求
| 参数名 | 默认值 | 说明 |
| --- | --- | --- |
| WECHAT_SITE_HOST | None | 用于接收微信回调的默认域名 |
| WECHAT_SITE_HTTPS | True | 接收微信回调域名是否是https |
| WECHAT_PATCHADMINSITE | True | 是否将django默认的adminsite替换为wechat_django默认的adminsite, 默认替换 |
| WECHAT_SESSIONSTORAGE | "django.core.cache.cache" | 用于存储微信accesstoken等数据的[`wechatpy.session.SessionStorage`](https://wechatpy.readthedocs.io/zh_CN/master/quickstart.html#id10) 对象,或接收 `wechat_django.models.WeChatApp` 对象并生成其实例的工厂方法 |
| WECHAT_MESSAGETIMEOFFSET | 180 | 微信请求消息时,timestamp与服务器时间差超过该值的请求将被抛弃 |
| WECHAT_MESSAGENOREPEATNONCE | True | 是否对微信消息防重放检查 默认检查 |
### 日志
| logger | 说明 |
| --- | --- |
| wechat.admin.{appname} | admin异常日志 最低级别warning |
| wechat.api.{appname} | api日志 最低级别debug |
| wechat.handler.{appname} | 消息处理日志 最低级别debug |
| wechat.oauth.{appname} | 网页授权异常日志 最低级别warning |
| wechat.site.{appname} | 站点view异常日志(如素材代理) 最低级别warning |
### 注意事项
* 框架默认采用django的cache管理accesstoken,如果有多个进程,或是多台机器部署,请确保所有worker使用公用cache以免造成token争用,如果希望不使用django的cache管理accesstoken,可以在配置项中定义SessionStorage
* 请确保在https环境下部署,否则有secretkey泄露的风险
## 部分功能使用说明
### 网页授权
可通过`wechat_django.oauth.wechat_auth`装饰器进行网页授权,授权后,request将被附上一个名为wechat的`wechat_django.oauth.WeChatOAuthInfo` 对象,可通过 request.wechat.user 拿到`wechat_django.models.WeChatUser`实例,通过 request.wechat.app 拿到`wechat_django.models.WeChatApp`实例,以下是一个基本示例
from wechat_django import wechat_auth
@wechat_auth("your_app_name")
def your_view(request, *args, **kwargs):
""":type request: wechat_django.requests.WeChatOAuthRequest"""
user = request.wechat.user
对于默认重定向行为不满意的,可以自定义response,具体的参数说明参见`wechat_django.oauth.wechat_auth`装饰器的docstring
对于class based view,可继承`wechat_django.oauth.WeChatOAuthView`类,具体参见代码
### 小程序授权
通过`wechat_django.models.WeChatApp.auth`进行授权,输入客户端传来的code, 输出一个用户对象以及原始响应.这个方法只能拿到用户的openid与unionid.
from wechat_django.models import WeChatApp
app = WeChatApp.objects.get_by_name("your app name")
user, data = app.auth(code)
对于授权后得到的session_key,框架会持久化至数据库,此后可以通过调用`wechat_django.models.WeChatUser.session`来执行相关操作.
auth方法同样适用于网页授权,第二个参数填写网页授权的scope,默认base.
#### 小程序信息加解密及用户数据更新
对于已经进行过小程序授权并且session_key尚未过期的用户,可以使用`wechat_django.models.Session.decrypt_message`来解密客户端传来的敏感数据
encrypted_data = ""
iv = ""
try:
data = user.session.decrypt_message(
encrypted_data, iv)
except ValueError:
pass # 无法正确解密数据 session_key可能过期了
亦可使用`wechat_django.models.Session.validate_message`来校验客户端传来的数据
from wechatpy.exceptions import InvalidSignatureException
signature = ""
raw_data = ""
try:
data = user.session.validate_message(raw_data, signature)
except InvalidSignatureException:
pass # 签名错误 session_key可能过期了
客户端调用`wx.getUserInfo`,可将rawData与signature传递至后端,后端通过调用`wechat_django.models.Session.validate_message`与`wechat_django.models.User.update`来更新用户信息
from django.http.response import HttpResponse
from wechatpy.exceptions import InvalidSignatureException
signature = request.POST["signature"]
raw_data = request.POST["rawData"]
try:
data = user.session.validate_message(raw_data, signature)
except InvalidSignatureException:
return HttpResponse(status=401)
使用update方法更新用户数据
user.update(data)
### 主动调用微信api
from wechat_django.models import WeChatApp
app = WeChatApp.get_by_name("your app name")
data = app.client.user.get_followers()
具体client的使用方式,请移步[wechatpy文档](https://wechatpy.readthedocs.io/zh_CN/master/client/index.html)
### 自定义微信回复
在后台配置自定义回复,填写自定义回复处理代码的路径,代码须由 `wechat_django.handler.message_handler` 装饰对应的方法接收一个 `wechat_django.models.WeChatMessageInfo` 对象,返回字符串或一个 [`wechatpy.replies.BaseReply`](https://wechatpy.readthedocs.io/zh_CN/master/replies.html) 对象
from wechat_django import message_handler
@message_handler
def custom_business(message):
"""
:type message: wechat_django.models.WeChatMessageInfo
"""
user = message.user
msg = message.message
text = "hello, {0}! we received a {1} message.".format(
user, msg.type)
return TextReply(content=text.encode())
### 微信支付
使用微信支付,需要在INSTALLED_APP的`wechat_django`后添加`wechat_django.pay`.
#### 统一下单
from wechat_django.models import WeChatApp
app = WeChatApp.objects.get_by_name("your app name")
order = app.pay.create_order(
user="user-instance", body="body", total_fee=1,
out_trade_no="***debug***20190613001") # 也可以用openid="openid"代替user参数
prepay = order.prepay(request)
将jsapi参数交给前端
jsapi_params = order.jsapi_params(prepay["prepay_id"])
主动查询订单状态
order.sync()
#### 订单更新(回调)通知
当订单更新时,会发出`wechat_django.pay.signals.order_updated`信号,sender为订单`wechat_django.utils.func.Static("{appname}.{payname}")`.信号提供4个变量
| 变量 | 说明 |
| --- | --- |
| result | 订单结果(`wechat_django.pay.models.UnifiedOrderResult`) |
| order | 更新的订单(`wechat_django.pay.models.UnifiedOrder`) |
| state | 订单状态(`wechat_django.pay.models.UnifiedOrderResult.State`) |
| attach | 结果附带的信息(生成订单时传给微信服务器的attach) |
使用示例
from django.dispatch import receiver
from wechat_django.pay import signals
@receiver(signals.order_updated)
def order_updated(result, order, state, attach):
if state == UnifiedOrderResult.State.SUCCESS:
pass
> 注意! 每次主动调用,微信通知或是后台重新触发都会发送信号,请自行确保订单成功信号逻辑只执行一次!
### django-rest-framework
本项目class-based OAuth授权兼容django-rest-framework.
1. 构造一个继承`wechat_django.oauth.WeChatOAuthViewMixin`的视图类;
2. 在视图类中定义`appname`属性;
3. 根据需要,定义`permission_classes`(如若资源必须授权才可访问,请在permission_classes中添加`wechat_django.oauth.WeChatAuthenticated`);
4. 根据需要,自行处理异常,在`handle_exception`方法中捕获`rest_framework.exceptions.NotAuthenticated`,自行处理.
可以参见示例项目的[rest.py](sample/wechat/rest.py)文件.
## 后台使用简介
参见[管理后台使用简介](docs/admin.md) 文档
## 示例项目
可参考[本项目sample文件夹](sample)
## TODOS:
* 是否可做成migrate权限全自助?重构权限模块?
* 可选加密存储敏感数据
* [Cookbook](docs/cookbook/readme.md)
* app层面的message log和reply log
* 完善单元测试
* 后台表单验证
### 计划的功能
* 命令行工具
* 第三方平台接入
* accesstoken开放给第三方并对接第三方accesstoken
* 客服消息/对话
* 清理及保护永久素材
* 回复及一些查询缓存
* 菜单及消息处理程序的导入导出
* 素材Storage
### 已知bugs
* 多次同步消息处理器会重复生成永久素材
## [Changelog](CHANGELOG.md)
Xavier-Lam@NetDragon
Raw data
{
"_id": null,
"home_page": "https://github.com/Xavier-Lam/wechat-django",
"name": "wechat-django",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "WeChat, weixin, wx, WeChatPay, micromessenger, django, \u5fae\u4fe1, \u5fae\u4fe1\u652f\u4ed8",
"author": "Xavier-Lam",
"author_email": "xavierlam7@hotmail.com",
"download_url": "https://files.pythonhosted.org/packages/d7/73/63ccec676bdfad039a4e1aeea24286c0977e07c6e1f8c8a61de901d03b6b/wechat_django-0.4.0.tar.gz",
"platform": null,
"description": "# WeChat-Django\r\r\n\r\r\n[![PyPI](https://img.shields.io/pypi/v/wechat-django.svg)](https://pypi.org/project/wechat-django)\r\r\n[![Build Status](https://travis-ci.org/Xavier-Lam/wechat-django.svg?branch=master)](https://travis-ci.org/Xavier-Lam/wechat-django)\r\r\n[![Donate with Bitcoin](https://en.cryptobadges.io/badge/micro/1BdJG31zinrMFWxRt2utGBU2jdpv8xSgju)](https://en.cryptobadges.io/donate/1BdJG31zinrMFWxRt2utGBU2jdpv8xSgju)\r\r\n\r\r\n**WeChat-Django**\u65e8\u5728\u4e3a\u63a5\u5165\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\u7684django\u5f00\u53d1\u8005\u63d0\u4f9b\u4fbf\u6377\u7684\u5fae\u4fe1\u53ca\u5fae\u4fe1\u652f\u4ed8\u529f\u80fd\u5c01\u88c5\u53ca\u57fa\u672c\u7684[**\u540e\u53f0\u7ba1\u7406\u652f\u6301**](docs/admin.md).\r\r\n\r\r\n\u9879\u76ee\u5b98\u65b9\u5730\u5740: https://github.com/Xavier-Lam/wechat-django\r\r\n\r\r\n\u672c\u62d3\u5c55\u57fa\u4e8e[wechatpy](https://github.com/jxtech/wechatpy) ,\u652f\u6301\u7684\u6700\u4f4edjango\u7248\u672c\u4e3a1.11. WeChat-Django\u53ea\u662f\u4e00\u4e2a\u9884\u89c8\u7248\u672c,\u53ef\u80fd\u5b58\u5728\u8f83\u591abug\u5e76\u4e14\u6709api\u53ca\u6570\u636e\u7ed3\u6784\u53d8\u66f4\u53ef\u80fd,\u8bf7\u5bc6\u5207\u5173\u6ce8[CHANGELOG](CHANGELOG.md)).\r\r\n\r\r\n\u76ee\u5f55\r\r\n======\r\r\n- [\u529f\u80fd](#%e5%8a%9f%e8%83%bd)\r\r\n- [\u5b89\u88c5\u53ca\u914d\u7f6e](#%e5%ae%89%e8%a3%85%e5%8f%8a%e9%85%8d%e7%bd%ae)\r\r\n - [\u521d\u6b21\u5b89\u88c5](#%e5%88%9d%e6%ac%a1%e5%ae%89%e8%a3%85)\r\r\n - [\u76f4\u63a5\u52a0\u5165\u9879\u76ee](#%e7%9b%b4%e6%8e%a5%e5%8a%a0%e5%85%a5%e9%a1%b9%e7%9b%ae)\r\r\n - [\u66f4\u65b0](#%e6%9b%b4%e6%96%b0)\r\r\n - [\u914d\u7f6e](#%e9%85%8d%e7%bd%ae)\r\r\n - [\u65e5\u5fd7](#%e6%97%a5%e5%bf%97)\r\r\n - [\u6ce8\u610f\u4e8b\u9879](#%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9)\r\r\n- [\u90e8\u5206\u529f\u80fd\u4f7f\u7528\u8bf4\u660e](#%e9%83%a8%e5%88%86%e5%8a%9f%e8%83%bd%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e)\r\r\n - [\u7f51\u9875\u6388\u6743](#%e7%bd%91%e9%a1%b5%e6%8e%88%e6%9d%83)\r\r\n - [\u5c0f\u7a0b\u5e8f\u6388\u6743](#%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%8e%88%e6%9d%83)\r\r\n - [\u5c0f\u7a0b\u5e8f\u4fe1\u606f\u52a0\u89e3\u5bc6\u53ca\u7528\u6237\u6570\u636e\u66f4\u65b0](#%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%bf%a1%e6%81%af%e5%8a%a0%e8%a7%a3%e5%af%86%e5%8f%8a%e7%94%a8%e6%88%b7%e6%95%b0%e6%8d%ae%e6%9b%b4%e6%96%b0)\r\r\n - [\u4e3b\u52a8\u8c03\u7528\u5fae\u4fe1api](#%e4%b8%bb%e5%8a%a8%e8%b0%83%e7%94%a8%e5%be%ae%e4%bf%a1api)\r\r\n - [\u81ea\u5b9a\u4e49\u5fae\u4fe1\u56de\u590d](#%e8%87%aa%e5%ae%9a%e4%b9%89%e5%be%ae%e4%bf%a1%e5%9b%9e%e5%a4%8d)\r\r\n - [\u5fae\u4fe1\u652f\u4ed8](#%e5%be%ae%e4%bf%a1%e6%94%af%e4%bb%98)\r\r\n - [\u7edf\u4e00\u4e0b\u5355](#%e7%bb%9f%e4%b8%80%e4%b8%8b%e5%8d%95)\r\r\n - [\u8ba2\u5355\u66f4\u65b0(\u56de\u8c03)\u901a\u77e5](#%e8%ae%a2%e5%8d%95%e6%9b%b4%e6%96%b0%e5%9b%9e%e8%b0%83%e9%80%9a%e7%9f%a5)\r\r\n - [django-rest-framework](#django-rest-framework)\r\r\n- [\u540e\u53f0\u4f7f\u7528\u7b80\u4ecb](#%e5%90%8e%e5%8f%b0%e4%bd%bf%e7%94%a8%e7%ae%80%e4%bb%8b)\r\r\n- [\u793a\u4f8b\u9879\u76ee](#%e7%a4%ba%e4%be%8b%e9%a1%b9%e7%9b%ae)\r\r\n- [TODOS:](#todos)\r\r\n - [\u8ba1\u5212\u7684\u529f\u80fd](#%e8%ae%a1%e5%88%92%e7%9a%84%e5%8a%9f%e8%83%bd)\r\r\n - [\u5df2\u77e5bugs](#%e5%b7%b2%e7%9f%a5bugs)\r\r\n- [Changelog](#changelog)\r\r\n\r\r\n\r\r\n## \u529f\u80fd\r\r\n* \u516c\u4f17\u53f7\u7ba1\u7406\r\r\n* \u540c\u6b65\u7528\u6237\u53ca\u7528\u6237\u67e5\u770b,\u5907\u6ce8,\u7528\u6237\u6807\u7b7e\u7ba1\u7406\r\r\n* \u83dc\u5355\u540c\u6b65,\u67e5\u770b\u53ca\u53d1\u5e03\r\r\n* \u540c\u6b65\u516c\u4f17\u53f7\u81ea\u52a8\u56de\u590d,\u7ba1\u7406\u81ea\u52a8\u56de\u590d,\u8f6c\u53d1\u548c\u81ea\u5b9a\u4e49\u81ea\u52a8\u56de\u590d\u4e1a\u52a1,\u63a5\u6536\u6d88\u606f\u65e5\u5fd7\r\r\n* \u6a21\u677f\u6d88\u606f\u6a21\u677f\u7684\u540c\u6b65\u53ca\u53d1\u9001\r\r\n* \u6c38\u4e45\u7d20\u6750,\u56fe\u6587\u7684\u540c\u6b65\u53ca\u67e5\u770b\r\r\n* \u5fae\u4fe1\u7f51\u9875\u6388\u6743\r\r\n* [\u5fae\u4fe1jsapi\u914d\u7f6e](docs/cookbook/web.md#jsapi)\r\r\n* \u4e3b\u52a8\u8c03\u7528\u5fae\u4fe1api\u5c01\u88c5\r\r\n* \u5fae\u4fe1\u652f\u4ed8api\u5c01\u88c5\r\r\n* \u5fae\u4fe1\u652f\u4ed8\u8ba2\u5355\u7ba1\u7406\u53ca\u4fe1\u53f7\r\r\n* \u540e\u53f0\u6743\u9650\u7ba1\u7406\r\r\n* django-rest-framework APIView\u517c\u5bb9\r\r\n* \u8fc1\u79fb\u516c\u4f17\u53f7\u81ea\u52a8\u56de\u590d/\u83dc\u5355/\u7d20\u6750(\u4e0d\u5efa\u8bae)\r\r\n\r\r\n## \u5b89\u88c5\u53ca\u914d\u7f6e\r\r\n### \u521d\u6b21\u5b89\u88c5\r\r\n1. \u8fd0\u884c**pip install wechat-django[cryptography]** \u6216 **pip install wechat-django[pycrypto]** \u5b89\u88c5\r\r\n2. \u5728settings.py\u7684**INSTALLED_APPS\u4e2d\u6dfb\u52a0wechat_django**\r\r\n3. \u8fd0\u884c**manage.py migrate wechat_django** \u6765\u66f4\u65b0\u6570\u636e\u5e93\u7ed3\u6784\r\r\n4. \u5728urls.py \u4e2d\u5f15\u5165wechat_django.sites.wechat.urls, \u5c06\u5176\u914d\u7f6e\u5230urlpatterns\u4e2d\r\r\n5. \u5728settings.py\u4e2d,\u8bbe\u7f6e`USE_TZ = True`\r\r\n\r\r\n\u81f3\u6b64,\u60a8\u5df2\u53ef\u4ee5\u5f00\u59cb\u8f7b\u677e\u4f7f\u7528wechat_django.\u9879\u76ee\u5c1a\u672a\u63d0\u4f9b\u5177\u4f53\u7684\u4f7f\u7528\u6587\u6863,\u5982\u9700\u5ba2\u5236\u5316\u9700\u6c42,\u70e6\u8bf7\u5148\u9605\u8bfb\u4ee3\u7801\r\r\n\r\r\n### \u76f4\u63a5\u52a0\u5165\u9879\u76ee\r\r\n\u60f3\u4f7f\u7528\u6700\u65b0\u7279\u6027\u6216\u662f\u81ea\u884c\u7f16\u8f91\u4ee3\u7801,\u53efclone\u672c\u9879\u76ee\u540e,\u91c7\u7528pip install -e \u76f4\u63a5\u5b89\u88c5\u5230\u4f60\u7684django\u9879\u76ee\u76ee\u5f55\r\r\n\r\r\n### \u66f4\u65b0\r\r\n1. \u8fd0\u884c**pip install -U wechat-django**\r\r\n2. \u8fd0\u884c**python manage.py migrate** \u6765\u66f4\u65b0\u6570\u636e\u5e93\u7ed3\u6784\r\r\n\r\r\n### \u914d\u7f6e\r\r\n\u4e00\u822c\u800c\u8a00,\u9ed8\u8ba4\u914d\u7f6e\u8db3\u4ee5\u6ee1\u8db3\u9700\u6c42\r\r\n\r\r\n| \u53c2\u6570\u540d | \u9ed8\u8ba4\u503c | \u8bf4\u660e |\r\r\n| --- | --- | --- |\r\r\n| WECHAT_SITE_HOST | None | \u7528\u4e8e\u63a5\u6536\u5fae\u4fe1\u56de\u8c03\u7684\u9ed8\u8ba4\u57df\u540d |\r\r\n| WECHAT_SITE_HTTPS | True | \u63a5\u6536\u5fae\u4fe1\u56de\u8c03\u57df\u540d\u662f\u5426\u662fhttps |\r\r\n| WECHAT_PATCHADMINSITE | True | \u662f\u5426\u5c06django\u9ed8\u8ba4\u7684adminsite\u66ff\u6362\u4e3awechat_django\u9ed8\u8ba4\u7684adminsite, \u9ed8\u8ba4\u66ff\u6362 |\r\r\n| WECHAT_SESSIONSTORAGE | \"django.core.cache.cache\" | \u7528\u4e8e\u5b58\u50a8\u5fae\u4fe1accesstoken\u7b49\u6570\u636e\u7684[`wechatpy.session.SessionStorage`](https://wechatpy.readthedocs.io/zh_CN/master/quickstart.html#id10) \u5bf9\u8c61,\u6216\u63a5\u6536 `wechat_django.models.WeChatApp` \u5bf9\u8c61\u5e76\u751f\u6210\u5176\u5b9e\u4f8b\u7684\u5de5\u5382\u65b9\u6cd5 |\r\r\n| WECHAT_MESSAGETIMEOFFSET | 180 | \u5fae\u4fe1\u8bf7\u6c42\u6d88\u606f\u65f6,timestamp\u4e0e\u670d\u52a1\u5668\u65f6\u95f4\u5dee\u8d85\u8fc7\u8be5\u503c\u7684\u8bf7\u6c42\u5c06\u88ab\u629b\u5f03 |\r\r\n| WECHAT_MESSAGENOREPEATNONCE | True | \u662f\u5426\u5bf9\u5fae\u4fe1\u6d88\u606f\u9632\u91cd\u653e\u68c0\u67e5 \u9ed8\u8ba4\u68c0\u67e5 |\r\r\n\r\r\n### \u65e5\u5fd7\r\r\n| logger | \u8bf4\u660e |\r\r\n| --- | --- |\r\r\n| wechat.admin.{appname} | admin\u5f02\u5e38\u65e5\u5fd7 \u6700\u4f4e\u7ea7\u522bwarning |\r\r\n| wechat.api.{appname} | api\u65e5\u5fd7 \u6700\u4f4e\u7ea7\u522bdebug |\r\r\n| wechat.handler.{appname} | \u6d88\u606f\u5904\u7406\u65e5\u5fd7 \u6700\u4f4e\u7ea7\u522bdebug |\r\r\n| wechat.oauth.{appname} | \u7f51\u9875\u6388\u6743\u5f02\u5e38\u65e5\u5fd7 \u6700\u4f4e\u7ea7\u522bwarning |\r\r\n| wechat.site.{appname} | \u7ad9\u70b9view\u5f02\u5e38\u65e5\u5fd7(\u5982\u7d20\u6750\u4ee3\u7406) \u6700\u4f4e\u7ea7\u522bwarning |\r\r\n\r\r\n### \u6ce8\u610f\u4e8b\u9879\r\r\n* \u6846\u67b6\u9ed8\u8ba4\u91c7\u7528django\u7684cache\u7ba1\u7406accesstoken,\u5982\u679c\u6709\u591a\u4e2a\u8fdb\u7a0b,\u6216\u662f\u591a\u53f0\u673a\u5668\u90e8\u7f72,\u8bf7\u786e\u4fdd\u6240\u6709worker\u4f7f\u7528\u516c\u7528cache\u4ee5\u514d\u9020\u6210token\u4e89\u7528,\u5982\u679c\u5e0c\u671b\u4e0d\u4f7f\u7528django\u7684cache\u7ba1\u7406accesstoken,\u53ef\u4ee5\u5728\u914d\u7f6e\u9879\u4e2d\u5b9a\u4e49SessionStorage\r\r\n* \u8bf7\u786e\u4fdd\u5728https\u73af\u5883\u4e0b\u90e8\u7f72,\u5426\u5219\u6709secretkey\u6cc4\u9732\u7684\u98ce\u9669\r\r\n\r\r\n## \u90e8\u5206\u529f\u80fd\u4f7f\u7528\u8bf4\u660e\r\r\n### \u7f51\u9875\u6388\u6743\r\r\n\u53ef\u901a\u8fc7`wechat_django.oauth.wechat_auth`\u88c5\u9970\u5668\u8fdb\u884c\u7f51\u9875\u6388\u6743,\u6388\u6743\u540e,request\u5c06\u88ab\u9644\u4e0a\u4e00\u4e2a\u540d\u4e3awechat\u7684`wechat_django.oauth.WeChatOAuthInfo` \u5bf9\u8c61,\u53ef\u901a\u8fc7 request.wechat.user \u62ff\u5230`wechat_django.models.WeChatUser`\u5b9e\u4f8b,\u901a\u8fc7 request.wechat.app \u62ff\u5230`wechat_django.models.WeChatApp`\u5b9e\u4f8b,\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\r\r\n\r\r\n from wechat_django import wechat_auth\r\r\n\r\r\n @wechat_auth(\"your_app_name\")\r\r\n def your_view(request, *args, **kwargs):\r\r\n \"\"\":type request: wechat_django.requests.WeChatOAuthRequest\"\"\"\r\r\n user = request.wechat.user\r\r\n\r\r\n\u5bf9\u4e8e\u9ed8\u8ba4\u91cd\u5b9a\u5411\u884c\u4e3a\u4e0d\u6ee1\u610f\u7684,\u53ef\u4ee5\u81ea\u5b9a\u4e49response,\u5177\u4f53\u7684\u53c2\u6570\u8bf4\u660e\u53c2\u89c1`wechat_django.oauth.wechat_auth`\u88c5\u9970\u5668\u7684docstring\r\r\n\r\r\n\u5bf9\u4e8eclass based view,\u53ef\u7ee7\u627f`wechat_django.oauth.WeChatOAuthView`\u7c7b,\u5177\u4f53\u53c2\u89c1\u4ee3\u7801\r\r\n\r\r\n\r\r\n### \u5c0f\u7a0b\u5e8f\u6388\u6743\r\r\n\u901a\u8fc7`wechat_django.models.WeChatApp.auth`\u8fdb\u884c\u6388\u6743,\u8f93\u5165\u5ba2\u6237\u7aef\u4f20\u6765\u7684code, \u8f93\u51fa\u4e00\u4e2a\u7528\u6237\u5bf9\u8c61\u4ee5\u53ca\u539f\u59cb\u54cd\u5e94.\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u80fd\u62ff\u5230\u7528\u6237\u7684openid\u4e0eunionid.\r\r\n\r\r\n from wechat_django.models import WeChatApp\r\r\n app = WeChatApp.objects.get_by_name(\"your app name\")\r\r\n user, data = app.auth(code)\r\r\n\r\r\n\u5bf9\u4e8e\u6388\u6743\u540e\u5f97\u5230\u7684session_key,\u6846\u67b6\u4f1a\u6301\u4e45\u5316\u81f3\u6570\u636e\u5e93,\u6b64\u540e\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528`wechat_django.models.WeChatUser.session`\u6765\u6267\u884c\u76f8\u5173\u64cd\u4f5c.\r\r\n\r\r\nauth\u65b9\u6cd5\u540c\u6837\u9002\u7528\u4e8e\u7f51\u9875\u6388\u6743,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u586b\u5199\u7f51\u9875\u6388\u6743\u7684scope,\u9ed8\u8ba4base.\r\r\n\r\r\n#### \u5c0f\u7a0b\u5e8f\u4fe1\u606f\u52a0\u89e3\u5bc6\u53ca\u7528\u6237\u6570\u636e\u66f4\u65b0\r\r\n\u5bf9\u4e8e\u5df2\u7ecf\u8fdb\u884c\u8fc7\u5c0f\u7a0b\u5e8f\u6388\u6743\u5e76\u4e14session_key\u5c1a\u672a\u8fc7\u671f\u7684\u7528\u6237,\u53ef\u4ee5\u4f7f\u7528`wechat_django.models.Session.decrypt_message`\u6765\u89e3\u5bc6\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u654f\u611f\u6570\u636e\r\r\n\r\r\n encrypted_data = \"\"\r\r\n iv = \"\"\r\r\n try:\r\r\n data = user.session.decrypt_message(\r\r\n encrypted_data, iv)\r\r\n except ValueError:\r\r\n pass # \u65e0\u6cd5\u6b63\u786e\u89e3\u5bc6\u6570\u636e session_key\u53ef\u80fd\u8fc7\u671f\u4e86\r\r\n\r\r\n\r\r\n\u4ea6\u53ef\u4f7f\u7528`wechat_django.models.Session.validate_message`\u6765\u6821\u9a8c\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u6570\u636e\r\r\n\r\r\n from wechatpy.exceptions import InvalidSignatureException\r\r\n\r\r\n signature = \"\"\r\r\n raw_data = \"\"\r\r\n try:\r\r\n data = user.session.validate_message(raw_data, signature)\r\r\n except InvalidSignatureException:\r\r\n pass # \u7b7e\u540d\u9519\u8bef session_key\u53ef\u80fd\u8fc7\u671f\u4e86\r\r\n\r\r\n\u5ba2\u6237\u7aef\u8c03\u7528`wx.getUserInfo`,\u53ef\u5c06rawData\u4e0esignature\u4f20\u9012\u81f3\u540e\u7aef,\u540e\u7aef\u901a\u8fc7\u8c03\u7528`wechat_django.models.Session.validate_message`\u4e0e`wechat_django.models.User.update`\u6765\u66f4\u65b0\u7528\u6237\u4fe1\u606f\r\r\n\r\r\n from django.http.response import HttpResponse\r\r\n from wechatpy.exceptions import InvalidSignatureException\r\r\n\r\r\n signature = request.POST[\"signature\"]\r\r\n raw_data = request.POST[\"rawData\"]\r\r\n \r\r\n try:\r\r\n data = user.session.validate_message(raw_data, signature)\r\r\n except InvalidSignatureException:\r\r\n return HttpResponse(status=401)\r\r\n\r\r\n\u4f7f\u7528update\u65b9\u6cd5\u66f4\u65b0\u7528\u6237\u6570\u636e\r\r\n\r\r\n user.update(data)\r\r\n\r\r\n### \u4e3b\u52a8\u8c03\u7528\u5fae\u4fe1api\r\r\n from wechat_django.models import WeChatApp\r\r\n app = WeChatApp.get_by_name(\"your app name\")\r\r\n data = app.client.user.get_followers()\r\r\n\r\r\n\u5177\u4f53client\u7684\u4f7f\u7528\u65b9\u5f0f,\u8bf7\u79fb\u6b65[wechatpy\u6587\u6863](https://wechatpy.readthedocs.io/zh_CN/master/client/index.html)\r\r\n\r\r\n### \u81ea\u5b9a\u4e49\u5fae\u4fe1\u56de\u590d\r\r\n\u5728\u540e\u53f0\u914d\u7f6e\u81ea\u5b9a\u4e49\u56de\u590d,\u586b\u5199\u81ea\u5b9a\u4e49\u56de\u590d\u5904\u7406\u4ee3\u7801\u7684\u8def\u5f84,\u4ee3\u7801\u987b\u7531 `wechat_django.handler.message_handler` \u88c5\u9970\u5bf9\u5e94\u7684\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a `wechat_django.models.WeChatMessageInfo` \u5bf9\u8c61,\u8fd4\u56de\u5b57\u7b26\u4e32\u6216\u4e00\u4e2a [`wechatpy.replies.BaseReply`](https://wechatpy.readthedocs.io/zh_CN/master/replies.html) \u5bf9\u8c61\r\r\n\r\r\n from wechat_django import message_handler\r\r\n\r\r\n @message_handler\r\r\n def custom_business(message):\r\r\n \"\"\"\r\r\n :type message: wechat_django.models.WeChatMessageInfo\r\r\n \"\"\"\r\r\n user = message.user\r\r\n msg = message.message\r\r\n text = \"hello, {0}! we received a {1} message.\".format(\r\r\n user, msg.type)\r\r\n return TextReply(content=text.encode())\r\r\n\r\r\n### \u5fae\u4fe1\u652f\u4ed8\r\r\n\u4f7f\u7528\u5fae\u4fe1\u652f\u4ed8,\u9700\u8981\u5728INSTALLED_APP\u7684`wechat_django`\u540e\u6dfb\u52a0`wechat_django.pay`.\r\r\n\r\r\n#### \u7edf\u4e00\u4e0b\u5355\r\r\n\r\r\n from wechat_django.models import WeChatApp\r\r\n app = WeChatApp.objects.get_by_name(\"your app name\")\r\r\n order = app.pay.create_order(\r\r\n user=\"user-instance\", body=\"body\", total_fee=1,\r\r\n out_trade_no=\"***debug***20190613001\") # \u4e5f\u53ef\u4ee5\u7528openid=\"openid\"\u4ee3\u66ffuser\u53c2\u6570\r\r\n prepay = order.prepay(request)\r\r\n\r\r\n\u5c06jsapi\u53c2\u6570\u4ea4\u7ed9\u524d\u7aef\r\r\n\r\r\n jsapi_params = order.jsapi_params(prepay[\"prepay_id\"])\r\r\n\r\r\n\u4e3b\u52a8\u67e5\u8be2\u8ba2\u5355\u72b6\u6001\r\r\n\r\r\n order.sync()\r\r\n\r\r\n#### \u8ba2\u5355\u66f4\u65b0(\u56de\u8c03)\u901a\u77e5\r\r\n\r\r\n\u5f53\u8ba2\u5355\u66f4\u65b0\u65f6,\u4f1a\u53d1\u51fa`wechat_django.pay.signals.order_updated`\u4fe1\u53f7,sender\u4e3a\u8ba2\u5355`wechat_django.utils.func.Static(\"{appname}.{payname}\")`.\u4fe1\u53f7\u63d0\u4f9b4\u4e2a\u53d8\u91cf\r\r\n\r\r\n| \u53d8\u91cf | \u8bf4\u660e |\r\r\n| --- | --- |\r\r\n| result | \u8ba2\u5355\u7ed3\u679c(`wechat_django.pay.models.UnifiedOrderResult`) |\r\r\n| order | \u66f4\u65b0\u7684\u8ba2\u5355(`wechat_django.pay.models.UnifiedOrder`) |\r\r\n| state | \u8ba2\u5355\u72b6\u6001(`wechat_django.pay.models.UnifiedOrderResult.State`) |\r\r\n| attach | \u7ed3\u679c\u9644\u5e26\u7684\u4fe1\u606f(\u751f\u6210\u8ba2\u5355\u65f6\u4f20\u7ed9\u5fae\u4fe1\u670d\u52a1\u5668\u7684attach) |\r\r\n\r\r\n\u4f7f\u7528\u793a\u4f8b\r\r\n\r\r\n from django.dispatch import receiver\r\r\n from wechat_django.pay import signals\r\r\n \r\r\n @receiver(signals.order_updated)\r\r\n def order_updated(result, order, state, attach):\r\r\n if state == UnifiedOrderResult.State.SUCCESS:\r\r\n pass\r\r\n\r\r\n> \u6ce8\u610f! \u6bcf\u6b21\u4e3b\u52a8\u8c03\u7528,\u5fae\u4fe1\u901a\u77e5\u6216\u662f\u540e\u53f0\u91cd\u65b0\u89e6\u53d1\u90fd\u4f1a\u53d1\u9001\u4fe1\u53f7,\u8bf7\u81ea\u884c\u786e\u4fdd\u8ba2\u5355\u6210\u529f\u4fe1\u53f7\u903b\u8f91\u53ea\u6267\u884c\u4e00\u6b21!\r\r\n\r\r\n### django-rest-framework\r\r\n\u672c\u9879\u76eeclass-based OAuth\u6388\u6743\u517c\u5bb9django-rest-framework.\r\r\n\r\r\n 1. \u6784\u9020\u4e00\u4e2a\u7ee7\u627f`wechat_django.oauth.WeChatOAuthViewMixin`\u7684\u89c6\u56fe\u7c7b;\r\r\n 2. \u5728\u89c6\u56fe\u7c7b\u4e2d\u5b9a\u4e49`appname`\u5c5e\u6027;\r\r\n 3. \u6839\u636e\u9700\u8981,\u5b9a\u4e49`permission_classes`(\u5982\u82e5\u8d44\u6e90\u5fc5\u987b\u6388\u6743\u624d\u53ef\u8bbf\u95ee,\u8bf7\u5728permission_classes\u4e2d\u6dfb\u52a0`wechat_django.oauth.WeChatAuthenticated`);\r\r\n 4. \u6839\u636e\u9700\u8981,\u81ea\u884c\u5904\u7406\u5f02\u5e38,\u5728`handle_exception`\u65b9\u6cd5\u4e2d\u6355\u83b7`rest_framework.exceptions.NotAuthenticated`,\u81ea\u884c\u5904\u7406.\r\r\n\r\r\n\u53ef\u4ee5\u53c2\u89c1\u793a\u4f8b\u9879\u76ee\u7684[rest.py](sample/wechat/rest.py)\u6587\u4ef6.\r\r\n\r\r\n## \u540e\u53f0\u4f7f\u7528\u7b80\u4ecb\r\r\n\u53c2\u89c1[\u7ba1\u7406\u540e\u53f0\u4f7f\u7528\u7b80\u4ecb](docs/admin.md) \u6587\u6863\r\r\n\r\r\n## \u793a\u4f8b\u9879\u76ee\r\r\n\u53ef\u53c2\u8003[\u672c\u9879\u76eesample\u6587\u4ef6\u5939](sample)\r\r\n\r\r\n## TODOS:\r\r\n* \u662f\u5426\u53ef\u505a\u6210migrate\u6743\u9650\u5168\u81ea\u52a9?\u91cd\u6784\u6743\u9650\u6a21\u5757?\r\r\n* \u53ef\u9009\u52a0\u5bc6\u5b58\u50a8\u654f\u611f\u6570\u636e\r\r\n* [Cookbook](docs/cookbook/readme.md)\r\r\n* app\u5c42\u9762\u7684message log\u548creply log\r\r\n* \u5b8c\u5584\u5355\u5143\u6d4b\u8bd5\r\r\n* \u540e\u53f0\u8868\u5355\u9a8c\u8bc1\r\r\n\r\r\n### \u8ba1\u5212\u7684\u529f\u80fd\r\r\n* \u547d\u4ee4\u884c\u5de5\u5177\r\r\n* \u7b2c\u4e09\u65b9\u5e73\u53f0\u63a5\u5165\r\r\n* accesstoken\u5f00\u653e\u7ed9\u7b2c\u4e09\u65b9\u5e76\u5bf9\u63a5\u7b2c\u4e09\u65b9accesstoken\r\r\n* \u5ba2\u670d\u6d88\u606f/\u5bf9\u8bdd\r\r\n* \u6e05\u7406\u53ca\u4fdd\u62a4\u6c38\u4e45\u7d20\u6750\r\r\n* \u56de\u590d\u53ca\u4e00\u4e9b\u67e5\u8be2\u7f13\u5b58\r\r\n* \u83dc\u5355\u53ca\u6d88\u606f\u5904\u7406\u7a0b\u5e8f\u7684\u5bfc\u5165\u5bfc\u51fa\r\r\n* \u7d20\u6750Storage\r\r\n\r\r\n### \u5df2\u77e5bugs\r\r\n* \u591a\u6b21\u540c\u6b65\u6d88\u606f\u5904\u7406\u5668\u4f1a\u91cd\u590d\u751f\u6210\u6c38\u4e45\u7d20\u6750\r\r\n\r\r\n## [Changelog](CHANGELOG.md)\r\r\n\r\r\n\r\r\nXavier-Lam@NetDragon\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Django WeChat Extension",
"version": "0.4.0",
"project_urls": {
"Homepage": "https://github.com/Xavier-Lam/wechat-django"
},
"split_keywords": [
"wechat",
" weixin",
" wx",
" wechatpay",
" micromessenger",
" django",
" \u5fae\u4fe1",
" \u5fae\u4fe1\u652f\u4ed8"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "383d4c54abc6f764d18d362e2ad0b23c5ba3a7198abf893f6365075afafc0ec7",
"md5": "e25d1d06956e67281fae0238bee5424a",
"sha256": "e938922c9bf135a45655be72a54abd7bdad91018f7b4ba71f5a307e390749134"
},
"downloads": -1,
"filename": "wechat_django-0.4.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "e25d1d06956e67281fae0238bee5424a",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 168532,
"upload_time": "2024-12-15T15:38:51",
"upload_time_iso_8601": "2024-12-15T15:38:51.346337Z",
"url": "https://files.pythonhosted.org/packages/38/3d/4c54abc6f764d18d362e2ad0b23c5ba3a7198abf893f6365075afafc0ec7/wechat_django-0.4.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d77363ccec676bdfad039a4e1aeea24286c0977e07c6e1f8c8a61de901d03b6b",
"md5": "387861ac78630290833f808976395936",
"sha256": "8543bce00ca314bca02114cdfd0af65d52f97624b52ad74ee9f268eebb1cab96"
},
"downloads": -1,
"filename": "wechat_django-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "387861ac78630290833f808976395936",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 120740,
"upload_time": "2024-12-15T15:38:53",
"upload_time_iso_8601": "2024-12-15T15:38:53.719970Z",
"url": "https://files.pythonhosted.org/packages/d7/73/63ccec676bdfad039a4e1aeea24286c0977e07c6e1f8c8a61de901d03b6b/wechat_django-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-15 15:38:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Xavier-Lam",
"github_project": "wechat-django",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "Django",
"specs": [
[
">=",
"3.2"
]
]
},
{
"name": "django-object-tool",
"specs": [
[
">=",
"0.0.9"
]
]
},
{
"name": "pytz",
"specs": []
},
{
"name": "requests",
"specs": [
[
">=",
"2.4.3"
]
]
},
{
"name": "wechatpy",
"specs": [
[
">=",
"1.8.3"
]
]
}
],
"tox": true,
"lcname": "wechat-django"
}