From d690b478c3e5189e55478aeeb86ad01a212861cd Mon Sep 17 00:00:00 2001 From: Kitakami Ooi Date: Tue, 1 Dec 2015 15:16:13 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E7=9A=84?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E3=80=81=E8=BF=87=E7=A8=8B=E5=92=8C=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handlers/frontend.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ ooi.py | 1 + templates/form.html | 5 +++++ 3 files changed, 57 insertions(+) diff --git a/handlers/frontend.py b/handlers/frontend.py index ad7a34a..307927c 100644 --- a/handlers/frontend.py +++ b/handlers/frontend.py @@ -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'] @@ -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 diff --git a/ooi.py b/ooi.py index ae51420..4119f26 100644 --- a/ooi.py +++ b/ooi.py @@ -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')) diff --git a/templates/form.html b/templates/form.html index 1182a22..9708927 100644 --- a/templates/form.html +++ b/templates/form.html @@ -2,6 +2,11 @@ {% block content -%}
+ {% if errmsg -%} +
+

{{ errmsg }}

+
+ {%- endif %}