package com.panzhihua.service_equipment.until;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.panzhihua.service_equipment.config.AccessConfig;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import javax.annotation.Resource;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import static com.panzhihua.common.utlis.HttpClientUtil.*;
|
|
|
@Slf4j
|
@Service
|
public class AccessUtil {
|
|
|
@Resource
|
private AccessConfig accessConfig;
|
|
|
/**
|
* 刷新token
|
* @return token
|
* @throws Exception
|
*/
|
public Map refreshToken() throws Exception {
|
String url = "https://dptest.d-power.com.cn:14404/v1/token/refresh";
|
return JSONObject.parseObject(httpPost(url, JSON.toJSONString(accessConfig)), Map.class);
|
}
|
|
/**
|
* 注册人脸
|
* @return 返回结果
|
* @throws Exception
|
*/
|
public Map faceEnroll(String tel,String name,String image,String communityId,String unitId) {
|
String url="https://dptest.d-power.com.cn:14404/v1/face";
|
String token="DpToken ";
|
Map headerMap = new HashMap();
|
try {
|
Map map = refreshToken();
|
if (map!=null&&map.get("token")!=null){
|
token =token+map.get("token").toString();
|
log.info("生成的token为:{}",token);
|
headerMap.put("Authorization",token);
|
headerMap.put("Host","dptest.d-power.com.cn");
|
}
|
else {
|
log.info("生成token错误");
|
return null;
|
}
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
Map result=new HashMap();
|
result.put("role","occupant");
|
result.put("communityId",communityId);
|
result.put("unitId",unitId);
|
result.put("tel",tel);
|
result.put("name",name);
|
result.put("timeout",10);
|
result.put("image",image);
|
try {
|
return JSONObject.parseObject(httpPostAndToken(url, JSON.toJSONString(result),headerMap), Map.class);
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
/**
|
* 开门
|
* @return 返回结果
|
* @throws Exception
|
*/
|
public Map deviceOpen(String sn,String opener,String kind,String role) {
|
String url="https://dptest.d-power.com.cn:14404/v1/device/open";
|
String token="DpToken ";
|
Map headerMap = new HashMap();
|
try {
|
Map map = refreshToken();
|
if (map!=null&&map.get("token")!=null){
|
token =token+map.get("token").toString();
|
log.info("生成的token为:{}",token);
|
headerMap.put("Authorization",token);
|
headerMap.put("Host","dptest.d-power.com.cn");
|
}
|
else {
|
log.info("生成token错误");
|
return null;
|
}
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
Map result=new HashMap();
|
result.put("sn",sn);
|
result.put("kind",kind);
|
result.put("role",role);
|
result.put("opener",opener);
|
try {
|
return JSONObject.parseObject(httpPostAndToken(url, JSON.toJSONString(result),headerMap), Map.class);
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
|
}
|