Skip to content

Commit

Permalink
WIP:
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantLiu committed Sep 23, 2021
1 parent 28a8676 commit 13c42f0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 77 deletions.
62 changes: 0 additions & 62 deletions CHANGELOG.md

This file was deleted.

24 changes: 12 additions & 12 deletions apps/dgiot_http/src/wechat/dgiot_wechat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@
%% https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
%% wechat绑定
post_sns(UserName, Password, OpenId) ->
case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OpenId}}) of
{ok, #{<<"results">> := [#{<<"objectId">> := _UserId, <<"username">> := Name} | _]}} ->
%% case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OpenId}}) of
%% {ok, #{<<"results">> := [#{<<"objectId">> := _UserId, <<"username">> := Name} | _]}} ->
%% {ok, UserInfo} = dgiot_parse_handler:create_session(UserId, dgiot_auth:ttl(), Name),
{error, <<OpenId/binary, " is bind ", Name/binary>>};
_ ->
case dgiot_parse:login(UserName, Password) of
%% {error, <<OpenId/binary, " is bind ", Name/binary>>};
%% _ ->
case dgiot_parse:login(UserName, Password) of
%% {ok, #{<<"objectId">> := _UserId, <<"tag">> := #{<<"wechat">> := #{<<"openid">> := OPENID}}}} when size(OPENID) > 0 ->
%% {error, <<UserName/binary, "is bind">>};
{ok, #{<<"objectId">> := UserId, <<"tag">> := Tag, <<"username">> := Name}} ->
dgiot_parse:update_object(<<"_User">>, UserId, #{<<"tag">> => Tag#{<<"wechat">> => #{<<"openid">> => OpenId}}}),
{ok, UserInfo} = dgiot_parse_handler:create_session(UserId, dgiot_auth:ttl(), Name),
{ok, UserInfo};
{error, Msg} ->
{error, Msg}
end
{ok, #{<<"objectId">> := UserId, <<"tag">> := Tag, <<"username">> := Name}} ->
dgiot_parse:update_object(<<"_User">>, UserId, #{<<"tag">> => Tag#{<<"wechat">> => #{<<"openid">> => OpenId}}}),
{ok, UserInfo} = dgiot_parse_handler:create_session(UserId, dgiot_auth:ttl(), Name),
{ok, UserInfo};
{error, Msg} ->
{error, Msg}
end.
%% end.


%% wechat解绑
Expand Down
66 changes: 66 additions & 0 deletions apps/dgiot_parse/src/dgiot_parse_rest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ request(Method, Header, Path0, Body, Options) ->
end,
do_request(Method, NewPath, NewHeads, Query, Options);
_ when Method == 'POST'; Method == 'PUT' ->
case to_binary(Path) of
<<"/classes/Product">> when Method == 'POST' ->
case Body of
#{<<"objectId">> := ProductId, <<"channel">> := Channel} ->
TdchannelId = maps:get(<<"tdchannel">>, Channel, <<"">>),
TaskchannelId = maps:get(<<"taskchannel">>, Channel, <<"">>),
Otherchannel = maps:get(<<"otherchannel">>, Channel, []),
channel_add_prduct_relation(Otherchannel ++ [TdchannelId] ++ [TaskchannelId], ProductId);
_ ->
pass
end;
<<"/classes/Product/", ProductId/binary>> when Method == 'PUT' ->
case Body of
#{<<"channel">> := Channel} ->
channel_delete_prduct_relation(ProductId),
TdchannelId = maps:get(<<"tdchannel">>, Channel, <<"">>),
TaskchannelId = maps:get(<<"taskchannel">>, Channel, <<"">>),
Otherchannel = maps:get(<<"otherchannel">>, Channel, []),
channel_add_prduct_relation(Otherchannel ++ [TdchannelId] ++ [TaskchannelId], ProductId);
_ ->
pass
end;
_ ->
pass
end,
do_request(Method, to_binary(Path), NewHeads, NewBody1, Options)
end
end,
Expand Down Expand Up @@ -448,3 +473,44 @@ log(Method, {Url, Header}) ->
log(Method, {Url, Header, _, Body}) ->
IsLog = application:get_env(dgiot_parse, log, false),
IsLog andalso ?LOG(info, "~s ~s Header:~p Body:~p", [method(Method), Url, Header, Body]).



channel_add_prduct_relation(ChannelIds, ProductId) ->
Map =
#{<<"product">> =>
#{
<<"__op">> => <<"AddRelation">>,
<<"objects">> => [
#{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"Product">>,
<<"objectId">> => ProductId
}
]
}
},
lists:map(fun(ChannelId) when size(ChannelId) > 0 ->
dgiot_parse:update_object(<<"Channel">>, ChannelId, Map)
end, ChannelIds).

channel_delete_prduct_relation(ProductId) ->
Map =
#{<<"product">> => #{
<<"__op">> => <<"RemoveRelation">>,
<<"objects">> => [
#{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"Product">>,
<<"objectId">> => ProductId
}
]}
},
case dgiot_parse:query_object(<<"Channel">>, #{<<"where">> => #{<<"product">> => #{<<"__type">> => <<"Pointer">>, <<"className">> => <<"Product">>, <<"objectId">> => ProductId}}, <<"limit">> => 20}) of
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
lists:foldl(fun(#{<<"objectId">> := ChannelId}, _Acc) ->
dgiot_parse:update_object(<<"Channel">>, ChannelId, Map)
end, [], Results);
_ ->
[]
end.
6 changes: 3 additions & 3 deletions src/emqx_tracer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ trace(publish, #message{topic = <<"logger_trace", _/binary>>}) ->
ignore;
trace(publish, #message{from = From, topic = Topic, payload = Payload})
when is_binary(From); is_atom(From) ->
io:format("From ~p", From),
?LOG(info, "From ~p", From),
emqx_logger:info(#{topic => Topic,
mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}},
"PUBLISH to ~s: ~0p ~p", [Topic, Payload, From]).
Expand Down Expand Up @@ -111,7 +111,7 @@ lookup_traces() ->
lists:foldl(fun filter_traces/2, [], emqx_logger:get_log_handlers(started)).

install_trace_handler(Who, Level, LogFile) ->
io:format("Who1 ~p", Who),
?LOG(info, "Who1 ~p", [Who]),
case logger:add_handler(handler_id(Who), logger_disk_log_h,
#{level => Level,
formatter => ?FORMAT,
Expand All @@ -128,7 +128,7 @@ install_trace_handler(Who, Level, LogFile) ->
end.

uninstall_trance_handler(Who) ->
io:format("Who2 ~p", Who),
?LOG(info, "Who2 ~p", [Who]),
case logger:remove_handler(handler_id(Who)) of
ok ->
?LOG(info, "Stop trace for ~p", [Who]);
Expand Down

0 comments on commit 13c42f0

Please sign in to comment.