|
|
@@ -1,8 +1,10 @@
|
|
|
package org.dromara.backstage.util;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
@@ -32,6 +34,7 @@ import java.util.Map;
|
|
|
public class LockUtils {
|
|
|
private final LockConfig lockConfig;
|
|
|
private String token;
|
|
|
+
|
|
|
/**
|
|
|
* 通过登陆去获取token,23小时刷新一次
|
|
|
*/
|
|
|
@@ -57,14 +60,20 @@ public class LockUtils {
|
|
|
formMap.put("SIGN", sign);
|
|
|
HttpRequest req = HttpUtil.createPost(lockConfig.getAppUrl() + "dmsLogin");
|
|
|
req.form(formMap);
|
|
|
- String resultJson = req.execute().body();
|
|
|
- JSONObject result = JSONUtil.parseObj(resultJson);
|
|
|
- if (result.getInt("result") == 0) {
|
|
|
- token = result.getStr("token");
|
|
|
- } else {
|
|
|
- log.error("获取酒店锁token失败!{}", result);
|
|
|
+ try (HttpResponse res = req.execute()) {
|
|
|
+ String resultJson = res.body();
|
|
|
+ JSONObject result = JSONUtil.parseObj(resultJson);
|
|
|
+ if (result.getInt("result") == 0) {
|
|
|
+ token = result.getStr("token");
|
|
|
+ } else {
|
|
|
+ log.error("获取酒店锁token失败!{}", result);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("连接酒店锁服务异常!{}", e.getMessage());
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
public String sendPost(Map<String, Object> formMap, String reqMethod) {
|
|
|
if (StrUtil.isEmpty(token)) {
|
|
|
refreshToken();
|
|
|
@@ -82,14 +91,28 @@ public class LockUtils {
|
|
|
formMap.put("SIGN", sign);
|
|
|
HttpRequest req = HttpUtil.createPost(lockConfig.getAppUrl() + reqMethod);
|
|
|
req.form(formMap);
|
|
|
- //String temp = req.execute().body();
|
|
|
- return req.execute().body();
|
|
|
+ try (HttpResponse res = req.execute()) {
|
|
|
+ return res.body();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("酒店锁接口调用异常!{}", e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 请求发卡数据
|
|
|
+ * @param mapForm 请求参数
|
|
|
+ * @return 发卡数据
|
|
|
+ */
|
|
|
public String getWriteCardData(Map<String, Object> mapForm) {
|
|
|
String sendResult = sendPost(mapForm, "cardSender/cardSender");
|
|
|
- return JSONUtil.parseObj(sendResult).getStr("data");
|
|
|
+ if (ObjectUtil.isNotEmpty(sendResult)) {
|
|
|
+ return JSONUtil.parseObj(sendResult).getStr("data");
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
public String getPwdCardData(Map<String, Object> mapForm) {
|
|
|
return sendPost(mapForm, "addLockKey");
|
|
|
}
|