mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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);
        }
    }
 
 
}