30 Star 112 Fork 0

Xavier-Lam / wechat-django

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
advance-dev.md 1.97 KB
一键复制 编辑 原始数据 按行查看 历史

进阶开发

使用自定义请求客户端

在开发过程中,默认的请求客户端可能不能满足开发需求(诸如对接的微信api非微信官方api,而是第三方微信api),需要修改model中默认的请求客户端.这种情况下,我们通过代理模型来实现.

  1. 首先,我们定义自己的请求客户端

     from wechat_django.client import WeChatClient
    
     class CustomWeChatClient(WeChatClient):
         def _fetch_access_token(self, url, params):
             return dict(
                 access_token="1234567",
                 expires_in=60*30
             )
  2. 实现一个wechat_django.models.WeChatApp的代理类,复写_get_client方法

     from wechat_django.models import WeChatApp
    
     class CustomWeChatApp(WeChatApp):
         class Meta:
             proxy = True
    
         def _get_client(self):
             return CustomWeChatClient(self)
  3. 使用代理类来获取app实例

     app = CustomWeChatApp.objects.get_by_name("111")

除了client外,修改wechat_django.models.WeChatApp.oauth可通过复写wechat_django.models.WeChatApp._get_oauth方法实现,修改wechat_django.pay.models.WeChatPay.client可通过复写wechat_django.pay.models.WeChatPay._get_client实现.

使用WeChatUser作为用户登录

  1. 由于wechat_django.models.WeChatUser没有last_login字段,需要在你的appconfig中的ready方法加入以下代码防止django在自动更新update_last_login时报错

     from django.contrib.auth.models import update_last_login
     from django.contrib.auth.signals import user_logged_in
    
     user_logged_in.disconnect(update_last_login, dispatch_uid='update_last_login')
  2. 使用django.contrib.auth.login登录

Python
1
https://gitee.com/xavier-lam/django-wechat.git
git@gitee.com:xavier-lam/django-wechat.git
xavier-lam
django-wechat
wechat-django
master

搜索帮助