微信服務號推送消息接口開發

1.登陸微信公衆開發平臺

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

拿到這個之後,下面會有一個二維碼生成,這二維碼就是沙箱環境的測試公招號!html

2.基於Django開發接口

首先把用到的URL所有貼出。jquery

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^index/$', views.index),

    url(r'^login/$', views.login),
    url(r'^bind/$', views.bind),
    url(r'^bind_qcode/$', views.bind_qcode),
    url(r'^callback/$', views.callback),
    url(r'^sendmsg/$', views.sendmsg),
]

在項目中須要用戶登陸,讓他關注咱們網站的服務號,而後給他推送消息等。git

 

準備工做:github

1.登陸的裝飾器web

def auth(func):
    '''
    登陸認證的裝飾器函數【其實能夠寫成一箇中間件】
    :param func:
    :return:
    '''
    @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
View Code
2.有一個models表
import hashlib
from django.db import models

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)

3.開始視圖部分:ajax

用戶登陸進來,註冊一個session,而後跳到了綁定頁面。django

def login(request):
    """
    用戶登陸
    :param request: 
    :return: 
    """
    # models.UserInfo.objects.create(username='jesi',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:
            # 登陸成功,將這三個值放到session中
            request.session['user_info'] = {'id': obj.id, 'name': obj.username, 'uid': obj.uid}
            return redirect('/bind/')
    else:
        return render(request, 'login.html')

4.bind給他返回一個頁面json

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

5.這個頁面把上面的公招號圖片放進來。api

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div style="width: 600px;margin: 0 auto">
    <h1>請關注郭成測試服務號,並綁定我的用戶(用於之後的消息提醒)</h1>
    <div>
        <h3>第一步:關注郭成微信服務號</h3>
        <img style="height: 100px;width: 100px" src="{% static "img/jesi.jpg" %}">
    </div>
    <input type="button" value="下一步【獲取綁定二維碼】" onclick="getBindUserQcode()">
    <div>
        <h3>第二步:綁定我的帳戶</h3>
        <div id="qrcode" style="width: 250px;height: 250px;background-color: white;margin: 100px auto;"></div>
    </div>
</div>
<script src="{% static "js/jquery.min.js" %}"></script>
<script src="{% static "js/jquery.qrcode.min.js" %}"></script>
<script src="{% static "js/qrcode.js" %}"></script>
<script>
    function getBindUserQcode() {
        $.ajax({
            url: '/bind_qcode/',
            type: 'GET',
            success: function (result) {
                console.log(result);
                $('#qrcode').empty().qrcode({text: result.data});
            }
        });
    }
</script>

</body>
</html>
View Code

 

6.這裏面掃碼關注後,點擊繼續掃碼,點擊事件發送一個請求數據,將這個數據作成一個URL自動生成一個二維碼。緩存

@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"], # 'wx961b82840037d073',
            redirect_uri=settings.WECHAT_CONFIG["redirect_uri"], # 'http://47.99.191.149/callback/',
            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)

7.這個JS事件將前面的url生成一個二維碼。

8.用戶掃完下面的二維碼之後,訪問這個URL,而後
 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"

掃完這個二維碼之後,綁定用戶,微信會自動訪問你的一個callback的URL,在callback的函數中將wx_id填入到登陸用戶中,以便之後推送消息。

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": 'wx961b82840037d073',
            "secret": 'c3f919fab6cd528a6ed727c911e1c599',
            "code": code,
            "grant_type": 'authorization_code',
        }
    ).json()
    # 獲取的到openid表示用戶受權成功
    openid = res.get("openid")
    if openid:
        #將這個用戶的wx_id和他的用戶這條數據進行一個綁定
        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()
    print(access_token)

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

    def send_custom_msg():
        body = {
            "touser": openid,
            "msgtype": "text",
            "text": {
                "content": '歡迎Python鬼才!'
            }
        }
        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": 'w7OBa8MhE6sNxgNSnooTqB93kgvMbA8JEhwN85vjUdY',
                "data": {
                    "first": {
                        "value": "楊康",
                        "color": "#173177"
                    },
                    "keyword": {
                        "value": "Python鬼才",
                        "color": "#173177"
                    },
                }
            }
        )
        result = res.json()
        return result

    result = send_template_msg()

    if result.get('errcode') == 0:
        return HttpResponse('發送成功')
    return HttpResponse('發送失敗')

這裏能夠直接發送消息,能夠發送一個模板,

收到的模板樣子是:

 

注意點:

這裏須要放你本身的IP公網地址,這樣子才能讓微信發起回調函數的執行!

 

 GitHub地址:

https://github.com/JesiDream/webcharAccounts

相關文章
相關標籤/搜索