一、生成二维码
为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送
目前有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 |
正确的返回值说明
代码语言:javascript复制{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm
3sUw==","expire_seconds":60,"url":"http://weixin.qq.com/q/kZgfwMTm72WWPkovabbI"}
myApp/urls.py
代码语言:javascript复制<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path, re_path
<span class="hljs-keyword">from</span> myApp <span class="hljs-keyword">import</span> views
urlpatterns = [
path(<span class="hljs-string">r'index/'</span>, views.index),
path(<span class="hljs-string">r'weixin/'</span>, views.weixin),
path(<span class="hljs-string">r'access/'</span>, views.access),
path(<span class="hljs-string">r'menu/'</span>, views.menu),
path(<span class="hljs-string">r'dlmenu/'</span>, views.menu),
path(<span class="hljs-string">r'empower/'</span>, views.empower),
path(<span class="hljs-string">r'auth/'</span>, views.auth),
re_path(<span class="hljs-string">r'qrcode/(d )/$'</span>, views.qrcode),
]
myApp/views.py
代码语言:javascript复制<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, HttpResponse, redirect
<span class="hljs-keyword">from</span> django.views.decorators.csrf <span class="hljs-keyword">import</span> csrf_exempt
<span class="hljs-keyword">import</span> hashlib
<span class="hljs-keyword">import</span> xmltodict
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">from</span> myApp.accessToken <span class="hljs-keyword">import</span> AccessToken
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">responseXML</span><span class="hljs-params">(ToUserName, FromUserName, MsgType, **kwargs)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-meta">@csrf_exempt</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">weixin</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">access</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">menu</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">empower</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">auth</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-comment"># 获取二维码</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">qrcode</span><span class="hljs-params">(request, info)</span>:</span>
<span class="hljs-comment"># 获取access_token</span>
access_token = AccessToken.getAccessToken(<span class="hljs-string">"client_credential"</span>, <span class="hljs-string">"wxffde55b11cc79754"</span>, <span class="hljs-string">"84e86527f090d6238ea1c0b96f5fc753"</span>)
<span class="hljs-comment"># 获取ticket</span>
url = <span class="hljs-string">"https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s"</span> % (access_token)
data = {
<span class="hljs-string">"expire_seconds"</span>: <span class="hljs-number">604800</span>,
<span class="hljs-string">"action_name"</span>: <span class="hljs-string">"QR_SCENE"</span>,
<span class="hljs-string">"action_info"</span>: {
<span class="hljs-string">"scene"</span>: {
<span class="hljs-string">"scene_id"</span>: int(info)
}
}
}
data = json.dumps(data)
res = requests.post(url, data, <span class="hljs-string">"json"</span>)
resDict = json.loads(res.content)
<span class="hljs-keyword">if</span> resDict.get(<span class="hljs-string">"ticket"</span>):
ticket = resDict.get(<span class="hljs-string">"ticket"</span>)
<span class="hljs-comment"># 通过ticket换取二维码</span>
qrcodeurl = <span class="hljs-string">"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"</span> % (ticket)
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"<img src='https://www.zutuanxue.com/home/8/%s' />"</span>%(qrcodeurl))
浏览器测试:
二、扫描二维码
用户扫描带场景值二维码时,可能推送以下两种事件:
- 如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者
- 如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
用户还未关注公众号
xml数据包
代码语言:javascript复制<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数据包
代码语言:javascript复制<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>
代码语言:javascript复制<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, HttpResponse, redirect
<span class="hljs-keyword">from</span> django.views.decorators.csrf <span class="hljs-keyword">import</span> csrf_exempt
<span class="hljs-keyword">import</span> hashlib
<span class="hljs-keyword">import</span> xmltodict
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">from</span> myApp.accessToken <span class="hljs-keyword">import</span> AccessToken
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">responseXML</span><span class="hljs-params">(ToUserName, FromUserName, MsgType, **kwargs)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-meta">@csrf_exempt</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">weixin</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">"GET"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">if</span> MsgType == <span class="hljs-string">"text"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"event"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">if</span> Event == <span class="hljs-string">"subscribe"</span>:
<span class="hljs-comment"># 验证FromUserName是否关注过,没关注过则认为是新客户,否则就是刷关注</span>
print(<span class="hljs-string">"--------------------FromUserName:"</span>, FromUserName)
<span class="hljs-comment"># 业务逻辑</span>
resXml = responseXML(FromUserName, ToUserName, <span class="hljs-string">"text"</span>,
Content=<span class="hljs-string">"欢迎光临红浪漫,三楼男宾一位(EventKey:%s)"</span> % (EventKey))
<span class="hljs-keyword">return</span> HttpResponse(resXml)
<span class="hljs-keyword">elif</span> Event == <span class="hljs-string">"SCAN"</span>:
<span class="hljs-comment"># 业务逻辑</span>
resXml = responseXML(FromUserName, ToUserName, <span class="hljs-string">"text"</span>,
Content=<span class="hljs-string">"列队欢迎尊敬的红浪漫会员贵宾上三楼(EventKey:%s)"</span> % (EventKey))
<span class="hljs-keyword">return</span> HttpResponse(resXml)
<span class="hljs-keyword">elif</span> Event == <span class="hljs-string">"unsubscribe"</span>:
<span class="hljs-comment"># 业务逻辑</span>
print(<span class="hljs-string">"--------------------FromUserName:"</span>, FromUserName)
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">""</span>)
<span class="hljs-keyword">elif</span> Event == <span class="hljs-string">"CLICK"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> Event == <span class="hljs-string">"VIEW"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"image"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"voice"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">access</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">menu</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">empower</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">auth</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">qrcode</span><span class="hljs-params">(request, info)</span>:</span>
<span class="hljs-keyword">pass</span>
扫描带参二维码:
扫描关注二维码: