Skip to content

Commit

Permalink
用户登录的界面、过程和错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
kitakamiooi committed Dec 1, 2015
1 parent 5e4bf1b commit d690b47
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
51 changes: 51 additions & 0 deletions handlers/frontend.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"""OOI3的前端部分,用于显示各种页面。
包含了登录表单、登录后的跳转、不同的游戏运行模式和注销页面。
"""

import aiohttp_jinja2
from aiohttp.web import HTTPFound
from aiohttp_session import get_session

from auth.kancolle import KancolleAuth, OOIAuthException


class FrontEndHandler:
"""OOI3前端请求处理类。"""

@aiohttp_jinja2.template('form.html')
async def form(self, request):
"""展示登录表单。
:param request: web.Request
:return: dict
"""
session = await get_session(request)
if 'mode' in session:
mode = session['mode']
Expand All @@ -15,6 +28,44 @@ async def form(self, request):

return {'mode': mode}

async def login(self, request):
"""接受登录表单提交的数据,登录后跳转或登录失败后展示错误信息。
:param request: web.Request
:return: web.HTTPFound or web.Response
"""
post = await request.post()
session = await get_session(request)

login_id = post.get('login_id', None)
password = post.get('password', None)
mode = int(post.get('mode', 1))

session['mode'] = mode

if login_id and password:

kancolle = KancolleAuth(login_id, password)
try:
await kancolle.get_flash()
session['api_token'] = kancolle.api_token
session['api_starttime'] = kancolle.api_starttime
session['world_ip'] = kancolle.world_ip
if mode == 2:
return HTTPFound('/kcv')
elif mode == 3:
return HTTPFound('/poi')
else:
return HTTPFound('/kancolle')

except OOIAuthException as e:
context = {'errmsg': e.message, 'mode': mode}
return aiohttp_jinja2.render_template('form.html', request, context)
else:

context = {'errmsg': '请输入完整的登录ID和密码', 'mode': mode}
return aiohttp_jinja2.render_template('form.html', request, context)

def normal(self):
pass

Expand Down
1 change: 1 addition & 0 deletions ooi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def main():

# 给应用添加路由
app.router.add_route('GET', '/', frontend.form)
app.router.add_route('POST', '/', frontend.login)
app.router.add_route('POST', '/service/osapi', service.get_osapi)
app.router.add_route('POST', '/service/flash', service.get_flash)
app.router.add_static('/static', os.path.join(base_dir, 'static'))
Expand Down
5 changes: 5 additions & 0 deletions templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

{% block content -%}
<div id="ooi-form" class="uk-width-medium-2-5">
{% if errmsg -%}
<div class="uk-panel uk-panel-box uk-panel-box-primary uk-margin-bottom">
<p class="uk-text-danger">{{ errmsg }}</p>
</div>
{%- endif %}
<form method="post" class="uk-form uk-form-stacked">
<div class="uk-form-row">
<label class="uk-form-label" for="login_id">DMM登录ID:</label>
Expand Down

0 comments on commit d690b47

Please sign in to comment.