Skip to content

Commit

Permalink
error page, add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
z-kidy committed Apr 15, 2016
1 parent a90597c commit bd6197a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
安装virtualenv:

sudo pip install virtualenv

创建虚拟环境:

virtualenv wechat_env

# 激活虚拟环境

source wechat_env/bin/activate

# 安装依赖包

pip install -r requirements.txt

# 直接运行

python manage.py runserver

# 生产环境下supervisor + gunicorn + ngix

参考
[http://beiyuu.com/vps-config-python-vitrualenv-flask-gunicorn-supervisor-nginx/](http://beiyuu.com/vps-config-python-vitrualenv-flask-gunicorn-supervisor-nginx/)

+ 配置appId, appsecret, Token
+ 按微信官方教程配置服务器地址
+ 配置Debug
+ 配置logger
+ 配置STATICFILES_DIRS, TEMPLATES
+ 此处没有涉及数据保存,生产环境下要配置好MySql,Redis等
27 changes: 6 additions & 21 deletions authorization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from rest_framework.decorators import api_view
from rest_framework.response import Response

from wechat_auth.settings import appID, appsecret, Token

Expand Down Expand Up @@ -46,8 +45,8 @@ def index(request):
toUserName = data.get('ToUserName', '')
fromUserName = data.get('FromUserName', '')
msgType = data.get('MsgType', '')
content = data.get('Content', '')

# content = data.get('Content', '')
content = 'http://' + request.get_host() + reverse('web_auth') # 作为测试,用户发送任何内容,都返回请求授权的地址
return render(request, 'reply.xml',
{'toUserName': fromUserName,
'fromUserName': toUserName,
Expand All @@ -57,7 +56,6 @@ def index(request):
},
)


def web_auth(request):
"""
网页提示是否授权
Expand All @@ -81,7 +79,6 @@ def web_auth(request):
else:
return redirect(reverse('web_auth'))

@api_view(['GET'])
def get_code(request):
"""
用户授予权限,获取code,通过code获取access_toekn, 再通过access_token获取用户信息
Expand All @@ -101,21 +98,15 @@ def get_code(request):
)
except Exception, reson:
logger.exception(u'调用微信api失败[%s]!', reson)
return Response({
'result': 0,
'error': u'调用微信api失败'
})
return render(request, 'error.html')

response_dic = json.loads(response.read()) # 解析json
errcode = response_dic.get('errcode', None) # 调用失败
if errcode:
# raise 错误原因
logger.exception(
u'调用微信api失败[%d:%s]!', errcode, response_dic.get('errmsg', ''))
return Response({
'result': 0,
'error': u'调用微信api失败'
})
return render(request, 'error.html')

access_token = response_dic.get('access_token', '')
openid = response_dic.get('openid', '')
Expand All @@ -133,21 +124,15 @@ def get_code(request):
)
except Exception, reson:
logger.exception(u'调用微信api失败[%s]!', reson)
return Response({
'result': 0,
'error': u'调用微信api失败'
})
return render(request, 'error.html')

info_dic = json.loads(info_response.read()) # 解析json
errcode = info_dic.get('errcode', None) # 调用失败
if errcode:
# raise 错误原因
logger.exception(
u'调用微信api失败[%d:%s]!', errcode, response_dic.get('errmsg', ''))
return Response({
'result': 0,
'error': u'调用微信api失败'
})
return render(request, 'error.html')

nickname = info_dic.get('nickname', '') # 昵称
sex = info_dic.get('sex', 0) # 性别
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
defusedxml==0.4.1
Django==1.9.5
django-debug-toolbar==1.4
djangorestframework==3.3.3
djangorestframework-xml==1.3.0
gunicorn==19.4.5
sqlparse==0.1.19
wheel==0.24.0

16 changes: 16 additions & 0 deletions templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

{% extends 'base.html' %}

{% block title %}
哦哦,发生了错误
{% endblock title %}
{% block main %}
<div class="weui_msg">
<div class="weui_icon_area"><i class="weui_icon_msg weui_icon_warn"></i></div>
<div class="weui_text_area">
<h2 class="weui_msg_title">微信服务发生了错误</h2>
<p class="weui_msg_desc">请再次尝试或联系管理员</p>
</div>
</div>

{% endblock main %}

0 comments on commit bd6197a

Please sign in to comment.