共计 4176 个字符,预计需要花费 11 分钟才能阅读完成。
一、生成二维码
为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送
目前有 2 种类型的二维码:
-
临时二维码
有过期时间的,最长可以设置为在二维码生成后的 30 天(即 2592000 秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
-
永久二维码
无过期时间的,但数量较少(目前为最多 10 万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景
生成二维码的步骤:
- 获取 access_token
- 创建二维码 ticket
- 凭借 ticket 到指定 URL 换取二维码
创建二维码 ticket:
原理:对微信服务器发起 POST 请求,得到 ticket 值
请求 URL:https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
请求方式:POST
POST 请求体参数 | 说明 |
---|---|
expire_seconds | 该二维码有效时间,以秒为单位。最大不超过 2592000(即 30 天),此字段如果不填,则默认有效期为 30 秒 |
action_name | 二维码类型,QR_SCENE 为临时的整型参数值,QR_STR_SCENE 为临时的字符串参数值,QR_LIMIT_SCENE 为永久的整型参数值,QR_LIMIT_STR_SCENE 为永久的字符串参数值 |
action_info | 二维码详细信息 |
scene_id | 场景值 ID,临时二维码时为 32 位非 0 整型,永久二维码时最大值为 100000(目前参数只支持 1–100000) |
scene_str | 场景值 ID(字符串形式的 ID),字符串类型,长度限制为 1 到 64 |
正确的返回值说明
{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm
3sUw==","expire_seconds":60,"url":"http://weixin.qq.com/q/kZgfwMTm72WWPkovabbI"}
myApp/urls.py
from django.urls import path, re_path
from myApp import views
urlpatterns = [path(r'index/', views.index),
path(r'weixin/', views.weixin),
path(r'access/', views.access),
path(r'menu/', views.menu),
path(r'dlmenu/', views.menu),
path(r'empower/', views.empower),
path(r'auth/', views.auth),
re_path(r'qrcode/(\d+)/$', views.qrcode),
]
myApp/views.py
from django.shortcuts import render, HttpResponse, redirect
from django.views.decorators.csrf import csrf_exempt
import hashlib
import xmltodict
import time
import json
import requests
from myApp.accessToken import AccessToken
def index(request):
pass
def responseXML(ToUserName, FromUserName, MsgType, **kwargs):
pass
@csrf_exempt
def weixin(request):
pass
def access(request):
pass
def menu(request):
pass
def empower(request):
pass
def auth(request):
pass
# 获取二维码
def qrcode(request, info):
# 获取 access_token
access_token = AccessToken.getAccessToken("client_credential", "wxffde55b11cc79754", "84e86527f090d6238ea1c0b96f5fc753")
# 获取 ticket
url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s" % (access_token)
data = {"expire_seconds": 604800,
"action_name": "QR_SCENE",
"action_info": {"scene": {"scene_id": int(info)
}
}
}
data = json.dumps(data)
res = requests.post(url, data, "json")
resDict = json.loads(res.content)
if resDict.get("ticket"):
ticket = resDict.get("ticket")
# 通过 ticket 换取二维码
qrcodeurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s" % (ticket)
return HttpResponse("<img src='https://www.zutuanxue.com/home/8/%s'/>"%(qrcodeurl))
浏览器测试:
二、扫描二维码
用户扫描带场景值二维码时,可能推送以下两种事件:
- 如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者
- 如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
-
用户还未关注公众号
xml 数据包
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[subscribe]]></Event> <EventKey><![CDATA[qrscene_sunck]]></EventKey> <Ticket><![CDATA[TICKET]]></Ticket> </xml>
-
用户已经关注公众号
xml 数据包
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[SCENE_VALUE]]></EventKey> <Ticket><![CDATA[TICKET]]></Ticket> </xml>
from django.shortcuts import render, HttpResponse, redirect
from django.views.decorators.csrf import csrf_exempt
import hashlib
import xmltodict
import time
import json
import requests
from myApp.accessToken import AccessToken
def index(request):
pass
def responseXML(ToUserName, FromUserName, MsgType, **kwargs):
pass
@csrf_exempt
def weixin(request):
if request.method == "GET":
pass
else:
pass
if MsgType == "text":
pass
elif MsgType == "event":
pass
if Event == "subscribe":
# 验证 FromUserName 是否关注过,没关注过则认为是新客户,否则就是刷关注
print("--------------------FromUserName:", FromUserName)
# 业务逻辑
resXml = responseXML(FromUserName, ToUserName, "text",
Content="欢迎光临红浪漫,三楼男宾一位(EventKey:%s)" % (EventKey))
return HttpResponse(resXml)
elif Event == "SCAN":
# 业务逻辑
resXml = responseXML(FromUserName, ToUserName, "text",
Content="列队欢迎尊敬的红浪漫会员贵宾上三楼(EventKey:%s)" % (EventKey))
return HttpResponse(resXml)
elif Event == "unsubscribe":
# 业务逻辑
print("--------------------FromUserName:", FromUserName)
return HttpResponse("")
elif Event == "CLICK":
pass
elif Event == "VIEW":
pass
elif MsgType == "image":
pass
elif MsgType == "voice":
pass
else:
pass
def access(request):
pass
def menu(request):
pass
def empower(request):
pass
def auth(request):
pass
def qrcode(request, info):
pass
扫描带参二维码:
扫描关注二维码:
正文完
星哥说事-微信公众号