2014年(9)
分类: Erlang
2014-09-11 16:03:42
-module(wmapi_sys_cp, [Id, Type, Cid, Account, Password, LoginTimes, LoginLast, Note]).
-compile(export_all).
-module(wmadmin_admin_controller, [Req, SessionId]).
-compile(export_all).
index('GET', []) ->
case app_cp:login_is(SessionId) of
true ->
ok;
false ->
{redirect, [{action, "login"}]}
end.
login('GET', []) ->
ok;
login('POST', []) ->
UserName = Req:post_param("username"),
PassWord = Req:post_param("password"),
io:format("UserName: ~p~n", [UserName]),
io:format("PassWord: ~p~n", [PassWord]),
case boss_db:find(wmapi_sys_cp, [{account, UserName}, {password, PassWord}]) of
[{wmapi_sys_cp, _Id, _Type, Cid, _Account, _Password, _LoginTimes, _LoginLast, _Note}|_] ->
io:format("Cid: ~p~n", [Cid]),
Hash = app_cp:login_key_rule(integer_to_list(Cid),UserName,PassWord),
boss_session:set_session_data(SessionId, "session_cid", integer_to_list(Cid)),
boss_session:set_session_data(SessionId, "session_account", UserName),
boss_session:set_session_data(SessionId, "session_password", PassWord),
boss_session:set_session_data(SessionId, "session_hash", Hash),
{redirect, [{action, "index"}]};
_Ca ->
io:format("_Ca: ~p~n", [_Ca]),
{redirect, [{action, "login"}]}
end.
这里的app_cp模块我是在src目录下新建一个文件夹下面的,要手动编译和加载,内容如下:
-module(app_cp).
-compile(export_all).
login_is(SessionId) ->
Cid = boss_session:get_session_data(SessionId, "session_cid"),
Account = boss_session:get_session_data(SessionId, "session_account"),
Passwd = boss_session:get_session_data(SessionId, "session_password"),
SHash = boss_session:get_session_data(SessionId, "session_hash"),
Hash = login_key_rule(Cid, Account, Passwd),
SHash =:= Hash.
login_key_rule(Cid, Account, Passwd) when Cid =:= undefined; Account =:= undefined; Passwd =:= undefined ->
"";
login_key_rule(Cid, Account, Passwd) ->
io:format("Cid: ~p~n", [Cid]),
io:format("Account: ~p~n", [Account]),
io:format("Passwd: ~p~n", [Passwd]),
crypto:sha(Cid ++ Account ++ Passwd).
下面贴出html代码
src/view/base.html:
{% extends "base.html" %}
{% block title %}
WMAdmin Login
{% endblock %}
{% block body %}
{% extends "base.html" %}
{% block title %}
WMAdmin Index
{% endblock %}
{% block body %}
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
{% endblock %}
完成之后可以在浏览器测试,session生存时间可以在boss.config文件配置,{session_exp_time, 10},为了测试我改成10秒了。
OK了!!!