mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-12-03 12:48:30 +08:00
feat:存储cookie、针对百度地图进行经纬度转换
This commit is contained in:
parent
52687f5bc2
commit
30a965b281
@ -137,11 +137,19 @@ handle_message(check, #state{id = ChannelId, env = #{<<"offline">> := OffLine, <
|
||||
|
||||
|
||||
handle_message({sync_parse, Pid, 'after', get, Token, <<"Device">>, #{<<"results">> := Results} = ResBody}, State) ->
|
||||
SessionToken = dgiot_parse_auth:get_usersession(dgiot_utils:to_binary(Token)),
|
||||
Cookie= case dgiot_parse_auth:get_cookie(SessionToken) of
|
||||
not_find ->
|
||||
#{};
|
||||
Cooki ->
|
||||
Cooki
|
||||
end,
|
||||
MapType = maps:get(<<"mapType">>,Cookie,<<"baidu">>),
|
||||
{NewResults, DeviceList} =
|
||||
lists:foldl(
|
||||
fun(#{<<"objectId">> := DeviceId} = Device, {NewResult, Dev}) ->
|
||||
fun(#{<<"objectId">> := DeviceId } = Device, {NewResult, Dev}) ->
|
||||
case dgiot_device:lookup(DeviceId) of
|
||||
{ok, #{<<"status">> := Status, <<"isEnable">> := IsEnable, <<"longitude">> := Longitude, <<"latitude">> := Latitude, <<"time">> := Time}} ->
|
||||
{ok, #{<<"status">> := Status, <<"isEnable">> := IsEnable, <<"time">> := Time}} ->
|
||||
NewStatus =
|
||||
case Status of
|
||||
true ->
|
||||
@ -149,39 +157,42 @@ handle_message({sync_parse, Pid, 'after', get, Token, <<"Device">>, #{<<"results
|
||||
_ ->
|
||||
<<"OFFLINE">>
|
||||
end,
|
||||
Location =
|
||||
case dgiot_data:get(?DGIOT_LOCATION, DeviceId) of
|
||||
not_fing ->
|
||||
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Longitude, <<"latitude">> => Latitude};
|
||||
{Bd_lng, Bd_lat} ->
|
||||
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat}
|
||||
end,
|
||||
{NewResult ++ [Device#{<<"location">> => Location, <<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Time}], Dev ++ [DeviceId]};
|
||||
Location = case maps:find(<<"location">>,Device)of
|
||||
error ->
|
||||
#{};
|
||||
{ok,A} ->
|
||||
A
|
||||
end,
|
||||
|
||||
NewLocation = get_new_location(Location,MapType),
|
||||
{NewResult ++ [Device#{<<"location">> => NewLocation, <<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Time}], Dev ++ [DeviceId]};
|
||||
_ ->
|
||||
{NewResult ++ [Device], Dev}
|
||||
end
|
||||
end, {[], []}, Results),
|
||||
case dgiot_parse_auth:get_usersession(dgiot_utils:to_binary(Token)) of
|
||||
case SessionToken of
|
||||
not_find ->
|
||||
pass;
|
||||
SessionToken ->
|
||||
_ ->
|
||||
dgiot_mqtt:subscribe_route_key(DeviceList, SessionToken, devicestate)
|
||||
end,
|
||||
dgiot_parse_hook:publish(Pid, ResBody#{<<"results">> => NewResults}),
|
||||
{ok, State};
|
||||
|
||||
handle_message({sync_parse, Pid, 'after', get, _Token, <<"Device">>, #{<<"objectId">> := ObjectId} = ResBody}, State) ->
|
||||
handle_message({sync_parse, Pid, 'after', get, Token, <<"Device">>, #{<<"objectId">> := _ObjectId} = ResBody}, State) ->
|
||||
SessionToken = dgiot_parse_auth:get_usersession(dgiot_utils:to_binary(Token)),
|
||||
Cookie= case dgiot_parse_auth:get_cookie(SessionToken) of
|
||||
not_find ->
|
||||
#{};
|
||||
A->
|
||||
A
|
||||
end,
|
||||
MapType = maps:get(<<"mapType">>,Cookie,<<"baidu">>),
|
||||
ResBody1 =
|
||||
case ResBody of
|
||||
#{<<"location">> := Location1} ->
|
||||
Location =
|
||||
case dgiot_data:get(?DGIOT_LOCATION, ObjectId) of
|
||||
not_fing ->
|
||||
Location1;
|
||||
{Bd_lng, Bd_lat} ->
|
||||
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat}
|
||||
end,
|
||||
ResBody#{<<"location">> => Location};
|
||||
#{<<"location">> := Location} ->
|
||||
NewLocation = get_new_location(Location,MapType),
|
||||
ResBody#{<<"location">> => NewLocation};
|
||||
_ ->
|
||||
ResBody
|
||||
end,
|
||||
@ -262,3 +273,19 @@ stop(ChannelType, ChannelId, _State) ->
|
||||
?LOG(warning, "Channel[~p,~p] stop", [ChannelType, ChannelId]),
|
||||
ok.
|
||||
|
||||
get_new_location(Location,MapType)->
|
||||
case MapType of
|
||||
<<"baidu">> ->
|
||||
Longitude = maps:get(<<"longitude">>, Location, 0),
|
||||
Latitude = maps:get(<<"latitude">>, Location, 0),
|
||||
case [Location, Latitude] of
|
||||
[0, 0] ->
|
||||
#{};
|
||||
_ ->
|
||||
[Bd_lng, Bd_lat] = dgiot_gps:gcj02tobd09(Longitude, Latitude),
|
||||
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat}
|
||||
end;
|
||||
|
||||
_ ->
|
||||
Location
|
||||
end.
|
||||
|
@ -154,6 +154,55 @@
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns operation status"
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized"
|
||||
},
|
||||
"403": {
|
||||
"description": "Forbidden"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Internal error"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
"Dlink"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/cookie": {
|
||||
"post": {
|
||||
"summary": "cookie存储",
|
||||
"description": "cookie存储",
|
||||
"parameters": [
|
||||
{
|
||||
"in": "body",
|
||||
"name": "data",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"UserSession": {
|
||||
"required": true,
|
||||
"type": "string",
|
||||
"example": "usersession"
|
||||
},
|
||||
"cookie": {
|
||||
"required": true,
|
||||
"type": "string",
|
||||
"example": "{}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns operation status"
|
||||
|
@ -155,5 +155,14 @@ do_request(get_thingecho, _Args, _Context, _Req) ->
|
||||
%% 发送mqtt消息
|
||||
{200, <<"success">>};
|
||||
|
||||
|
||||
do_request(post_cookie, #{<<"UserSession">> := UserSession, <<"cookie">> := Cookie} = _Args, _Context, _Req) ->
|
||||
case dgiot_parse_auth:put_cookie(UserSession, Cookie) of
|
||||
true ->
|
||||
{ok, <<"success">>};
|
||||
_ ->
|
||||
{500, <<"save_cookie_failed">>}
|
||||
end;
|
||||
|
||||
do_request(_OperationId, _Args, _Context, _Req) ->
|
||||
{error, <<"Not Allowed.">>}.
|
||||
|
@ -22,6 +22,7 @@
|
||||
-dgiot_data("ets").
|
||||
-export([init_ets/0]).
|
||||
-define(DGIOT_USERSESSION, dgiot_usersession).
|
||||
-define(DGIOT_COOKIE,dgiot_cookie).
|
||||
|
||||
%% API
|
||||
-export([
|
||||
@ -49,10 +50,12 @@
|
||||
-export([login_by_account/2, login_by_token/2, login_by_mail_phone/1, do_login/1]).
|
||||
-export([create_user_for_app/1, get_token/1, set_cookies/3, add_acl/5]).
|
||||
-export([get_usersession/1, put_usersession/1,del_usersession/1]).
|
||||
-export([get_cookie/1,put_cookie/2,del_cookie/1]).
|
||||
|
||||
|
||||
init_ets() ->
|
||||
dgiot_data:init(?DGIOT_USERSESSION).
|
||||
dgiot_data:init(?DGIOT_USERSESSION),
|
||||
dgiot_data:init(?DGIOT_COOKIE).
|
||||
|
||||
get_usersession(Depart_token) ->
|
||||
dgiot_data:get(?DGIOT_USERSESSION, {Depart_token}).
|
||||
@ -78,6 +81,14 @@ del_usersession(User_session)->
|
||||
|
||||
|
||||
|
||||
put_cookie(UserSession,Cookie)->
|
||||
dgiot_data:insert(?DGIOT_COOKIE, {UserSession}, Cookie).
|
||||
|
||||
get_cookie(UserSession)->
|
||||
dgiot_data:get(?DGIOT_COOKIE, {UserSession}).
|
||||
del_cookie(UserSession)->
|
||||
dgiot_data:delete(?DGIOT_COOKIE, UserSession).
|
||||
|
||||
%% 登录
|
||||
login(UserName, Password) ->
|
||||
login(?DEFAULT, UserName, Password).
|
||||
|
@ -208,7 +208,8 @@ do_request(post_logout, #{<<"sessionToken">> := SessionToken}, _Context, _Req)
|
||||
SessionId = dgiot_parse_id:get_sessionId(SessionToken),
|
||||
dgiot_parse:del_object(<<"_Session">>, SessionId),
|
||||
dgiot_mqtt:unsubscribe_route_key(SessionToken),
|
||||
dgiot_parse_auth:del_usersession(SessionToken);
|
||||
dgiot_parse_auth:del_usersession(SessionToken),
|
||||
dgiot_parse_auth:del_cookie(SessionToken);
|
||||
|
||||
%% RoleUser 概要: 导库 描述:json文件导库
|
||||
%% OperationId:get_roleuser
|
||||
|
Loading…
Reference in New Issue
Block a user