Django實現微信消息推送

一 所需準備條件

微信公衆號的分類html

  • 微信消息推送
    • 公衆號
      • 已認證公衆號
      • 服務號
      • 已認證服務號
      • 企業號

基於:微信認證服務號 主動推送微信消息。
前提:關注服務號
環境:沙箱環境jquery

沙箱環境地址: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/loginajax

二 基本流程

    1. 註冊開發者帳號數據庫

      得到:appID、appsecretdjango

      網頁受權獲取用戶基本信息:47.98.134.86 或 域名json

    2. 關注公衆號(已認證的服務號)api

      iHwTKA.png

    3. 生成二維碼,用戶掃描;
      將用戶信息發送給微信,微信再將數據發送給設置redirect_uri地址(md5值)緩存

      iH09rn.png

    4. 回調地址:47.98.134.86/callback/微信

      iHfnQP.png

      • 受權
      • 用戶md5
      • 獲取wx_id
        在數據庫中更新設置:wx_id
    5. 發送消息(模板消息)session

      • wx_id

      • access_token(2小時有效期)

        iHfZRI.png


 三 核心代碼

import json
import functools
import requests
from django.conf import settings
from django.shortcuts import render, redirect, HttpResponse
from django.http import JsonResponse
from app01 import models
# 沙箱環境地質:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
def index(request):
    obj = models.UserInfo.objects.get(id=1)
    return render(request,'index.html',{'obj':obj})


def auth(func):
    @functools.wraps(func)
    def inner(request, *args, **kwargs):
        user_info = request.session.get('user_info')
        if not user_info:
            return redirect('/login/')
        return func(request, *args, **kwargs)

    return inner


def login(request):
    """
    用戶登陸
    :param request: 
    :return: 
    """
    # models.UserInfo.objects.create(username='luffy',password=123)

    if request.method == "POST":
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        obj = models.UserInfo.objects.filter(username=user, password=pwd).first()
        if obj:
            request.session['user_info'] = {'id': obj.id, 'name': obj.username, 'uid': obj.uid}
            return redirect('/bind/')
    else:
        return render(request, 'login.html')


@auth
def bind(request):
    """
    用戶登陸後,關注公衆號,並綁定我的微信(用於之後消息推送)
    :param request: 
    :return: 
    """
    return render(request, 'bind.html')


@auth
def bind_qcode(request):
    """
    生成二維碼
    :param request: 
    :return: 
    """
    ret = {'code': 1000}
    try:
        access_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={appid}&redirect_uri={redirect_uri}&response_type=code&scope=snsapi_userinfo&state={state}#wechat_redirect"
        access_url = access_url.format(
            appid=settings.WECHAT_CONFIG["app_id"], # 'wx89085e915d351cae',
            redirect_uri=settings.WECHAT_CONFIG["redirect_uri"], # 'http://47.93.4.198/test/',
            state=request.session['user_info']['uid'] # 爲當前用戶生成MD5值
        )
        ret['data'] = access_url
    except Exception as e:
        ret['code'] = 1001
        ret['msg'] = str(e)

    return JsonResponse(ret)


def callback(request):
    """
    用戶在手機微信上掃碼後,微信自動調用該方法。
    用於獲取掃碼用戶的惟一ID,之後用於給他推送消息。
    :param request: 
    :return: 
    """
    code = request.GET.get("code")

    # 用戶md5值
    state = request.GET.get("state")

    # 獲取該用戶openId(用戶惟一,用於給用戶發送消息)
    res = requests.get(
        url="https://api.weixin.qq.com/sns/oauth2/access_token",
        params={
            "appid": 'wx89085e915d351cae',
            "secret": '64f87abfc664f1d4f11d0ac98b24c42d',
            "code": code,
            "grant_type": 'authorization_code',
        }
    ).json()
    # 獲取的到openid表示用戶受權成功
    openid = res.get("openid")
    if openid:
        models.UserInfo.objects.filter(uid=state).update(wx_id=openid)
        response = "<h1>受權成功 %s </h1>" % openid
    else:
        response = "<h1>用戶掃碼以後,手機上的提示</h1>"
    return HttpResponse(response)


def sendmsg(request):
    def get_access_token():
        """
        獲取微信全局接口的憑證(默認有效期倆個小時)
        若是不天天請求次數過多, 經過設置緩存便可
        """
        result = requests.get(
            url="https://api.weixin.qq.com/cgi-bin/token",
            params={
                "grant_type": "client_credential",
                "appid": settings.WECHAT_CONFIG['app_id'],
                "secret": settings.WECHAT_CONFIG['appsecret'],
            }
        ).json()
        if result.get("access_token"):
            access_token = result.get('access_token')
        else:
            access_token = None
        return access_token

    access_token = get_access_token()

    openid = models.UserInfo.objects.get(id=1).wx_id

    def send_custom_msg():
        body = {
            "touser": openid,
            "msgtype": "text",
            "text": {
                "content": '雲姐好美呀'
            }
        }
        response = requests.post(
            url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
            params={
                'access_token': access_token
            },
            data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8')
        )
        # 這裏可根據回執code進行斷定是否發送成功(也能夠根據code根據錯誤信息)
        result = response.json()
        return result

    def send_template_msg():
        """
        發送模版消息
        """
        res = requests.post(
            url="https://api.weixin.qq.com/cgi-bin/message/template/send",
            params={
                'access_token': access_token
            },
            json={
                "touser": openid,
                "template_id": '0XbLbuNkn3wPPAYRVXM-MZ0gU0tPvVbsjfc1qoSH6CM',
                "data": {
                    "first": {
                        "value": "羅毛",
                        "color": "#173177"
                    },
                    "keyword1": {
                        "value": "傻屌",
                        "color": "#173177"
                    },
                }
            }
        )
        result = res.json()
        return result

    result = send_template_msg()

    if result.get('errcode') == 0:
        return HttpResponse('發送成功')
    return HttpResponse('發送失敗')
view.py
 1 {% load staticfiles %}
 2 
 3 <!DOCTYPE html>
 4 <html lang="en">
 5 <head>
 6     <meta charset="UTF-8">
 7     <title>Title</title>
 8 </head>
 9 <body>
10 <div style="width: 600px;margin: 0 auto">
11     <h1>請關注路飛學城服務號,並綁定我的用戶(用於之後的消息提醒)</h1>
12     <div>
13         <h3>第一步:關注路飛學城微信服務號</h3>
14         <img style="height: 100px;width: 100px" src="{% static "img/luffy.jpeg" %}">
15     </div>
16     <input type="button" value="下一步【獲取綁定二維碼】" onclick="getBindUserQcode()">
17     <div>
18         <h3>第二步:綁定我的帳戶</h3>
19         <div id="qrcode" style="width: 250px;height: 250px;background-color: white;margin: 100px auto;"></div>
20     </div>
21 </div>
22 <script src="{% static "js/jquery.min.js" %}"></script>
23 <script src="{% static "js/jquery.qrcode.min.js" %}"></script>
24 <script src="{% static "js/qrcode.js" %}"></script>
25 <script>
26     function getBindUserQcode() {
27         $.ajax({
28             url: '/bind_qcode/',
29             type: 'GET',
30             success: function (result) {
31                 console.log(result);
32                 $('#qrcode').empty().qrcode({text: result.data});
33             }
34         });
35     }
36 </script>
37 
38 </body>
39 </html>
bind.html
class UserInfo(models.Model):
    username = models.CharField("用戶名", max_length=64, unique=True)
    password = models.CharField("密碼", max_length=64)
    uid = models.CharField(verbose_name='我的惟一ID',max_length=64, unique=True)
    wx_id = models.CharField(verbose_name="微信ID", max_length=128, blank=True, null=True, db_index=True)

    def save(self, *args, **kwargs):
        # 建立用戶時,爲用戶自動生成我的惟一ID
        if not self.pk:
            m = hashlib.md5()
            m.update(self.username.encode(encoding="utf-8"))
            self.uid = m.hexdigest()
        super(UserInfo, self).save(*args, **kwargs)
model.py
相關文章
相關標籤/搜索