feat: roleuser

This commit is contained in:
AvantLiu 2021-11-23 21:23:27 +08:00
parent ed4b93ef0e
commit 2bfc96ab31
6 changed files with 114 additions and 98 deletions

View File

@ -18,7 +18,7 @@
-include_lib("dgiot/include/logger.hrl").
-author("johnliu").
-export([url_join/1, qs/1, urldecode/1, urlencode/1]).
-export([upload/2, download/2, get_category/2, get_categorys/0, fileUpload/2]).
-export([upload/2, download/2, get_category/2, get_categorys/0, fileUpload/3]).
-define(CRLF, "\r\n").
upload(Url, Path) ->
@ -51,29 +51,37 @@ upload(Url, Path) ->
{error, Reason}
end.
fileUpload(Url, Path) ->
fileUpload(Url, Path, Producttempid) ->
case file:read_file(Path) of
{ok, Stream} ->
FileName = dgiot_utils:to_binary(filename:basename(Path)),
Boundary = <<"----WebKitFormBoundaryPs0yRvKAsnNnI4uA">>,
Boundary = <<"----WebKitFormBoundaryCEh4tVfSNDV7cY9B">>,
Header = <<"--", Boundary/binary, "\r\n", "Content-Disposition: form-data;name=\"file\"; filename=\"", FileName/binary, "\"", "\r\n">>,
Tail = <<"--", Boundary/binary, "--", "\r\n", "\r\n">>,
FileBody = <<"Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document", "\r\n", "\r\n",
Stream/binary, "\r\n">>,
FileBody = <<Header/binary, "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document", "\r\n", "\r\n",
Stream/binary, "\r\n", Tail/binary>>,
FilenameBody = <<"--", Boundary/binary, "\r\n", "Content-Disposition: form-data; name=\"filename\"", "\r\n", "\r\n", Producttempid/binary, "\r\n">>,
PathBody = <<"--", Boundary/binary, "\r\n", "Content-Disposition: form-data; name=\"path\"", "\r\n", "\r\n", "dgiot_file/product/topo", "\r\n">>,
Tail = <<"--", Boundary/binary, "--">>,
Body = <<Header/binary, FileBody/binary, FilenameBody/binary, PathBody/binary, Tail/binary>>,
Body = <<FileBody/binary>>,
Size = byte_size(Body),
ContentType = <<"multipart/form-data; boundary=", Boundary/binary>>,
case httpc:request(post, {dgiot_utils:to_list(Url), [{"Content-Length", integer_to_list(Size)}], binary_to_list(ContentType), Body}, [], []) of
{ok, {{"HTTP/1.1", 200, "OK"}, _, Json}} ->
case jsx:decode(dgiot_utils:to_binary(Json), [{labels, binary}, return_maps]) of
#{<<"content">> := _content} = Data ->
#{<<"msg">> := <<"SUCCESS">>, <<"code">> := 0} = Data ->
{ok, Data};
Error1 -> Error1
Error1 ->
Error1
end;
Error -> Error
Error ->
Error
end;
{error, Reason} ->
?LOG(info, "Reason ~p ", [Reason]),

View File

@ -371,10 +371,11 @@ post_roleuser(#{<<"userid">> := UserId} = Body, SessionToken) ->
del_roleuser(#{<<"userid">> := UserId} = Body, SessionToken) ->
R1 =
case maps:is_key(<<"delfilter">>, Body) of
true ->
DelFilter = maps:get(<<"delfilter">>, Body),
case dgiot_parse:query_object(<<"_Role">>, DelFilter, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
case maps:find(<<"filter">>, Body) of
error ->
[];
{ok, Filter} ->
case dgiot_parse:query_object(<<"_Role">>, jsx:decode(Filter, [{labels, binary}, return_maps]), [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := DelRoles}} ->
lists:foldl(
fun(#{<<"objectId">> := RoleId}, Acc) ->
@ -392,8 +393,7 @@ del_roleuser(#{<<"userid">> := UserId} = Body, SessionToken) ->
Acc ++ [#{<<"del">> => R0}]
end, [], DelRoles);
_ -> []
end;
_ -> []
end
end,
{ok, #{<<"result">> => R1}}.
@ -463,14 +463,14 @@ get_role(Name, SessionToken) ->
Users =
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := User}} when length(User) > 0 ->
lists:foldl(fun(X,Acc)->
case maps:get(<<"username">>,X) of
<<"user_for_",_/binary>>->
Acc;
_->
Acc++[X]
end
end,[],User);
lists:foldl(fun(X, Acc) ->
case maps:get(<<"username">>, X) of
<<"user_for_", _/binary>> ->
Acc;
_ ->
Acc ++ [X]
end
end, [], User);
_ -> []
end,
%% ?LOG(info,"Users ~p", [Users]),

View File

@ -51,22 +51,22 @@ handle(OperationID, Args, Context, Req) ->
Headers = #{},
case catch do_request(OperationID, Args, Context, Req) of
{ErrType, Reason} when ErrType == 'EXIT'; ErrType == error ->
?LOG(debug,"do request: ~p, ~p, ~p~n", [OperationID, Args, Reason]),
?LOG(debug, "do request: ~p, ~p, ~p~n", [OperationID, Args, Reason]),
Err = case is_binary(Reason) of
true -> Reason;
false -> dgiot_utils:format("~p", [Reason])
end,
{500, Headers, #{<<"error">> => Err}};
ok ->
?LOG(debug,"do request: ~p, ~p ->ok ~n", [OperationID, Args]),
?LOG(debug, "do request: ~p, ~p ->ok ~n", [OperationID, Args]),
{200, Headers, #{}, Req};
{ok, Res} ->
?LOG(debug,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
?LOG(debug, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
{200, Headers, Res, Req};
{Status, Res} ->
{Status, Headers, Res, Req};
{Status, NewHeaders, Res} ->
?LOG(debug,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
?LOG(debug, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
{Status, maps:merge(Headers, NewHeaders), Res, Req}
end.
@ -79,34 +79,34 @@ handle(OperationID, Args, Context, Req) ->
%% OperationId:post_login
%% :POST /iotapi/post_login
do_request(post_login, #{<<"username">> := UserName, <<"password">> := Password}, _Context, _Req) ->
?LOG(debug,"UserName ~p ", [UserName]),
?LOG(debug, "UserName ~p ", [UserName]),
dgiot_parse_handler:login_by_account(UserName, Password);
%% Role模版 : :json文件导库
%% OperationId:post_role
%% :POST /iotapi/role
do_request(post_role, #{<<"name">> := _Name, <<"tempname">> := _TempName} = Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:post_role(Body, SessionToken);
%% Role模版 : :json文件导库
%% OperationId:delete_role
%% :DELETE /iotapi/role
do_request(delete_role, #{<<"name">> := _Name, <<"tempname">> := _TempName} = Body, _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]);
do_request(delete_role, #{<<"name">> := _Name, <<"tempname">> := _TempName} = Body, _Context, _Req0) ->
?LOG(debug, "Body ~p ", [Body]);
%% Role模版 : :json文件导库
%% OperationId:post_role
%% :POST /iotapi/role
do_request(put_role, Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:put_role(Body, SessionToken);
%% Role : :json文件导库
%% OperationId:get_role
%% :GET /iotapi/role
do_request(get_role, #{<<"name">> := Name} = Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:get_role(Name, SessionToken);
%% RoleUser : :json文件导库
@ -119,37 +119,37 @@ do_request(get_roleuser, #{<<"where">> := Where} = Filter, #{<<"sessionToken">>
%% OperationId:put_roleuser
%% :POST /iotapi/roleuser
do_request(put_roleuser, Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:put_roleuser(Body, SessionToken);
%% Role模版 : :json文件导库
%% OperationId:delete_roleuser
%% :POST /iotapi/roleuser
do_request(delete_roleuser, Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
dgiot_role:del_roleuser(jsx:decode(Body, [return_maps]), SessionToken);
%% io:format("Body ~p~n", [Body]),
dgiot_role:del_roleuser(Body, SessionToken);
%% Role模版 : :json文件导库
%% OperationId:delete_roleuser
%% :POST /iotapi/roleuser
do_request(post_roleuser, Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:post_roleuser(Body, SessionToken);
%% Role : :json文件导库
%% OperationId:get_roletree
%% :GET /iotapi/roletree
do_request(get_roletree, _Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"SessionToken ~p ", [SessionToken]),
dgiot_parse_handler:get_classtree(<<"_Role">>,<<"parent">>, #{}, SessionToken);
?LOG(debug, "SessionToken ~p ", [SessionToken]),
dgiot_parse_handler:get_classtree(<<"_Role">>, <<"parent">>, #{}, SessionToken);
%% dgiot_role:get_roletree(SessionToken);
%% Role模版 : :json文件导库
%% OperationId:post_roletemp
%% :GET /iotapi/roletemp
do_request(get_roletemp, #{<<"name">> := Name} = Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
#{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug, "Body ~p ", [Body]),
FileName = dgiot_utils:to_list(Name) ++ ".zip",
case dgiot_role:get_roletemp(FileName, Name, SessionToken) of
{ok, ZipFile} ->
@ -166,19 +166,19 @@ do_request(get_roletemp, #{<<"name">> := Name} = Body,
%% OperationId:post_roletemp
%% :POST /iotapi/roletemp
do_request(post_roletemp, #{<<"name">> := Name, <<"tempname">> := TempName} = Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
dgiot_role:post_roletemp(Name, TempName, SessionToken);
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:post_roletemp(Name, TempName, SessionToken);
%% Role模版 : :json文件导库
%% OperationId:post_roletemp
%% :POST /iotapi/roletemp
do_request(put_roletemp, Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
?LOG(debug,"Body ~p ", [Body]),
?LOG(debug, "Body ~p ", [Body]),
dgiot_role:put_roletemp(Body, SessionToken);
%% API接口
do_request(_OperationId, _Args, _Context, _Req) ->
?LOG(debug,"_Args ~p", [_Args]),
?LOG(debug, "_Args ~p", [_Args]),
{error, <<"Not Allowed.">>}.

View File

@ -211,7 +211,10 @@
"config": {
"description": "模版描述信息",
"type": "object",
"example": {"name":"image0","identifier":"a28b138a-0f31-fe7d-cadc-0be47faf8a8d"}
"example": {
"name": "image0",
"identifier": "a28b138a-0f31-fe7d-cadc-0be47faf8a8d"
}
}
}
}
@ -260,7 +263,10 @@
"in": "formData",
"description": "报告其他信息",
"type": "object",
"example": {"client":"离心泵","identifier":"a28b138a-0f31-fe7d-cadc-0be47faf8a8d"}
"example": {
"client": "离心泵",
"identifier": "a28b138a-0f31-fe7d-cadc-0be47faf8a8d"
}
},
{
"name": "file",
@ -344,7 +350,11 @@
"basedata": {
"description": "报告描述信息",
"type": "object",
"example": {"starttime":"1637251200000","endtime":"1637942400000","bedname":"南方泵业台体"}
"example": {
"starttime": "1637251200000",
"endtime": "1637942400000",
"bedname": "南方泵业台体"
}
}
}
}
@ -384,7 +394,7 @@
"path": {
"description": "文件路径",
"type": "string",
"example":"/shapes/利欧泵业测试部/5a27bcd88f.zip"
"example": "/shapes/利欧泵业测试部/5a27bcd88f.zip"
}
}
}
@ -483,7 +493,7 @@
"name": "id",
"required": true,
"type": "string",
"default":"IMAWAarmTP"
"default": "IMAWAarmTP"
},
{
"description": "电子数据",
@ -505,7 +515,11 @@
"original": {
"description": "原件",
"type": "object",
"example": {"type":"image","controlid":"controlid", "taskid": "taskid"}
"example": {
"type": "image",
"controlid": "controlid",
"taskid": "taskid"
}
},
"md5": {
"description": "校验码",
@ -548,22 +562,22 @@
"reportId": {
"description": "报告Id",
"type": "string",
"example":"d3f9a0a820"
"example": "d3f9a0a820"
},
"type": {
"description": "批处理类型single或者all",
"type": "string",
"example":"single"
"example": "single"
},
"objectId": {
"description": "书证号",
"type": "string",
"example":"83f990a820"
"example": "83f990a820"
},
"status": {
"description": "审核状态",
"type": "string",
"example":"未审核",
"example": "未审核",
"required": true
}
}
@ -754,12 +768,12 @@
"type": "number",
"example": 1
},
"begin":{
"begin": {
"description": "开始时间",
"type": "number",
"example": 1591609983
},
"end":{
"end": {
"description": "开始时间",
"type": "number",
"example": 1591609983

View File

@ -38,7 +38,7 @@
file_stat/0,
list_dir/1,
file_info/1,
create_report/6,
create_report/8,
get_capture/1,
get_report_package/6,
post_data/2,
@ -518,15 +518,14 @@ get_app(SessionToken) ->
_ -> <<"">>
end.
create_report(ProductParentId, Config, Num, Imagurl, WordUrl, SessionToken) ->
NewNum = dgiot_utils:to_binary(dgiot_utils:to_int(Num) + 1),
create_report(ProductParentId, Config, Num, Imagurl, Heigh, Width, WordUrl, SessionToken) ->
NewConfig = maps:merge(Config, #{
<<"icon">> => Imagurl,
<<"konva">> => #{
<<"Stage">> => #{
<<"attrs">> => #{
<<"width">> => 595,
<<"height">> => 842},
<<"width">> => Width,
<<"height">> => Heigh},
<<"className">> => <<"Stage">>,
<<"children">> => [#{
<<"attrs">> => #{
@ -536,12 +535,12 @@ create_report(ProductParentId, Config, Num, Imagurl, WordUrl, SessionToken) ->
<<"attrs">> => #{
<<"id">> => <<"bg">>,
<<"type">> => <<"bg-image">>,
<<"width">> => 595,
<<"height">> => 842,
<<"width">> => Width,
<<"height">> => Heigh,
<<"src">> => Imagurl},
<<"className">> => <<"Image">>}]}]}}}),
dgiot_parse:create_object(<<"View">>, #{
<<"title">> => NewNum,
<<"title">> => Num,
<<"key">> => ProductParentId,
<<"type">> => <<"topo">>,
<<"class">> => <<"Product">>,

View File

@ -250,41 +250,36 @@ do_request(_OperationId, _Args, _Context, _Req) ->
{error, <<"Not Allowed.">>}.
do_report(Config, DevType, Name, SessionToken, FullPath, Uri) ->
case dgiot_httpc:fileUpload(Uri ++ "/WordController/fileUpload", dgiot_utils:to_list(FullPath)) of
{ok, #{<<"content">> := Content, <<"success">> := true}} ->
Url = cow_uri:urlencode(base64:encode(Content)),
WordPreview = Uri ++ "/onlinePreview?url=" ++ dgiot_utils:to_list(Url) ++ "&officePreviewType=image",
List = dgiot_html:find(WordPreview, {<<"img">>, {<<"class">>, <<"my-photo">>}}, <<"data-src">>),
WordUrl = Uri ++ "/wordServer/" ++ dgiot_utils:to_list(filename:basename(FullPath)),
CategoryId = maps:get(<<"category">>, Config, <<"d6ad425529">>),
Producttempid = maps:get(<<"producttemplet">>, Config, <<"">>),
ProductParentId =
case dgiot_product:create_product(#{
<<"name">> => Name,
<<"devType">> => DevType,
<<"desc">> => <<"0">>,
<<"nodeType">> => 1,
<<"channel">> => #{<<"type">> => 1, <<"tdchannel">> => <<"24b9b4bc50">>, <<"taskchannel">> => <<"0edaeb918e">>, <<"otherchannel">> => [<<"11ed8ad9f2">>]},
<<"netType">> => <<"Evidence">>,
<<"category">> => #{<<"objectId">> => CategoryId, <<"__type">> => <<"Pointer">>, <<"className">> => <<"Category">>},
<<"producttemplet">> => #{<<"objectId">> => Producttempid, <<"__type">> => <<"Pointer">>, <<"className">> => <<"ProductTemplet">>},
<<"config">> => Config,
<<"thing">> => #{},
<<"productSecret">> => license_loader:random(),
<<"dynamicReg">> => true}, SessionToken) of
{_, #{<<"objectId">> := ProductId}} ->
ProductId;
_ ->
dgiot_parse:get_productid(CategoryId, DevType, Name)
end,
lists:foldl(fun(ImageUrl, Acc) ->
%% <<"https://192.168.0.183:5094/wordServer/20211112142832/1.jpg">>
NewImageUrl = dgiot_utils:get_url_path(ImageUrl),
CategoryId = maps:get(<<"category">>, Config, <<"d6ad425529">>),
Producttempid = maps:get(<<"producttemplet">>, Config, <<"">>),
ProductParentId =
case dgiot_product:create_product(#{
<<"name">> => Name,
<<"devType">> => DevType,
<<"desc">> => <<"0">>,
<<"nodeType">> => 1,
<<"channel">> => #{<<"type">> => 1, <<"tdchannel">> => <<"24b9b4bc50">>, <<"taskchannel">> => <<"0edaeb918e">>, <<"otherchannel">> => [<<"11ed8ad9f2">>]},
<<"netType">> => <<"Evidence">>,
<<"category">> => #{<<"objectId">> => CategoryId, <<"__type">> => <<"Pointer">>, <<"className">> => <<"Category">>},
<<"producttemplet">> => #{<<"objectId">> => Producttempid, <<"__type">> => <<"Pointer">>, <<"className">> => <<"ProductTemplet">>},
<<"config">> => Config,
<<"thing">> => #{},
<<"productSecret">> => license_loader:random(),
<<"dynamicReg">> => true}, SessionToken) of
{_, #{<<"objectId">> := ProductId}} ->
ProductId;
_ ->
dgiot_parse:get_productid(CategoryId, DevType, Name)
end,
case dgiot_httpc:fileUpload(Uri ++ "/WordController/fileUpload", dgiot_utils:to_list(FullPath), Producttempid) of
{ok, #{<<"code">> := 0, <<"msg">> := <<"SUCCESS">>, <<"path">> := WordPath, <<"images">> := Images}} ->
lists:foldl(fun(Image, Acc) ->
#{<<"heigh">> := Heigh, <<"url">> := ImageUrl, <<"width">> := Width} = Image,
case binary:split(filename:basename(ImageUrl), <<$.>>, [global, trim]) of
[Index, _] ->
Acc ++ [dgiot_evidence:create_report(ProductParentId, Config, Index, NewImageUrl, WordUrl, SessionToken)]
Acc ++ [dgiot_evidence:create_report(ProductParentId, Config, Index, ImageUrl, Heigh, Width, WordPath, SessionToken)]
end
end, [], List);
end, [], Images);
_Oth ->
io:format("_Oth ~p~n", [_Oth]),
[]