def weixin(request):
try: # 微信接口认证 使用GET方式
if request.method == 'GET':
token = 'air2you'
tmpArr =[token, request.GET['timestamp'], request.GET['nonce']]
tmpArr.sort()
tmpArr.sort()
tmpStr = ''.join(tmpArr)
code = hashlib.sha1(tmpStr).hexdigest()
if code == request.GET['signature']:
return render_to_response('air/weixin.html',{'echostr':request.GET['echostr']})
else:
return render_to_response('air/weixin.html',{'echostr':''})
# 微信接口通讯 返回用户需要数据
elif request.method == 'POST':
xml = et.fromstring(request.raw_post_data)
_to = xml.find('FromUserName').text
_from = xml.find('ToUserName').text
_content = 'welcome!'
_type = 'text'
return render_to_response('air/weixin.xml',{'_to':_to, '_from': _from, '_time' : int(time.time()), '_type': _type, '_content' : _content}, mimetype='application/xml')
except Exception, e:
return render_to_response('air/weixin.html',{'echostr':''})
#weixin.xml<xml><ToUserName><![CDATA[{{ _to }}]]></ToUserName><FromUserName><![CDATA[{{ _from }}]]></FromUserName><CreateTime>{{ _time }}</CreateTime><MsgType><![CDATA[{{ _type }}]]></MsgType><Content><![CDATA[{{ _content }}]]></Content><FuncFlag>0</FuncFlag></xml>