From 28ff371de59972871e720f90bd147c706078f53e Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 23 Sep 2024 11:28:57 +0800 Subject: [PATCH] fix http handler --- .../njau_bot/src/http_handler/api_handler.erl | 75 +++++++++++++------ apps/njau_bot/src/njau_bot_app.erl | 2 +- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/apps/njau_bot/src/http_handler/api_handler.erl b/apps/njau_bot/src/http_handler/api_handler.erl index 37efc47..5f3d249 100644 --- a/apps/njau_bot/src/http_handler/api_handler.erl +++ b/apps/njau_bot/src/http_handler/api_handler.erl @@ -15,16 +15,14 @@ init(Req0, Opts) -> Method = binary_to_list(cowboy_req:method(Req0)), Path = binary_to_list(cowboy_req:path(Req0)), - GetParams0 = cowboy_req:parse_qs(Req0), - GetParams = maps:from_list(GetParams0), {ok, ReqBody, Req1} = parse_body(Req0), Sign = cowboy_req:header(<<"sign">>, Req1, <<>>), case njau_bot_signer:sign(ReqBody) =:= Sign of true -> - {ok, StatusCode, Resp} = handle_request(Method, Path, GetParams, ReqBody), - lager:debug("[http_protocol] request path: ~p, get_params: ~p, post_params: ~p, response: ~ts", - [Path, GetParams, ReqBody, Resp]), + {ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody), + lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts", + [Path, ReqBody, Resp]), Req2 = cowboy_req:reply(StatusCode, #{ <<"Content-Type">> => <<"application/json">> }, Resp, Req1), @@ -37,14 +35,54 @@ init(Req0, Opts) -> {ok, Req2, Opts} end. -handle_request("POST", "/api/device_info", _, Params) -> - njau_bot_logger:write(jiffy:encode(Params, [force_utf8])), +%% 场地信息 +handle_request("POST", "/storeInfo", ReqBody) when is_binary(ReqBody) -> + #{<<"storeInfos">> := StoreInfos} = jiffy:decode(ReqBody, [return_maps]), + lager:debug("[api_handler] get storeInfo: ~p", [StoreInfos]), - {ok, 200, json_data(<<"ok">>)}; + njau_bot_logger:write(ReqBody), -handle_request(_, Path, _, _) -> + {ok, 200, json_reply(true, <<"接收成功"/utf8>>)}; + +%% 设备信息 +handle_request("POST", "/equip", ReqBody) -> + #{<<"equips">> := Equips} = jiffy:decode(ReqBody, [return_maps]), + lager:debug("[api_handler] get equips: ~p", [Equips]), + + njau_bot_logger:write(ReqBody), + + {ok, 200, json_reply(true, <<"接收成功"/utf8>>)}; + +%% 设备离线,在线状态 +handle_request("POST", "/online", ReqBody) -> + #{<<"equipStatus">> := Equips} = jiffy:decode(ReqBody, [return_maps]), + lager:debug("[api_handler] get online: ~p", [Equips]), + + njau_bot_logger:write(ReqBody), + + {ok, 200, json_reply(true, <<"接收成功"/utf8>>)}; + +%% 设备运行状态 +handle_request("POST", "/runStatus", ReqBody) -> + #{<<"equipRunStatus">> := EquipRunStatus} = jiffy:decode(ReqBody, [return_maps]), + lager:debug("[api_handler] get equipRunStatus: ~p", [EquipRunStatus]), + + njau_bot_logger:write(ReqBody), + + {ok, 200, json_reply(true, <<"接收成功"/utf8>>)}; + +%% 订单信息推送 +handle_request("POST", "/order", ReqBody) -> + %#{<<"equipRunStatus">> := EquipRunStatus} = jiffy:decode(ReqBody, [return_maps]), + lager:debug("[api_handler] get order: ~p", [ReqBody]), + + njau_bot_logger:write(ReqBody), + + {ok, 200, json_reply(true, <<"接收成功"/utf8>>)}; + +handle_request(_, Path, _) -> Path1 = list_to_binary(Path), - {ok, 200, json_error(-1, <<"url: ", Path1/binary, " not found">>)}. + {ok, 200, json_reply(false, <<"url: ", Path1/binary, " not found">>)}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% helper methods @@ -71,14 +109,9 @@ read_body(Req, AccData) -> read_body(Req1, <>) end. -json_data(Data) -> - Json = jiffy:encode(#{<<"result">> => Data}, [force_utf8]), - iolist_to_binary(Json). - -json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) -> - jiffy:encode(#{ - <<"error">> => #{ - <<"code">> => ErrCode, - <<"message">> => ErrMessage - } - }, [force_utf8]). \ No newline at end of file +json_reply(Result, Message) when is_boolean(Result), is_binary(Message) -> + Json = jiffy:encode(#{ + <<"result">> => Result, + <<"message">> => Message + }, [force_utf8]), + iolist_to_binary(Json). \ No newline at end of file diff --git a/apps/njau_bot/src/njau_bot_app.erl b/apps/njau_bot/src/njau_bot_app.erl index fc73f3b..4e462fe 100644 --- a/apps/njau_bot/src/njau_bot_app.erl +++ b/apps/njau_bot/src/njau_bot_app.erl @@ -35,7 +35,7 @@ start_http_server() -> Dispatcher = cowboy_router:compile([ {'_', [ - {"/api/[...]", api_handler, []} + {"/[...]", api_handler, []} ]} ]),