Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cloud-fb
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王衍超
cloud-fb
Commits
cb38293e
Commit
cb38293e
authored
May 28, 2021
by
shiwenbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加OA同步接口
parent
b4e6a606
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
408 additions
and
0 deletions
+408
-0
DinasCommonConfiguration.java
...a/com/xyst/dinas/biz/config/DinasCommonConfiguration.java
+12
-0
SyncOaController.java
...rc/main/java/com/xyst/dinas/biz/web/SyncOaController.java
+121
-0
HttpClientUtil.java
...n/java/com/xyst/dinas/biz/web/request/HttpClientUtil.java
+275
-0
No files found.
backend/xyst.dinas.biz/src/main/java/com/xyst/dinas/biz/config/DinasCommonConfiguration.java
View file @
cb38293e
...
...
@@ -11,12 +11,24 @@ import com.xyst.dinas.biz.web.DinasCommonController;
import
com.xyst.dinas.biz.web.SandAttachmentController
;
import
com.xyst.dinas.biz.web.SandDictController
;
import
com.xyst.dinas.biz.web.SandQueryController
;
import
com.xyst.dinas.biz.web.SyncOaController
;
import
com.xyst.dinas.biz.web.WarehouseAttachmentController
;
import
com.xyst.dinas.biz.web.WarehouseDictController
;
import
com.xyst.dinas.biz.web.WarehouseQueryController
;
import
com.xyst.dinas.biz.web.request.HttpClientUtil
;
@Configuration
public
class
DinasCommonConfiguration
{
@Bean
public
HttpClientUtil
httpClientUtil
(){
return
new
HttpClientUtil
();
}
@Bean
public
SyncOaController
syncOaController
()
{
return
new
SyncOaController
();
}
@Bean
public
DinasCommonController
dinasCommonController
()
{
...
...
backend/xyst.dinas.biz/src/main/java/com/xyst/dinas/biz/web/SyncOaController.java
0 → 100644
View file @
cb38293e
package
com
.
xyst
.
dinas
.
biz
.
web
;
import
org.json.JSONObject
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.xyst.dinas.biz.web.request.HttpClientUtil
;
/**
* 向OA系统同步组织、用户、角色、职员、用户角色关系
* @author shiwenbo
*
*/
@RestController
public
class
SyncOaController
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SyncOaController
.
class
);
@Autowired
private
HttpClientUtil
httpClientUtil
;
@Value
(
"${dna.oa.url:}"
)
private
String
dnaOaUrl
=
null
;
/**
* 同步组织
* @param body
* @return
*/
@ResponseBody
@RequestMapping
(
value
=
"/oa/syncOrg"
,
method
=
RequestMethod
.
POST
)
public
Object
syncOrg
(
@RequestBody
String
body
)
{
JSONObject
paramData
=
new
JSONObject
(
body
);
String
result
=
this
.
httpClientUtil
.
postJson
(
dnaOaUrl
+
"/oa/syncOrg"
,
paramData
,
""
);
JSONObject
param
=
new
JSONObject
(
result
);
String
resultStr
=
param
.
optString
(
"result"
);
if
(
resultStr
.
equals
(
"fail"
)){
logger
.
error
(
"向DNA服务同步组织时失败! "
+
param
.
optString
(
"errorMessage"
));
}
return
param
.
toString
();
}
/**
* 同步用户
* @param body
* @return
*/
@ResponseBody
@RequestMapping
(
value
=
"/oa/syncUser"
,
method
=
RequestMethod
.
POST
)
public
Object
syncUser
(
@RequestBody
String
body
)
{
JSONObject
paramData
=
new
JSONObject
(
body
);
String
result
=
this
.
httpClientUtil
.
postJson
(
dnaOaUrl
+
"/oa/syncOrg"
,
paramData
,
""
);
JSONObject
param
=
new
JSONObject
(
result
);
String
resultStr
=
param
.
optString
(
"result"
);
if
(
resultStr
.
equals
(
"fail"
)){
logger
.
error
(
"向DNA服务同步用户时失败! "
+
param
.
optString
(
"errorMessage"
));
}
return
param
.
toString
();
}
/**
* 同步角色
* @param body
* @return
*/
@ResponseBody
@RequestMapping
(
value
=
"/oa/syncRole"
,
method
=
RequestMethod
.
POST
)
public
Object
syncRole
(
@RequestBody
String
body
)
{
JSONObject
paramData
=
new
JSONObject
(
body
);
String
result
=
this
.
httpClientUtil
.
postJson
(
dnaOaUrl
+
"/oa/syncOrg"
,
paramData
,
""
);
JSONObject
param
=
new
JSONObject
(
result
);
String
resultStr
=
param
.
optString
(
"result"
);
if
(
resultStr
.
equals
(
"fail"
)){
logger
.
error
(
"向DNA服务同步角色时失败! "
+
param
.
optString
(
"errorMessage"
));
}
return
param
.
toString
();
}
/**
* 同步职员
* @param body
* @return
*/
@ResponseBody
@RequestMapping
(
value
=
"/oa/syncStaff"
,
method
=
RequestMethod
.
POST
)
public
Object
syncStaff
(
@RequestBody
String
body
)
{
JSONObject
paramData
=
new
JSONObject
(
body
);
String
result
=
this
.
httpClientUtil
.
postJson
(
dnaOaUrl
+
"/oa/syncOrg"
,
paramData
,
""
);
JSONObject
param
=
new
JSONObject
(
result
);
String
resultStr
=
param
.
optString
(
"result"
);
if
(
resultStr
.
equals
(
"fail"
)){
logger
.
error
(
"向DNA服务同步职员时失败! "
+
param
.
optString
(
"errorMessage"
));
}
return
param
.
toString
();
}
/**
* 同步用户与角色关系
* @param body
* @return
*/
@ResponseBody
@RequestMapping
(
value
=
"/oa/addRoleToUser"
,
method
=
RequestMethod
.
POST
)
public
Object
addRoleToUser
(
@RequestBody
String
body
)
{
JSONObject
paramData
=
new
JSONObject
(
body
);
String
result
=
this
.
httpClientUtil
.
postJson
(
dnaOaUrl
+
"/oa/syncOrg"
,
paramData
,
""
);
JSONObject
param
=
new
JSONObject
(
result
);
String
resultStr
=
param
.
optString
(
"result"
);
if
(
resultStr
.
equals
(
"fail"
)){
logger
.
error
(
"向DNA服务同步用户与角色关系时失败! "
+
param
.
optString
(
"errorMessage"
));
}
return
param
.
toString
();
}
}
backend/xyst.dinas.biz/src/main/java/com/xyst/dinas/biz/web/request/HttpClientUtil.java
0 → 100644
View file @
cb38293e
package
com
.
xyst
.
dinas
.
biz
.
web
.
request
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.NameValuePair
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpDelete
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpPut
;
import
org.apache.http.client.utils.URIBuilder
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.util.EntityUtils
;
import
org.json.JSONObject
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
public
class
HttpClientUtil
{
private
static
final
String
CONTENT_TYPE_TEXT_JSON
=
"text/json"
;
private
CloseableHttpClient
httpClient
;
public
HttpClientUtil
()
{
// 1 创建HttpClinet,相当于打开浏览器
this
.
httpClient
=
HttpClients
.
createDefault
();
}
/**
* 带参数的get请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public
String
doGet
(
String
url
,
Map
<
String
,
Object
>
map
,
String
tenant_id
)
throws
Exception
{
// 声明URIBuilder
URIBuilder
uriBuilder
=
new
URIBuilder
(
url
);
// 判断参数map是否为非空
if
(
map
!=
null
)
{
// 遍历参数
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
// 设置参数
uriBuilder
.
setParameter
(
entry
.
getKey
(),
entry
.
getValue
().
toString
());
}
}
// 2 创建httpGet对象,相当于设置url请求地址
HttpGet
httpGet
=
new
HttpGet
(
uriBuilder
.
build
());
// httpGet.addHeader("tenant-id", tenant_id);
// 3 使用HttpClient执行httpGet,相当于按回车,发起请求
CloseableHttpResponse
response
=
this
.
httpClient
.
execute
(
httpGet
);
// 4 解析结果,封装返回对象httpResult,相当于显示相应的结果
// 状态码
// response.getStatusLine().getStatusCode();
// 响应体,字符串,如果response.getEntity()为空,下面这个代码会报错,所以解析之前要做非空的判断
// EntityUtils.toString(response.getEntity(), "UTF-8");
String
body
=
null
;
// 解析数据封装HttpResult
if
(
response
.
getEntity
()
!=
null
)
{
body
=
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
);
}
else
{
body
=
null
;
}
// 返回
return
body
;
}
/**
* 不带参数的get请求
*
* @param url
* @return
* @throws Exception
*/
public
String
doGet
(
String
url
,
String
tenant_id
)
throws
Exception
{
String
body
=
this
.
doGet
(
url
,
null
,
tenant_id
);
return
body
;
}
/**
* 带参数的post请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public
String
doPost
(
String
url
,
Map
<
String
,
Object
>
map
,
String
tenant_id
)
throws
Exception
{
// 声明httpPost请求
HttpPost
httpPost
=
new
HttpPost
(
url
);
// httpPost.addHeader("tenant-id", tenant_id);
httpPost
.
addHeader
(
"Content-Type"
,
"application/json"
);
// 判断map不为空
if
(
map
!=
null
)
{
// 声明存放参数的List集合
List
<
NameValuePair
>
params
=
new
ArrayList
<
NameValuePair
>();
// 遍历map,设置参数到list中
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
params
.
add
(
new
BasicNameValuePair
(
entry
.
getKey
(),
entry
.
getValue
().
toString
()));
}
// 创建form表单对象
UrlEncodedFormEntity
formEntity
=
new
UrlEncodedFormEntity
(
params
,
"UTF-8"
);
// 把表单对象设置到httpPost中
httpPost
.
setEntity
(
formEntity
);
}
// 使用HttpClient发起请求,返回response
CloseableHttpResponse
response
=
this
.
httpClient
.
execute
(
httpPost
);
// 解析response封装返回对象httpResult
String
body
=
null
;
if
(
response
.
getEntity
()
!=
null
)
{
body
=
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
);
}
// 返回结果
return
body
;
}
/**
* POST请求 携带Json格式的参数
*
* @param url
* @param param
* @return
* @throws IOException
*/
public
String
postJson
(
String
url
,
JSONObject
param
,
String
tenant_id
)
{
HttpPost
httpPost
=
new
HttpPost
(
url
);
// httpPost.setHeader("tenant-id", tenant_id);
httpPost
.
setHeader
(
"Content-Type"
,
"application/json;charset=utf8"
);
// ObjectMapper mapper = new ObjectMapper();
// mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
String
parameter
=
param
.
toString
();
String
result
=
null
;
try
{
// parameter = mapper.writeValueAsString(param);
StringEntity
se
=
null
;
se
=
new
StringEntity
(
parameter
,
"UTF-8"
);
se
.
setContentEncoding
(
"UTF-8"
);
se
.
setContentType
(
CONTENT_TYPE_TEXT_JSON
);
httpPost
.
setEntity
(
se
);
CloseableHttpResponse
response
=
null
;
response
=
this
.
httpClient
.
execute
(
httpPost
);
HttpEntity
httpEntity
=
response
.
getEntity
();
result
=
EntityUtils
.
toString
(
httpEntity
,
"UTF-8"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
/**
* 不带参数的post请求
*
* @param url
* @return
* @throws Exception
*/
public
String
doPost
(
String
url
,
String
tenant_id
)
throws
Exception
{
String
body
=
this
.
doPost
(
url
,
null
,
tenant_id
);
return
body
;
}
/**
* 带参数的Put请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public
String
doPut
(
String
url
,
Map
<
String
,
Object
>
map
)
throws
Exception
{
// 声明httpPost请求
HttpPut
httpPut
=
new
HttpPut
(
url
);
// 判断map不为空
if
(
map
!=
null
)
{
// 声明存放参数的List集合
List
<
NameValuePair
>
params
=
new
ArrayList
<
NameValuePair
>();
// 遍历map,设置参数到list中
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
params
.
add
(
new
BasicNameValuePair
(
entry
.
getKey
(),
entry
.
getValue
().
toString
()));
}
// 创建form表单对象
UrlEncodedFormEntity
formEntity
=
new
UrlEncodedFormEntity
(
params
,
"UTF-8"
);
// 把表单对象设置到httpPost中
httpPut
.
setEntity
(
formEntity
);
}
// 使用HttpClient发起请求,返回response
CloseableHttpResponse
response
=
this
.
httpClient
.
execute
(
httpPut
);
// 解析response封装返回对象httpResult
String
body
=
null
;
if
(
response
.
getEntity
()
!=
null
)
{
body
=
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
);
}
// 返回结果
return
body
;
}
/**
* 带参数的Delete请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public
String
doDelete
(
String
url
,
Map
<
String
,
Object
>
map
)
throws
Exception
{
// 声明URIBuilder
URIBuilder
uriBuilder
=
new
URIBuilder
(
url
);
// 判断参数map是否为非空
if
(
map
!=
null
)
{
// 遍历参数
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
// 设置参数
uriBuilder
.
setParameter
(
entry
.
getKey
(),
entry
.
getValue
().
toString
());
}
}
// 2 创建httpGet对象,相当于设置url请求地址
HttpDelete
httpDelete
=
new
HttpDelete
(
uriBuilder
.
build
());
// 3 使用HttpClient执行httpGet,相当于按回车,发起请求
CloseableHttpResponse
response
=
this
.
httpClient
.
execute
(
httpDelete
);
// 4 解析结果,封装返回对象httpResult,相当于显示相应的结果
// 状态码
// response.getStatusLine().getStatusCode();
// 响应体,字符串,如果response.getEntity()为空,下面这个代码会报错,所以解析之前要做非空的判断
// EntityUtils.toString(response.getEntity(), "UTF-8");
String
body
=
null
;
// 解析数据封装HttpResult
if
(
response
.
getEntity
()
!=
null
)
{
body
=
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
);
}
// 返回
return
body
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment